Skip to content

Fix nonce issues in 'near-js/client' #1594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SignedTransactionOptions,
TransactionOptions,
} from '../../interfaces';
import { signTransaction } from '../sign_and_send';
import { getSignerNonce, signTransaction } from '../sign_and_send';
import { TransactionComposer } from './transaction_composer';
import { Account } from '@near-js/accounts';

Expand Down Expand Up @@ -74,16 +74,23 @@ export class SignedTransactionComposer extends TransactionComposer {

/**
* Sign and send the composed transaction
* Fetch the block hash and nonce from the provider if not already set.
* @param blockReference block to use for determining hash
*/
async signAndSend<T extends SerializedReturnValue>(blockReference: BlockReference = { finality: 'final' }) {
this.verifySigner(this.sender);
const { signedTransaction } = await this.toSignedTransaction({
publicKey: this.publicKey || await this.account.getSigner().getPublicKey(),
blockHash: this.blockHash || (await this.account.provider.viewBlock(blockReference))?.header?.hash,
nonce: this.nonce || (await getSignerNonce({ account: this.sender, deps: { rpcProvider: this.account.provider, signer: this.account.getSigner() } })) + 1n
});

const outcome = await this.account.provider.sendTransaction(signedTransaction);
if (outcome.final_execution_status !== 'NONE') {
this.nonce = signedTransaction.transaction.nonce + 1n;
} else if (!this.nonce) {
this.nonce = signedTransaction.transaction.nonce;
}
return {
outcome,
result: getTransactionLastResult(outcome) as T,
Expand Down
21 changes: 19 additions & 2 deletions packages/client/src/transactions/composers/transaction_composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class TransactionComposer {
/**
* Validate and return the object used for Transaction instantiation
* @param transaction transaction values to override composed transaction fields
* @throws an error if any required fields are missing
* @private
*/
private buildTransactionObject(transaction?: TransactionOptions) {
Expand All @@ -49,8 +50,24 @@ export class TransactionComposer {
signerId: transaction?.sender || this.sender,
};

if (!tx.actions.length || !tx.blockHash || !tx.nonce || !tx.publicKey || !tx.receiverId || !tx.signerId) {
throw new Error(`invalid transaction: ${JSON.stringify(tx)}`);
const missingFields = [];
if (!tx.actions.length) missingFields.push('actions');
if (!tx.blockHash) missingFields.push('blockHash');
if (!tx.nonce) missingFields.push('nonce');
if (!tx.publicKey) missingFields.push('publicKey');
if (!tx.receiverId) missingFields.push('receiverId');
if (!tx.signerId) missingFields.push('signerId');

if (missingFields.length > 0) {
const safeTxPreview = {
signerId: tx.signerId,
receiverId: tx.receiverId,
publicKey: tx.publicKey.toString(),
blockHash: tx.blockHash.toString(),
nonce: tx.nonce?.toString?.(),
actions: tx.actions?.map((a) => Object.keys(a)[0]),
};
throw new Error(`invalid transaction: missing ${missingFields.join(', ')}\nPreciew: ${JSON.stringify(safeTxPreview, null, 2)}`);
}

return tx;
Expand Down