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

Mintbase / mintbase-js / 4116485634

pending completion
4116485634

push

github

ruisantiago
remove eslint

519 of 722 branches covered (71.88%)

Branch coverage included in aggregate %.

765 of 828 relevant lines covered (92.39%)

4.67 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 {
5
  CallBackArgs,
6
  ContractCall,
7
  TxnOptionalSignerId,
8
  NearExecuteOptions,
9
  ExecuteArgsResponse,
10
  ComposableCall,
11
} from '../types';
12
import BN from 'bn.js';
1✔
13
import { NoSigningMethodPassedError } from '../errors';
1✔
14

15

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

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

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

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

39
    if (callbackArgs) {
×
40

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

46
      const signMeta = encodeURIComponent(args);
×
47

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

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

56

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

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

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

74
  return { transactionHash };
×
75
};
76

77

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

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

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

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

92
  return url;
6✔
93
};
94

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

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

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

135
  return outcomes;
3✔
136
};
137

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

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

149
  return res;
3✔
150
};
151

152

153
export const convertGenericCallToWalletCall = (
1✔
154
  call: ContractCall<ExecuteArgsResponse>,
155
): BrowserWalletSignAndSendTransactionParams | TxnOptionalSignerId => {
156

157
  return {
5✔
158
    signerId: call.signerId,
159
    receiverId: call.contractAddress,
160
    actions:[{
161
      type: 'FunctionCall',
162
      params: {
163
        methodName: call.methodName,
164
        args: call.args,
165
        gas: call.gas as string,
166
        deposit: call.deposit as string,
167
      },
168
    }],
169
  };
170
};
171

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

184

185
export const validateSigningOptions = ({ wallet, account }: NearExecuteOptions): void => {
1✔
186
  if (!wallet && !account) {
7✔
187
    throw NoSigningMethodPassedError;
1✔
188
  }
189
};
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