Skip to content

Commit

Permalink
nonce increment after sign before send
Browse files Browse the repository at this point in the history
  • Loading branch information
juliancwirko committed Jun 4, 2023
1 parent 08ff5fb commit eb8bd5b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export interface TransactionCallbackParams {
txResult?: ITransactionOnNetwork | null;
}

export const preSendTxOperations = (signedTx: Transaction) => {
const sender = signedTx.getSender();
const currentNonce = signedTx.getNonce().valueOf();
const senderAccount = new Account(sender);
senderAccount.incrementNonce();
setAccountState('nonce', currentNonce + 1);
};

export const postSendTxOperations = async (
signedTx: Transaction,
setTransaction: Dispatch<SetStateAction<Transaction | null>>,
Expand All @@ -47,7 +55,7 @@ export const postSendTxOperations = async (
}
};

export const sendTxOperations = async (
export const signAndSendTxOperations = async (
dappProvider: DappProvider,
tx: Transaction,
loginInfoSnap: LoginInfoState,
Expand Down Expand Up @@ -77,6 +85,7 @@ export const sendTxOperations = async (
signedTx = await dappProvider.signTransaction(tx);
}
if (loginInfoSnap.loginMethod !== LoginMethodsEnum.wallet) {
preSendTxOperations(signedTx);
await apiNetworkProvider.sendTransaction(signedTx);
await postSendTxOperations(
signedTx,
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/common-helpers/useWebWalletTxSend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { Transaction, ITransactionOnNetwork } from '@multiversx/sdk-core';
import { getParamFromUrl } from '../../utils/getParamFromUrl';
import {
postSendTxOperations,
preSendTxOperations,
TransactionCallbackParams,
} from './sendTxOperations';
} from './signAndSendTxOperations';
import { errorParse } from '../../utils/errorParse';
import { ApiNetworkProvider } from '@multiversx/sdk-network-providers/out';
import { useAccount } from '../useAccount';
Expand Down Expand Up @@ -59,6 +60,7 @@ export const useWebWalletTxSend = ({
const transaction = Transaction.fromPlainObject(transactionObj);

try {
preSendTxOperations(transaction);
await networkStateSnap.apiNetworkProvider.sendTransaction(
transaction
);
Expand Down
12 changes: 3 additions & 9 deletions src/hooks/useTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import {
IGasLimit,
ITransactionValue,
ITransactionOnNetwork,
Account,
} from '@multiversx/sdk-core';
import { useWebWalletTxSend } from './common-helpers/useWebWalletTxSend';
import {
TransactionCallbackParams,
sendTxOperations,
} from './common-helpers/sendTxOperations';
signAndSendTxOperations,
} from './common-helpers/signAndSendTxOperations';
import { DappProvider } from '../types/network';
import { setAccountState } from '../store/auth';
import { ApiNetworkProvider } from '@multiversx/sdk-network-providers/out';
import { useConfig } from './useConfig';
import { useLoginInfo } from './useLoginInfo';
Expand Down Expand Up @@ -85,11 +83,7 @@ export function useTransaction(
sender,
});

const senderAccount = new Account(sender);
senderAccount.incrementNonce();
setAccountState('nonce', currentNonce + 1);

sendTxOperations(
signAndSendTxOperations(
networkStateSnap.dappProvider as DappProvider,
tx,
loginInfoSnap,
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export { useScQuery } from './hooks/useScQuery';
export * from './types/account';
export * from './types/enums';
export * from './types/network';
export { TransactionCallbackParams } from './hooks/common-helpers/sendTxOperations';
export { TransactionCallbackParams } from './hooks/common-helpers/signAndSendTxOperations';
export { SCQueryType } from './hooks/useScQuery';
export { PairingTypes } from '@multiversx/sdk-wallet-connect-provider';

0 comments on commit eb8bd5b

Please sign in to comment.