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/.gitignore b/.gitignore index 58cac35eb0..c6cd4ab017 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ #IDE .idea +.vscode storage node_modules diff --git a/packages/near-api-js/src/account.ts b/packages/near-api-js/src/account.ts index a9ea789c38..081e86e491 100644 --- a/packages/near-api-js/src/account.ts +++ b/packages/near-api-js/src/account.ts @@ -225,7 +225,7 @@ export class Account { * 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 352f8554b8..31412ba484 100644 --- a/packages/near-api-js/src/account_multisig.ts +++ b/packages/near-api-js/src/account_multisig.ts @@ -62,7 +62,7 @@ export class AccountMultisig extends Account { return super.signAndSendTransaction({ receiverId, actions }); } - protected async signAndSendTransaction({ receiverId, actions }: SignAndSendTransactionOptions): Promise { + async signAndSendTransaction({ receiverId, actions }: SignAndSendTransactionOptions): Promise { const { accountId } = this; const args = Buffer.from(JSON.stringify({ @@ -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(); diff --git a/packages/near-api-js/src/wallet-account.ts b/packages/near-api-js/src/wallet-account.ts index c7e685d4f9..64b902c92d 100644 --- a/packages/near-api-js/src/wallet-account.ts +++ b/packages/near-api-js/src/wallet-account.ts @@ -295,7 +295,7 @@ export class ConnectedWalletAccount extends Account { * 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 ad998f0b46..7839d31814 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 signAndSendTransaction', async() => { + const sender = await testUtils.createAccount(nearjs); + const receiver = await testUtils.createAccount(nearjs); + const { amount: receiverAmount } = await receiver.state(); + 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()); +}); + 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..ff638c4bc7 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 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.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())); + }); + });