Skip to content

Commit

Permalink
Merge pull request #1002 from near/NAJ-44-make-sign-and-send-transact…
Browse files Browse the repository at this point in the history
…ion-functionality-public

Naj 44 make sign and send transaction functionality public
  • Loading branch information
esaminu authored Oct 3, 2022
2 parents 4f3a69b + 330b2db commit a330763
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-suits-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"near-api-js": major
---

Make `Account.signAndSendTransaction` `public` so transactions can be sent directly from `Account` instances
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#IDE
.idea
.vscode

storage
node_modules
Expand Down
2 changes: 1 addition & 1 deletion packages/near-api-js/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FinalExecutionOutcome> {
async signAndSendTransaction({ receiverId, actions, returnError }: SignAndSendTransactionOptions): Promise<FinalExecutionOutcome> {
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 () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/near-api-js/src/account_multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class AccountMultisig extends Account {
return super.signAndSendTransaction({ receiverId, actions });
}

protected async signAndSendTransaction({ receiverId, actions }: SignAndSendTransactionOptions): Promise<FinalExecutionOutcome> {
async signAndSendTransaction({ receiverId, actions }: SignAndSendTransactionOptions): Promise<FinalExecutionOutcome> {
const { accountId } = this;

const args = Buffer.from(JSON.stringify({
Expand Down Expand Up @@ -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<FinalExecutionOutcome> {
async signAndSendTransaction({ receiverId, actions }: SignAndSendTransactionOptions): Promise<FinalExecutionOutcome> {
await super.signAndSendTransaction({ receiverId, actions });
// TODO: Should following override onRequestResult in superclass instead of doing custom signAndSendTransaction?
await this.sendCode();
Expand Down
2 changes: 1 addition & 1 deletion packages/near-api-js/src/wallet-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FinalExecutionOutcome> {
async signAndSendTransaction({ receiverId, actions, walletMeta, walletCallbackUrl = window.location.href }: SignAndSendTransactionOptions): Promise<FinalExecutionOutcome> {
const localKey = await this.connection.signer.getPublicKey(this.accountId, this.connection.networkId);
let accessKey = await this.accessKeyForTransaction(receiverId, actions, localKey);
if (!accessKey) {
Expand Down
10 changes: 10 additions & 0 deletions packages/near-api-js/test/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 13 additions & 1 deletion packages/near-api-js/test/account_multisig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
});

});

0 comments on commit a330763

Please sign in to comment.