-
Notifications
You must be signed in to change notification settings - Fork 12
/
pair.ts
329 lines (303 loc) 路 11.5 KB
/
pair.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import { getCreate2Address } from '@ethersproject/address'
import { keccak256, pack } from '@ethersproject/solidity'
import JSBI from 'jsbi'
import invariant from 'tiny-invariant'
import {
_10000,
BigintIsh,
defaultProtocolFeeDenominator,
defaultSwapFee,
MINIMUM_LIQUIDITY,
ONE,
ZERO,
} from '../constants'
import { ChainId } from '../constants'
import { InsufficientInputAmountError, InsufficientReservesError } from '../errors'
import { parseBigintIsh, sqrt } from '../utils'
import { Price } from './fractions/price'
import { TokenAmount } from './fractions/tokenAmount'
import { LiquidityMiningCampaign } from './liquidity-mining-campaign'
import { Token } from './token'
import { UniswapV2RoutablePlatform } from './trades/routable-platform'
const INITIAL_CACHE_STATE: { [chainId in ChainId]: any } = {
[ChainId.MAINNET]: {},
[ChainId.RINKEBY]: {},
[ChainId.ARBITRUM_ONE]: {},
[ChainId.ARBITRUM_RINKEBY]: {},
[ChainId.XDAI]: {},
[ChainId.POLYGON]: {},
}
let PAIR_ADDRESS_CACHE: {
[supportedPlatformName: string]: {
[chainId: number]: { [token0Address: string]: { [token1Address: string]: string } }
}
} = {
[UniswapV2RoutablePlatform.SWAPR.name]: {
...INITIAL_CACHE_STATE,
},
[UniswapV2RoutablePlatform.SUSHISWAP.name]: {
...INITIAL_CACHE_STATE,
},
[UniswapV2RoutablePlatform.UNISWAP.name]: {
...INITIAL_CACHE_STATE,
},
[UniswapV2RoutablePlatform.HONEYSWAP.name]: {
...INITIAL_CACHE_STATE,
},
[UniswapV2RoutablePlatform.BAOSWAP.name]: {
...INITIAL_CACHE_STATE,
},
[UniswapV2RoutablePlatform.LEVINSWAP.name]: {
...INITIAL_CACHE_STATE,
},
[UniswapV2RoutablePlatform.QUICKSWAP.name]: {
...INITIAL_CACHE_STATE,
},
[UniswapV2RoutablePlatform.DFYN.name]: {
...INITIAL_CACHE_STATE,
},
}
export class Pair {
public readonly liquidityToken: Token
private readonly tokenAmounts: [TokenAmount, TokenAmount]
public readonly swapFee: BigintIsh = defaultSwapFee
public readonly protocolFeeDenominator: BigintIsh = defaultProtocolFeeDenominator
public readonly platform: UniswapV2RoutablePlatform
public liquidityMiningCampaigns: LiquidityMiningCampaign[]
/**
* Returns true if the two pairs are equivalent, i.e. have the same address (calculated using create2).
* @param other other pair to compare
*/
public equals(other: Pair): boolean {
// short circuit on reference equality
if (this === other) {
return true
}
return this.liquidityToken.address === other.liquidityToken.address
}
public static getAddress(
tokenA: Token,
tokenB: Token,
platform: UniswapV2RoutablePlatform = UniswapV2RoutablePlatform.SWAPR
): string {
const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
const chainId = tokenA.chainId
invariant(platform.supportsChain(chainId), 'INVALID_PLATFORM_CHAIN_ID')
if (PAIR_ADDRESS_CACHE?.[platform.name]?.[chainId]?.[tokens[0].address]?.[tokens[1].address] === undefined) {
PAIR_ADDRESS_CACHE = {
...PAIR_ADDRESS_CACHE,
[platform.name]: {
...PAIR_ADDRESS_CACHE[platform.name],
[chainId]: {
...PAIR_ADDRESS_CACHE[platform.name][chainId],
[tokens[0].address]: {
...PAIR_ADDRESS_CACHE?.[platform.name]?.[chainId]?.[tokens[0].address],
[tokens[1].address]: getCreate2Address(
platform.factoryAddress[chainId] as string,
keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
platform.initCodeHash
),
},
},
},
}
}
return PAIR_ADDRESS_CACHE[platform.name][chainId][tokens[0].address][tokens[1].address]
}
constructor(
tokenAmountA: TokenAmount,
tokenAmountB: TokenAmount,
swapFee?: BigintIsh,
protocolFeeDenominator?: BigintIsh,
platform: UniswapV2RoutablePlatform = UniswapV2RoutablePlatform.SWAPR,
liquidityMiningCampaigns: LiquidityMiningCampaign[] = []
) {
invariant(tokenAmountA.token.chainId === tokenAmountB.token.chainId, 'CHAIN_ID')
const tokenAmounts = tokenAmountA.token.sortsBefore(tokenAmountB.token) // does safety checks
? [tokenAmountA, tokenAmountB]
: [tokenAmountB, tokenAmountA]
this.platform = platform ? platform : UniswapV2RoutablePlatform.SWAPR
const liquidityTokenAddress = Pair.getAddress(tokenAmounts[0].token, tokenAmounts[1].token, platform)
this.liquidityToken = new Token(tokenAmounts[0].token.chainId, liquidityTokenAddress, 18, 'DXS', 'DXswap')
this.protocolFeeDenominator = protocolFeeDenominator ? protocolFeeDenominator : defaultProtocolFeeDenominator
this.tokenAmounts = tokenAmounts as [TokenAmount, TokenAmount]
this.swapFee = swapFee ? swapFee : platform.defaultSwapFee
this.liquidityMiningCampaigns = liquidityMiningCampaigns
}
/**
* Returns true if the token is either token0 or token1
* @param token to check
*/
public involvesToken(token: Token): boolean {
return token.equals(this.token0) || token.equals(this.token1)
}
/**
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
*/
public get token0Price(): Price {
return new Price({
baseCurrency: this.token0,
quoteCurrency: this.token1,
denominator: this.tokenAmounts[0].raw,
numerator: this.tokenAmounts[1].raw,
})
}
/**
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
*/
public get token1Price(): Price {
return new Price({
baseCurrency: this.token1,
quoteCurrency: this.token0,
denominator: this.tokenAmounts[1].raw,
numerator: this.tokenAmounts[0].raw,
})
}
/**
* Return the price of the given token in terms of the other token in the pair.
* @param token token to return price of
*/
public priceOf(token: Token): Price {
invariant(this.involvesToken(token), 'TOKEN')
return token.equals(this.token0) ? this.token0Price : this.token1Price
}
/**
* Returns the chain ID of the tokens in the pair.
*/
public get chainId(): ChainId {
return this.token0.chainId
}
public get token0(): Token {
return this.tokenAmounts[0].token
}
public get token1(): Token {
return this.tokenAmounts[1].token
}
public get reserve0(): TokenAmount {
return this.tokenAmounts[0]
}
public get reserve1(): TokenAmount {
return this.tokenAmounts[1]
}
public reserveOf(token: Token): TokenAmount {
invariant(this.involvesToken(token), 'TOKEN')
return token.equals(this.token0) ? this.reserve0 : this.reserve1
}
public getOutputAmount(inputAmount: TokenAmount): [TokenAmount, Pair] {
invariant(this.involvesToken(inputAmount.token), 'TOKEN')
if (JSBI.equal(this.reserve0.raw, ZERO) || JSBI.equal(this.reserve1.raw, ZERO)) {
throw new InsufficientReservesError()
}
const inputReserve = this.reserveOf(inputAmount.token)
const outputReserve = this.reserveOf(inputAmount.token.equals(this.token0) ? this.token1 : this.token0)
const inputAmountWithFee = JSBI.multiply(inputAmount.raw, JSBI.subtract(_10000, parseBigintIsh(this.swapFee)))
const numerator = JSBI.multiply(inputAmountWithFee, outputReserve.raw)
const denominator = JSBI.add(JSBI.multiply(inputReserve.raw, _10000), inputAmountWithFee)
const outputAmount = new TokenAmount(
inputAmount.token.equals(this.token0) ? this.token1 : this.token0,
JSBI.divide(numerator, denominator)
)
if (JSBI.equal(outputAmount.raw, ZERO)) {
throw new InsufficientInputAmountError()
}
return [
outputAmount,
new Pair(
inputReserve.add(inputAmount),
outputReserve.subtract(outputAmount),
this.swapFee,
this.protocolFeeDenominator
),
]
}
public getInputAmount(outputAmount: TokenAmount): [TokenAmount, Pair] {
invariant(this.involvesToken(outputAmount.token), 'TOKEN')
if (
JSBI.equal(this.reserve0.raw, ZERO) ||
JSBI.equal(this.reserve1.raw, ZERO) ||
JSBI.greaterThanOrEqual(outputAmount.raw, this.reserveOf(outputAmount.token).raw)
) {
throw new InsufficientReservesError()
}
const outputReserve = this.reserveOf(outputAmount.token)
const inputReserve = this.reserveOf(outputAmount.token.equals(this.token0) ? this.token1 : this.token0)
const numerator = JSBI.multiply(JSBI.multiply(inputReserve.raw, outputAmount.raw), _10000)
const denominator = JSBI.multiply(
JSBI.subtract(outputReserve.raw, outputAmount.raw),
JSBI.subtract(_10000, parseBigintIsh(this.swapFee))
)
const inputAmount = new TokenAmount(
outputAmount.token.equals(this.token0) ? this.token1 : this.token0,
JSBI.add(JSBI.divide(numerator, denominator), ONE)
)
return [
inputAmount,
new Pair(
inputReserve.add(inputAmount),
outputReserve.subtract(outputAmount),
this.swapFee,
this.protocolFeeDenominator
),
]
}
public getLiquidityMinted(
totalSupply: TokenAmount,
tokenAmountA: TokenAmount,
tokenAmountB: TokenAmount
): TokenAmount {
invariant(totalSupply.token.equals(this.liquidityToken), 'LIQUIDITY')
const tokenAmounts = tokenAmountA.token.sortsBefore(tokenAmountB.token) // does safety checks
? [tokenAmountA, tokenAmountB]
: [tokenAmountB, tokenAmountA]
invariant(tokenAmounts[0].token.equals(this.token0) && tokenAmounts[1].token.equals(this.token1), 'TOKEN')
let liquidity: JSBI
if (JSBI.equal(totalSupply.raw, ZERO)) {
liquidity = JSBI.subtract(sqrt(JSBI.multiply(tokenAmounts[0].raw, tokenAmounts[1].raw)), MINIMUM_LIQUIDITY)
} else {
const amount0 = JSBI.divide(JSBI.multiply(tokenAmounts[0].raw, totalSupply.raw), this.reserve0.raw)
const amount1 = JSBI.divide(JSBI.multiply(tokenAmounts[1].raw, totalSupply.raw), this.reserve1.raw)
liquidity = JSBI.lessThanOrEqual(amount0, amount1) ? amount0 : amount1
}
if (!JSBI.greaterThan(liquidity, ZERO)) {
throw new InsufficientInputAmountError()
}
return new TokenAmount(this.liquidityToken, liquidity)
}
public getLiquidityValue(
token: Token,
totalSupply: TokenAmount,
liquidity: TokenAmount,
feeOn = false,
kLast?: BigintIsh
): TokenAmount {
invariant(this.involvesToken(token), 'TOKEN')
invariant(totalSupply.token.equals(this.liquidityToken), 'TOTAL_SUPPLY')
invariant(liquidity.token.equals(this.liquidityToken), 'LIQUIDITY')
invariant(JSBI.lessThanOrEqual(liquidity.raw, totalSupply.raw), 'LIQUIDITY')
let totalSupplyAdjusted: TokenAmount
if (!feeOn) {
totalSupplyAdjusted = totalSupply
} else {
invariant(!!kLast, 'K_LAST')
const kLastParsed = parseBigintIsh(kLast)
if (!JSBI.equal(kLastParsed, ZERO)) {
const rootK = sqrt(JSBI.multiply(this.reserve0.raw, this.reserve1.raw))
const rootKLast = sqrt(kLastParsed)
if (JSBI.greaterThan(rootK, rootKLast)) {
const numerator = JSBI.multiply(totalSupply.raw, JSBI.subtract(rootK, rootKLast))
const denominator = JSBI.add(JSBI.multiply(rootK, parseBigintIsh(this.protocolFeeDenominator)), rootKLast)
const feeLiquidity = JSBI.divide(numerator, denominator)
totalSupplyAdjusted = totalSupply.add(new TokenAmount(this.liquidityToken, feeLiquidity))
} else {
totalSupplyAdjusted = totalSupply
}
} else {
totalSupplyAdjusted = totalSupply
}
}
return new TokenAmount(
token,
JSBI.divide(JSBI.multiply(liquidity.raw, this.reserveOf(token).raw), totalSupplyAdjusted.raw)
)
}
}