diff --git a/src/flowtype/npm/web3.js b/src/flowtype/npm/web3.js index 110a8e92..8af20550 100644 --- a/src/flowtype/npm/web3.js +++ b/src/flowtype/npm/web3.js @@ -47,11 +47,43 @@ declare module 'web3' { id: string; } + declare export type TransactionStatus = { + blockHash: string, + blockNumber: ?number, + from: string, + gas: number, + gasPrice: BigNumber, + hash: string, + input: string, + nonce: number, + r: string, + s: string, + v: string, + to: string, + transactionIndex: number, + value: BigNumber + } + + declare export type TransactionReceipt = { + blockHash: string, + blockNumber: number, + contractAddress: ?string, + cumulativeGasUsed: number, + from: string, + gasUsed: number, + logs: Array, + status: string, + to: string, + transactionHash: string, + transactionIndex: number + } + declare class Eth { getGasPrice: (callback: (error: Error, gasPrice: string) => void) => void, getBalance: (address: string, callback: (error: Error, balance: BigNumber) => void) => void, getTransactionCount: (address: string, callback: (error: Error, result: number) => void) => void, - getTransaction: (txid: string, callback: (error: Error, result: any) => void) => void, + getTransaction: (txid: string, callback: (error: Error, result: TransactionStatus) => void) => void, + getTransactionReceipt: (txid: string, callback: (error: Error, result: TransactionReceipt) => void) => void, getBlockNumber: (callback: (error: Error, blockNumber: number) => void) => void, getBlock: (hash: string, callback: (error: Error, result: any) => void) => void, // getAccounts: (callback: (error: Error, accounts: Array) => void) => void, diff --git a/src/js/actions/PendingTxActions.js b/src/js/actions/PendingTxActions.js index 1a3eb0fb..3b263dc8 100644 --- a/src/js/actions/PendingTxActions.js +++ b/src/js/actions/PendingTxActions.js @@ -11,5 +11,10 @@ export type PendingTxAction = { type: typeof PENDING.TX_RESOLVED, tx: PendingTx, receipt: Object, - block: string +} | { + type: typeof PENDING.TX_NOT_FOUND, + tx: PendingTx, +} | { + type: typeof PENDING.TX_TOKEN_ERROR, + tx: PendingTx, } \ No newline at end of file diff --git a/src/js/actions/Web3Actions.js b/src/js/actions/Web3Actions.js index e30f64aa..a89f60c1 100644 --- a/src/js/actions/Web3Actions.js +++ b/src/js/actions/Web3Actions.js @@ -27,6 +27,7 @@ import type { PendingTx } from '../reducers/PendingTxReducer'; import type { Web3Instance } from '../reducers/Web3Reducer'; import type { Token } from '../reducers/TokensReducer'; import type { NetworkToken } from '../reducers/LocalStorageReducer'; +import type { TransactionStatus, TransactionReceipt } from 'web3'; export type Web3Action = { type: typeof WEB3.READY, @@ -309,16 +310,28 @@ export const getTransactionReceipt = (tx: PendingTx): AsyncAction => { const web3instance = getState().web3.filter(w3 => w3.network === tx.network)[0]; const web3 = web3instance.web3; - //web3.eth.getTransactionReceipt(txid, (error, tx) => { - web3.eth.getTransaction(tx.id, (error: Error, receipt: any) => { - if (receipt && receipt.blockNumber) { - web3.eth.getBlock(receipt.blockHash, (error, block) => { - dispatch({ - type: PENDING.TX_RESOLVED, - tx, - receipt, - block - }) + web3.eth.getTransaction(tx.id, (error: Error, status: TransactionStatus) => { + if (!error && !status) { + dispatch({ + type: PENDING.TX_NOT_FOUND, + tx, + }) + } else if (status && status.blockNumber) { + web3.eth.getTransactionReceipt(tx.id, (error: Error, receipt: TransactionReceipt) => { + if (receipt) { + if (status.gas !== receipt.gasUsed) { + dispatch({ + type: PENDING.TX_TOKEN_ERROR, + tx + }) + } + dispatch({ + type: PENDING.TX_RESOLVED, + tx, + receipt + }) + } + }); } }); diff --git a/src/js/actions/constants/pendingTx.js b/src/js/actions/constants/pendingTx.js index a6b654a1..08a58b36 100644 --- a/src/js/actions/constants/pendingTx.js +++ b/src/js/actions/constants/pendingTx.js @@ -2,4 +2,6 @@ 'use strict'; export const FROM_STORAGE: 'pending__from_storage' = 'pending__from_storage'; -export const TX_RESOLVED: 'pending__tx_resolved' = 'pending__tx_resolved'; \ No newline at end of file +export const TX_RESOLVED: 'pending__tx_resolved' = 'pending__tx_resolved'; +export const TX_NOT_FOUND: 'pending__tx_not_found' = 'pending__tx_not_found'; +export const TX_TOKEN_ERROR: 'pending__tx_token_error' = 'pending__tx_token_error'; \ No newline at end of file diff --git a/src/js/reducers/PendingTxReducer.js b/src/js/reducers/PendingTxReducer.js index 42950360..42491005 100644 --- a/src/js/reducers/PendingTxReducer.js +++ b/src/js/reducers/PendingTxReducer.js @@ -44,6 +44,7 @@ export default function pending(state: State = initialState, action: Action): St return add(state, action); case PENDING.TX_RESOLVED : + case PENDING.TX_NOT_FOUND : return remove(state, action.tx.id); case PENDING.FROM_STORAGE : diff --git a/src/js/services/LocalStorageService.js b/src/js/services/LocalStorageService.js index 50d43908..0367ccbf 100644 --- a/src/js/services/LocalStorageService.js +++ b/src/js/services/LocalStorageService.js @@ -135,6 +135,7 @@ const LocalStorageService: Middleware = (api: MiddlewareAPI) => (next: Middlewar case SEND.TX_COMPLETE : case PENDING.TX_RESOLVED : + case PENDING.TX_NOT_FOUND : save(api.dispatch, api.getState); break;