mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-23 02:10:12 +01:00
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
This commit is contained in:
parent
45ba657ca1
commit
f3bd717184
@ -583,11 +583,15 @@ export default class TransactionController extends EventEmitter {
|
||||
const postTxBalance = await this.query.getBalance(txMeta.txParams.from)
|
||||
const latestTxMeta = this.txStateManager.getTx(txId)
|
||||
|
||||
const approvalTxMeta = latestTxMeta.approvalTxId
|
||||
? this.txStateManager.getTx(latestTxMeta.approvalTxId)
|
||||
: null
|
||||
|
||||
latestTxMeta.postTxBalance = postTxBalance.toString(16)
|
||||
|
||||
this.txStateManager.updateTx(latestTxMeta, 'transactions#confirmTransaction - add postTxBalance')
|
||||
|
||||
this._trackSwapsMetrics(latestTxMeta)
|
||||
this._trackSwapsMetrics(latestTxMeta, approvalTxMeta)
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
@ -822,7 +826,7 @@ export default class TransactionController extends EventEmitter {
|
||||
this.memStore.updateState({ unapprovedTxs, currentNetworkTxList })
|
||||
}
|
||||
|
||||
_trackSwapsMetrics (txMeta) {
|
||||
_trackSwapsMetrics (txMeta, approvalTxMeta) {
|
||||
if (this._getParticipateInMetrics() && txMeta.swapMetaData) {
|
||||
if (txMeta.txReceipt.status === '0x0') {
|
||||
this._trackSegmentEvent({
|
||||
@ -844,6 +848,7 @@ export default class TransactionController extends EventEmitter {
|
||||
txMeta.destinationTokenAddress,
|
||||
txMeta.txParams.from,
|
||||
txMeta.destinationTokenDecimals,
|
||||
approvalTxMeta,
|
||||
)
|
||||
|
||||
const quoteVsExecutionRatio = `${
|
||||
|
@ -484,11 +484,12 @@ export const signAndSendTransactions = (history, metaMetricsEvent) => {
|
||||
metaMetricsEvent({ ...metaMetricsConfig })
|
||||
metaMetricsEvent({ ...metaMetricsConfig, excludeMetaMetricsId: true, properties: swapMetaData })
|
||||
|
||||
let finalApproveTxMeta
|
||||
const approveTxParams = getApproveTxParams(state)
|
||||
if (approveTxParams) {
|
||||
const approveTxMeta = await dispatch(addUnapprovedTransaction({ ...approveTxParams, amount: '0x0' }, 'metamask'))
|
||||
await dispatch(setApproveTxId(approveTxMeta.id))
|
||||
const finalApproveTxMeta = await (dispatch(updateTransaction({
|
||||
finalApproveTxMeta = await (dispatch(updateTransaction({
|
||||
...approveTxMeta,
|
||||
transactionCategory: SWAP_APPROVAL,
|
||||
sourceTokenSymbol: sourceTokenInfo.symbol,
|
||||
@ -513,6 +514,7 @@ export const signAndSendTransactions = (history, metaMetricsEvent) => {
|
||||
destinationTokenAddress: destinationTokenInfo.address,
|
||||
swapMetaData,
|
||||
swapTokenValue,
|
||||
approvalTxId: finalApproveTxMeta?.id,
|
||||
}, true)))
|
||||
try {
|
||||
await dispatch(updateAndApproveTx(finalTradeTxMeta, true))
|
||||
|
@ -102,6 +102,7 @@ export default function Swap () {
|
||||
destinationTokenInfo?.address,
|
||||
selectedAccountAddress,
|
||||
destinationTokenInfo?.decimals,
|
||||
approveTxData,
|
||||
)
|
||||
const tradeConfirmed = tradeTxData?.status === 'confirmed'
|
||||
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
|
||||
if (tokenSymbol === 'ETH') {
|
||||
if (!txReceipt || !txMeta || !txMeta.postTxBalance || !txMeta.preTxBalance) {
|
||||
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 preTxBalanceLessGasCost = subtractCurrencies(txMeta.preTxBalance, gasCost, {
|
||||
const totalGasCost = (new BigNumber(gasCost, 16)).plus(approvalTxGasCost, 16).toString(16)
|
||||
|
||||
const preTxBalanceLessGasCost = subtractCurrencies(txMeta.preTxBalance, totalGasCost, {
|
||||
aBase: 16,
|
||||
bBase: 16,
|
||||
toNumericBase: 'hex',
|
||||
})
|
||||
|
||||
const ethReceived = subtractCurrencies(txMeta.postTxBalance, preTxBalanceLessGasCost, {
|
||||
aBase: 16,
|
||||
bBase: 16,
|
||||
|
Loading…
Reference in New Issue
Block a user