-
Notifications
You must be signed in to change notification settings - Fork 13
/
tokenSwap.ts
479 lines (361 loc) · 12.5 KB
/
tokenSwap.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import { computeOutputAmount } from '@orca-so/stablecurve';
import {
Connection, PublicKey,
} from '@solana/web3.js';
import BN from 'bn.js';
import { FarmingClient, PRECISION_NOMINATOR, TokenClient } from '.';
import {
CURVE, PoolClient,
PoolRpcResponse,
PoolRpcV2Response,
SIDE,
SOLANA_RPC_ENDPOINT, SWAP_FEE_DENOMINATOR, SWAP_FEE_NUMERATOR, TokenSwapAddlLiquidityParams, TokenSwapGetPriceParams,
TokenSwapLoadParams,
TokenSwapParams,
TokenSwapWithdrawLiquidityParams,
} from './pools';
import { SwapBase } from './swapBase';
import { TokenSwapGetFarmedParams, Wallet, WithReferral } from './types';
/**
* High-level API for Aldrin AMM Pools
*/
export class TokenSwap extends SwapBase {
constructor(
private pools: PoolRpcResponse[],
private poolClient: PoolClient,
protected tokenClient: TokenClient,
private farmingClient: FarmingClient,
protected connection = new Connection(SOLANA_RPC_ENDPOINT),
private wallet: Wallet | null = null,
private referralParams: WithReferral | undefined = undefined
) {
super(tokenClient, connection)
}
findPool(mintFrom: PublicKey, mintTo: PublicKey): { pool: PoolRpcResponse, isInverted: boolean } | null {
const pool = this.pools.find((p) =>
(p.baseTokenMint.equals(mintFrom) && p.quoteTokenMint.equals(mintTo)) ||
(p.baseTokenMint.equals(mintTo) && p.quoteTokenMint.equals(mintFrom))
)
if (!pool) {
return null
}
const isInverted = pool.quoteTokenMint.equals(mintTo)
return { pool, isInverted }
}
async swap(params: TokenSwapParams) {
const resolvedInputs = await this.resolveSwapInputs(params)
return this.poolClient.swap({ ...resolvedInputs, slippage: params.slippage, referralParams: this.referralParams })
}
/**
* Make tokens swap
* @returns Transaction Id
*/
private async resolveSwapInputs(params: TokenSwapParams) {
const { wallet = this.wallet, mintFrom, mintTo } = params
let { minIncomeAmount, outcomeAmount } = params
if (!wallet) {
throw new Error('Wallet not provided')
}
const poolSearch = this.findPool(mintFrom, mintTo)
if (!poolSearch) {
throw new Error('Pool for mints not found') // TODO: pools routing
}
const { pool, isInverted } = poolSearch
const { baseTokenVault, quoteTokenVault, curveType } = pool
const [
baseVaultAccount,
quoteVaultAccount,
] = await Promise.all([
this.tokenClient.getTokenAccount(baseTokenVault),
this.tokenClient.getTokenAccount(quoteTokenVault),
])
/**
*
* Buy BASE on B QUOTE
*
* Calculate exact buy price & amount
* Calculation does not consider any fees, please check {PoolRpcResponse#fees}
*
* X - Base token amount in pool
* Y - Quote token amount in pool
* A - Token amount to buy
* B - Quote token amount
*
* X * Y = (X - A) * (Y + B)
*
* X - A = (X * Y) / (Y + B)
*
* A = X - (X * Y) / (Y + B)
*
*
*
* Y + B = (X * Y) / (X - A)
*
* B = (X * Y) / (X - A) - Y
*
* */
const X = isInverted ? quoteVaultAccount.amount : baseVaultAccount.amount
const Y = isInverted ? baseVaultAccount.amount : quoteVaultAccount.amount
if (!minIncomeAmount) {
if (!outcomeAmount) {
throw new Error('No amounts defined') // Type-check hack
}
const B = outcomeAmount
minIncomeAmount = curveType === CURVE.STABLE
? outcomeAmount
: X
.sub(
X.mul(Y)
.div(
Y.add(B)
)
)
}
if (!outcomeAmount) {
const A = minIncomeAmount
outcomeAmount = curveType === CURVE.STABLE
? minIncomeAmount
: X
.mul(Y)
.div(
X.sub(A)
)
.sub(Y)
}
const walletTokens = await this.getWalletTokens(wallet)
const baseMint = baseVaultAccount.mint.toBase58()
const quoteMint = quoteVaultAccount.mint.toBase58()
const userBaseTokenAccount = walletTokens.find((wt) => wt.account.data.parsed.info.mint === baseMint)
const userQuoteTokenAccount = walletTokens.find((wt) => wt.account.data.parsed.info.mint === quoteMint)
return {
pool,
minIncomeAmount,
outcomeAmount,
userBaseTokenAccount: userBaseTokenAccount?.pubkey,
userQuoteTokenAccount: userQuoteTokenAccount?.pubkey,
side: isInverted ? SIDE.ASK : SIDE.BID,
isInverted,
wallet,
}
}
public async getSwapImpact(params: TokenSwapParams) {
const {
pool,
minIncomeAmount,
outcomeAmount,
isInverted,
} = await this.resolveSwapInputs(params)
const { baseTokenVault } = pool
const baseVaultAccount = await this.tokenClient.getTokenAccount(baseTokenVault);
// isInverted probably is not correct, remove ! later
const poolsAmountDiff = !isInverted
? baseVaultAccount.amount.div(minIncomeAmount)
: baseVaultAccount.amount.div(outcomeAmount)
const priceImpact = 100 / (poolsAmountDiff.toNumber() + 1)
const fee = outcomeAmount.mul(SWAP_FEE_NUMERATOR).div(SWAP_FEE_DENOMINATOR)
return {
minIncomeAmount,
outcomeAmount,
priceImpact,
isInverted,
fee,
}
}
/**
* Add liquidity to Aldrin's AMM pool
* @param params
* @returns Transaction Id
*/
async depositLiquidity(params: TokenSwapAddlLiquidityParams): Promise<string> {
const { poolMint, wallet = this.wallet } = params
let { maxBase, maxQuote } = params
if (!wallet) {
throw new Error('Wallet not provided')
}
const pool = this.pools.find((p) => p.poolMint.equals(poolMint))
if (!pool) {
throw new Error(`Pool with mint ${poolMint.toBase58()} not found`)
}
const { baseTokenVault, quoteTokenVault, baseTokenMint, quoteTokenMint } = pool
const [
baseVaultAccount,
quoteVaultAccount,
] = await Promise.all([
this.tokenClient.getTokenAccount(baseTokenVault),
this.tokenClient.getTokenAccount(quoteTokenVault),
])
const price = quoteVaultAccount.amount.mul(PRECISION_NOMINATOR).div(baseVaultAccount.amount)
const walletTokens = await this.getWalletTokens(wallet)
const baseMint = baseTokenMint.toBase58()
const quoteMint = quoteTokenMint.toBase58()
const userBaseTokenAccount = walletTokens.find((wt) => wt.account.data.parsed.info.mint === baseMint)
const userQuoteTokenAccount = walletTokens.find((wt) => wt.account.data.parsed.info.mint === quoteMint)
if (!userBaseTokenAccount) {
throw new Error('Unable to add liquidity: base token account not found')
}
if (!userQuoteTokenAccount) {
throw new Error('Unable to add liquidity: quote token account not found')
}
if (!maxBase) {
if (!maxQuote) {
throw new Error('Neither base nor quote amounts does not provided!') // TODO: max?
}
maxBase = maxQuote.mul(PRECISION_NOMINATOR).div(price)
}
if (!maxQuote) {
maxQuote = maxBase.mul(price).div(PRECISION_NOMINATOR)
}
const poolTokenAccount = walletTokens.find((wt) => wt.account.data.parsed.info.mint === pool.poolMint.toBase58())
return this.poolClient.depositLiquidity({
pool,
userPoolTokenAccount: poolTokenAccount ? poolTokenAccount.pubkey : null,
maxBaseTokenAmount: maxBase,
maxQuoteTokenAmount: maxQuote,
userBaseTokenAccount: userBaseTokenAccount.pubkey,
userQuoteTokenAccount: userQuoteTokenAccount.pubkey,
wallet,
})
}
/**
* Withdraw liquidity from Aldrin's AMM pool
* @param params
* @returns Transaction Id
*/
async withdrawLiquidity(params: TokenSwapWithdrawLiquidityParams): Promise<string> {
const { poolMint, wallet = this.wallet, poolTokenAmount } = params
if (!wallet) {
throw new Error('Wallet not provided')
}
const pool = this.pools.find((p) => p.poolMint.equals(poolMint))
if (!pool) {
throw new Error(`Pool with mint ${poolMint.toBase58()} not found`)
}
const { baseTokenMint, quoteTokenMint } = pool
const walletTokens = await this.getWalletTokens(wallet)
const baseMint = baseTokenMint.toBase58()
const quoteMint = quoteTokenMint.toBase58()
const userBaseTokenAccount = walletTokens.find((wt) => wt.account.data.parsed.info.mint === baseMint)
const userQuoteTokenAccount = walletTokens.find((wt) => wt.account.data.parsed.info.mint === quoteMint)
const poolTokenAccount = walletTokens.find((wt) => wt.account.data.parsed.info.mint === pool.poolMint.toBase58())
if (!poolTokenAccount) {
throw new Error('Unable to withdraw liquidity: pool token account not found')
}
return this.poolClient.withdrawLiquidity({
pool,
userPoolTokenAccount: poolTokenAccount.pubkey,
userBaseTokenAccount: userBaseTokenAccount?.pubkey,
userQuoteTokenAccount: userQuoteTokenAccount?.pubkey,
poolTokenAmount,
slippage: params.slippage,
wallet,
baseTokenReturnedMin: params.minBase,
quoteTokenReturnedMin: params.minQuote,
})
}
/**
* Calculate price of mintForm/mintTo tokens
* @param params
* @returns
*/
async getPrice(params: TokenSwapGetPriceParams) {
const { mintFrom, mintTo } = params
const poolSearch = this.findPool(mintFrom, mintTo)
if (!poolSearch) {
throw new Error('Pool for mints not found') // TODO: pools routing
}
const { pool, isInverted } = poolSearch
const { baseTokenMint, baseTokenVault, quoteTokenMint, quoteTokenVault, poolVersion } = pool
const [
baseMintInfo,
quoteMintInfo,
baseVaultAccount,
quoteVaultAccount,
] = await Promise.all([
this.getMintInfo(baseTokenMint),
this.getMintInfo(quoteTokenMint),
this.tokenClient.getTokenAccount(baseTokenVault),
this.tokenClient.getTokenAccount(quoteTokenVault),
])
if (poolVersion === 2) {
const { curveType } = pool as PoolRpcV2Response
if (curveType === 1) {
const amountToSwap = isInverted ? baseVaultAccount.amount.divn(2) : quoteVaultAccount.amount.divn(2)
const poolInputAmount = isInverted ? quoteVaultAccount.amount : baseVaultAccount.amount
const poolOutputAmount = isInverted ? baseVaultAccount.amount : quoteVaultAccount.amount
const outputAmount = computeOutputAmount(
amountToSwap,
poolInputAmount,
poolOutputAmount,
new BN(170), // Fixed
)
return parseFloat(outputAmount
.mul(PRECISION_NOMINATOR)
.mul(baseMintInfo.decimalDenominator)
.div(quoteMintInfo.decimalDenominator)
.toString()) / parseFloat(amountToSwap.toString()) / PRECISION_NOMINATOR.toNumber()
}
}
const price = quoteVaultAccount.amount
.mul(PRECISION_NOMINATOR)
.mul(baseMintInfo.decimalDenominator)
.div(quoteMintInfo.decimalDenominator)
.div(baseVaultAccount.amount)
if (isInverted) {
return PRECISION_NOMINATOR.toNumber() / price.toNumber()
}
return price.toNumber() / PRECISION_NOMINATOR.toNumber()
}
/**
* Auto-initialize Tokenswap
*/
static async initialize(params: TokenSwapLoadParams = {}): Promise<TokenSwap> {
const { connection = new Connection(SOLANA_RPC_ENDPOINT), wallet } = params
const poolClient = new PoolClient(connection)
const tokenClient = new TokenClient(connection)
const farmingClient = new FarmingClient(connection)
const [pools, v2Pools] = await Promise.all([
poolClient.getPools(),
poolClient.getV2Pools(),
])
return new TokenSwap(
[...pools, ...v2Pools],
poolClient,
tokenClient,
farmingClient,
connection,
wallet,
params.referralParams,
)
}
async getFarmed(params: TokenSwapGetFarmedParams) {
const farms = await this.farmingClient.getFarms({
stakeMint: params.poolMint,
})
const farm = farms[0]
if (!farm) {
throw new Error('No farm found')
}
const farmers = await this.farmingClient.getFarmers({
farm: farm.publicKey,
authority: params.wallet.publicKey,
})
const farmer = farmers[0]
if (!farmer) {
throw new Error('Farmer not found!')
}
return farmer.harvests
}
async claimFarmed(params: TokenSwapGetFarmedParams) {
const farms = await this.farmingClient.getFarms({
stakeMint: params.poolMint,
})
const farm = farms[0]
if (!farm) {
throw new Error('No farm found')
}
return this. farmingClient.claimFarmed({
farm:farm.publicKey,
wallet: params.wallet,
})
}
}