From 1e899937274b356023fd2530ba4e5ef968c26906 Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Mon, 28 May 2018 16:58:32 +0200 Subject: [PATCH] calculate nonce runtime (account.nonce + pending.nonce) --- src/js/actions/SendFormActions.js | 15 ++++++++++++--- src/js/reducers/PendingTxReducer.js | 2 ++ src/js/reducers/utils/index.js | 6 ++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/js/actions/SendFormActions.js b/src/js/actions/SendFormActions.js index 4867afd6..03cb0ee0 100644 --- a/src/js/actions/SendFormActions.js +++ b/src/js/actions/SendFormActions.js @@ -45,6 +45,7 @@ export type SendTxAction = { amount: string, total: string, tx: any, + nonce: number, txid: string, txData: any, }; @@ -822,7 +823,8 @@ export const onSend = (): AsyncAction => { const { account, network, - web3 + web3, + pending } = getState().selectedAccount; if (!account || !web3 || !network) return; @@ -830,7 +832,7 @@ export const onSend = (): AsyncAction => { const isToken: boolean = currentState.currency !== currentState.networkSymbol; const w3 = web3.web3; - + const address_n = account.addressPath; let data: string = '0x' + currentState.data; @@ -852,6 +854,12 @@ export const onSend = (): AsyncAction => { txAddress = token.address; } + const pendingAmount: BigNumber = stateUtils.getPendingAmount(pending, currentState.currency); + const pendingNonce: number = stateUtils.getPendingNonce(pending); + const nonce = pendingNonce >= account.nonce ? pendingNonce + 1 : account.nonce; + + console.warn("NONCEE", nonce, account.nonce, stateUtils.getPendingNonce(pending)) + const txData = { address_n, // from: currentAddress.address @@ -859,7 +867,7 @@ export const onSend = (): AsyncAction => { value: txAmount, data, chainId: web3.chainId, - nonce: w3.toHex(account.nonce), + nonce: w3.toHex(nonce), gasLimit: w3.toHex(currentState.gasLimit), gasPrice: w3.toHex( EthereumjsUnits.convert(currentState.gasPrice, 'gwei', 'wei') ), r: '', @@ -920,6 +928,7 @@ export const onSend = (): AsyncAction => { amount: currentState.amount, total: currentState.total, tx, + nonce, txid, txData, }); diff --git a/src/js/reducers/PendingTxReducer.js b/src/js/reducers/PendingTxReducer.js index 7042d812..bb8cebde 100644 --- a/src/js/reducers/PendingTxReducer.js +++ b/src/js/reducers/PendingTxReducer.js @@ -15,6 +15,7 @@ export type PendingTx = { +amount: string; +total: string; +tx: any; + +nonce: number; +address: string; } @@ -31,6 +32,7 @@ const add = (state: State, action: SendTxAction): State => { amount: action.amount, total: action.total, tx: action.tx, + nonce: action.nonce, address: action.account.address, }); return newState; diff --git a/src/js/reducers/utils/index.js b/src/js/reducers/utils/index.js index 3f81e7e2..4f676091 100644 --- a/src/js/reducers/utils/index.js +++ b/src/js/reducers/utils/index.js @@ -91,6 +91,12 @@ export const getAccountPendingTx = (pending: Array, account: ?Account return pending.filter(p => p.network === a.network && p.address === a.address); } +export const getPendingNonce = (pending: Array): number => { + return pending.reduce((value: number, tx: PendingTx) => { + return Math.max(value, tx.nonce); + }, 0); +} + export const getPendingAmount = (pending: Array, currency: string): BigNumber => { return pending.reduce((value: BigNumber, tx: PendingTx) => { if (tx.currency === currency) {