forked from Uniswap/v3-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquoter.test.ts
158 lines (142 loc) · 7.28 KB
/
quoter.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import JSBI from 'jsbi'
import { CurrencyAmount, Token, TradeType, WETH9 } from '@uniswap/sdk-core'
import { FeeAmount, TICK_SPACINGS } from './constants'
import { SwapQuoter } from './quoter'
import { nearestUsableTick, encodeSqrtRatioX96, TickMath } from './utils'
import { Route, Trade, Pool } from './entities'
describe('SwapQuoter', () => {
const token0 = new Token(1, '0x0000000000000000000000000000000000000001', 18, 't0', 'token0')
const token1 = new Token(1, '0x0000000000000000000000000000000000000002', 18, 't1', 'token1')
const feeAmount = FeeAmount.MEDIUM
const sqrtRatioX96 = encodeSqrtRatioX96(1, 1)
const liquidity = 1_000_000
const WETH = WETH9[1]
const makePool = (token0: Token, token1: Token) => {
return new Pool(token0, token1, feeAmount, sqrtRatioX96, liquidity, TickMath.getTickAtSqrtRatio(sqrtRatioX96), [
{
index: nearestUsableTick(TickMath.MIN_TICK, TICK_SPACINGS[feeAmount]),
liquidityNet: liquidity,
liquidityGross: liquidity
},
{
index: nearestUsableTick(TickMath.MAX_TICK, TICK_SPACINGS[feeAmount]),
liquidityNet: -liquidity,
liquidityGross: liquidity
}
])
}
const pool_0_1 = makePool(token0, token1)
const pool_1_weth = makePool(token1, WETH)
describe('#swapCallParameters', () => {
describe('single trade input', () => {
it('single-hop exact input', async () => {
const trade = await Trade.fromRoute(
new Route([pool_0_1], token0, token1),
CurrencyAmount.fromRawAmount(token0, 100),
TradeType.EXACT_INPUT
)
const { calldata, value } = SwapQuoter.quoteCallParameters(
trade.swaps[0].route,
trade.inputAmount,
trade.tradeType
)
expect(calldata).toBe(
'0xf7729d43000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000bb800000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000000'
)
expect(value).toBe('0x00')
})
it('single-hop exact output', async () => {
const trade = await Trade.fromRoute(
new Route([pool_0_1], token0, token1),
CurrencyAmount.fromRawAmount(token1, 100),
TradeType.EXACT_OUTPUT
)
const { calldata, value } = SwapQuoter.quoteCallParameters(
trade.swaps[0].route,
trade.outputAmount,
trade.tradeType
)
expect(calldata).toBe(
'0x30d07f21000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000bb800000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000000'
)
expect(value).toBe('0x00')
})
it('multi-hop exact input', async () => {
const trade = await Trade.fromRoute(
new Route([pool_0_1, pool_1_weth], token0, WETH),
CurrencyAmount.fromRawAmount(token0, 100),
TradeType.EXACT_INPUT
)
const { calldata, value } = SwapQuoter.quoteCallParameters(trade.route, trade.inputAmount, trade.tradeType)
expect(calldata).toBe(
'0xcdca17530000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000420000000000000000000000000000000000000001000bb80000000000000000000000000000000000000002000bb8c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000000000000000000000'
)
expect(value).toBe('0x00')
})
it('multi-hop exact output', async () => {
const trade = await Trade.fromRoute(
new Route([pool_0_1, pool_1_weth], token0, WETH),
CurrencyAmount.fromRawAmount(WETH, 100),
TradeType.EXACT_OUTPUT
)
const { calldata, value } = SwapQuoter.quoteCallParameters(trade.route, trade.outputAmount, trade.tradeType)
expect(calldata).toBe(
'0x2f80bb1d000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000042c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000bb80000000000000000000000000000000000000002000bb80000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000'
)
expect(value).toBe('0x00')
})
it('sqrtPriceLimitX96', async () => {
const trade = await Trade.fromRoute(
new Route([pool_0_1], token0, token1),
CurrencyAmount.fromRawAmount(token0, 100),
TradeType.EXACT_INPUT
)
const { calldata, value } = SwapQuoter.quoteCallParameters(trade.route, trade.inputAmount, trade.tradeType, {
sqrtPriceLimitX96: JSBI.exponentiate(JSBI.BigInt(2), JSBI.BigInt(128))
})
expect(calldata).toBe(
'0xf7729d43000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000bb800000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000100000000000000000000000000000000'
)
expect(value).toBe('0x00')
})
})
describe('single trade input using Quoter V2', () => {
it('single-hop exact output', async () => {
const trade = await Trade.fromRoute(
new Route([pool_0_1], token0, token1),
CurrencyAmount.fromRawAmount(token1, 100),
TradeType.EXACT_OUTPUT
)
const { calldata, value } = SwapQuoter.quoteCallParameters(
trade.swaps[0].route,
trade.outputAmount,
trade.tradeType,
{
useQuoterV2: true
}
)
expect(calldata).toBe(
'0xbd21704a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000bb80000000000000000000000000000000000000000000000000000000000000000'
)
expect(value).toBe('0x00')
})
it('single-hop exact input', async () => {
const trade = await Trade.fromRoute(
new Route([pool_0_1], token0, token1),
CurrencyAmount.fromRawAmount(token0, 100),
TradeType.EXACT_INPUT
)
const { calldata, value } = SwapQuoter.quoteCallParameters(
trade.swaps[0].route,
trade.inputAmount,
trade.tradeType,
{ useQuoterV2: true }
)
expect(calldata).toBe(
'0xc6a5026a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000bb80000000000000000000000000000000000000000000000000000000000000000'
)
expect(value).toBe('0x00')
})
})
})
})