From be40bacd85b6f96f3afa254f4242b59b51365d06 Mon Sep 17 00:00:00 2001 From: Abdul Baseer Khan Date: Thu, 11 Aug 2022 15:58:48 +0500 Subject: [PATCH 1/7] feat: signAndSendBundledActions method added to expose the signAndSendTransaction --- packages/near-api-js/src/account.ts | 8 ++++++++ packages/near-api-js/src/account_multisig.ts | 16 ++++++++++++++++ packages/near-api-js/src/wallet-account.ts | 8 ++++++++ packages/near-api-js/test/account.test.js | 10 ++++++++++ .../near-api-js/test/account_multisig.test.js | 14 +++++++++++++- 5 files changed, 55 insertions(+), 1 deletion(-) diff --git a/packages/near-api-js/src/account.ts b/packages/near-api-js/src/account.ts index a9ea789c38..b4ebe2faa6 100644 --- a/packages/near-api-js/src/account.ts +++ b/packages/near-api-js/src/account.ts @@ -221,6 +221,14 @@ export class Account { ); } + /** + * Sign a transaction to perform a list of actions and broadcast it using the RPC API. + * @see {@link JsonRpcProvider.sendTransaction} + */ + signAndSendBundledActions(options: SignAndSendTransactionOptions): Promise { + return this.signAndSendTransaction(options); + } + /** * Sign a transaction to preform a list of actions and broadcast it using the RPC API. * @see {@link providers/json-rpc-provider!JsonRpcProvider#sendTransaction | JsonRpcProvider.sendTransaction} diff --git a/packages/near-api-js/src/account_multisig.ts b/packages/near-api-js/src/account_multisig.ts index 352f8554b8..d16153a32c 100644 --- a/packages/near-api-js/src/account_multisig.ts +++ b/packages/near-api-js/src/account_multisig.ts @@ -62,6 +62,14 @@ export class AccountMultisig extends Account { return super.signAndSendTransaction({ receiverId, actions }); } + /** + * Sign a transaction to perform a list of actions and broadcast it using the RPC API. + * @see {@link JsonRpcProvider.sendTransaction} + */ + signAndSendBundledActions(options: SignAndSendTransactionOptions): Promise { + return this.signAndSendTransaction(options); + } + protected async signAndSendTransaction({ receiverId, actions }: SignAndSendTransactionOptions): Promise { const { accountId } = this; @@ -230,6 +238,14 @@ export class Account2FA extends AccountMultisig { this.onConfirmResult = options.onConfirmResult; } + /** + * Sign a transaction to perform a list of actions and broadcast it using the RPC API. + * @see {@link JsonRpcProvider.sendTransaction} + */ + signAndSendBundledActions(options: SignAndSendTransactionOptions): Promise { + return this.signAndSendTransaction(options); + } + /** * Sign a transaction to preform a list of actions and broadcast it using the RPC API. * @see {@link providers/json-rpc-provider!JsonRpcProvider#sendTransaction | JsonRpcProvider.sendTransaction} diff --git a/packages/near-api-js/src/wallet-account.ts b/packages/near-api-js/src/wallet-account.ts index c7e685d4f9..59cebfe71f 100644 --- a/packages/near-api-js/src/wallet-account.ts +++ b/packages/near-api-js/src/wallet-account.ts @@ -291,6 +291,14 @@ export class ConnectedWalletAccount extends Account { // Overriding Account methods + /** + * Sign a transaction to perform a list of actions and broadcast it using the RPC API. + * @see {@link JsonRpcProvider.sendTransaction} + */ + signAndSendBundledActions(options: SignAndSendTransactionOptions): Promise { + return this.signAndSendTransaction(options); + } + /** * Sign a transaction by redirecting to the NEAR Wallet * @see {@link WalletConnection.requestSignTransactions} diff --git a/packages/near-api-js/test/account.test.js b/packages/near-api-js/test/account.test.js index ad998f0b46..38460becf9 100644 --- a/packages/near-api-js/test/account.test.js +++ b/packages/near-api-js/test/account.test.js @@ -4,6 +4,7 @@ const testUtils = require('./test-utils'); const { TypedError } = require('../src/utils/errors'); const fs = require('fs'); const BN = require('bn.js'); +const { transfer } = require('../src/transaction'); let nearjs; let workingAccount; @@ -46,6 +47,15 @@ test('send money', async() => { expect(state.amount).toEqual(new BN(receiverAmount).add(new BN(10000)).toString()); }); +test('send money through signAndSendBundledActions', async() => { + const sender = await testUtils.createAccount(nearjs); + const receiver = await testUtils.createAccount(nearjs); + const { amount: receiverAmount } = await receiver.state(); + await sender.signAndSendBundledActions({receiverId: receiver.accountId, actions: [transfer(new BN(10000))]}); + const state = await receiver.state(); + expect(state.amount).toEqual(new BN(receiverAmount).add(new BN(10000)).toString()); +}); + test('delete account', async() => { const sender = await testUtils.createAccount(nearjs); const receiver = await testUtils.createAccount(nearjs); diff --git a/packages/near-api-js/test/account_multisig.test.js b/packages/near-api-js/test/account_multisig.test.js index d88bf6c277..eb7e6901e3 100644 --- a/packages/near-api-js/test/account_multisig.test.js +++ b/packages/near-api-js/test/account_multisig.test.js @@ -4,6 +4,7 @@ const fs = require('fs'); const BN = require('bn.js'); const testUtils = require('./test-utils'); const semver = require('semver'); +const { transfer } = require('../src/transaction'); let nearjs; let startFromVersion; @@ -119,5 +120,16 @@ describe('account2fa transactions', () => { const state = await receiver.state(); expect(BigInt(state.amount)).toBeGreaterThanOrEqual(BigInt(new BN(receiverAmount).add(new BN(parseNearAmount('0.9'))).toString())); }); - + + test('send money through signAndSendBundledActions', async() => { + let sender = await testUtils.createAccount(nearjs); + let receiver = await testUtils.createAccount(nearjs); + sender = await getAccount2FA(sender); + receiver = await getAccount2FA(receiver); + const { amount: receiverAmount } = await receiver.state(); + await sender.signAndSendBundledActions({receiverId: receiver.accountId, actions: [transfer(new BN(parseNearAmount('1')))]}); + const state = await receiver.state(); + expect(BigInt(state.amount)).toBeGreaterThanOrEqual(BigInt(new BN(receiverAmount).add(new BN(parseNearAmount('0.9'))).toString())); + }); + }); From e920ba82fa2098c116b44cabb9468611e3d84756 Mon Sep 17 00:00:00 2001 From: Abdul Baseer Khan Date: Thu, 11 Aug 2022 16:12:32 +0500 Subject: [PATCH 2/7] fix: vscode folder added in gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 58cac35eb0..c6cd4ab017 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ #IDE .idea +.vscode storage node_modules From 490343918d63da993033e21ba927d2dc7469afb3 Mon Sep 17 00:00:00 2001 From: esaminu Date: Fri, 30 Sep 2022 20:04:24 +0400 Subject: [PATCH 3/7] feat(near-api-js): make signAndSendTransaction public --- packages/near-api-js/src/account.ts | 10 +--------- packages/near-api-js/src/account_multisig.ts | 18 +----------------- packages/near-api-js/src/wallet-account.ts | 10 +--------- packages/near-api-js/test/account.test.js | 2 +- .../near-api-js/test/account_multisig.test.js | 4 ++-- 5 files changed, 6 insertions(+), 38 deletions(-) diff --git a/packages/near-api-js/src/account.ts b/packages/near-api-js/src/account.ts index b4ebe2faa6..081e86e491 100644 --- a/packages/near-api-js/src/account.ts +++ b/packages/near-api-js/src/account.ts @@ -221,19 +221,11 @@ export class Account { ); } - /** - * Sign a transaction to perform a list of actions and broadcast it using the RPC API. - * @see {@link JsonRpcProvider.sendTransaction} - */ - signAndSendBundledActions(options: SignAndSendTransactionOptions): Promise { - return this.signAndSendTransaction(options); - } - /** * Sign a transaction to preform a list of actions and broadcast it using the RPC API. * @see {@link providers/json-rpc-provider!JsonRpcProvider#sendTransaction | JsonRpcProvider.sendTransaction} */ - protected async signAndSendTransaction({ receiverId, actions, returnError }: SignAndSendTransactionOptions): Promise { + async signAndSendTransaction({ receiverId, actions, returnError }: SignAndSendTransactionOptions): Promise { let txHash, signedTx; // TODO: TX_NONCE (different constants for different uses of exponentialBackoff?) const result = await exponentialBackoff(TX_NONCE_RETRY_WAIT, TX_NONCE_RETRY_NUMBER, TX_NONCE_RETRY_WAIT_BACKOFF, async () => { diff --git a/packages/near-api-js/src/account_multisig.ts b/packages/near-api-js/src/account_multisig.ts index d16153a32c..59c88f91b7 100644 --- a/packages/near-api-js/src/account_multisig.ts +++ b/packages/near-api-js/src/account_multisig.ts @@ -62,15 +62,7 @@ export class AccountMultisig extends Account { return super.signAndSendTransaction({ receiverId, actions }); } - /** - * Sign a transaction to perform a list of actions and broadcast it using the RPC API. - * @see {@link JsonRpcProvider.sendTransaction} - */ - signAndSendBundledActions(options: SignAndSendTransactionOptions): Promise { - return this.signAndSendTransaction(options); - } - - protected async signAndSendTransaction({ receiverId, actions }: SignAndSendTransactionOptions): Promise { + async signAndSendTransaction({ receiverId, actions }: SignAndSendTransactionOptions): Promise { const { accountId } = this; const args = Buffer.from(JSON.stringify({ @@ -238,14 +230,6 @@ export class Account2FA extends AccountMultisig { this.onConfirmResult = options.onConfirmResult; } - /** - * Sign a transaction to perform a list of actions and broadcast it using the RPC API. - * @see {@link JsonRpcProvider.sendTransaction} - */ - signAndSendBundledActions(options: SignAndSendTransactionOptions): Promise { - return this.signAndSendTransaction(options); - } - /** * Sign a transaction to preform a list of actions and broadcast it using the RPC API. * @see {@link providers/json-rpc-provider!JsonRpcProvider#sendTransaction | JsonRpcProvider.sendTransaction} diff --git a/packages/near-api-js/src/wallet-account.ts b/packages/near-api-js/src/wallet-account.ts index 59cebfe71f..64b902c92d 100644 --- a/packages/near-api-js/src/wallet-account.ts +++ b/packages/near-api-js/src/wallet-account.ts @@ -291,19 +291,11 @@ export class ConnectedWalletAccount extends Account { // Overriding Account methods - /** - * Sign a transaction to perform a list of actions and broadcast it using the RPC API. - * @see {@link JsonRpcProvider.sendTransaction} - */ - signAndSendBundledActions(options: SignAndSendTransactionOptions): Promise { - return this.signAndSendTransaction(options); - } - /** * Sign a transaction by redirecting to the NEAR Wallet * @see {@link WalletConnection.requestSignTransactions} */ - protected async signAndSendTransaction({ receiverId, actions, walletMeta, walletCallbackUrl = window.location.href }: SignAndSendTransactionOptions): Promise { + async signAndSendTransaction({ receiverId, actions, walletMeta, walletCallbackUrl = window.location.href }: SignAndSendTransactionOptions): Promise { const localKey = await this.connection.signer.getPublicKey(this.accountId, this.connection.networkId); let accessKey = await this.accessKeyForTransaction(receiverId, actions, localKey); if (!accessKey) { diff --git a/packages/near-api-js/test/account.test.js b/packages/near-api-js/test/account.test.js index 38460becf9..fa6e3118c7 100644 --- a/packages/near-api-js/test/account.test.js +++ b/packages/near-api-js/test/account.test.js @@ -51,7 +51,7 @@ test('send money through signAndSendBundledActions', async() => { const sender = await testUtils.createAccount(nearjs); const receiver = await testUtils.createAccount(nearjs); const { amount: receiverAmount } = await receiver.state(); - await sender.signAndSendBundledActions({receiverId: receiver.accountId, actions: [transfer(new BN(10000))]}); + await sender.signAndSendTransaction({receiverId: receiver.accountId, actions: [transfer(new BN(10000))]}); const state = await receiver.state(); expect(state.amount).toEqual(new BN(receiverAmount).add(new BN(10000)).toString()); }); diff --git a/packages/near-api-js/test/account_multisig.test.js b/packages/near-api-js/test/account_multisig.test.js index eb7e6901e3..ff638c4bc7 100644 --- a/packages/near-api-js/test/account_multisig.test.js +++ b/packages/near-api-js/test/account_multisig.test.js @@ -121,13 +121,13 @@ describe('account2fa transactions', () => { expect(BigInt(state.amount)).toBeGreaterThanOrEqual(BigInt(new BN(receiverAmount).add(new BN(parseNearAmount('0.9'))).toString())); }); - test('send money through signAndSendBundledActions', async() => { + test('send money through signAndSendTransaction', async() => { let sender = await testUtils.createAccount(nearjs); let receiver = await testUtils.createAccount(nearjs); sender = await getAccount2FA(sender); receiver = await getAccount2FA(receiver); const { amount: receiverAmount } = await receiver.state(); - await sender.signAndSendBundledActions({receiverId: receiver.accountId, actions: [transfer(new BN(parseNearAmount('1')))]}); + await sender.signAndSendTransaction({receiverId: receiver.accountId, actions: [transfer(new BN(parseNearAmount('1')))]}); const state = await receiver.state(); expect(BigInt(state.amount)).toBeGreaterThanOrEqual(BigInt(new BN(receiverAmount).add(new BN(parseNearAmount('0.9'))).toString())); }); From d23d8969ba9026d53399665e2c45cde74442a177 Mon Sep 17 00:00:00 2001 From: esaminu Date: Fri, 30 Sep 2022 20:06:49 +0400 Subject: [PATCH 4/7] chore: add changeset --- .changeset/lazy-candles-hide.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/lazy-candles-hide.md diff --git a/.changeset/lazy-candles-hide.md b/.changeset/lazy-candles-hide.md new file mode 100644 index 0000000000..4b35fc699e --- /dev/null +++ b/.changeset/lazy-candles-hide.md @@ -0,0 +1,5 @@ +--- +"near-api-js": minor +--- + +Make Account.signAndSendTransaction public so transactions can be sent directly from Account instances From 6a35ac621a1d6cf26a188d161e1ddbb579b660f4 Mon Sep 17 00:00:00 2001 From: esaminu Date: Fri, 30 Sep 2022 20:08:03 +0400 Subject: [PATCH 5/7] fix: test name --- packages/near-api-js/test/account.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/near-api-js/test/account.test.js b/packages/near-api-js/test/account.test.js index fa6e3118c7..7839d31814 100644 --- a/packages/near-api-js/test/account.test.js +++ b/packages/near-api-js/test/account.test.js @@ -47,7 +47,7 @@ test('send money', async() => { expect(state.amount).toEqual(new BN(receiverAmount).add(new BN(10000)).toString()); }); -test('send money through signAndSendBundledActions', async() => { +test('send money through signAndSendTransaction', async() => { const sender = await testUtils.createAccount(nearjs); const receiver = await testUtils.createAccount(nearjs); const { amount: receiverAmount } = await receiver.state(); From 115bd7b108425a71cf9dd3276461651149a0ea92 Mon Sep 17 00:00:00 2001 From: esaminu Date: Fri, 30 Sep 2022 20:26:47 +0400 Subject: [PATCH 6/7] fix: make Account2FA.signAndSendTransaction public --- packages/near-api-js/src/account_multisig.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/near-api-js/src/account_multisig.ts b/packages/near-api-js/src/account_multisig.ts index 59c88f91b7..31412ba484 100644 --- a/packages/near-api-js/src/account_multisig.ts +++ b/packages/near-api-js/src/account_multisig.ts @@ -234,7 +234,7 @@ export class Account2FA extends AccountMultisig { * Sign a transaction to preform a list of actions and broadcast it using the RPC API. * @see {@link providers/json-rpc-provider!JsonRpcProvider#sendTransaction | JsonRpcProvider.sendTransaction} */ - protected async signAndSendTransaction({ receiverId, actions }: SignAndSendTransactionOptions): Promise { + async signAndSendTransaction({ receiverId, actions }: SignAndSendTransactionOptions): Promise { await super.signAndSendTransaction({ receiverId, actions }); // TODO: Should following override onRequestResult in superclass instead of doing custom signAndSendTransaction? await this.sendCode(); From 330b2dbabd6f3bce0bf80fa799380b9d58227efe Mon Sep 17 00:00:00 2001 From: esaminu Date: Sat, 1 Oct 2022 00:05:32 +0400 Subject: [PATCH 7/7] chore: update changeset --- .changeset/beige-suits-march.md | 5 +++++ .changeset/lazy-candles-hide.md | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 .changeset/beige-suits-march.md delete mode 100644 .changeset/lazy-candles-hide.md diff --git a/.changeset/beige-suits-march.md b/.changeset/beige-suits-march.md new file mode 100644 index 0000000000..ff665f4860 --- /dev/null +++ b/.changeset/beige-suits-march.md @@ -0,0 +1,5 @@ +--- +"near-api-js": major +--- + +Make `Account.signAndSendTransaction` `public` so transactions can be sent directly from `Account` instances diff --git a/.changeset/lazy-candles-hide.md b/.changeset/lazy-candles-hide.md deleted file mode 100644 index 4b35fc699e..0000000000 --- a/.changeset/lazy-candles-hide.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"near-api-js": minor ---- - -Make Account.signAndSendTransaction public so transactions can be sent directly from Account instances