Skip to content

Commit 834e775

Browse files
refactor (#26)
* refactor * refactor * refactor * refactor * refactor * refactor * refactor * refactor
1 parent 0114403 commit 834e775

25 files changed

+229
-98
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const msAccount = MultiSendAccount.fromAccount(account);
5555

5656
// one transaction that contains two actions
5757
const mTx = MultiTransaction
58-
.batch('wrap.near')
58+
.batch({ receiverId: 'wrap.near' })
5959
.functionCall({
6060
methodName: 'ft_transfer',
6161
args: {
@@ -88,7 +88,7 @@ const msAccount = MultiSendAccount.fromAccount(account);
8888

8989
// two transactions, each contains one action
9090
const mTx = MultiTransaction
91-
.batch('wrap.near')
91+
.batch({ receiverId: 'wrap.near' })
9292
.functionCall({
9393
methodName: 'ft_transfer',
9494
args: {
@@ -98,7 +98,7 @@ const mTx = MultiTransaction
9898
attachedDeposit: Amount.ONE_YOCTO,
9999
gas: Gas.parse('10', 'T')
100100
})
101-
.batch('usdt.tether-token.near')
101+
.batch({ receiverId: 'usdt.tether-token.near' })
102102
.functionCall({
103103
methodName: 'ft_transfer',
104104
args: {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"@near-wallet-selector/core": "^8.9.2",
2121
"bignumber.js": "^9.1.2",
2222
"bn.js": "^5.2.1",
23-
"borsher": "^2.3.0",
23+
"borsher": "^2.4.0",
2424
"buffer": "^6.0.3",
2525
"near-api-js": "^2.1.4"
2626
},

src/core/MultiSendAccount.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import {
66
throwReceiptErrorsFromOutcomes,
77
Stringifier,
88
Parser,
9-
parseOutcomeValue,
9+
parseOutcome,
1010
BlockQuery,
1111
} from '../utils';
1212
import { MultiTransaction, EmptyArgs } from './transaction';
13+
import { SendError } from '../errors';
1314

1415
export class MultiSendAccount extends Account implements View, Call, Send {
1516
private constructor(connection: Connection, accountId: string) {
@@ -50,7 +51,7 @@ export class MultiSendAccount extends Account implements View, Call, Send {
5051
*/
5152
async call<Value, Args = EmptyArgs>(options: MultiSendAccountCallOptions<Value, Args>): Promise<Value> {
5253
const outcome = await this.callRaw(options);
53-
return parseOutcomeValue(outcome, options.parser);
54+
return parseOutcome(outcome, options.parser);
5455
}
5556

5657
/**
@@ -65,7 +66,7 @@ export class MultiSendAccount extends Account implements View, Call, Send {
6566
stringifier,
6667
...options
6768
}: MultiSendAccountCallRawOptions<Args>): Promise<FinalExecutionOutcome> {
68-
const mTx = MultiTransaction.batch(contractId).functionCall({
69+
const mTx = MultiTransaction.batch({ receiverId: contractId }).functionCall({
6970
methodName,
7071
args,
7172
attachedDeposit,
@@ -78,23 +79,27 @@ export class MultiSendAccount extends Account implements View, Call, Send {
7879

7980
/**
8081
* Send multiple transactions and return success value of last transaction
81-
* @param mTx Multiple transactions
82-
* @param options Options
82+
* @param mTx mTx
83+
* @param options options
8384
*/
8485
async send<Value>(mTx: MultiTransaction, options?: MultiSendAccountSendOptions<Value>): Promise<Value> {
8586
const outcomes = await this.sendRaw(mTx, options);
8687
const outcome = outcomes[outcomes.length - 1];
87-
return parseOutcomeValue(outcome, options?.parser);
88+
return parseOutcome(outcome, options?.parser);
8889
}
8990

9091
/**
9192
* Send multiple transactions
92-
* @param mTx Multiple transactions
93-
* @param options Options
93+
* @param mTx mTx
94+
* @param options options
9495
*/
9596
async sendRaw(mTx: MultiTransaction, options?: MultiSendAccountSendRawOptions): Promise<FinalExecutionOutcome[]> {
9697
const transactions = parseNearApiJsTransactions(mTx);
9798

99+
if (transactions.length === 0) {
100+
throw new SendError('Transaction not found.');
101+
}
102+
98103
const outcomes: FinalExecutionOutcome[] = [];
99104

100105
for (const transaction of transactions) {

src/core/MultiSendWalletSelector.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ import {
1818
Stringifier,
1919
throwReceiptErrorsFromOutcomes,
2020
endless,
21-
parseOutcomeValue,
21+
parseOutcome,
2222
BlockQuery,
2323
} from '../utils';
2424
import { MultiSendWalletSelectorSendOptions } from '../types';
2525
import { BigNumber } from 'bignumber.js';
26+
import { SendError } from '../errors';
2627

2728
let multiSendWalletSelector: MultiSendWalletSelector | null = null;
2829

2930
export async function setupMultiSendWalletSelector(
30-
options: MultiSendWalletSelectorOptions
31+
options: MultiSendWalletSelectorOptions,
3132
): Promise<MultiSendWalletSelector> {
3233
if (!multiSendWalletSelector) {
3334
let selector: WalletSelector;
@@ -54,7 +55,7 @@ export async function setupMultiSendWalletSelector(
5455

5556
async isLoginAccessKeyActive(
5657
accountId?: string,
57-
requiredMinAllowance = Amount.parse('0.01', 'NEAR')
58+
requiredMinAllowance = Amount.parse('0.01', 'NEAR'),
5859
): Promise<boolean> {
5960
accountId = accountId ?? this.getActiveAccountId();
6061
if (!accountId) {
@@ -73,7 +74,7 @@ export async function setupMultiSendWalletSelector(
7374
const accessKeys = await this.near.account(accountId).then((account) => account.getAccessKeys());
7475
const loginAccessKey = accessKeys.find(
7576
(accessKey) =>
76-
PublicKey.fromString(accessKey.public_key).toString() === PublicKey.fromString(loginPublicKey).toString()
77+
PublicKey.fromString(accessKey.public_key).toString() === PublicKey.fromString(loginPublicKey).toString(),
7778
);
7879

7980
if (!loginAccessKey) {
@@ -115,7 +116,7 @@ export async function setupMultiSendWalletSelector(
115116

116117
async call<Value, Args = EmptyArgs>(options: MultiSendWalletSelectorCallOptions<Value, Args>): Promise<Value> {
117118
const outcome = await this.callRaw(options);
118-
return parseOutcomeValue(outcome, options.parser);
119+
return parseOutcome(outcome, options.parser);
119120
},
120121

121122
async callRaw<Args = EmptyArgs>({
@@ -127,7 +128,7 @@ export async function setupMultiSendWalletSelector(
127128
stringifier,
128129
...options
129130
}: MultiSendWalletSelectorCallRawOptions<Args>): Promise<FinalExecutionOutcome> {
130-
const mTx = MultiTransaction.batch(contractId).functionCall({
131+
const mTx = MultiTransaction.batch({ receiverId: contractId }).functionCall({
131132
methodName,
132133
args,
133134
attachedDeposit,
@@ -141,20 +142,24 @@ export async function setupMultiSendWalletSelector(
141142
async send<Value>(mTx: MultiTransaction, options?: MultiSendWalletSelectorSendOptions<Value>): Promise<Value> {
142143
const outcomes = await this.sendRaw(mTx, options);
143144
const outcome = outcomes?.[outcomes.length - 1];
144-
return parseOutcomeValue(outcome, options?.parser);
145+
return parseOutcome(outcome, options?.parser);
145146
},
146147

147148
async sendRaw(
148149
mTx: MultiTransaction,
149-
options?: MultiSendWalletSelectorSendRawOptions
150+
options?: MultiSendWalletSelectorSendRawOptions,
150151
): Promise<FinalExecutionOutcome[]> {
151-
const wallet = await this.wallet(options?.walletId);
152152
const transactions = parseNearWalletSelectorTransactions(mTx);
153-
let outcomes: FinalExecutionOutcome[] | undefined;
154153

155154
if (transactions.length === 0) {
156-
throw Error(`Transaction not found.`);
157-
} else if (transactions.length === 1) {
155+
throw new SendError('Transaction not found.');
156+
}
157+
158+
const wallet = await this.wallet(options?.walletId);
159+
160+
let outcomes: FinalExecutionOutcome[] | undefined;
161+
162+
if (transactions.length === 1) {
158163
const outcome = await wallet.signAndSendTransaction({
159164
...transactions[0],
160165
callbackUrl: options?.callbackUrl,

0 commit comments

Comments
 (0)