Account for approval gas costs in eth received (#9559)

* Account for approval gas costs in eth received

* Pass approval txMeta to getSwapsTokensReceivedFromTxMeta in swaps/index.js
feature/default_network_editable
Dan J Miller 4 years ago committed by GitHub
parent 45ba657ca1
commit f3bd717184
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      app/scripts/controllers/transactions/index.js
  2. 4
      ui/app/ducks/swaps/swaps.js
  3. 1
      ui/app/pages/swaps/index.js
  4. 13
      ui/app/pages/swaps/swaps.util.js

@ -583,11 +583,15 @@ export default class TransactionController extends EventEmitter {
const postTxBalance = await this.query.getBalance(txMeta.txParams.from) const postTxBalance = await this.query.getBalance(txMeta.txParams.from)
const latestTxMeta = this.txStateManager.getTx(txId) const latestTxMeta = this.txStateManager.getTx(txId)
const approvalTxMeta = latestTxMeta.approvalTxId
? this.txStateManager.getTx(latestTxMeta.approvalTxId)
: null
latestTxMeta.postTxBalance = postTxBalance.toString(16) latestTxMeta.postTxBalance = postTxBalance.toString(16)
this.txStateManager.updateTx(latestTxMeta, 'transactions#confirmTransaction - add postTxBalance') this.txStateManager.updateTx(latestTxMeta, 'transactions#confirmTransaction - add postTxBalance')
this._trackSwapsMetrics(latestTxMeta) this._trackSwapsMetrics(latestTxMeta, approvalTxMeta)
} }
} catch (err) { } catch (err) {
@ -822,7 +826,7 @@ export default class TransactionController extends EventEmitter {
this.memStore.updateState({ unapprovedTxs, currentNetworkTxList }) this.memStore.updateState({ unapprovedTxs, currentNetworkTxList })
} }
_trackSwapsMetrics (txMeta) { _trackSwapsMetrics (txMeta, approvalTxMeta) {
if (this._getParticipateInMetrics() && txMeta.swapMetaData) { if (this._getParticipateInMetrics() && txMeta.swapMetaData) {
if (txMeta.txReceipt.status === '0x0') { if (txMeta.txReceipt.status === '0x0') {
this._trackSegmentEvent({ this._trackSegmentEvent({
@ -844,6 +848,7 @@ export default class TransactionController extends EventEmitter {
txMeta.destinationTokenAddress, txMeta.destinationTokenAddress,
txMeta.txParams.from, txMeta.txParams.from,
txMeta.destinationTokenDecimals, txMeta.destinationTokenDecimals,
approvalTxMeta,
) )
const quoteVsExecutionRatio = `${ const quoteVsExecutionRatio = `${

@ -484,11 +484,12 @@ export const signAndSendTransactions = (history, metaMetricsEvent) => {
metaMetricsEvent({ ...metaMetricsConfig }) metaMetricsEvent({ ...metaMetricsConfig })
metaMetricsEvent({ ...metaMetricsConfig, excludeMetaMetricsId: true, properties: swapMetaData }) metaMetricsEvent({ ...metaMetricsConfig, excludeMetaMetricsId: true, properties: swapMetaData })
let finalApproveTxMeta
const approveTxParams = getApproveTxParams(state) const approveTxParams = getApproveTxParams(state)
if (approveTxParams) { if (approveTxParams) {
const approveTxMeta = await dispatch(addUnapprovedTransaction({ ...approveTxParams, amount: '0x0' }, 'metamask')) const approveTxMeta = await dispatch(addUnapprovedTransaction({ ...approveTxParams, amount: '0x0' }, 'metamask'))
await dispatch(setApproveTxId(approveTxMeta.id)) await dispatch(setApproveTxId(approveTxMeta.id))
const finalApproveTxMeta = await (dispatch(updateTransaction({ finalApproveTxMeta = await (dispatch(updateTransaction({
...approveTxMeta, ...approveTxMeta,
transactionCategory: SWAP_APPROVAL, transactionCategory: SWAP_APPROVAL,
sourceTokenSymbol: sourceTokenInfo.symbol, sourceTokenSymbol: sourceTokenInfo.symbol,
@ -513,6 +514,7 @@ export const signAndSendTransactions = (history, metaMetricsEvent) => {
destinationTokenAddress: destinationTokenInfo.address, destinationTokenAddress: destinationTokenInfo.address,
swapMetaData, swapMetaData,
swapTokenValue, swapTokenValue,
approvalTxId: finalApproveTxMeta?.id,
}, true))) }, true)))
try { try {
await dispatch(updateAndApproveTx(finalTradeTxMeta, true)) await dispatch(updateAndApproveTx(finalTradeTxMeta, true))

@ -102,6 +102,7 @@ export default function Swap () {
destinationTokenInfo?.address, destinationTokenInfo?.address,
selectedAccountAddress, selectedAccountAddress,
destinationTokenInfo?.decimals, destinationTokenInfo?.decimals,
approveTxData,
) )
const tradeConfirmed = tradeTxData?.status === 'confirmed' const tradeConfirmed = tradeTxData?.status === 'confirmed'
const approveError = approveTxData?.status === 'failed' || approveTxData?.txReceipt?.status === '0x0' const approveError = approveTxData?.status === 'failed' || approveTxData?.txReceipt?.status === '0x0'

@ -364,18 +364,27 @@ export function quotesToRenderableData (quotes, gasPrice, conversionRate, curren
}) })
} }
export function getSwapsTokensReceivedFromTxMeta (tokenSymbol, txMeta, tokenAddress, accountAddress, tokenDecimals) { export function getSwapsTokensReceivedFromTxMeta (tokenSymbol, txMeta, tokenAddress, accountAddress, tokenDecimals, approvalTxMeta) {
const txReceipt = txMeta?.txReceipt const txReceipt = txMeta?.txReceipt
if (tokenSymbol === 'ETH') { if (tokenSymbol === 'ETH') {
if (!txReceipt || !txMeta || !txMeta.postTxBalance || !txMeta.preTxBalance) { if (!txReceipt || !txMeta || !txMeta.postTxBalance || !txMeta.preTxBalance) {
return null return null
} }
let approvalTxGasCost = '0x0'
if (approvalTxMeta && approvalTxMeta.txReceipt) {
approvalTxGasCost = calcGasTotal(approvalTxMeta.txReceipt.gasUsed, approvalTxMeta.txParams.gasPrice)
}
const gasCost = calcGasTotal(txReceipt.gasUsed, txMeta.txParams.gasPrice) const gasCost = calcGasTotal(txReceipt.gasUsed, txMeta.txParams.gasPrice)
const preTxBalanceLessGasCost = subtractCurrencies(txMeta.preTxBalance, gasCost, { const totalGasCost = (new BigNumber(gasCost, 16)).plus(approvalTxGasCost, 16).toString(16)
const preTxBalanceLessGasCost = subtractCurrencies(txMeta.preTxBalance, totalGasCost, {
aBase: 16, aBase: 16,
bBase: 16, bBase: 16,
toNumericBase: 'hex', toNumericBase: 'hex',
}) })
const ethReceived = subtractCurrencies(txMeta.postTxBalance, preTxBalanceLessGasCost, { const ethReceived = subtractCurrencies(txMeta.postTxBalance, preTxBalanceLessGasCost, {
aBase: 16, aBase: 16,
bBase: 16, bBase: 16,

Loading…
Cancel
Save