Skip to content

Commit

Permalink
refactor: remove unused quote calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiTimesChi committed Nov 8, 2024
1 parent bbb906d commit 6205011
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 217 deletions.
199 changes: 1 addition & 198 deletions packages/sdk-router/src/rfq/quote.test.ts
Original file line number Diff line number Diff line change
@@ -1,143 +1,12 @@
import { BigNumber, parseFixed } from '@ethersproject/bignumber'
import { BigNumber } from '@ethersproject/bignumber'

import {
FastBridgeQuote,
FastBridgeQuoteAPI,
marshallFastBridgeQuote,
unmarshallFastBridgeQuote,
applyQuote,
} from './quote'

const createZeroAmountTests = (quote: FastBridgeQuote) => {
describe('Returns zero', () => {
it('If origin amount is zero', () => {
expect(applyQuote(quote, BigNumber.from(0))).toEqual(BigNumber.from(0))
})

it('If origin amount is lower than fixed fee', () => {
expect(applyQuote(quote, quote.fixedFee.sub(1))).toEqual(
BigNumber.from(0)
)
})

it('If origin amount is equal to fixed fee', () => {
expect(applyQuote(quote, quote.fixedFee)).toEqual(BigNumber.from(0))
})

it('If origin amount is greater than max origin amount + fixed fee', () => {
const amount = quote.maxOriginAmount.add(quote.fixedFee).add(1)
expect(applyQuote(quote, amount)).toEqual(BigNumber.from(0))
})
})

describe('Returns non-zero', () => {
it('If origin amount is equal to max origin amount', () => {
expect(applyQuote(quote, quote.maxOriginAmount)).not.toEqual(
BigNumber.from(0)
)
})

it('If origin amount is 1 wei greater than max origin amount', () => {
const amount = quote.maxOriginAmount.add(1)
expect(applyQuote(quote, amount)).not.toEqual(BigNumber.from(0))
})

it('If origin amount is max origin amount + fixed fee', () => {
const amount = quote.maxOriginAmount.add(quote.fixedFee)
expect(applyQuote(quote, amount)).not.toEqual(BigNumber.from(0))
})
})
}

const createCorrectAmountTest = (
quote: FastBridgeQuote,
amount: BigNumber,
expected: BigNumber
) => {
it(`${amount.toString()} -> ${expected.toString()}`, () => {
expect(applyQuote(quote, amount)).toEqual(expected)
})
}

const createQuoteTests = (
quoteTemplate: FastBridgeQuote,
originDecimals: number,
destDecimals: number
) => {
describe(`Origin decimals: ${originDecimals}, dest decimals: ${destDecimals}`, () => {
describe(`origin:destination price 1:1`, () => {
const quote: FastBridgeQuote = {
...quoteTemplate,
maxOriginAmount: parseFixed('100000', originDecimals),
destAmount: parseFixed('100000', destDecimals),
fixedFee: parseFixed('1', originDecimals),
}

// 10 origin -> 9 dest
createCorrectAmountTest(
quote,
parseFixed('10', originDecimals),
parseFixed('9', destDecimals)
)
createZeroAmountTests(quote)
})

describe(`origin:destination price 1:1.0001`, () => {
const quote: FastBridgeQuote = {
...quoteTemplate,
maxOriginAmount: parseFixed('100000', originDecimals),
destAmount: parseFixed('100010', destDecimals),
fixedFee: parseFixed('1', originDecimals),
}

// 10 origin -> 9.0009 dest
createCorrectAmountTest(
quote,
parseFixed('10', originDecimals),
parseFixed('9.0009', destDecimals)
)
createZeroAmountTests(quote)
})

describe(`origin:destination price 1:0.9999`, () => {
const quote: FastBridgeQuote = {
...quoteTemplate,
maxOriginAmount: parseFixed('100000', originDecimals),
destAmount: parseFixed('99990', destDecimals),
fixedFee: parseFixed('1', originDecimals),
}

// 10 origin -> 8.9991 dest
createCorrectAmountTest(
quote,
parseFixed('10', originDecimals),
parseFixed('8.9991', destDecimals)
)
createZeroAmountTests(quote)
})
})
}

const createRoundDownTest = (
quoteTemplate: FastBridgeQuote,
maxOriginAmount: BigNumber,
destAmount: BigNumber,
fixedFee: BigNumber,
amountIn: BigNumber,
expected: BigNumber
) => {
describe(`Rounds down with price ${maxOriginAmount.toString()} -> ${destAmount.toString()} and fixed fee ${fixedFee.toString()}`, () => {
const quote: FastBridgeQuote = {
...quoteTemplate,
maxOriginAmount,
destAmount,
fixedFee,
}

createCorrectAmountTest(quote, amountIn, expected)
})
}

describe('quote', () => {
const quoteAPI: FastBridgeQuoteAPI = {
origin_chain_id: 1,
Expand Down Expand Up @@ -180,70 +49,4 @@ describe('quote', () => {
it('should marshall a quote', () => {
expect(marshallFastBridgeQuote(quote)).toEqual(quoteAPI)
})

describe('applyQuote', () => {
// Equal decimals
createQuoteTests(quote, 18, 18)
createRoundDownTest(
quote,
parseFixed('1234', 18),
parseFixed('2345', 18),
parseFixed('1', 18),
parseFixed('2', 18),
// (2 - 1) * 2345 / 1234 = 1.900324149108589951
BigNumber.from('1900324149108589951')
)

// // Bigger decimals
createQuoteTests(quote, 6, 18)
createRoundDownTest(
quote,
parseFixed('1234', 6),
parseFixed('2345', 18),
parseFixed('1', 6),
parseFixed('2', 6),
// (2 - 1) * 2345 / 1234 = 1.900324149108589951
BigNumber.from('1900324149108589951')
)

// Smaller decimals
createQuoteTests(quote, 18, 6)
createRoundDownTest(
quote,
parseFixed('1234', 18),
parseFixed('2345', 6),
parseFixed('1', 18),
parseFixed('2', 18),
// (2 - 1) * 2345 / 1234 = 1.900324149108589951
BigNumber.from('1900324')
)

it('Returns zero when max origin amount is zero', () => {
const zeroQuote: FastBridgeQuote = {
...quote,
maxOriginAmount: BigNumber.from(0),
}
const amount = zeroQuote.fixedFee.mul(2)
expect(applyQuote(zeroQuote, amount)).toEqual(BigNumber.from(0))
})

it('Returns zero when dest amount is zero', () => {
const zeroQuote: FastBridgeQuote = {
...quote,
destAmount: BigNumber.from(0),
}
const amount = zeroQuote.fixedFee.mul(2)
expect(applyQuote(zeroQuote, amount)).toEqual(BigNumber.from(0))
})

it('Returns zero when max origin amount and dest amount are zero', () => {
const zeroQuote: FastBridgeQuote = {
...quote,
maxOriginAmount: BigNumber.from(0),
destAmount: BigNumber.from(0),
}
const amount = zeroQuote.fixedFee.mul(2)
expect(applyQuote(zeroQuote, amount)).toEqual(BigNumber.from(0))
})
})
})
19 changes: 0 additions & 19 deletions packages/sdk-router/src/rfq/quote.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BigNumber } from 'ethers'
import { Zero } from '@ethersproject/constants'

import { Ticker } from './ticker'

Expand Down Expand Up @@ -69,21 +68,3 @@ export const marshallFastBridgeQuote = (
updated_at: new Date(quote.updatedAt).toISOString(),
}
}

export const applyQuote = (
quote: FastBridgeQuote,
originAmount: BigNumber
): BigNumber => {
// Check that the origin amount covers the fixed fee
if (originAmount.lte(quote.fixedFee)) {
return Zero
}
// Check that the Relayer is able to process the origin amount (post fixed fee)
const amountAfterFee = originAmount.sub(quote.fixedFee)
if (amountAfterFee.gt(quote.maxOriginAmount)) {
return Zero
}
// After these checks: 0 < amountAfterFee <= quote.maxOriginAmount
// Solve (amountAfterFee -> ?) using (maxOriginAmount -> destAmount) pricing ratio
return amountAfterFee.mul(quote.destAmount).div(quote.maxOriginAmount)
}

0 comments on commit 6205011

Please sign in to comment.