fix: detect refunds on exact_out native swaps (#7227)

* fix: detect refunds on exact_out native swaps

* fix: correct comment
This commit is contained in:
cartcrom 2023-09-01 14:55:42 -04:00 committed by GitHub
parent 26d2ab53e8
commit 45a138dec1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -159,10 +159,16 @@ function parseSwap(changes: TransactionChanges) {
}
// Some swaps may have more than 2 transfers, e.g. swaps with fees on tranfer
if (changes.TokenTransfer.length >= 2) {
const sent = changes.TokenTransfer.find((t) => t?.__typename === 'TokenTransfer' && t.direction === 'OUT')
const received = changes.TokenTransfer.find((t) => t?.__typename === 'TokenTransfer' && t.direction === 'IN')
const sent = changes.TokenTransfer.find((t) => t.direction === 'OUT')
// Any leftover native token is refunded on exact_out swaps where the input token is native
const refund = changes.TokenTransfer.find(
(t) => t.direction === 'IN' && t.asset.id === sent?.asset.id && t.asset.standard === 'NATIVE'
)
const received = changes.TokenTransfer.find((t) => t.direction === 'IN' && t !== refund)
if (sent && received) {
const inputAmount = formatNumberOrString(sent.quantity, NumberType.TokenNonTx)
const adjustedInput = parseFloat(sent.quantity) - parseFloat(refund?.quantity ?? '0')
const inputAmount = formatNumberOrString(adjustedInput, NumberType.TokenNonTx)
const outputAmount = formatNumberOrString(received.quantity, NumberType.TokenNonTx)
return {
title: getSwapTitle(sent, received),