Skip to content

Commit

Permalink
feat: add getting curve swap quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
janndriessen committed Jul 8, 2024
1 parent 3b59c57 commit 0917456
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
40 changes: 32 additions & 8 deletions src/quote/swap/adapters/curve/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { ETH, stETH, USDC } from 'constants/tokens'
import { ETH, stETH, WETH } from 'constants/tokens'
import { AlchemyProviderUrl } from 'tests/utils'

import { CurveSwapQuoteProvider } from './'
Expand All @@ -9,10 +9,33 @@ const rpcUrl = AlchemyProviderUrl

const eth = ETH.address!
const steth = stETH.address!
const usdc = USDC.address!
const weth = WETH.address!
const ONE = '1000000000000000000'

describe('CurveSwapQuoteProvider', () => {
test('getting a swap quote for a specified input amount', async () => {
const request = {
chainId: 1,
inputToken: eth,
outputToken: steth,
inputAmount: ONE,
}
const provider = new CurveSwapQuoteProvider(rpcUrl)
const quote = await provider.getSwapQuote(request)
if (!quote) fail()
expect(quote).not.toBeNull()
expect(quote.swapData?.exchange).toBe(Exchange.Curve)
expect(quote.swapData?.path.length).toBe(2)
expect(quote.swapData?.fees.length).toBe(0)
expect(quote.swapData?.path).toEqual([weth, request.outputToken])
expect(quote.swapData?.pool).toBe(
'0xdc24316b9ae028f1497c275eb9192a3ea0f67022'
)
console.log(quote)
// expect(quote.callData).not.toBe('0x')
expect(BigInt(quote.inputAmount) > BigInt(0)).toBe(true)
})

test('getting a swap quote for a specified output amount', async () => {
const request = {
chainId: 1,
Expand All @@ -24,13 +47,14 @@ describe('CurveSwapQuoteProvider', () => {
const quote = await provider.getSwapQuote(request)
if (!quote) fail()
expect(quote).not.toBeNull()
expect(quote.swapData?.exchange).toBe(Exchange.UniV3)
expect(quote.swapData?.exchange).toBe(Exchange.Curve)
expect(quote.swapData?.path.length).toBe(2)
expect(quote.swapData?.fees.length).toBe(1)
expect(quote.swapData?.path).toEqual([
request.inputToken,
request.outputToken,
])
expect(quote.swapData?.fees.length).toBe(0)
expect(quote.swapData?.path).toEqual([weth, request.outputToken])
expect(quote.swapData?.pool).toBe(
'0xdc24316b9ae028f1497c275eb9192a3ea0f67022'
)
console.log(quote)
// expect(quote.callData).not.toBe('0x')
expect(BigInt(quote.inputAmount) > BigInt(0)).toBe(true)
})
Expand Down
18 changes: 10 additions & 8 deletions src/quote/swap/adapters/curve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ export class CurveSwapQuoteProvider implements SwapQuoteProvider {
slippage,
} = request
const pool = this.getPoolContract()
const quoteAmount = await pool.get_dy(
0,
1,
BigNumber.from('1000000000000000000')
)
let quoteAmount = BigNumber.from(0)
if (outputAmount) {
quoteAmount = await pool.get_dy(1, 0, BigNumber.from(outputAmount))
} else {
quoteAmount = await pool.get_dy(0, 1, BigNumber.from(inputAmount))
}
console.log(quoteAmount.toString())
return {
chainId,
inputToken,
outputToken,
inputAmount: BigNumber.from(0).toString(),
outputAmount: BigNumber.from(0).toString(),
callData: '0x', // TOOD: result.transactionRequest?.data ?? '0x',
inputAmount: inputAmount ?? quoteAmount.toString(),
outputAmount: outputAmount ?? quoteAmount.toString(),
// TOOD:
callData: '0x',
slippage: slippage ?? 0,
swapData: getSwapData(),
}
Expand Down
9 changes: 4 additions & 5 deletions src/quote/swap/adapters/curve/swap-data.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { stETH, WETH } from 'constants/tokens'
import { Exchange, SwapData } from 'utils'

export function getSwapData(): SwapData {
return {
exchange: Exchange.Curve,
path: [
'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
'0xae7ab96520de3a18e5e111b5eaab095312d7fe84',
],
fees: [],
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
path: [WETH.address!, stETH.address!],
fees: [], // not needed for curve
pool: '0xdc24316b9ae028f1497c275eb9192a3ea0f67022',
}
}

0 comments on commit 0917456

Please sign in to comment.