Skip to content

Commit

Permalink
feat: show true amounts in and out
Browse files Browse the repository at this point in the history
  • Loading branch information
berteotti committed Oct 23, 2023
1 parent 06fea0b commit 43864c5
Showing 1 changed file with 10 additions and 29 deletions.
39 changes: 10 additions & 29 deletions src/entities/trades/gnosis-protocol/CoWTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import invariant from 'tiny-invariant'
import { ChainId, ONE, TradeType, ZERO } from '../../../constants'
import { Currency } from '../../currency'
import { CurrencyAmount } from '../../fractions/currencyAmount'
import { Fraction } from '../../fractions/fraction'
import { Percent } from '../../fractions/percent'
import { Price } from '../../fractions/price'
import { TokenAmount } from '../../fractions/tokenAmount'
Expand All @@ -25,6 +24,8 @@ import cowAppData from './app-data.json'
import { CoWTradeError } from './CoWTradeError'
import { CoWTradeGetBestTradeExactInParams, CoWTradeGetBestTradeExactOutParams, CoWTradeParams } from './types'

const ZERO_PERCENT = new Percent(ZERO, ONE)

/**
* CoWTrade uses CowFi API to find and route trades through the MEV-protected Gnosis Protocol v2
*/
Expand Down Expand Up @@ -109,31 +110,11 @@ export class CoWTrade extends Trade {
}

public minimumAmountOut(): CurrencyAmount {
if (this.tradeType === TradeType.EXACT_OUTPUT) {
return this.outputAmount
} else {
const slippageAdjustedAmountOut = new Fraction(ONE)
.add(this.maximumSlippage)
.invert()
.multiply(this.outputAmount.raw).quotient

return this.outputAmount instanceof TokenAmount
? new TokenAmount(this.outputAmount.token, slippageAdjustedAmountOut)
: CurrencyAmount.nativeCurrency(slippageAdjustedAmountOut, this.chainId)
}
return this.outputAmount
}

public maximumAmountIn(): CurrencyAmount {
if (this.tradeType === TradeType.EXACT_INPUT) {
return this.inputAmount
} else {
const slippageAdjustedAmountIn = new Fraction(ONE)
.add(this.maximumSlippage)
.multiply(this.inputAmount.raw).quotient
return this.inputAmount instanceof TokenAmount
? new TokenAmount(this.inputAmount.token, slippageAdjustedAmountIn)
: CurrencyAmount.nativeCurrency(slippageAdjustedAmountIn, this.chainId)
}
return this.inputAmount
}

/**
Expand Down Expand Up @@ -207,11 +188,11 @@ export class CoWTrade extends Trade {
})

// CoW Swap doesn't charge any fee
const fee = new Percent(ZERO, ONE)
const fee = ZERO_PERCENT

const feeAmount = Currency.isNative(currencyAmountIn.currency)
? CurrencyAmount.nativeCurrency('0', chainId)
: new TokenAmount(currencyAmountIn.currency as Token, '0')
? CurrencyAmount.nativeCurrency(ZERO, chainId)
: new TokenAmount(currencyAmountIn.currency as Token, ZERO)

return new CoWTrade({
chainId,
Expand Down Expand Up @@ -279,11 +260,11 @@ export class CoWTrade extends Trade {
: new TokenAmount(tokenOut, quoteResponse.quote.buyAmount.toString())

// CoW Swap doesn't charge any fee
const fee = new Percent(ZERO, ONE)
const fee = ZERO_PERCENT

const feeAmount = Currency.isNative(currencyIn)
? CurrencyAmount.nativeCurrency('0', chainId)
: new TokenAmount(currencyIn as Token, '0')
? CurrencyAmount.nativeCurrency(ZERO, chainId)
: new TokenAmount(currencyIn as Token, ZERO)

return new CoWTrade({
chainId,
Expand Down

0 comments on commit 43864c5

Please sign in to comment.