@exponent-labs/exponent-sdk 0.0.9 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/addressLookupTableUtil.js +3 -2
- package/build/addressLookupTableUtil.js.map +1 -1
- package/build/events.d.ts +3 -3
- package/build/events.js +49 -47
- package/build/events.js.map +1 -1
- package/build/flavors.d.ts +3 -3
- package/build/flavors.js +34 -38
- package/build/flavors.js.map +1 -1
- package/build/index.js +7 -17
- package/build/index.js.map +1 -1
- package/build/lpPosition.d.ts +6 -5
- package/build/lpPosition.js +37 -19
- package/build/lpPosition.js.map +1 -1
- package/build/market.d.ts +34 -34
- package/build/market.js +207 -203
- package/build/market.js.map +1 -1
- package/build/syPosition.d.ts +2 -2
- package/build/syPosition.js +21 -17
- package/build/syPosition.js.map +1 -1
- package/build/tokenUtil.d.ts +2 -2
- package/build/tokenUtil.js +7 -6
- package/build/tokenUtil.js.map +1 -1
- package/build/utils/altUtil.d.ts +3 -3
- package/build/utils/altUtil.js +18 -11
- package/build/utils/altUtil.js.map +1 -1
- package/build/utils/binSolver.js +2 -1
- package/build/utils/binSolver.js.map +1 -1
- package/build/utils/binSolver.test.js +6 -5
- package/build/utils/binSolver.test.js.map +1 -1
- package/build/utils/index.js +4 -3
- package/build/utils/index.js.map +1 -1
- package/build/vault.d.ts +12 -11
- package/build/vault.js +69 -68
- package/build/vault.js.map +1 -1
- package/build/ytPosition.d.ts +9 -8
- package/build/ytPosition.js +42 -34
- package/build/ytPosition.js.map +1 -1
- package/package.json +22 -22
- package/src/addressLookupTableUtil.ts +2 -1
- package/src/events.ts +60 -45
- package/src/flavors.ts +33 -39
- package/src/lpPosition.ts +49 -21
- package/src/market.ts +307 -229
- package/src/syPosition.ts +38 -26
- package/src/tokenUtil.ts +7 -5
- package/src/utils/altUtil.ts +17 -12
- package/src/utils/binSolver.test.ts +2 -0
- package/src/utils/index.ts +2 -0
- package/src/vault.ts +108 -91
- package/src/ytPosition.ts +57 -37
package/src/market.ts
CHANGED
|
@@ -1,32 +1,38 @@
|
|
|
1
|
-
import { AnchorProvider, Program, Wallet, web3 } from "@coral-xyz/anchor"
|
|
2
|
-
import { ExponentFetcher, LiquidityNetBalanceLimits, LpFarm } from "@exponent-labs/exponent-fetcher"
|
|
1
|
+
import { AnchorProvider, BN, Program, Wallet, web3 } from "@coral-xyz/anchor"
|
|
3
2
|
import {
|
|
3
|
+
TOKEN_PROGRAM_ID,
|
|
4
4
|
createAssociatedTokenAccountIdempotentInstruction,
|
|
5
5
|
getAssociatedTokenAddressSync,
|
|
6
|
-
TOKEN_PROGRAM_ID,
|
|
7
6
|
} from "@solana/spl-token"
|
|
7
|
+
|
|
8
|
+
import { ExponentFetcher, LiquidityNetBalanceLimits, LpFarm } from "@exponent-labs/exponent-fetcher"
|
|
9
|
+
import { ExponentCore, IDL } from "@exponent-labs/exponent-idl"
|
|
10
|
+
import { ExponentPDA } from "@exponent-labs/exponent-pda"
|
|
11
|
+
import {
|
|
12
|
+
AnchorizedPNum,
|
|
13
|
+
CpiAccountsRaw,
|
|
14
|
+
CpiAccountsRawJson,
|
|
15
|
+
Flavor,
|
|
16
|
+
SyPosition,
|
|
17
|
+
serializeCpiAccountsRaw,
|
|
18
|
+
} from "@exponent-labs/exponent-types"
|
|
19
|
+
import { LiquidityAdd, MarketCalculator, lpOutForTokensIn } from "@exponent-labs/market-math"
|
|
20
|
+
import { PreciseNumber } from "@exponent-labs/precise-number"
|
|
21
|
+
|
|
8
22
|
import { fetchAddressLookupTable, makeCpiAccountMetaLists } from "./addressLookupTableUtil"
|
|
9
23
|
import { Environment } from "./environment"
|
|
10
|
-
import { getMultipleMintSupply } from "./tokenUtil"
|
|
11
24
|
import {
|
|
12
|
-
|
|
13
|
-
makeFlavorKaminoSync,
|
|
25
|
+
makeFlavorGenericSync,
|
|
14
26
|
makeFlavorJitoRestakingSync,
|
|
27
|
+
makeFlavorKaminoSync,
|
|
28
|
+
makeFlavorMarginfiSync,
|
|
15
29
|
makeFlavorPerenaSync,
|
|
16
|
-
makeFlavorGenericSync,
|
|
17
30
|
} from "./flavors"
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import { ExponentPDA } from "@exponent-labs/exponent-pda"
|
|
21
|
-
import { Vault } from "./vault"
|
|
22
|
-
import { LiquidityAdd, lpOutForTokensIn, MarketCalculator } from "@exponent-labs/market-math"
|
|
31
|
+
import { makeSyPosition } from "./syPosition"
|
|
32
|
+
import { getMultipleMintSupply } from "./tokenUtil"
|
|
23
33
|
import { emitEventAuthority, getExponentAdminStatePda, uniqueRemainingAccounts } from "./utils"
|
|
24
|
-
import { AccountInfo, CpiAccountsRaw, Flavor, serializeCpiAccountsRaw, SyPosition } from "@exponent-labs/exponent-types"
|
|
25
|
-
import { PreciseNumber } from "@exponent-labs/precise-number"
|
|
26
|
-
import { AnchorizedPNum } from "@exponent-labs/exponent-types"
|
|
27
34
|
import { extendAddressLookupTable } from "./utils/altUtil"
|
|
28
|
-
import {
|
|
29
|
-
import { CpiAccountsRawJson } from "@exponent-labs/exponent-types"
|
|
35
|
+
import { Vault } from "./vault"
|
|
30
36
|
|
|
31
37
|
export { LiquidityAdd }
|
|
32
38
|
|
|
@@ -104,6 +110,7 @@ type MarketArgs = {
|
|
|
104
110
|
}
|
|
105
111
|
}
|
|
106
112
|
|
|
113
|
+
//TODO Put it in the shared package because this code is repeated in many packages
|
|
107
114
|
export class MyWallet implements Wallet {
|
|
108
115
|
constructor(readonly payer: web3.Keypair) {
|
|
109
116
|
this.payer = payer
|
|
@@ -163,13 +170,13 @@ export class Market {
|
|
|
163
170
|
options: MarketLoadOptions = {},
|
|
164
171
|
): Promise<Market> {
|
|
165
172
|
const fetcher = new ExponentFetcher({ connection })
|
|
166
|
-
const
|
|
173
|
+
const fetchedMarket = await fetcher.fetchMarket(address)
|
|
167
174
|
const [[lpSupply, sySupply], alt, loadedVault] = await Promise.all([
|
|
168
|
-
getMultipleMintSupply(connection, [
|
|
169
|
-
fetchAddressLookupTable(connection,
|
|
170
|
-
vault || Vault.load(env, connection,
|
|
175
|
+
getMultipleMintSupply(connection, [fetchedMarket.mintLp, fetchedMarket.mintSy]),
|
|
176
|
+
fetchAddressLookupTable(connection, fetchedMarket.addressLookupTable),
|
|
177
|
+
vault || Vault.load(env, connection, fetchedMarket.vault, options),
|
|
171
178
|
])
|
|
172
|
-
const cpiAccounts = makeCpiAccountMetaLists(alt,
|
|
179
|
+
const cpiAccounts = makeCpiAccountMetaLists(alt, fetchedMarket.cpiAccounts)
|
|
173
180
|
|
|
174
181
|
const flavor = (() => {
|
|
175
182
|
switch (loadedVault.flavor.flavor) {
|
|
@@ -188,10 +195,10 @@ export class Market {
|
|
|
188
195
|
}
|
|
189
196
|
})()
|
|
190
197
|
|
|
191
|
-
const syPosition = await makeSyPosition(fetcher, flavor,
|
|
198
|
+
const syPosition = await makeSyPosition(fetcher, flavor, fetchedMarket.syProgram, address)
|
|
192
199
|
|
|
193
200
|
const state: MarketArgs = {
|
|
194
|
-
...
|
|
201
|
+
...fetchedMarket,
|
|
195
202
|
vault: loadedVault,
|
|
196
203
|
cpiAccounts,
|
|
197
204
|
lpSupply,
|
|
@@ -202,10 +209,10 @@ export class Market {
|
|
|
202
209
|
return new Market(state, address, env, connection)
|
|
203
210
|
}
|
|
204
211
|
|
|
205
|
-
async reload(
|
|
206
|
-
const
|
|
207
|
-
this.state =
|
|
208
|
-
return
|
|
212
|
+
async reload(connection: web3.Connection = this.connection) {
|
|
213
|
+
const market = await Market.load(this.env, connection, this.selfAddress)
|
|
214
|
+
this.state = market.state
|
|
215
|
+
return market
|
|
209
216
|
}
|
|
210
217
|
|
|
211
218
|
/** Convert to a JSON representation */
|
|
@@ -361,8 +368,8 @@ export class Market {
|
|
|
361
368
|
}
|
|
362
369
|
|
|
363
370
|
get currentPtPriceInAsset(): number {
|
|
364
|
-
const
|
|
365
|
-
return
|
|
371
|
+
const marketCalculator = this.marketCalculator()
|
|
372
|
+
return marketCalculator.exchangeRate
|
|
366
373
|
}
|
|
367
374
|
|
|
368
375
|
get ptDiscount(): number {
|
|
@@ -378,8 +385,8 @@ export class Market {
|
|
|
378
385
|
}
|
|
379
386
|
|
|
380
387
|
get secondsRemaining(): number {
|
|
381
|
-
const
|
|
382
|
-
return Math.max(0, Math.round(this.state.expirationTs -
|
|
388
|
+
const timeNow = Date.now() / 1000
|
|
389
|
+
return Math.max(0, Math.round(this.state.expirationTs - timeNow))
|
|
383
390
|
}
|
|
384
391
|
|
|
385
392
|
get absolutePtYield(): number {
|
|
@@ -388,8 +395,12 @@ export class Market {
|
|
|
388
395
|
}
|
|
389
396
|
|
|
390
397
|
/** Annualize a rate given the number of seconds remaining until maturity */
|
|
391
|
-
static annualize(
|
|
392
|
-
return (
|
|
398
|
+
static annualize(rate: number, secondsRemaining: number) {
|
|
399
|
+
return (rate * SECONDS_PER_YEAR) / secondsRemaining
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
static annualizeApy(rate: number, secondsRemaining: number) {
|
|
403
|
+
return (1 + rate) ** (SECONDS_PER_YEAR / secondsRemaining) - 1
|
|
393
404
|
}
|
|
394
405
|
|
|
395
406
|
/** Annualized PT fixed rate */
|
|
@@ -397,6 +408,10 @@ export class Market {
|
|
|
397
408
|
return Market.annualize(this.absolutePtYield, this.secondsRemaining)
|
|
398
409
|
}
|
|
399
410
|
|
|
411
|
+
get ptApy(): number {
|
|
412
|
+
return Market.annualizeApy(this.absolutePtYield, this.secondsRemaining)
|
|
413
|
+
}
|
|
414
|
+
|
|
400
415
|
/** The fee rate taken off of trade fees (typically around 20%) expressed as a BPS number */
|
|
401
416
|
get feeTreasuryBps(): number {
|
|
402
417
|
return this.state.feeTreasurySyBps
|
|
@@ -415,7 +430,7 @@ export class Market {
|
|
|
415
430
|
return this.feeTreasuryBps / 10_000
|
|
416
431
|
}
|
|
417
432
|
|
|
418
|
-
/** Calculate
|
|
433
|
+
/** Calculate amount of LP tokens to expect for tokens in */
|
|
419
434
|
lpOutForTokensIn(syInIntent: bigint, ptInIntent: bigint): LiquidityAdd {
|
|
420
435
|
return lpOutForTokensIn({
|
|
421
436
|
syIntent: Number(syInIntent),
|
|
@@ -427,7 +442,8 @@ export class Market {
|
|
|
427
442
|
}
|
|
428
443
|
|
|
429
444
|
marketCalculator() {
|
|
430
|
-
const
|
|
445
|
+
const timeNow = Date.now() / 1000
|
|
446
|
+
const secondsRemaining = Math.max(0, Math.round(this.state.expirationTs - timeNow))
|
|
431
447
|
return new MarketCalculator({
|
|
432
448
|
liquiditySy: parseInt(this.syBalance.toString()),
|
|
433
449
|
liquidityPt: parseInt(this.ptBalance.toString()),
|
|
@@ -442,6 +458,7 @@ export class Market {
|
|
|
442
458
|
windowStartTimestamp: this.state.liquidityNetBalanceLimits.windowStartTimestamp,
|
|
443
459
|
windowStartNetBalance: this.state.liquidityNetBalanceLimits.windowStartNetBalance.toNumber(),
|
|
444
460
|
windowDurationSeconds: this.state.liquidityNetBalanceLimits.windowDurationSeconds,
|
|
461
|
+
feeTreasurySyBps: this.state.feeTreasurySyBps,
|
|
445
462
|
})
|
|
446
463
|
}
|
|
447
464
|
|
|
@@ -458,9 +475,9 @@ export class Market {
|
|
|
458
475
|
syInIntent,
|
|
459
476
|
minLpOut,
|
|
460
477
|
depositor,
|
|
461
|
-
ptSrc,
|
|
462
|
-
sySrc,
|
|
463
|
-
lpDst,
|
|
478
|
+
ptSrc: ptSrcParam,
|
|
479
|
+
sySrc: sySrcParam,
|
|
480
|
+
lpDst: lpDstParam,
|
|
464
481
|
}: {
|
|
465
482
|
/** Intended (maximum) amount of PT in */
|
|
466
483
|
ptInIntent: bigint
|
|
@@ -475,9 +492,9 @@ export class Market {
|
|
|
475
492
|
}) {
|
|
476
493
|
const tokenProgram = TOKEN_PROGRAM_ID
|
|
477
494
|
|
|
478
|
-
sySrc =
|
|
479
|
-
ptSrc =
|
|
480
|
-
lpDst =
|
|
495
|
+
const sySrc = sySrcParam || getAssociatedTokenAddressSync(this.mintSy, depositor, true, TOKEN_PROGRAM_ID)
|
|
496
|
+
const ptSrc = ptSrcParam || getAssociatedTokenAddressSync(this.mintPt, depositor, true, TOKEN_PROGRAM_ID)
|
|
497
|
+
const lpDst = lpDstParam || getAssociatedTokenAddressSync(this.mintLp, depositor, true, TOKEN_PROGRAM_ID)
|
|
481
498
|
|
|
482
499
|
const syRemAccounts = this.cpiAccounts.depositSy
|
|
483
500
|
|
|
@@ -524,7 +541,7 @@ export class Market {
|
|
|
524
541
|
signer,
|
|
525
542
|
farmMint,
|
|
526
543
|
farmTokenProgram,
|
|
527
|
-
farmTokenSrc,
|
|
544
|
+
farmTokenSrc: farmTokenSrcParam,
|
|
528
545
|
}: {
|
|
529
546
|
newRate: bigint
|
|
530
547
|
untilTimestamp: number
|
|
@@ -533,7 +550,7 @@ export class Market {
|
|
|
533
550
|
farmTokenProgram: web3.PublicKey
|
|
534
551
|
farmTokenSrc?: web3.PublicKey
|
|
535
552
|
}) {
|
|
536
|
-
farmTokenSrc =
|
|
553
|
+
const farmTokenSrc = farmTokenSrcParam || getAssociatedTokenAddressSync(farmMint, signer, true, farmTokenProgram)
|
|
537
554
|
|
|
538
555
|
const tokenFarm = getAssociatedTokenAddressSync(farmMint, this.selfAddress, true, farmTokenProgram)
|
|
539
556
|
|
|
@@ -550,6 +567,7 @@ export class Market {
|
|
|
550
567
|
})
|
|
551
568
|
.instruction()
|
|
552
569
|
}
|
|
570
|
+
|
|
553
571
|
/**
|
|
554
572
|
* Redeem LP tokens for PT & SY (liquidity removal)
|
|
555
573
|
*
|
|
@@ -564,9 +582,9 @@ export class Market {
|
|
|
564
582
|
withdrawer,
|
|
565
583
|
minPtOut,
|
|
566
584
|
minSyOut,
|
|
567
|
-
ptDst,
|
|
568
|
-
syDst,
|
|
569
|
-
lpSrc,
|
|
585
|
+
ptDst: ptDstParam,
|
|
586
|
+
syDst: syDstParam,
|
|
587
|
+
lpSrc: lpSrcParam,
|
|
570
588
|
}: {
|
|
571
589
|
lpIn: bigint
|
|
572
590
|
withdrawer: web3.PublicKey
|
|
@@ -576,9 +594,9 @@ export class Market {
|
|
|
576
594
|
syDst?: web3.PublicKey
|
|
577
595
|
lpSrc?: web3.PublicKey
|
|
578
596
|
}) {
|
|
579
|
-
ptDst =
|
|
580
|
-
syDst =
|
|
581
|
-
lpSrc =
|
|
597
|
+
const ptDst = ptDstParam || getAssociatedTokenAddressSync(this.mintPt, withdrawer, true, TOKEN_PROGRAM_ID)
|
|
598
|
+
const syDst = syDstParam || getAssociatedTokenAddressSync(this.mintSy, withdrawer, true, TOKEN_PROGRAM_ID)
|
|
599
|
+
const lpSrc = lpSrcParam || getAssociatedTokenAddressSync(this.mintLp, withdrawer, true, TOKEN_PROGRAM_ID)
|
|
582
600
|
|
|
583
601
|
const ptDstAtaIx = createAssociatedTokenAccountIdempotentInstruction(withdrawer, ptDst, withdrawer, this.mintPt)
|
|
584
602
|
const syDstAtaIx = createAssociatedTokenAccountIdempotentInstruction(withdrawer, syDst, withdrawer, this.mintSy)
|
|
@@ -676,8 +694,8 @@ export class Market {
|
|
|
676
694
|
traderPt,
|
|
677
695
|
syConstraint,
|
|
678
696
|
isBuy,
|
|
679
|
-
tokenPt,
|
|
680
|
-
tokenSy,
|
|
697
|
+
tokenPt: tokenPtParam,
|
|
698
|
+
tokenSy: tokenSyParam,
|
|
681
699
|
}: {
|
|
682
700
|
trader: web3.PublicKey
|
|
683
701
|
traderPt: bigint
|
|
@@ -686,8 +704,8 @@ export class Market {
|
|
|
686
704
|
tokenPt?: web3.PublicKey
|
|
687
705
|
tokenSy?: web3.PublicKey
|
|
688
706
|
}) {
|
|
689
|
-
tokenPt =
|
|
690
|
-
tokenSy =
|
|
707
|
+
const tokenPt = tokenPtParam || getAssociatedTokenAddressSync(this.mintPt, trader, true, TOKEN_PROGRAM_ID)
|
|
708
|
+
const tokenSy = tokenSyParam || getAssociatedTokenAddressSync(this.mintSy, trader, true, TOKEN_PROGRAM_ID)
|
|
691
709
|
|
|
692
710
|
const tokenSyAtaIx = createAssociatedTokenAccountIdempotentInstruction(trader, tokenSy, trader, this.mintSy)
|
|
693
711
|
const tokenPtAtaIx = createAssociatedTokenAccountIdempotentInstruction(trader, tokenPt, trader, this.mintPt)
|
|
@@ -737,9 +755,9 @@ export class Market {
|
|
|
737
755
|
trader,
|
|
738
756
|
ytIn,
|
|
739
757
|
minSyOut,
|
|
740
|
-
ytSrc,
|
|
741
|
-
ptSrc,
|
|
742
|
-
syDst,
|
|
758
|
+
ytSrc: ytSrcParam,
|
|
759
|
+
ptSrc: ptSrcParam,
|
|
760
|
+
syDst: syDstParam,
|
|
743
761
|
}: {
|
|
744
762
|
trader: web3.PublicKey
|
|
745
763
|
ytIn: bigint
|
|
@@ -748,9 +766,9 @@ export class Market {
|
|
|
748
766
|
ptSrc?: web3.PublicKey
|
|
749
767
|
syDst?: web3.PublicKey
|
|
750
768
|
}) {
|
|
751
|
-
syDst =
|
|
752
|
-
ptSrc =
|
|
753
|
-
ytSrc =
|
|
769
|
+
const syDst = syDstParam || getAssociatedTokenAddressSync(this.mintSy, trader, true, TOKEN_PROGRAM_ID)
|
|
770
|
+
const ptSrc = ptSrcParam || getAssociatedTokenAddressSync(this.mintPt, trader, true, TOKEN_PROGRAM_ID)
|
|
771
|
+
const ytSrc = ytSrcParam || getAssociatedTokenAddressSync(this.mintYt, trader, true, TOKEN_PROGRAM_ID)
|
|
754
772
|
|
|
755
773
|
const syDstAtaIxs = createAssociatedTokenAccountIdempotentInstruction(trader, syDst, trader, this.mintSy)
|
|
756
774
|
const ptSrcAtaIxs = createAssociatedTokenAccountIdempotentInstruction(trader, ptSrc, trader, this.mintPt)
|
|
@@ -808,9 +826,9 @@ export class Market {
|
|
|
808
826
|
trader,
|
|
809
827
|
ytOut,
|
|
810
828
|
maxSyIn,
|
|
811
|
-
ytTrader,
|
|
812
|
-
ptTrader,
|
|
813
|
-
syTrader,
|
|
829
|
+
ytTrader: ytTraderParam,
|
|
830
|
+
ptTrader: ptTraderParam,
|
|
831
|
+
syTrader: syTraderParam,
|
|
814
832
|
}: {
|
|
815
833
|
trader: web3.PublicKey
|
|
816
834
|
ytOut: bigint
|
|
@@ -819,9 +837,9 @@ export class Market {
|
|
|
819
837
|
ptTrader?: web3.PublicKey
|
|
820
838
|
syTrader?: web3.PublicKey
|
|
821
839
|
}) {
|
|
822
|
-
syTrader =
|
|
823
|
-
ptTrader =
|
|
824
|
-
ytTrader =
|
|
840
|
+
const syTrader = syTraderParam || getAssociatedTokenAddressSync(this.mintSy, trader, true, TOKEN_PROGRAM_ID)
|
|
841
|
+
const ptTrader = ptTraderParam || getAssociatedTokenAddressSync(this.mintPt, trader, true, TOKEN_PROGRAM_ID)
|
|
842
|
+
const ytTrader = ytTraderParam || getAssociatedTokenAddressSync(this.mintYt, trader, true, TOKEN_PROGRAM_ID)
|
|
825
843
|
|
|
826
844
|
const syTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(trader, syTrader, trader, this.mintSy)
|
|
827
845
|
const ptTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(trader, ptTrader, trader, this.mintPt)
|
|
@@ -889,8 +907,16 @@ export class Market {
|
|
|
889
907
|
}
|
|
890
908
|
|
|
891
909
|
/** Deposit LP tokens into the farming module to earn rewards */
|
|
892
|
-
async ixDepositLp({
|
|
893
|
-
|
|
910
|
+
async ixDepositLp({
|
|
911
|
+
owner,
|
|
912
|
+
amount,
|
|
913
|
+
lpSrc: lpSrcParam,
|
|
914
|
+
}: {
|
|
915
|
+
owner: web3.PublicKey
|
|
916
|
+
amount: bigint
|
|
917
|
+
lpSrc?: web3.PublicKey
|
|
918
|
+
}) {
|
|
919
|
+
const lpSrc = lpSrcParam || getAssociatedTokenAddressSync(this.mintLp, owner, true, TOKEN_PROGRAM_ID)
|
|
894
920
|
const lpPosition = this.xponPda.marketLpPosition({ market: this.selfAddress, owner })
|
|
895
921
|
|
|
896
922
|
return this.coreProgram.methods
|
|
@@ -914,8 +940,16 @@ export class Market {
|
|
|
914
940
|
}
|
|
915
941
|
|
|
916
942
|
/** Withdraw LP tokens from the farming module */
|
|
917
|
-
async ixWithdrawLp({
|
|
918
|
-
|
|
943
|
+
async ixWithdrawLp({
|
|
944
|
+
owner,
|
|
945
|
+
amount,
|
|
946
|
+
lpDst: lpDstParam,
|
|
947
|
+
}: {
|
|
948
|
+
owner: web3.PublicKey
|
|
949
|
+
amount: bigint
|
|
950
|
+
lpDst?: web3.PublicKey
|
|
951
|
+
}) {
|
|
952
|
+
const lpDst = lpDstParam || getAssociatedTokenAddressSync(this.mintLp, owner, true, TOKEN_PROGRAM_ID)
|
|
919
953
|
|
|
920
954
|
const lpPosition = this.xponPda.marketLpPosition({ market: this.selfAddress, owner })
|
|
921
955
|
return this.coreProgram.methods
|
|
@@ -938,9 +972,15 @@ export class Market {
|
|
|
938
972
|
.instruction()
|
|
939
973
|
}
|
|
940
974
|
|
|
941
|
-
async ixWrapperCollectInterest({
|
|
975
|
+
async ixWrapperCollectInterest({
|
|
976
|
+
claimer,
|
|
977
|
+
tokenSyDst: tokenSyDstParam,
|
|
978
|
+
}: {
|
|
979
|
+
claimer: web3.PublicKey
|
|
980
|
+
tokenSyDst?: web3.PublicKey
|
|
981
|
+
}) {
|
|
942
982
|
const yieldPosition = this.xponPda.yieldPosition({ owner: claimer, vault: this.vault.selfAddress })
|
|
943
|
-
tokenSyDst =
|
|
983
|
+
const tokenSyDst = tokenSyDstParam || getAssociatedTokenAddressSync(this.mintSy, claimer, true, TOKEN_PROGRAM_ID)
|
|
944
984
|
const tokenBaseClaimer = getAssociatedTokenAddressSync(
|
|
945
985
|
this.flavor.mintBase,
|
|
946
986
|
claimer,
|
|
@@ -1007,9 +1047,9 @@ export class Market {
|
|
|
1007
1047
|
owner,
|
|
1008
1048
|
ptOut,
|
|
1009
1049
|
maxBaseIn,
|
|
1010
|
-
tokenSyTrader,
|
|
1011
|
-
tokenPtTrader,
|
|
1012
|
-
tokenBaseTrader,
|
|
1050
|
+
tokenSyTrader: tokenSyTraderParam,
|
|
1051
|
+
tokenPtTrader: tokenPtTraderParam,
|
|
1052
|
+
tokenBaseTrader: tokenBaseTraderParam,
|
|
1013
1053
|
}: {
|
|
1014
1054
|
owner: web3.PublicKey
|
|
1015
1055
|
ptOut: bigint
|
|
@@ -1018,10 +1058,13 @@ export class Market {
|
|
|
1018
1058
|
tokenPtTrader?: web3.PublicKey
|
|
1019
1059
|
tokenBaseTrader?: web3.PublicKey
|
|
1020
1060
|
}) {
|
|
1021
|
-
tokenSyTrader =
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1061
|
+
const tokenSyTrader =
|
|
1062
|
+
tokenSyTraderParam || getAssociatedTokenAddressSync(this.mintSy, owner, true, TOKEN_PROGRAM_ID)
|
|
1063
|
+
const tokenPtTrader =
|
|
1064
|
+
tokenPtTraderParam || getAssociatedTokenAddressSync(this.mintPt, owner, true, TOKEN_PROGRAM_ID)
|
|
1065
|
+
const tokenBaseTrader =
|
|
1066
|
+
tokenBaseTraderParam ||
|
|
1067
|
+
getAssociatedTokenAddressSync(this.flavor.mintBase, owner, true, this.flavor.baseTokenProgram)
|
|
1025
1068
|
|
|
1026
1069
|
const tokenSyTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1027
1070
|
owner,
|
|
@@ -1051,7 +1094,7 @@ export class Market {
|
|
|
1051
1094
|
|
|
1052
1095
|
const mintSyRemAccounts = mintSyIx.keys
|
|
1053
1096
|
|
|
1054
|
-
console.log("mintSyRemAccounts", mintSyRemAccounts)
|
|
1097
|
+
// console.log("mintSyRemAccounts", mintSyRemAccounts)
|
|
1055
1098
|
|
|
1056
1099
|
mintSyRemAccounts.push({
|
|
1057
1100
|
pubkey: this.coreProgram.programId,
|
|
@@ -1094,9 +1137,9 @@ export class Market {
|
|
|
1094
1137
|
owner,
|
|
1095
1138
|
amount,
|
|
1096
1139
|
minBaseOut,
|
|
1097
|
-
tokenSyTrader,
|
|
1098
|
-
tokenPtTrader,
|
|
1099
|
-
tokenBaseTrader,
|
|
1140
|
+
tokenSyTrader: tokenSyTraderParam,
|
|
1141
|
+
tokenPtTrader: tokenPtTraderParam,
|
|
1142
|
+
tokenBaseTrader: tokenBaseTraderParam,
|
|
1100
1143
|
}: {
|
|
1101
1144
|
owner: web3.PublicKey
|
|
1102
1145
|
amount: bigint
|
|
@@ -1105,10 +1148,13 @@ export class Market {
|
|
|
1105
1148
|
tokenPtTrader?: web3.PublicKey
|
|
1106
1149
|
tokenBaseTrader?: web3.PublicKey
|
|
1107
1150
|
}) {
|
|
1108
|
-
tokenSyTrader =
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1151
|
+
const tokenSyTrader =
|
|
1152
|
+
tokenSyTraderParam || getAssociatedTokenAddressSync(this.mintSy, owner, true, TOKEN_PROGRAM_ID)
|
|
1153
|
+
const tokenPtTrader =
|
|
1154
|
+
tokenPtTraderParam || getAssociatedTokenAddressSync(this.mintPt, owner, true, TOKEN_PROGRAM_ID)
|
|
1155
|
+
const tokenBaseTrader =
|
|
1156
|
+
tokenBaseTraderParam ||
|
|
1157
|
+
getAssociatedTokenAddressSync(this.flavor.mintBase, owner, true, this.flavor.baseTokenProgram)
|
|
1112
1158
|
|
|
1113
1159
|
const tokenSyTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1114
1160
|
owner,
|
|
@@ -1169,10 +1215,10 @@ export class Market {
|
|
|
1169
1215
|
owner,
|
|
1170
1216
|
ytOut,
|
|
1171
1217
|
maxBaseIn,
|
|
1172
|
-
tokenSyTrader,
|
|
1173
|
-
tokenPtTrader,
|
|
1174
|
-
tokenYtTrader,
|
|
1175
|
-
tokenBaseTrader,
|
|
1218
|
+
tokenSyTrader: tokenSyTraderParam,
|
|
1219
|
+
tokenPtTrader: tokenPtTraderParam,
|
|
1220
|
+
tokenYtTrader: tokenYtTraderParam,
|
|
1221
|
+
tokenBaseTrader: tokenBaseTraderParam,
|
|
1176
1222
|
}: {
|
|
1177
1223
|
owner: web3.PublicKey
|
|
1178
1224
|
ytOut: bigint
|
|
@@ -1182,11 +1228,15 @@ export class Market {
|
|
|
1182
1228
|
tokenYtTrader?: web3.PublicKey
|
|
1183
1229
|
tokenBaseTrader?: web3.PublicKey
|
|
1184
1230
|
}) {
|
|
1185
|
-
tokenSyTrader =
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1231
|
+
const tokenSyTrader =
|
|
1232
|
+
tokenSyTraderParam || getAssociatedTokenAddressSync(this.mintSy, owner, true, TOKEN_PROGRAM_ID)
|
|
1233
|
+
const tokenPtTrader =
|
|
1234
|
+
tokenPtTraderParam || getAssociatedTokenAddressSync(this.mintPt, owner, true, TOKEN_PROGRAM_ID)
|
|
1235
|
+
const tokenYtTrader =
|
|
1236
|
+
tokenYtTraderParam || getAssociatedTokenAddressSync(this.mintYt, owner, true, TOKEN_PROGRAM_ID)
|
|
1237
|
+
const tokenBaseTrader =
|
|
1238
|
+
tokenBaseTraderParam ||
|
|
1239
|
+
getAssociatedTokenAddressSync(this.flavor.mintBase, owner, true, this.flavor.baseTokenProgram)
|
|
1190
1240
|
|
|
1191
1241
|
const tokenSyTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1192
1242
|
owner,
|
|
@@ -1227,7 +1277,7 @@ export class Market {
|
|
|
1227
1277
|
const allRemainingAccounts = mintSyRemAccounts.concat(remainingAccounts)
|
|
1228
1278
|
|
|
1229
1279
|
const ix1 = await this.coreProgram.methods
|
|
1230
|
-
.wrapperBuyYt(new BN(ytOut.toString()), new BN(maxBaseIn.toString()), mintSyRemAccounts.length
|
|
1280
|
+
.wrapperBuyYt(new BN(ytOut.toString()), new BN(maxBaseIn.toString()), mintSyRemAccounts.length)
|
|
1231
1281
|
.accountsStrict({
|
|
1232
1282
|
buyer: owner,
|
|
1233
1283
|
market: this.selfAddress,
|
|
@@ -1290,10 +1340,10 @@ export class Market {
|
|
|
1290
1340
|
owner,
|
|
1291
1341
|
amount,
|
|
1292
1342
|
minBaseOut,
|
|
1293
|
-
tokenBaseTrader,
|
|
1294
|
-
tokenSyTrader,
|
|
1295
|
-
tokenYtTrader,
|
|
1296
|
-
tokenPtTrader,
|
|
1343
|
+
tokenBaseTrader: tokenBaseTraderParam,
|
|
1344
|
+
tokenSyTrader: tokenSyTraderParam,
|
|
1345
|
+
tokenYtTrader: tokenYtTraderParam,
|
|
1346
|
+
tokenPtTrader: tokenPtTraderParam,
|
|
1297
1347
|
}: {
|
|
1298
1348
|
owner: web3.PublicKey
|
|
1299
1349
|
amount: bigint
|
|
@@ -1303,11 +1353,15 @@ export class Market {
|
|
|
1303
1353
|
tokenYtTrader?: web3.PublicKey
|
|
1304
1354
|
tokenPtTrader?: web3.PublicKey
|
|
1305
1355
|
}) {
|
|
1306
|
-
tokenBaseTrader =
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1356
|
+
const tokenBaseTrader =
|
|
1357
|
+
tokenBaseTraderParam ||
|
|
1358
|
+
getAssociatedTokenAddressSync(this.flavor.mintBase, owner, true, this.flavor.baseTokenProgram)
|
|
1359
|
+
const tokenSyTrader =
|
|
1360
|
+
tokenSyTraderParam || getAssociatedTokenAddressSync(this.mintSy, owner, true, TOKEN_PROGRAM_ID)
|
|
1361
|
+
const tokenYtTrader =
|
|
1362
|
+
tokenYtTraderParam || getAssociatedTokenAddressSync(this.mintYt, owner, true, TOKEN_PROGRAM_ID)
|
|
1363
|
+
const tokenPtTrader =
|
|
1364
|
+
tokenPtTraderParam || getAssociatedTokenAddressSync(this.mintPt, owner, true, TOKEN_PROGRAM_ID)
|
|
1311
1365
|
|
|
1312
1366
|
const tokenSyTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1313
1367
|
owner,
|
|
@@ -1349,7 +1403,7 @@ export class Market {
|
|
|
1349
1403
|
...this.vault.cpiAccounts.depositSy,
|
|
1350
1404
|
])
|
|
1351
1405
|
|
|
1352
|
-
console.log(" remaining accounts length", remainingAccounts.length)
|
|
1406
|
+
// console.log(" remaining accounts length", remainingAccounts.length)
|
|
1353
1407
|
|
|
1354
1408
|
// remainingAccounts.push({
|
|
1355
1409
|
// pubkey: this.coreProgram.programId,
|
|
@@ -1462,11 +1516,11 @@ export class Market {
|
|
|
1462
1516
|
depositor,
|
|
1463
1517
|
amountBase,
|
|
1464
1518
|
minLpOut,
|
|
1465
|
-
tokenSyDepositor,
|
|
1466
|
-
tokenYtDepositor,
|
|
1467
|
-
tokenPtDepositor,
|
|
1468
|
-
tokenBaseDepositor,
|
|
1469
|
-
tokenLpDepositor,
|
|
1519
|
+
tokenSyDepositor: tokenSyDepositorParam,
|
|
1520
|
+
tokenYtDepositor: tokenYtDepositorParam,
|
|
1521
|
+
tokenPtDepositor: tokenPtDepositorParam,
|
|
1522
|
+
tokenBaseDepositor: tokenBaseDepositorParam,
|
|
1523
|
+
tokenLpDepositor: tokenLpDepositorParam,
|
|
1470
1524
|
}: {
|
|
1471
1525
|
depositor: web3.PublicKey
|
|
1472
1526
|
amountBase: bigint
|
|
@@ -1477,13 +1531,17 @@ export class Market {
|
|
|
1477
1531
|
tokenBaseDepositor?: web3.PublicKey
|
|
1478
1532
|
tokenLpDepositor?: web3.PublicKey
|
|
1479
1533
|
}) {
|
|
1480
|
-
tokenSyDepositor =
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1534
|
+
const tokenSyDepositor =
|
|
1535
|
+
tokenSyDepositorParam || getAssociatedTokenAddressSync(this.mintSy, depositor, true, TOKEN_PROGRAM_ID)
|
|
1536
|
+
const tokenYtDepositor =
|
|
1537
|
+
tokenYtDepositorParam || getAssociatedTokenAddressSync(this.mintYt, depositor, true, TOKEN_PROGRAM_ID)
|
|
1538
|
+
const tokenPtDepositor =
|
|
1539
|
+
tokenPtDepositorParam || getAssociatedTokenAddressSync(this.mintPt, depositor, true, TOKEN_PROGRAM_ID)
|
|
1540
|
+
const tokenBaseDepositor =
|
|
1541
|
+
tokenBaseDepositorParam ||
|
|
1485
1542
|
getAssociatedTokenAddressSync(this.flavor.mintBase, depositor, true, this.flavor.baseTokenProgram)
|
|
1486
|
-
tokenLpDepositor =
|
|
1543
|
+
const tokenLpDepositor =
|
|
1544
|
+
tokenLpDepositorParam || getAssociatedTokenAddressSync(this.mintLp, depositor, true, TOKEN_PROGRAM_ID)
|
|
1487
1545
|
|
|
1488
1546
|
const tokenSyDepositorAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1489
1547
|
depositor,
|
|
@@ -1525,9 +1583,9 @@ export class Market {
|
|
|
1525
1583
|
|
|
1526
1584
|
const mintSyRemAccounts = mintSyIx.keys
|
|
1527
1585
|
|
|
1528
|
-
// Log vault accounts before filtering
|
|
1529
1586
|
const beforeVaultAccounts = [...this.vault.cpiAccounts.getSyState, ...this.vault.cpiAccounts.depositSy]
|
|
1530
|
-
|
|
1587
|
+
// Log vault accounts before filtering
|
|
1588
|
+
// console.log("Before filtering vault accounts:", beforeVaultAccounts.length, "accounts")
|
|
1531
1589
|
|
|
1532
1590
|
// Filter out TOKEN_PROGRAM_ID and SystemProgram.programId from vault remaining accounts
|
|
1533
1591
|
const vaultCpiRemainingAccounts = beforeVaultAccounts.filter(
|
|
@@ -1535,11 +1593,11 @@ export class Market {
|
|
|
1535
1593
|
)
|
|
1536
1594
|
|
|
1537
1595
|
// Log vault accounts after filtering
|
|
1538
|
-
console.log("After filtering vault accounts:", vaultCpiRemainingAccounts.length, "accounts")
|
|
1596
|
+
// console.log("After filtering vault accounts:", vaultCpiRemainingAccounts.length, "accounts")
|
|
1539
1597
|
|
|
1540
1598
|
// Log market accounts before filtering
|
|
1541
1599
|
const beforeMarketAccounts = [...this.cpiAccounts.depositSy]
|
|
1542
|
-
console.log("Before filtering market accounts:", beforeMarketAccounts.length, "accounts")
|
|
1600
|
+
// console.log("Before filtering market accounts:", beforeMarketAccounts.length, "accounts")
|
|
1543
1601
|
|
|
1544
1602
|
// Filter out TOKEN_PROGRAM_ID and SystemProgram.programId from market remaining accounts
|
|
1545
1603
|
const marketCpiRemainingAccounts = beforeMarketAccounts.filter(
|
|
@@ -1550,7 +1608,7 @@ export class Market {
|
|
|
1550
1608
|
)
|
|
1551
1609
|
|
|
1552
1610
|
// Log market accounts after filtering
|
|
1553
|
-
console.log("After filtering market accounts:", marketCpiRemainingAccounts.length, "accounts")
|
|
1611
|
+
// console.log("After filtering market accounts:", marketCpiRemainingAccounts.length, "accounts")
|
|
1554
1612
|
|
|
1555
1613
|
// These remaining accounts can be in any order, and they can be unique-ified
|
|
1556
1614
|
const unorderedRemainingAccounts = uniqueRemainingAccounts([
|
|
@@ -1563,7 +1621,7 @@ export class Market {
|
|
|
1563
1621
|
const lpPosition = this.xponPda.marketLpPosition({ market: this.selfAddress, owner: depositor })
|
|
1564
1622
|
|
|
1565
1623
|
const ix = await this.coreProgram.methods
|
|
1566
|
-
.wrapperProvideLiquidity(amountBaseBn, minLpOutBn, mintSyRemAccounts.length
|
|
1624
|
+
.wrapperProvideLiquidity(amountBaseBn, minLpOutBn, mintSyRemAccounts.length)
|
|
1567
1625
|
.accountsStrict({
|
|
1568
1626
|
depositor,
|
|
1569
1627
|
authority: this.vault.authority,
|
|
@@ -1595,7 +1653,7 @@ export class Market {
|
|
|
1595
1653
|
.remainingAccounts(allRemainingAccounts)
|
|
1596
1654
|
.instruction()
|
|
1597
1655
|
|
|
1598
|
-
console.log("ix accounts length", ix.keys.length)
|
|
1656
|
+
// console.log("ix accounts length", ix.keys.length)
|
|
1599
1657
|
|
|
1600
1658
|
return {
|
|
1601
1659
|
ixs: [
|
|
@@ -1613,11 +1671,11 @@ export class Market {
|
|
|
1613
1671
|
minLpOut,
|
|
1614
1672
|
externalPtToBuy,
|
|
1615
1673
|
externalSyConstraint,
|
|
1616
|
-
tokenSyDepositor,
|
|
1617
|
-
tokenYtDepositor,
|
|
1618
|
-
tokenPtDepositor,
|
|
1619
|
-
tokenBaseDepositor,
|
|
1620
|
-
tokenLpDepositor,
|
|
1674
|
+
tokenSyDepositor: tokenSyDepositorParam,
|
|
1675
|
+
tokenYtDepositor: tokenYtDepositorParam,
|
|
1676
|
+
tokenPtDepositor: tokenPtDepositorParam,
|
|
1677
|
+
tokenBaseDepositor: tokenBaseDepositorParam,
|
|
1678
|
+
tokenLpDepositor: tokenLpDepositorParam,
|
|
1621
1679
|
}: {
|
|
1622
1680
|
depositor: web3.PublicKey
|
|
1623
1681
|
amountBase: bigint
|
|
@@ -1630,13 +1688,17 @@ export class Market {
|
|
|
1630
1688
|
tokenBaseDepositor?: web3.PublicKey
|
|
1631
1689
|
tokenLpDepositor?: web3.PublicKey
|
|
1632
1690
|
}) {
|
|
1633
|
-
tokenSyDepositor =
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1691
|
+
const tokenSyDepositor =
|
|
1692
|
+
tokenSyDepositorParam || getAssociatedTokenAddressSync(this.mintSy, depositor, true, TOKEN_PROGRAM_ID)
|
|
1693
|
+
const tokenYtDepositor =
|
|
1694
|
+
tokenYtDepositorParam || getAssociatedTokenAddressSync(this.mintYt, depositor, true, TOKEN_PROGRAM_ID)
|
|
1695
|
+
const tokenPtDepositor =
|
|
1696
|
+
tokenPtDepositorParam || getAssociatedTokenAddressSync(this.mintPt, depositor, true, TOKEN_PROGRAM_ID)
|
|
1697
|
+
const tokenBaseDepositor =
|
|
1698
|
+
tokenBaseDepositorParam ||
|
|
1638
1699
|
getAssociatedTokenAddressSync(this.flavor.mintBase, depositor, true, this.flavor.baseTokenProgram)
|
|
1639
|
-
tokenLpDepositor =
|
|
1700
|
+
const tokenLpDepositor =
|
|
1701
|
+
tokenLpDepositorParam || getAssociatedTokenAddressSync(this.mintLp, depositor, true, TOKEN_PROGRAM_ID)
|
|
1640
1702
|
|
|
1641
1703
|
const tokenSyAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1642
1704
|
depositor,
|
|
@@ -1690,7 +1752,7 @@ export class Market {
|
|
|
1690
1752
|
.wrapperProvideLiquidityBase(
|
|
1691
1753
|
new BN(amountBase.toString()),
|
|
1692
1754
|
new BN(minLpOut.toString()),
|
|
1693
|
-
mintSyRemAccounts.length
|
|
1755
|
+
mintSyRemAccounts.length,
|
|
1694
1756
|
new BN(externalPtToBuy.toString()),
|
|
1695
1757
|
new BN(externalSyConstraint.toString()),
|
|
1696
1758
|
)
|
|
@@ -1731,11 +1793,11 @@ export class Market {
|
|
|
1731
1793
|
amountBase,
|
|
1732
1794
|
amountPt,
|
|
1733
1795
|
minLpOut,
|
|
1734
|
-
tokenSyDepositor,
|
|
1735
|
-
tokenYtDepositor,
|
|
1736
|
-
tokenPtDepositor,
|
|
1737
|
-
tokenBaseDepositor,
|
|
1738
|
-
tokenLpDepositor,
|
|
1796
|
+
tokenSyDepositor: tokenSyDepositorParam,
|
|
1797
|
+
tokenYtDepositor: tokenYtDepositorParam,
|
|
1798
|
+
tokenPtDepositor: tokenPtDepositorParam,
|
|
1799
|
+
tokenBaseDepositor: tokenBaseDepositorParam,
|
|
1800
|
+
tokenLpDepositor: tokenLpDepositorParam,
|
|
1739
1801
|
}: {
|
|
1740
1802
|
depositor: web3.PublicKey
|
|
1741
1803
|
amountBase: bigint
|
|
@@ -1747,13 +1809,17 @@ export class Market {
|
|
|
1747
1809
|
tokenBaseDepositor?: web3.PublicKey
|
|
1748
1810
|
tokenLpDepositor?: web3.PublicKey
|
|
1749
1811
|
}) {
|
|
1750
|
-
tokenSyDepositor =
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1812
|
+
const tokenSyDepositor =
|
|
1813
|
+
tokenSyDepositorParam || getAssociatedTokenAddressSync(this.mintSy, depositor, true, TOKEN_PROGRAM_ID)
|
|
1814
|
+
const tokenYtDepositor =
|
|
1815
|
+
tokenYtDepositorParam || getAssociatedTokenAddressSync(this.mintYt, depositor, true, TOKEN_PROGRAM_ID)
|
|
1816
|
+
const tokenPtDepositor =
|
|
1817
|
+
tokenPtDepositorParam || getAssociatedTokenAddressSync(this.mintPt, depositor, true, TOKEN_PROGRAM_ID)
|
|
1818
|
+
const tokenBaseDepositor =
|
|
1819
|
+
tokenBaseDepositorParam ||
|
|
1755
1820
|
getAssociatedTokenAddressSync(this.flavor.mintBase, depositor, true, this.flavor.baseTokenProgram)
|
|
1756
|
-
tokenLpDepositor =
|
|
1821
|
+
const tokenLpDepositor =
|
|
1822
|
+
tokenLpDepositorParam || getAssociatedTokenAddressSync(this.mintLp, depositor, true, TOKEN_PROGRAM_ID)
|
|
1757
1823
|
|
|
1758
1824
|
const tokenSyAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1759
1825
|
depositor,
|
|
@@ -1804,7 +1870,7 @@ export class Market {
|
|
|
1804
1870
|
new BN(amountBase.toString()),
|
|
1805
1871
|
new BN(amountPt.toString()),
|
|
1806
1872
|
new BN(minLpOut.toString()),
|
|
1807
|
-
mintSyRemAccounts.length
|
|
1873
|
+
mintSyRemAccounts.length,
|
|
1808
1874
|
)
|
|
1809
1875
|
.accountsStrict({
|
|
1810
1876
|
depositor,
|
|
@@ -1841,11 +1907,11 @@ export class Market {
|
|
|
1841
1907
|
owner,
|
|
1842
1908
|
amountLp,
|
|
1843
1909
|
minBaseOut,
|
|
1844
|
-
tokenSyWithdrawer,
|
|
1845
|
-
tokenYtWithdrawer,
|
|
1846
|
-
tokenPtWithdrawer,
|
|
1847
|
-
tokenBaseWithdrawer,
|
|
1848
|
-
tokenLpWithdrawer,
|
|
1910
|
+
tokenSyWithdrawer: tokenSyWithdrawerParam,
|
|
1911
|
+
tokenYtWithdrawer: tokenYtWithdrawerParam,
|
|
1912
|
+
tokenPtWithdrawer: tokenPtWithdrawerParam,
|
|
1913
|
+
tokenBaseWithdrawer: tokenBaseWithdrawerParam,
|
|
1914
|
+
tokenLpWithdrawer: tokenLpWithdrawerParam,
|
|
1849
1915
|
}: {
|
|
1850
1916
|
owner: web3.PublicKey
|
|
1851
1917
|
amountLp: bigint
|
|
@@ -1856,13 +1922,17 @@ export class Market {
|
|
|
1856
1922
|
tokenBaseWithdrawer?: web3.PublicKey
|
|
1857
1923
|
tokenLpWithdrawer?: web3.PublicKey
|
|
1858
1924
|
}) {
|
|
1859
|
-
tokenSyWithdrawer =
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1925
|
+
const tokenSyWithdrawer =
|
|
1926
|
+
tokenSyWithdrawerParam || getAssociatedTokenAddressSync(this.mintSy, owner, true, TOKEN_PROGRAM_ID)
|
|
1927
|
+
const tokenYtWithdrawer =
|
|
1928
|
+
tokenYtWithdrawerParam || getAssociatedTokenAddressSync(this.mintYt, owner, true, TOKEN_PROGRAM_ID)
|
|
1929
|
+
const tokenPtWithdrawer =
|
|
1930
|
+
tokenPtWithdrawerParam || getAssociatedTokenAddressSync(this.mintPt, owner, true, TOKEN_PROGRAM_ID)
|
|
1931
|
+
const tokenBaseWithdrawer =
|
|
1932
|
+
tokenBaseWithdrawerParam ||
|
|
1864
1933
|
getAssociatedTokenAddressSync(this.flavor.mintBase, owner, true, this.flavor.baseTokenProgram)
|
|
1865
|
-
tokenLpWithdrawer =
|
|
1934
|
+
const tokenLpWithdrawer =
|
|
1935
|
+
tokenLpWithdrawerParam || getAssociatedTokenAddressSync(this.mintLp, owner, true, TOKEN_PROGRAM_ID)
|
|
1866
1936
|
|
|
1867
1937
|
const tokenSyAtaIx = createAssociatedTokenAccountIdempotentInstruction(owner, tokenSyWithdrawer, owner, this.mintSy)
|
|
1868
1938
|
const tokenYtAtaIx = createAssociatedTokenAccountIdempotentInstruction(owner, tokenYtWithdrawer, owner, this.mintYt)
|
|
@@ -1931,29 +2001,31 @@ export class Market {
|
|
|
1931
2001
|
async ixWithdrawLiquidityClassic({
|
|
1932
2002
|
owner,
|
|
1933
2003
|
amountLp,
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
tokenLpWithdrawer,
|
|
2004
|
+
tokenSyWithdrawer: tokenSyWithdrawerParam,
|
|
2005
|
+
tokenYtWithdrawer: tokenYtWithdrawerParam,
|
|
2006
|
+
tokenPtWithdrawer: tokenPtWithdrawerParam,
|
|
2007
|
+
tokenBaseWithdrawer: tokenBaseWithdrawerParam,
|
|
2008
|
+
tokenLpWithdrawer: tokenLpWithdrawerParam,
|
|
1940
2009
|
}: {
|
|
1941
2010
|
owner: web3.PublicKey
|
|
1942
2011
|
amountLp: bigint
|
|
1943
|
-
minBaseOut: bigint
|
|
1944
2012
|
tokenSyWithdrawer?: web3.PublicKey
|
|
1945
2013
|
tokenYtWithdrawer?: web3.PublicKey
|
|
1946
2014
|
tokenPtWithdrawer?: web3.PublicKey
|
|
1947
2015
|
tokenBaseWithdrawer?: web3.PublicKey
|
|
1948
2016
|
tokenLpWithdrawer?: web3.PublicKey
|
|
1949
2017
|
}) {
|
|
1950
|
-
tokenSyWithdrawer =
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
2018
|
+
const tokenSyWithdrawer =
|
|
2019
|
+
tokenSyWithdrawerParam || getAssociatedTokenAddressSync(this.mintSy, owner, true, TOKEN_PROGRAM_ID)
|
|
2020
|
+
const tokenYtWithdrawer =
|
|
2021
|
+
tokenYtWithdrawerParam || getAssociatedTokenAddressSync(this.mintYt, owner, true, TOKEN_PROGRAM_ID)
|
|
2022
|
+
const tokenPtWithdrawer =
|
|
2023
|
+
tokenPtWithdrawerParam || getAssociatedTokenAddressSync(this.mintPt, owner, true, TOKEN_PROGRAM_ID)
|
|
2024
|
+
const tokenBaseWithdrawer =
|
|
2025
|
+
tokenBaseWithdrawerParam ||
|
|
1955
2026
|
getAssociatedTokenAddressSync(this.flavor.mintBase, owner, true, this.flavor.baseTokenProgram)
|
|
1956
|
-
tokenLpWithdrawer =
|
|
2027
|
+
const tokenLpWithdrawer =
|
|
2028
|
+
tokenLpWithdrawerParam || getAssociatedTokenAddressSync(this.mintLp, owner, true, TOKEN_PROGRAM_ID)
|
|
1957
2029
|
|
|
1958
2030
|
const tokenSyAtaIx = createAssociatedTokenAccountIdempotentInstruction(owner, tokenSyWithdrawer, owner, this.mintSy)
|
|
1959
2031
|
const tokenYtAtaIx = createAssociatedTokenAccountIdempotentInstruction(owner, tokenYtWithdrawer, owner, this.mintYt)
|
|
@@ -2016,14 +2088,14 @@ export class Market {
|
|
|
2016
2088
|
owner,
|
|
2017
2089
|
mint,
|
|
2018
2090
|
tokenProgram,
|
|
2019
|
-
tokenDst,
|
|
2091
|
+
tokenDst: tokenDstParam,
|
|
2020
2092
|
}: {
|
|
2021
2093
|
owner: web3.PublicKey
|
|
2022
2094
|
mint: web3.PublicKey
|
|
2023
2095
|
tokenProgram: web3.PublicKey
|
|
2024
2096
|
tokenDst?: web3.PublicKey
|
|
2025
2097
|
}) {
|
|
2026
|
-
tokenDst =
|
|
2098
|
+
const tokenDst = tokenDstParam || getAssociatedTokenAddressSync(mint, owner, true, tokenProgram)
|
|
2027
2099
|
const lpPosition = this.xponPda.marketLpPosition({ market: this.selfAddress, owner })
|
|
2028
2100
|
|
|
2029
2101
|
const tokenFarm = getAssociatedTokenAddressSync(mint, this.selfAddress, true, tokenProgram)
|
|
@@ -2092,8 +2164,8 @@ export class Market {
|
|
|
2092
2164
|
farmRewardTokenProgram,
|
|
2093
2165
|
emissionsRate,
|
|
2094
2166
|
untilTimestamp,
|
|
2095
|
-
farmRewardSrc,
|
|
2096
|
-
feePayer,
|
|
2167
|
+
farmRewardSrc: farmRewardSrcParam,
|
|
2168
|
+
feePayer: feePayerParam,
|
|
2097
2169
|
}: {
|
|
2098
2170
|
signer: web3.PublicKey
|
|
2099
2171
|
farmMint: web3.PublicKey
|
|
@@ -2103,9 +2175,10 @@ export class Market {
|
|
|
2103
2175
|
farmRewardSrc?: web3.PublicKey
|
|
2104
2176
|
feePayer?: web3.PublicKey
|
|
2105
2177
|
}) {
|
|
2106
|
-
feePayer =
|
|
2178
|
+
const feePayer = feePayerParam || signer
|
|
2107
2179
|
const farmRewardEscrow = getAssociatedTokenAddressSync(farmMint, this.selfAddress, true, farmRewardTokenProgram)
|
|
2108
|
-
farmRewardSrc =
|
|
2180
|
+
const farmRewardSrc =
|
|
2181
|
+
farmRewardSrcParam || getAssociatedTokenAddressSync(farmMint, signer, true, farmRewardTokenProgram)
|
|
2109
2182
|
|
|
2110
2183
|
const ix = await this.coreProgram.methods
|
|
2111
2184
|
.addFarm(new BN(emissionsRate), untilTimestamp)
|
|
@@ -2128,22 +2201,22 @@ export class Market {
|
|
|
2128
2201
|
}
|
|
2129
2202
|
|
|
2130
2203
|
/** Calculate available liquidity for PT trades
|
|
2131
|
-
* @param
|
|
2132
|
-
* @param
|
|
2204
|
+
* @param isBuy - true if buying PT with SY, false if selling PT for SY
|
|
2205
|
+
* @param sizePt - amount of PT to trade (in PT decimals)
|
|
2133
2206
|
* @returns amount of PT that can be traded given current liquidity
|
|
2134
2207
|
*/
|
|
2135
|
-
liquidityAvailable(
|
|
2208
|
+
liquidityAvailable(isBuy: boolean, sizePt: bigint): bigint {
|
|
2136
2209
|
const calc = this.marketCalculator()
|
|
2137
2210
|
|
|
2138
|
-
if (
|
|
2211
|
+
if (isBuy) {
|
|
2139
2212
|
// When buying PT, we're limited by SY liquidity
|
|
2140
2213
|
// Convert PT amount to SY using current price
|
|
2141
|
-
const syNeeded = Number(
|
|
2214
|
+
const syNeeded = Number(sizePt) * calc.exchangeRate
|
|
2142
2215
|
// Available SY in the market
|
|
2143
2216
|
const syAvailable = Number(this.syBalance)
|
|
2144
2217
|
|
|
2145
2218
|
if (syNeeded <= syAvailable) {
|
|
2146
|
-
return
|
|
2219
|
+
return sizePt
|
|
2147
2220
|
} else {
|
|
2148
2221
|
// Calculate max PT that can be bought with available SY
|
|
2149
2222
|
return BigInt(Math.floor(syAvailable / calc.exchangeRate))
|
|
@@ -2152,8 +2225,8 @@ export class Market {
|
|
|
2152
2225
|
// When selling PT, we're limited by PT liquidity in the market
|
|
2153
2226
|
const ptAvailable = this.ptBalance
|
|
2154
2227
|
|
|
2155
|
-
if (
|
|
2156
|
-
return
|
|
2228
|
+
if (sizePt <= ptAvailable) {
|
|
2229
|
+
return sizePt
|
|
2157
2230
|
} else {
|
|
2158
2231
|
return ptAvailable
|
|
2159
2232
|
}
|
|
@@ -2244,6 +2317,8 @@ export type MarketJson = {
|
|
|
2244
2317
|
/** Annualized yield for PT */
|
|
2245
2318
|
ptApr: number
|
|
2246
2319
|
|
|
2320
|
+
ptApy: number
|
|
2321
|
+
|
|
2247
2322
|
/** Emission tokens */
|
|
2248
2323
|
emissions: {
|
|
2249
2324
|
trackers: {
|
|
@@ -2274,58 +2349,61 @@ export type MarketJson = {
|
|
|
2274
2349
|
}
|
|
2275
2350
|
}
|
|
2276
2351
|
|
|
2277
|
-
function toJson(
|
|
2278
|
-
const
|
|
2352
|
+
function toJson(market: Market): MarketJson {
|
|
2353
|
+
const marketCalculator = market.marketCalculator()
|
|
2279
2354
|
return {
|
|
2280
|
-
treasurySyTokenAccount:
|
|
2281
|
-
address:
|
|
2282
|
-
addressLookupTable:
|
|
2283
|
-
tokenSyEscrow:
|
|
2284
|
-
tokenPtEscrow:
|
|
2285
|
-
tokenLpEscrow:
|
|
2286
|
-
lpSupply:
|
|
2287
|
-
syProgram:
|
|
2288
|
-
mintPt:
|
|
2289
|
-
mintSy:
|
|
2290
|
-
mintLp:
|
|
2291
|
-
feeTreasurySyBps:
|
|
2292
|
-
ptBalance:
|
|
2293
|
-
syBalance:
|
|
2294
|
-
currentSyRate:
|
|
2295
|
-
expirationTs:
|
|
2296
|
-
expirationDate: new Date(
|
|
2297
|
-
lnFeeRateRoot:
|
|
2298
|
-
rateScalarRoot:
|
|
2299
|
-
lastLnImpliedRate:
|
|
2300
|
-
currentPtPriceInSy:
|
|
2301
|
-
currentPtPriceInAsset:
|
|
2302
|
-
secondsRemaining:
|
|
2303
|
-
proportionPtInAsset:
|
|
2304
|
-
proportionPtInSy:
|
|
2305
|
-
currentFeeRate:
|
|
2306
|
-
currentRateScalar:
|
|
2307
|
-
currentRateAnchor:
|
|
2308
|
-
cpiAccounts: serializeCpiAccountsRaw(
|
|
2355
|
+
treasurySyTokenAccount: market.state.tokenFeeTreasurySy.toBase58(),
|
|
2356
|
+
address: market.selfAddress.toBase58(),
|
|
2357
|
+
addressLookupTable: market.addressLookupTable.toBase58(),
|
|
2358
|
+
tokenSyEscrow: market.tokenSyEscrow.toBase58(),
|
|
2359
|
+
tokenPtEscrow: market.tokenPtEscrow.toBase58(),
|
|
2360
|
+
tokenLpEscrow: market.tokenLpEscrow.toBase58(),
|
|
2361
|
+
lpSupply: market.state.lpSupply.toString(),
|
|
2362
|
+
syProgram: market.state.syProgram.toBase58(),
|
|
2363
|
+
mintPt: market.state.mintPt.toBase58(),
|
|
2364
|
+
mintSy: market.state.mintSy.toBase58(),
|
|
2365
|
+
mintLp: market.state.mintLp.toBase58(),
|
|
2366
|
+
feeTreasurySyBps: market.state.feeTreasurySyBps,
|
|
2367
|
+
ptBalance: market.state.ptBalance.toString(),
|
|
2368
|
+
syBalance: market.state.syBalance.toString(),
|
|
2369
|
+
currentSyRate: market.flavor.currentSyExchangeRate,
|
|
2370
|
+
expirationTs: market.state.expirationTs,
|
|
2371
|
+
expirationDate: new Date(market.state.expirationTs * 1000).toISOString(),
|
|
2372
|
+
lnFeeRateRoot: market.state.lnFeeRateRoot,
|
|
2373
|
+
rateScalarRoot: market.state.rateScalarRoot,
|
|
2374
|
+
lastLnImpliedRate: market.state.lastLnImpliedRate,
|
|
2375
|
+
currentPtPriceInSy: market.currentPtPriceInSy,
|
|
2376
|
+
currentPtPriceInAsset: market.currentPtPriceInAsset,
|
|
2377
|
+
secondsRemaining: market.secondsRemaining,
|
|
2378
|
+
proportionPtInAsset: marketCalculator.proportionPtInAsset,
|
|
2379
|
+
proportionPtInSy: marketCalculator.proportionPtInSy,
|
|
2380
|
+
currentFeeRate: marketCalculator.feeRate,
|
|
2381
|
+
currentRateScalar: marketCalculator.rateScalar,
|
|
2382
|
+
currentRateAnchor: marketCalculator.findRateAnchor(),
|
|
2383
|
+
cpiAccounts: serializeCpiAccountsRaw(market.cpiAccounts),
|
|
2309
2384
|
emissions: {
|
|
2310
|
-
trackers:
|
|
2385
|
+
trackers: market.marketEmissions.trackers.map((e) => ({
|
|
2311
2386
|
tokenEscrow: e.tokenEscrow.toBase58(),
|
|
2312
2387
|
lpShareIndex: e.lpShareIndex,
|
|
2313
2388
|
lastSeenStaged: e.lastSeenStaged,
|
|
2314
2389
|
})),
|
|
2315
2390
|
},
|
|
2316
|
-
ptApr:
|
|
2317
|
-
|
|
2318
|
-
|
|
2391
|
+
ptApr: market.ptApr,
|
|
2392
|
+
ptApy: market.ptApy,
|
|
2393
|
+
lpEscrowAmount: market.state.lpEscrowAmount.toString(),
|
|
2394
|
+
maxLpSupply: market.state.maxLpSupply.toString(),
|
|
2319
2395
|
liquidityNetBalanceLimits: {
|
|
2320
|
-
windowStartTimestamp:
|
|
2321
|
-
windowStartNetBalance:
|
|
2322
|
-
maxNetBalanceChangeNegativePercentage:
|
|
2323
|
-
|
|
2324
|
-
|
|
2396
|
+
windowStartTimestamp: market.state.liquidityNetBalanceLimits.windowStartTimestamp,
|
|
2397
|
+
windowStartNetBalance: market.state.liquidityNetBalanceLimits.windowStartNetBalance.toString(),
|
|
2398
|
+
maxNetBalanceChangeNegativePercentage:
|
|
2399
|
+
market.state.liquidityNetBalanceLimits.maxNetBalanceChangeNegativePercentage,
|
|
2400
|
+
maxNetBalanceChangePositivePercentage:
|
|
2401
|
+
market.state.liquidityNetBalanceLimits.maxNetBalanceChangePositivePercentage,
|
|
2402
|
+
windowDurationSeconds: market.state.liquidityNetBalanceLimits.windowDurationSeconds,
|
|
2325
2403
|
},
|
|
2326
2404
|
lpFarm: {
|
|
2327
|
-
lastSeenTimestamp:
|
|
2328
|
-
farmEmissions:
|
|
2405
|
+
lastSeenTimestamp: market.state.lpFarm.lastSeenTimestamp,
|
|
2406
|
+
farmEmissions: market.state.lpFarm.farmEmissions.map((e) => ({
|
|
2329
2407
|
mint: e.mint.toBase58(),
|
|
2330
2408
|
tokenRate: e.tokenRate.toString(),
|
|
2331
2409
|
expiryTimestamp: e.expiryTimestamp,
|