forked from superrare/v3-periphery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuoter.spec.ts
201 lines (167 loc) · 5.7 KB
/
Quoter.spec.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { Fixture } from 'ethereum-waffle'
import { constants, Wallet } from 'ethers'
import { ethers, waffle } from 'hardhat'
import { MockTimeNonfungiblePositionManager, Quoter, TestERC20 } from '../typechain'
import completeFixture from './shared/completeFixture'
import { FeeAmount, MaxUint128, TICK_SPACINGS } from './shared/constants'
import { encodePriceSqrt } from './shared/encodePriceSqrt'
import { expandTo18Decimals } from './shared/expandTo18Decimals'
import { expect } from './shared/expect'
import { encodePath } from './shared/path'
import { createPool } from './shared/quoter'
describe('Quoter', () => {
let wallet: Wallet
let trader: Wallet
const swapRouterFixture: Fixture<{
nft: MockTimeNonfungiblePositionManager
tokens: [TestERC20, TestERC20, TestERC20]
quoter: Quoter
}> = async (wallets, provider) => {
const { weth9, factory, router, tokens, nft } = await completeFixture(wallets, provider)
// approve & fund wallets
for (const token of tokens) {
await token.approve(router.address, constants.MaxUint256)
await token.approve(nft.address, constants.MaxUint256)
await token.connect(trader).approve(router.address, constants.MaxUint256)
await token.transfer(trader.address, expandTo18Decimals(1_000_000))
}
const quoterFactory = await ethers.getContractFactory('Quoter')
quoter = (await quoterFactory.deploy(factory.address, weth9.address)) as Quoter
return {
tokens,
nft,
quoter,
}
}
let nft: MockTimeNonfungiblePositionManager
let tokens: [TestERC20, TestERC20, TestERC20]
let quoter: Quoter
let loadFixture: ReturnType<typeof waffle.createFixtureLoader>
before('create fixture loader', async () => {
const wallets = await (ethers as any).getSigners()
;[wallet, trader] = wallets
loadFixture = waffle.createFixtureLoader(wallets)
})
// helper for getting weth and token balances
beforeEach('load fixture', async () => {
;({ tokens, nft, quoter } = await loadFixture(swapRouterFixture))
})
describe('quotes', () => {
beforeEach(async () => {
await createPool(nft, wallet, tokens[0].address, tokens[1].address)
await createPool(nft, wallet, tokens[1].address, tokens[2].address)
})
describe('#quoteExactInput', () => {
it('0 -> 1', async () => {
const quote = await quoter.callStatic.quoteExactInput(
encodePath([tokens[0].address, tokens[1].address], [FeeAmount.MEDIUM]),
3
)
expect(quote).to.eq(1)
})
it('1 -> 0', async () => {
const quote = await quoter.callStatic.quoteExactInput(
encodePath([tokens[1].address, tokens[0].address], [FeeAmount.MEDIUM]),
3
)
expect(quote).to.eq(1)
})
it('0 -> 1 -> 2', async () => {
const quote = await quoter.callStatic.quoteExactInput(
encodePath(
tokens.map((token) => token.address),
[FeeAmount.MEDIUM, FeeAmount.MEDIUM]
),
5
)
expect(quote).to.eq(1)
})
it('2 -> 1 -> 0', async () => {
const quote = await quoter.callStatic.quoteExactInput(
encodePath(tokens.map((token) => token.address).reverse(), [FeeAmount.MEDIUM, FeeAmount.MEDIUM]),
5
)
expect(quote).to.eq(1)
})
})
describe('#quoteExactInputSingle', () => {
it('0 -> 1', async () => {
const quote = await quoter.callStatic.quoteExactInputSingle(
tokens[0].address,
tokens[1].address,
FeeAmount.MEDIUM,
MaxUint128,
// -2%
encodePriceSqrt(100, 102)
)
expect(quote).to.eq(9852)
})
it('1 -> 0', async () => {
const quote = await quoter.callStatic.quoteExactInputSingle(
tokens[1].address,
tokens[0].address,
FeeAmount.MEDIUM,
MaxUint128,
// +2%
encodePriceSqrt(102, 100)
)
expect(quote).to.eq(9852)
})
})
describe('#quoteExactOutput', () => {
it('0 -> 1', async () => {
const quote = await quoter.callStatic.quoteExactOutput(
encodePath([tokens[1].address, tokens[0].address], [FeeAmount.MEDIUM]),
1
)
expect(quote).to.eq(3)
})
it('1 -> 0', async () => {
const quote = await quoter.callStatic.quoteExactOutput(
encodePath([tokens[0].address, tokens[1].address], [FeeAmount.MEDIUM]),
1
)
expect(quote).to.eq(3)
})
it('0 -> 1 -> 2', async () => {
const quote = await quoter.callStatic.quoteExactOutput(
encodePath(tokens.map((token) => token.address).reverse(), [FeeAmount.MEDIUM, FeeAmount.MEDIUM]),
1
)
expect(quote).to.eq(5)
})
it('2 -> 1 -> 0', async () => {
const quote = await quoter.callStatic.quoteExactOutput(
encodePath(
tokens.map((token) => token.address),
[FeeAmount.MEDIUM, FeeAmount.MEDIUM]
),
1
)
expect(quote).to.eq(5)
})
})
describe('#quoteExactOutputSingle', () => {
it('0 -> 1', async () => {
const quote = await quoter.callStatic.quoteExactOutputSingle(
tokens[0].address,
tokens[1].address,
FeeAmount.MEDIUM,
MaxUint128,
encodePriceSqrt(100, 102)
)
expect(quote).to.eq(9981)
})
it('1 -> 0', async () => {
const quote = await quoter.callStatic.quoteExactOutputSingle(
tokens[1].address,
tokens[0].address,
FeeAmount.MEDIUM,
MaxUint128,
encodePriceSqrt(102, 100)
)
expect(quote).to.eq(9981)
})
})
})
})