Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
fix: fix token estimation of fees and transfer, and proper error repo…
Browse files Browse the repository at this point in the history
…rt to server
  • Loading branch information
patogallaiovlabs committed May 13, 2021
1 parent 12b3470 commit 00a139e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/common/transaction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 7 additions & 8 deletions src/common/transaction/rbtccoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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 {
Expand All @@ -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 = {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)}`);
Expand Down
81 changes: 46 additions & 35 deletions src/pages/wallet/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 00a139e

Please sign in to comment.