From 00a139e943f7b3cc6836b5a39bcddc492317a11e Mon Sep 17 00:00:00 2001 From: Patricio Date: Thu, 13 May 2021 13:26:05 -0300 Subject: [PATCH] fix: fix token estimation of fees and transfer, and proper error report to server --- src/common/transaction/index.js | 2 +- src/common/transaction/rbtccoin.js | 15 +++--- src/pages/wallet/transfer.js | 81 +++++++++++++++++------------- 3 files changed, 54 insertions(+), 44 deletions(-) diff --git a/src/common/transaction/index.js b/src/common/transaction/index.js index 0c4058f7..f6b65f08 100644 --- a/src/common/transaction/index.js +++ b/src/common/transaction/index.js @@ -79,7 +79,7 @@ export default class Transaction { result = await rbtc.signTransaction(this.rawTransaction, privateKey); } } catch (e) { - console.log('Transaction.processTransaction err: ', e.message); + console.log('Transaction.processTransaction err: ', e); throw e; } console.log('Transaction.processTransaction finished, result: ', result); diff --git a/src/common/transaction/rbtccoin.js b/src/common/transaction/rbtccoin.js index d54517b8..d07ae021 100644 --- a/src/common/transaction/rbtccoin.js +++ b/src/common/transaction/rbtccoin.js @@ -18,9 +18,9 @@ export const getContractAddress = async (symbol, type) => { export const encodeContractTransfer = async (contractAddress, type, to, value) => { const rskEndpoint = type === 'Mainnet' ? MAINNET.RSK_END_POINT : TESTNET.RSK_END_POINT; - const networkId = type === 'Mainnet' ? MAINNET.NETWORK_VERSION : TESTNET.NETWORK_VERSION; const rsk3 = new Rsk3(rskEndpoint); - const contract = rsk3.Contract(assetAbi, Rsk3.utils.toChecksumAddress(contractAddress, networkId)); + const contractLower = contractAddress.toLowerCase(); + const contract = rsk3.Contract(assetAbi, contractLower); const data = await contract.methods.transfer(to, value).encodeABI(); return data; }; @@ -45,7 +45,7 @@ export const getTransactionFees = async (type, coin, address, toAddress, value, const contractAddr = contractAddress || await getContractAddress(symbol, type); const data = await encodeContractTransfer(contractAddr, type, to, value); gas = await rsk3.estimateGas({ - from, to: contractAddr, value: 0, data, + from, to: contractAddr.toLowerCase(), value: 0, data, }); } return { @@ -62,7 +62,6 @@ export const createRawTransaction = async ({ symbol, type, sender: from, receiver: to, value, memo, gasPrice, gas, contractAddress, }) => { const rskEndpoint = type === 'Mainnet' ? MAINNET.RSK_END_POINT : TESTNET.RSK_END_POINT; - const networkId = type === 'Mainnet' ? MAINNET.NETWORK_VERSION : TESTNET.NETWORK_VERSION; const rsk3 = new Rsk3(rskEndpoint); const nonce = await rsk3.getTransactionCount(from, 'pending'); const rawTransaction = { @@ -77,8 +76,8 @@ export const createRawTransaction = async ({ rawTransaction.to = to; rawTransaction.data = memo; } else if (contractAddress) { - const contract = rsk3.Contract(assetAbi, Rsk3.utils.toChecksumAddress(contractAddress, networkId)); - rawTransaction.to = contractAddress; + const contract = rsk3.Contract(assetAbi, contractAddress.toLowerCase()); + rawTransaction.to = contractAddress.toLowerCase(); rawTransaction.data = await contract.methods.transfer(to, value).encodeABI(); rawTransaction.value = '0x00'; } else { @@ -148,9 +147,9 @@ export const processRawTransaction = async ({ symbol, netType, sender, receiver, value, data, memo, gasFee, fallback: isUseTransactionFallback, contractAddress, }); console.log(`rbtc.processRawTransaction, rawTransactionParam: ${JSON.stringify(param)}`); - result = await createRawTransaction({ ...param, contractAddress }); + result = await createRawTransaction({ ...param, contractAddress: contractAddress.toLowerCase() }); } catch (e) { - console.log('rbtc.processRawTransaction err: ', e.message); + console.log('rbtc.processRawTransaction err: ', e); throw e; } console.log(`rbtc.processRawTransaction finished, result: ${JSON.stringify(result)}`); diff --git a/src/pages/wallet/transfer.js b/src/pages/wallet/transfer.js index 4ddd3700..20b2b451 100644 --- a/src/pages/wallet/transfer.js +++ b/src/pages/wallet/transfer.js @@ -667,6 +667,7 @@ class Transfer extends Component { } this.processFees(transactionFees); } catch (error) { + reportErrorToServer({ developerComment: 'request fees', errorObject: error }); const confirmation = createErrorConfirmation( defaultErrorNotification.title, defaultErrorNotification.message, @@ -722,45 +723,50 @@ class Transfer extends Component { } async loadTransactionFees() { - const { amount, memo } = this.state; - const { - coin, txFeesCache, toAddress, txSize, - } = this; - const { - symbol, type, address, precision, - } = coin; - - if (symbol === 'BTC' && !txSize) { - throw new FeeCalculationError(); - } + try { + const { amount, memo } = this.state; + const { + coin, txFeesCache, toAddress, txSize, + } = this; + const { + symbol, type, address, precision, + } = coin; + + if (symbol === 'BTC' && !txSize) { + throw new FeeCalculationError(); + } - const { amount: lastAmount, to: lastTo, memo: lastMemo } = txFeesCache; - const value = common.convertCoinAmountToUnitHex(symbol, amount, precision); - console.log(`amount: ${amount}, to: ${toAddress}, memo: ${memo}`); - console.log(`lastAmount: ${lastAmount}, lastTo: ${lastTo}, lastMemo: ${lastMemo}`); + const { amount: lastAmount, to: lastTo, memo: lastMemo } = txFeesCache; + const value = common.convertCoinAmountToUnitHex(symbol, amount, precision); + console.log(`amount: ${amount}, to: ${toAddress}, memo: ${memo}`); + console.log(`lastAmount: ${lastAmount}, lastTo: ${lastTo}, lastMemo: ${lastMemo}`); - let isMatched = false; - if (amount === lastAmount && toAddress === lastTo) { - if (symbol !== 'BTC') { - isMatched = memo === lastMemo; - } else { - isMatched = true; + let isMatched = false; + if (amount === lastAmount && toAddress === lastTo) { + if (symbol !== 'BTC') { + isMatched = memo === lastMemo; + } else { + isMatched = true; + } } - } - if (isMatched) { + if (isMatched) { + this.setState({ enableConfirm: true }); + return null; + } + + const transactionFees = symbol === 'BTC' + ? await parseHelper.getBtcTransactionFees(symbol, type, txSize) + : await rbtcTransaction.getTransactionFees(type, coin, address, toAddress, value, memo); + console.log('transactionFees: ', transactionFees); + this.txFeesCache = { + amount, toAddress, memo, transactionFees, + }; this.setState({ enableConfirm: true }); - return null; + return transactionFees; + } catch (e) { + console.log('error in loadTransactionFees', e); + throw e; } - - const transactionFees = symbol === 'BTC' - ? await parseHelper.getBtcTransactionFees(symbol, type, txSize) - : await rbtcTransaction.getTransactionFees(type, coin, address, toAddress, value, memo); - console.log('transactionFees: ', transactionFees); - this.txFeesCache = { - amount, toAddress, memo, transactionFees, - }; - this.setState({ enableConfirm: true }); - return transactionFees; } calcCustomFee(value) { @@ -1209,7 +1215,12 @@ Transfer.propTypes = { navigate: PropTypes.func.isRequired, dispatch: PropTypes.func.isRequired, goBack: PropTypes.func.isRequired, - state: PropTypes.object.isRequired, + state: PropTypes.shape({ + params: PropTypes.shape({ + coin: PropTypes.string.isRequired, + toAddress: PropTypes.string.isRequired, + }).isRequired, + }).isRequired, }).isRequired, addNotification: PropTypes.func.isRequired, addConfirmation: PropTypes.func.isRequired,