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

Mintbase / mintbase-js / 4053782737

pending completion
4053782737

push

github

Ruben Marcus
fix test

521 of 749 branches covered (69.56%)

Branch coverage included in aggregate %.

764 of 837 relevant lines covered (91.28%)

4.61 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 { BrowserWalletSignAndSendTransactionParams } from '@near-wallet-selector/core/lib/wallet';
3
import type { providers, Account } from 'near-api-js';
4
import { NearContractCall, CallBackArgs, ContractCall, TxnOptionalSignerId, NearExecuteOptions, ExecuteArgsResponse } from '../types';
5
import BN from 'bn.js';
1✔
6
import { NoSigningMethodPassedError } from '../errors';
1✔
7

8

9
/**
10
 * checkCallbackUrl()
11
 * method to check if its a regular browser wallet or a InjectedWallet, and make them have the same behavior (redirect) to the desired url.
12
 * @param callbackUrl url that should redirect after transaction
13
 * @param callbackArgs metadata that should be passed via url to the success page 
14
 * @returns an outcome object or an array of outcome objects if batching calls {@link FinalExecutionOutcome[]} | {@link FinalExecutionOutcome}, or a redirect to selected callbackUrl
15
 */
16

17
export const checkCallbackUrl = (callbackUrl: string, callbackArgs: CallBackArgs ,wallet: Wallet, outcomes: void | FinalExecutionOutcome[]): 
1✔
18
void | FinalExecutionOutcome[] | FinalExecutionOutcome => {
19

20
  const browserWallets = ['my-near-wallet', 'near-wallet'];
6✔
21
  const IsntBrowserWallets = !browserWallets.includes(wallet?.id);
6✔
22
  const hasCallbackUrl = Boolean(typeof window !== 'undefined' && callbackUrl?.length > 0);
6!
23
  
24
  if (hasCallbackUrl && IsntBrowserWallets) {
6!
25
    
26
    const { transactionHash } = checkTransactionHash(outcomes);
×
27
    
28
    let finalUrl = `${callbackUrl}?transactionHashes=${transactionHash}`;
×
29

30
    if (callbackArgs) {
×
31

32
      const args = JSON.stringify({
×
33
        type: callbackArgs?.type ?? '',
×
34
        args: callbackArgs?.args ?? '',
×
35
      });
36

37
      const signMeta = encodeURIComponent(args);
×
38
      
39
      finalUrl = `${callbackUrl}?transactionHashes=${transactionHash}&signMeta=${signMeta}`;
×
40
    }
41

42
    return window.location.assign(finalUrl);
×
43
  }
44

45
  return outcomes && outcomes.length == 1?  outcomes[0]: outcomes;
6✔
46

47
};
48

49

50
/**
51
 * checkTransactionHash()
52
 * check what transaction receipt to return to the user.
53
 * @param receipt near transaction Receipt object
54
 * @returns transactionHash object
55
 */
56

57

58
const checkTransactionHash = (receipt): {transactionHash: string}  => {
1✔
59
  let transactionHash = receipt?.transaction_outcome?.id;
×
60

61
  if (receipt?.length == 1) {
×
62
    transactionHash = receipt[0]?.transaction_outcome?.id;
×
63
  }
64

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

69
  return { transactionHash };
×
70
};
71

72

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

75
  let url = callbackUrl && typeof callbackUrl !== 'undefined' ?  callbackUrl : null;
6✔
76

77
  if (callbackArgs?.type && callbackUrl) {
6!
78
    const args = JSON.stringify({
×
79
      type: callbackArgs?.type,
×
80
      args: callbackArgs?.args,
×
81
    });
82

83
    const signMeta = encodeURIComponent(args);
×
84
    url = `${callbackUrl}/?signMeta=${signMeta}`;
×
85
  }
86

87
  return url;
6✔
88
};
89

90
export const genericBatchExecute 
1✔
91
 = async (call: ContractCall<ExecuteArgsResponse>[], wallet: Wallet, account: Account, callbackUrl: string , callbackArgs: CallBackArgs): Promise<void | providers.FinalExecutionOutcome[]> =>{
6✔
92

93
   const url = callbackUrlFormatter(callbackUrl, callbackArgs);
6✔
94

95
   if (wallet) {
6✔
96
     return url ?  batchExecuteWithBrowserWallet(call, wallet, url) : batchExecuteWithBrowserWallet(call, wallet);
3✔
97
   }
98
   return batchExecuteWithNearAccount(call, account, url);
3✔
99

100
 };
101

102
// account call translation wrappers https://docs.near.org/tools/near-api-js/faq#how-to-send-batch-transactions
103
// TODO: share batch signature with wallet selector sendAndSignTransaction when method becomes public
104

105
const batchExecuteWithNearAccount = async (
1✔
106
  calls: ContractCall<ExecuteArgsResponse>[],
107
  account: Account,
108
  callbackUrl?: string,
109
): Promise<FinalExecutionOutcome[]> => {
3✔
110
  const outcomes: any[] =[];
3✔
111
  for (const call of calls) {
3✔
112

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

129

130
  return outcomes;
3✔
131
};
132

133
const batchExecuteWithBrowserWallet = async (
1✔
134
  calls: ContractCall<ExecuteArgsResponse>[],
135
  wallet: Wallet,
136
  callback?: string,
137
): Promise<void | FinalExecutionOutcome[]> => {
3✔
138

139
  const res = await wallet.signAndSendTransactions({
3✔
140
    transactions: calls.map(convertGenericCallToWalletCall) as TxnOptionalSignerId[],
141
    ...(callback && { callbackUrl: callback }),
4✔
142
  }); 
143

144
  return res;
3✔
145
};
146

147

148
export const convertGenericCallToWalletCall = (
1✔
149
  call: ContractCall<ExecuteArgsResponse>,
150
): BrowserWalletSignAndSendTransactionParams | TxnOptionalSignerId => {
151

152
  return {
5✔
153
    signerId: call.signerId,
154
    receiverId: call.contractAddress,
155
    actions:[{
156
      type: 'FunctionCall',
157
      params: {
158
        methodName: call.methodName,
159
        args: call.args,
160
        gas: call.gas as string,
161
        deposit: call.deposit as string,
162
      },
163
    }],
164
  };
165
};
166

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

179

180
export const validateSigningOptions = ({ wallet, account }: NearExecuteOptions): void => {
1✔
181
  if (!wallet && !account) {
7✔
182
    throw NoSigningMethodPassedError;
1✔
183
  }
184
};
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