• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Mintbase / mintbase-js / 5268867622

pending completion
5268867622

push

github

sainthiago
:alien: update token attributes props

605 of 821 branches covered (73.69%)

Branch coverage included in aggregate %.

905 of 976 relevant lines covered (92.73%)

4.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.58
/packages/sdk/src/execute/execute.utils.ts
1
import type { Wallet, FinalExecutionOutcome } from '@near-wallet-selector/core';
2
import type { providers, Account } from 'near-api-js';
3
import {
4
  CallBackArgs,
5
  ContractCall,
6
  TxnOptionalSignerId,
7
  NearExecuteOptions,
8
  ExecuteArgsResponse,
9
  ComposableCall,
10
} from '../types';
11
import BN from 'bn.js';
1✔
12
import { NoSigningMethodPassedError } from '../errors';
1✔
13

14

15
/**
16
 * checkCallbackUrl()
17
 * method to check if its a regular browser wallet or a InjectedWallet, and make them have the same behavior (redirect) to the desired url.
18
 * @param callbackUrl url that should redirect after transaction
19
 * @param callbackArgs metadata that should be passed via url to the success page
20
 * @returns an outcome object or an array of outcome objects if batching calls {@link FinalExecutionOutcome[]} | {@link FinalExecutionOutcome}, or a redirect to selected callbackUrl
21
 */
22
export const checkCallbackUrl = (
1✔
23
  callbackUrl: string,
24
  callbackArgs: CallBackArgs,
25
  wallet: Wallet,
26
  outcomes: void | FinalExecutionOutcome[],
27
): void | FinalExecutionOutcome[] | FinalExecutionOutcome => {
28
  const browserWallets = ['my-near-wallet', 'near-wallet'];
6✔
29
  const isNotBrowserWallet = !browserWallets.includes(wallet?.id);
6✔
30
  const hasCallbackUrl = Boolean(typeof window !== 'undefined' && callbackUrl?.length > 0);
6!
31

32
  if (hasCallbackUrl && isNotBrowserWallet) {
6!
33

34
    const { transactionHash } = checkTransactionHash(outcomes);
×
35

36
    let finalUrl = `${callbackUrl}?transactionHashes=${transactionHash}`;
×
37

38
    if (callbackArgs) {
×
39

40
      const args = JSON.stringify({
×
41
        type: callbackArgs?.type ?? '',
×
42
        args: callbackArgs?.args ?? '',
×
43
      });
44

45
      const signMeta = encodeURIComponent(args);
×
46

47
      finalUrl = `${callbackUrl}?transactionHashes=${transactionHash}&signMeta=${signMeta}`;
×
48
    }
49
    return window.location.assign(finalUrl);
×
50
  }
51

52
  return outcomes && outcomes.length == 1?  outcomes[0]: outcomes;
6✔
53
};
54

55

56
/**
57
 * checkTransactionHash()
58
 * check what transaction receipt to return to the user.
59
 * @param receipt near transaction Receipt object
60
 * @returns transactionHash object
61
 */
62
const checkTransactionHash = (receipt): {transactionHash: string}  => {
1✔
63
  let transactionHash = receipt?.transaction_outcome?.id;
×
64

65
  if (receipt?.length == 1) {
×
66
    transactionHash = receipt[0]?.transaction_outcome?.id;
×
67
  }
68

69
  if (receipt?.length > 1) {
×
70
    transactionHash = receipt[1]?.transaction_outcome?.id;
×
71
  }
72

73
  return { transactionHash };
×
74
};
75

76

77
export const callbackUrlFormatter = (callbackUrl: string, callbackArgs: CallBackArgs): string => {
1✔
78

79
  let url = callbackUrl && typeof callbackUrl !== 'undefined' ?  callbackUrl : null;
6✔
80

81
  if (callbackArgs?.type && callbackUrl) {
6!
82
    const args = JSON.stringify({
×
83
      type: callbackArgs?.type,
×
84
      args: callbackArgs?.args,
×
85
    });
86

87
    const signMeta = encodeURIComponent(args);
×
88
    url = `${callbackUrl}/?signMeta=${signMeta}`;
×
89
  }
90

91
  return url;
6✔
92
};
93

94
export const genericBatchExecute = async (
1✔
95
  call: ContractCall<ExecuteArgsResponse>[],
96
  wallet: Wallet,
97
  account: Account,
98
  callbackUrl: string,
99
  callbackArgs: CallBackArgs,
100
): Promise<void | providers.FinalExecutionOutcome[]> =>{
6✔
101
  const url = callbackUrlFormatter(callbackUrl, callbackArgs);
6✔
102
  if (wallet) {
6✔
103
    return url ?  batchExecuteWithBrowserWallet(call, wallet, url) : batchExecuteWithBrowserWallet(call, wallet);
3✔
104
  }
105
  return batchExecuteWithNearAccount(call, account, url);
3✔
106
};
107

108
// account call translation wrappers https://docs.near.org/tools/near-api-js/faq#how-to-send-batch-transactions
109
// TODO: share batch signature with wallet selector sendAndSignTransaction when method becomes public
110
const batchExecuteWithNearAccount = async (
1✔
111
  calls: ContractCall<ExecuteArgsResponse>[],
112
  account: Account,
113
  callbackUrl?: string,
114
): Promise<FinalExecutionOutcome[]> => {
3✔
115
  const outcomes: FinalExecutionOutcome[] =[];
3✔
116
  for (const call of calls) {
3✔
117

118
    try {
9✔
119
      outcomes.push(await account.functionCall({
9✔
120
        contractId: call.contractAddress,
121
        methodName: call.methodName,
122
        args: call.args,
123
        gas: new BN(call.gas),
124
        attachedDeposit: new BN(call.deposit),
125
        ...(callbackUrl && { walletCallbackUrl: callbackUrl }),
9!
126
      }));
127
    } catch (err: unknown) {
128
      console.error(
3✔
129
        `${call.contractAddress}:${call.methodName} in batch failed: ${err}`,
130
      );
131
    }
132
  }
133

134
  return outcomes;
3✔
135
};
136

137
const batchExecuteWithBrowserWallet = async (
1✔
138
  calls: ContractCall<ExecuteArgsResponse>[],
139
  wallet: Wallet,
140
  callback?: string,
141
): Promise<void | FinalExecutionOutcome[]> => {
3✔
142

143
  const res = await wallet.signAndSendTransactions({
3✔
144
    transactions: calls.map(convertGenericCallToWalletCall) as TxnOptionalSignerId[],
145
    ...(callback && { callbackUrl: callback }),
4✔
146
  });
147

148
  return res;
3✔
149
};
150

151

152
export const convertGenericCallToWalletCall = (
1✔
153
  call: ContractCall<ExecuteArgsResponse>,
154
): TxnOptionalSignerId => {
155
  return {
5✔
156
    signerId: call.signerId,
157
    receiverId: call.contractAddress,
158
    actions:[{
159
      type: 'FunctionCall',
160
      params: {
161
        methodName: call.methodName,
162
        args: call.args,
163
        gas: call.gas as string,
164
        deposit: call.deposit as string,
165
      },
166
    }],
167
  };
168
};
169

170
export function flattenArgs(calls: ComposableCall[] ): ContractCall<ExecuteArgsResponse>[] {
1✔
171
  const contractCalls: ContractCall<ExecuteArgsResponse>[] =[];
6✔
172
  for (const call of calls) {
6✔
173
    if (call instanceof Array && call.length > 0 && call as ContractCall<ExecuteArgsResponse>[]) {
11✔
174
      call.map((item: ComposableCall) => contractCalls.push(item as ContractCall<ExecuteArgsResponse>));
7✔
175
    } else {
176
      contractCalls.push(call as ContractCall<ExecuteArgsResponse>);
7✔
177
    }
178
  }
179
  return contractCalls;
6✔
180
}
181

182

183
export const validateSigningOptions = ({ wallet, account }: NearExecuteOptions): void => {
1✔
184
  if (!wallet && !account) {
7✔
185
    throw NoSigningMethodPassedError;
1✔
186
  }
187
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc