@exponent-labs/exponent-sdk 0.0.10 → 0.1.1
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.map +1 -1
- package/build/events.d.ts +2 -2
- package/build/events.js +43 -49
- package/build/events.js.map +1 -1
- package/build/flavors.d.ts +3 -3
- package/build/flavors.js +19 -24
- package/build/flavors.js.map +1 -1
- package/build/lpPosition.d.ts +4 -4
- package/build/lpPosition.js +19 -19
- package/build/lpPosition.js.map +1 -1
- package/build/market.d.ts +31 -35
- package/build/market.js +202 -205
- package/build/market.js.map +1 -1
- package/build/syPosition.d.ts +2 -2
- package/build/syPosition.js +19 -16
- package/build/syPosition.js.map +1 -1
- package/build/tokenUtil.d.ts +2 -2
- package/build/tokenUtil.js +4 -4
- package/build/tokenUtil.js.map +1 -1
- package/build/utils/altUtil.d.ts +3 -3
- package/build/utils/altUtil.js +15 -9
- package/build/utils/altUtil.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.map +1 -1
- package/build/vault.d.ts +11 -11
- package/build/vault.js +69 -68
- package/build/vault.js.map +1 -1
- package/build/ytPosition.d.ts +7 -7
- package/build/ytPosition.js +33 -33
- package/build/ytPosition.js.map +1 -1
- package/package.json +22 -22
- package/src/addressLookupTableUtil.ts +2 -1
- package/src/events.ts +44 -44
- package/src/flavors.ts +33 -39
- package/src/lpPosition.ts +22 -21
- package/src/market.ts +296 -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 +40 -36
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,12 +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
|
|
393
400
|
}
|
|
394
401
|
|
|
395
|
-
static annualizeApy(
|
|
396
|
-
return (1 +
|
|
402
|
+
static annualizeApy(rate: number, secondsRemaining: number) {
|
|
403
|
+
return (1 + rate) ** (SECONDS_PER_YEAR / secondsRemaining) - 1
|
|
397
404
|
}
|
|
398
405
|
|
|
399
406
|
/** Annualized PT fixed rate */
|
|
@@ -423,7 +430,7 @@ export class Market {
|
|
|
423
430
|
return this.feeTreasuryBps / 10_000
|
|
424
431
|
}
|
|
425
432
|
|
|
426
|
-
/** Calculate
|
|
433
|
+
/** Calculate amount of LP tokens to expect for tokens in */
|
|
427
434
|
lpOutForTokensIn(syInIntent: bigint, ptInIntent: bigint): LiquidityAdd {
|
|
428
435
|
return lpOutForTokensIn({
|
|
429
436
|
syIntent: Number(syInIntent),
|
|
@@ -435,7 +442,8 @@ export class Market {
|
|
|
435
442
|
}
|
|
436
443
|
|
|
437
444
|
marketCalculator() {
|
|
438
|
-
const
|
|
445
|
+
const timeNow = Date.now() / 1000
|
|
446
|
+
const secondsRemaining = Math.max(0, Math.round(this.state.expirationTs - timeNow))
|
|
439
447
|
return new MarketCalculator({
|
|
440
448
|
liquiditySy: parseInt(this.syBalance.toString()),
|
|
441
449
|
liquidityPt: parseInt(this.ptBalance.toString()),
|
|
@@ -450,6 +458,7 @@ export class Market {
|
|
|
450
458
|
windowStartTimestamp: this.state.liquidityNetBalanceLimits.windowStartTimestamp,
|
|
451
459
|
windowStartNetBalance: this.state.liquidityNetBalanceLimits.windowStartNetBalance.toNumber(),
|
|
452
460
|
windowDurationSeconds: this.state.liquidityNetBalanceLimits.windowDurationSeconds,
|
|
461
|
+
feeTreasurySyBps: this.state.feeTreasurySyBps,
|
|
453
462
|
})
|
|
454
463
|
}
|
|
455
464
|
|
|
@@ -466,9 +475,9 @@ export class Market {
|
|
|
466
475
|
syInIntent,
|
|
467
476
|
minLpOut,
|
|
468
477
|
depositor,
|
|
469
|
-
ptSrc,
|
|
470
|
-
sySrc,
|
|
471
|
-
lpDst,
|
|
478
|
+
ptSrc: ptSrcParam,
|
|
479
|
+
sySrc: sySrcParam,
|
|
480
|
+
lpDst: lpDstParam,
|
|
472
481
|
}: {
|
|
473
482
|
/** Intended (maximum) amount of PT in */
|
|
474
483
|
ptInIntent: bigint
|
|
@@ -483,9 +492,9 @@ export class Market {
|
|
|
483
492
|
}) {
|
|
484
493
|
const tokenProgram = TOKEN_PROGRAM_ID
|
|
485
494
|
|
|
486
|
-
sySrc =
|
|
487
|
-
ptSrc =
|
|
488
|
-
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)
|
|
489
498
|
|
|
490
499
|
const syRemAccounts = this.cpiAccounts.depositSy
|
|
491
500
|
|
|
@@ -532,7 +541,7 @@ export class Market {
|
|
|
532
541
|
signer,
|
|
533
542
|
farmMint,
|
|
534
543
|
farmTokenProgram,
|
|
535
|
-
farmTokenSrc,
|
|
544
|
+
farmTokenSrc: farmTokenSrcParam,
|
|
536
545
|
}: {
|
|
537
546
|
newRate: bigint
|
|
538
547
|
untilTimestamp: number
|
|
@@ -541,7 +550,7 @@ export class Market {
|
|
|
541
550
|
farmTokenProgram: web3.PublicKey
|
|
542
551
|
farmTokenSrc?: web3.PublicKey
|
|
543
552
|
}) {
|
|
544
|
-
farmTokenSrc =
|
|
553
|
+
const farmTokenSrc = farmTokenSrcParam || getAssociatedTokenAddressSync(farmMint, signer, true, farmTokenProgram)
|
|
545
554
|
|
|
546
555
|
const tokenFarm = getAssociatedTokenAddressSync(farmMint, this.selfAddress, true, farmTokenProgram)
|
|
547
556
|
|
|
@@ -558,6 +567,7 @@ export class Market {
|
|
|
558
567
|
})
|
|
559
568
|
.instruction()
|
|
560
569
|
}
|
|
570
|
+
|
|
561
571
|
/**
|
|
562
572
|
* Redeem LP tokens for PT & SY (liquidity removal)
|
|
563
573
|
*
|
|
@@ -572,9 +582,9 @@ export class Market {
|
|
|
572
582
|
withdrawer,
|
|
573
583
|
minPtOut,
|
|
574
584
|
minSyOut,
|
|
575
|
-
ptDst,
|
|
576
|
-
syDst,
|
|
577
|
-
lpSrc,
|
|
585
|
+
ptDst: ptDstParam,
|
|
586
|
+
syDst: syDstParam,
|
|
587
|
+
lpSrc: lpSrcParam,
|
|
578
588
|
}: {
|
|
579
589
|
lpIn: bigint
|
|
580
590
|
withdrawer: web3.PublicKey
|
|
@@ -584,9 +594,9 @@ export class Market {
|
|
|
584
594
|
syDst?: web3.PublicKey
|
|
585
595
|
lpSrc?: web3.PublicKey
|
|
586
596
|
}) {
|
|
587
|
-
ptDst =
|
|
588
|
-
syDst =
|
|
589
|
-
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)
|
|
590
600
|
|
|
591
601
|
const ptDstAtaIx = createAssociatedTokenAccountIdempotentInstruction(withdrawer, ptDst, withdrawer, this.mintPt)
|
|
592
602
|
const syDstAtaIx = createAssociatedTokenAccountIdempotentInstruction(withdrawer, syDst, withdrawer, this.mintSy)
|
|
@@ -684,8 +694,8 @@ export class Market {
|
|
|
684
694
|
traderPt,
|
|
685
695
|
syConstraint,
|
|
686
696
|
isBuy,
|
|
687
|
-
tokenPt,
|
|
688
|
-
tokenSy,
|
|
697
|
+
tokenPt: tokenPtParam,
|
|
698
|
+
tokenSy: tokenSyParam,
|
|
689
699
|
}: {
|
|
690
700
|
trader: web3.PublicKey
|
|
691
701
|
traderPt: bigint
|
|
@@ -694,8 +704,8 @@ export class Market {
|
|
|
694
704
|
tokenPt?: web3.PublicKey
|
|
695
705
|
tokenSy?: web3.PublicKey
|
|
696
706
|
}) {
|
|
697
|
-
tokenPt =
|
|
698
|
-
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)
|
|
699
709
|
|
|
700
710
|
const tokenSyAtaIx = createAssociatedTokenAccountIdempotentInstruction(trader, tokenSy, trader, this.mintSy)
|
|
701
711
|
const tokenPtAtaIx = createAssociatedTokenAccountIdempotentInstruction(trader, tokenPt, trader, this.mintPt)
|
|
@@ -745,9 +755,9 @@ export class Market {
|
|
|
745
755
|
trader,
|
|
746
756
|
ytIn,
|
|
747
757
|
minSyOut,
|
|
748
|
-
ytSrc,
|
|
749
|
-
ptSrc,
|
|
750
|
-
syDst,
|
|
758
|
+
ytSrc: ytSrcParam,
|
|
759
|
+
ptSrc: ptSrcParam,
|
|
760
|
+
syDst: syDstParam,
|
|
751
761
|
}: {
|
|
752
762
|
trader: web3.PublicKey
|
|
753
763
|
ytIn: bigint
|
|
@@ -756,9 +766,9 @@ export class Market {
|
|
|
756
766
|
ptSrc?: web3.PublicKey
|
|
757
767
|
syDst?: web3.PublicKey
|
|
758
768
|
}) {
|
|
759
|
-
syDst =
|
|
760
|
-
ptSrc =
|
|
761
|
-
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)
|
|
762
772
|
|
|
763
773
|
const syDstAtaIxs = createAssociatedTokenAccountIdempotentInstruction(trader, syDst, trader, this.mintSy)
|
|
764
774
|
const ptSrcAtaIxs = createAssociatedTokenAccountIdempotentInstruction(trader, ptSrc, trader, this.mintPt)
|
|
@@ -816,9 +826,9 @@ export class Market {
|
|
|
816
826
|
trader,
|
|
817
827
|
ytOut,
|
|
818
828
|
maxSyIn,
|
|
819
|
-
ytTrader,
|
|
820
|
-
ptTrader,
|
|
821
|
-
syTrader,
|
|
829
|
+
ytTrader: ytTraderParam,
|
|
830
|
+
ptTrader: ptTraderParam,
|
|
831
|
+
syTrader: syTraderParam,
|
|
822
832
|
}: {
|
|
823
833
|
trader: web3.PublicKey
|
|
824
834
|
ytOut: bigint
|
|
@@ -827,9 +837,9 @@ export class Market {
|
|
|
827
837
|
ptTrader?: web3.PublicKey
|
|
828
838
|
syTrader?: web3.PublicKey
|
|
829
839
|
}) {
|
|
830
|
-
syTrader =
|
|
831
|
-
ptTrader =
|
|
832
|
-
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)
|
|
833
843
|
|
|
834
844
|
const syTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(trader, syTrader, trader, this.mintSy)
|
|
835
845
|
const ptTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(trader, ptTrader, trader, this.mintPt)
|
|
@@ -897,8 +907,16 @@ export class Market {
|
|
|
897
907
|
}
|
|
898
908
|
|
|
899
909
|
/** Deposit LP tokens into the farming module to earn rewards */
|
|
900
|
-
async ixDepositLp({
|
|
901
|
-
|
|
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)
|
|
902
920
|
const lpPosition = this.xponPda.marketLpPosition({ market: this.selfAddress, owner })
|
|
903
921
|
|
|
904
922
|
return this.coreProgram.methods
|
|
@@ -922,8 +940,16 @@ export class Market {
|
|
|
922
940
|
}
|
|
923
941
|
|
|
924
942
|
/** Withdraw LP tokens from the farming module */
|
|
925
|
-
async ixWithdrawLp({
|
|
926
|
-
|
|
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)
|
|
927
953
|
|
|
928
954
|
const lpPosition = this.xponPda.marketLpPosition({ market: this.selfAddress, owner })
|
|
929
955
|
return this.coreProgram.methods
|
|
@@ -946,9 +972,15 @@ export class Market {
|
|
|
946
972
|
.instruction()
|
|
947
973
|
}
|
|
948
974
|
|
|
949
|
-
async ixWrapperCollectInterest({
|
|
975
|
+
async ixWrapperCollectInterest({
|
|
976
|
+
claimer,
|
|
977
|
+
tokenSyDst: tokenSyDstParam,
|
|
978
|
+
}: {
|
|
979
|
+
claimer: web3.PublicKey
|
|
980
|
+
tokenSyDst?: web3.PublicKey
|
|
981
|
+
}) {
|
|
950
982
|
const yieldPosition = this.xponPda.yieldPosition({ owner: claimer, vault: this.vault.selfAddress })
|
|
951
|
-
tokenSyDst =
|
|
983
|
+
const tokenSyDst = tokenSyDstParam || getAssociatedTokenAddressSync(this.mintSy, claimer, true, TOKEN_PROGRAM_ID)
|
|
952
984
|
const tokenBaseClaimer = getAssociatedTokenAddressSync(
|
|
953
985
|
this.flavor.mintBase,
|
|
954
986
|
claimer,
|
|
@@ -1015,9 +1047,9 @@ export class Market {
|
|
|
1015
1047
|
owner,
|
|
1016
1048
|
ptOut,
|
|
1017
1049
|
maxBaseIn,
|
|
1018
|
-
tokenSyTrader,
|
|
1019
|
-
tokenPtTrader,
|
|
1020
|
-
tokenBaseTrader,
|
|
1050
|
+
tokenSyTrader: tokenSyTraderParam,
|
|
1051
|
+
tokenPtTrader: tokenPtTraderParam,
|
|
1052
|
+
tokenBaseTrader: tokenBaseTraderParam,
|
|
1021
1053
|
}: {
|
|
1022
1054
|
owner: web3.PublicKey
|
|
1023
1055
|
ptOut: bigint
|
|
@@ -1026,10 +1058,13 @@ export class Market {
|
|
|
1026
1058
|
tokenPtTrader?: web3.PublicKey
|
|
1027
1059
|
tokenBaseTrader?: web3.PublicKey
|
|
1028
1060
|
}) {
|
|
1029
|
-
tokenSyTrader =
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
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)
|
|
1033
1068
|
|
|
1034
1069
|
const tokenSyTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1035
1070
|
owner,
|
|
@@ -1059,7 +1094,7 @@ export class Market {
|
|
|
1059
1094
|
|
|
1060
1095
|
const mintSyRemAccounts = mintSyIx.keys
|
|
1061
1096
|
|
|
1062
|
-
console.log("mintSyRemAccounts", mintSyRemAccounts)
|
|
1097
|
+
// console.log("mintSyRemAccounts", mintSyRemAccounts)
|
|
1063
1098
|
|
|
1064
1099
|
mintSyRemAccounts.push({
|
|
1065
1100
|
pubkey: this.coreProgram.programId,
|
|
@@ -1074,7 +1109,7 @@ export class Market {
|
|
|
1074
1109
|
])
|
|
1075
1110
|
|
|
1076
1111
|
const ix = await this.coreProgram.methods
|
|
1077
|
-
.wrapperBuyPt(new BN(ptOut.toString()), new BN(maxBaseIn.toString()), mintSyRemAccounts.length)
|
|
1112
|
+
.wrapperBuyPt(new BN(ptOut.toString()), new BN(maxBaseIn.toString()), mintSyRemAccounts.length - 1)
|
|
1078
1113
|
.accountsStrict({
|
|
1079
1114
|
addressLookupTable: this.addressLookupTable,
|
|
1080
1115
|
buyer: owner,
|
|
@@ -1102,9 +1137,9 @@ export class Market {
|
|
|
1102
1137
|
owner,
|
|
1103
1138
|
amount,
|
|
1104
1139
|
minBaseOut,
|
|
1105
|
-
tokenSyTrader,
|
|
1106
|
-
tokenPtTrader,
|
|
1107
|
-
tokenBaseTrader,
|
|
1140
|
+
tokenSyTrader: tokenSyTraderParam,
|
|
1141
|
+
tokenPtTrader: tokenPtTraderParam,
|
|
1142
|
+
tokenBaseTrader: tokenBaseTraderParam,
|
|
1108
1143
|
}: {
|
|
1109
1144
|
owner: web3.PublicKey
|
|
1110
1145
|
amount: bigint
|
|
@@ -1113,10 +1148,13 @@ export class Market {
|
|
|
1113
1148
|
tokenPtTrader?: web3.PublicKey
|
|
1114
1149
|
tokenBaseTrader?: web3.PublicKey
|
|
1115
1150
|
}) {
|
|
1116
|
-
tokenSyTrader =
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
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)
|
|
1120
1158
|
|
|
1121
1159
|
const tokenSyTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1122
1160
|
owner,
|
|
@@ -1177,10 +1215,10 @@ export class Market {
|
|
|
1177
1215
|
owner,
|
|
1178
1216
|
ytOut,
|
|
1179
1217
|
maxBaseIn,
|
|
1180
|
-
tokenSyTrader,
|
|
1181
|
-
tokenPtTrader,
|
|
1182
|
-
tokenYtTrader,
|
|
1183
|
-
tokenBaseTrader,
|
|
1218
|
+
tokenSyTrader: tokenSyTraderParam,
|
|
1219
|
+
tokenPtTrader: tokenPtTraderParam,
|
|
1220
|
+
tokenYtTrader: tokenYtTraderParam,
|
|
1221
|
+
tokenBaseTrader: tokenBaseTraderParam,
|
|
1184
1222
|
}: {
|
|
1185
1223
|
owner: web3.PublicKey
|
|
1186
1224
|
ytOut: bigint
|
|
@@ -1190,11 +1228,15 @@ export class Market {
|
|
|
1190
1228
|
tokenYtTrader?: web3.PublicKey
|
|
1191
1229
|
tokenBaseTrader?: web3.PublicKey
|
|
1192
1230
|
}) {
|
|
1193
|
-
tokenSyTrader =
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
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)
|
|
1198
1240
|
|
|
1199
1241
|
const tokenSyTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1200
1242
|
owner,
|
|
@@ -1298,10 +1340,10 @@ export class Market {
|
|
|
1298
1340
|
owner,
|
|
1299
1341
|
amount,
|
|
1300
1342
|
minBaseOut,
|
|
1301
|
-
tokenBaseTrader,
|
|
1302
|
-
tokenSyTrader,
|
|
1303
|
-
tokenYtTrader,
|
|
1304
|
-
tokenPtTrader,
|
|
1343
|
+
tokenBaseTrader: tokenBaseTraderParam,
|
|
1344
|
+
tokenSyTrader: tokenSyTraderParam,
|
|
1345
|
+
tokenYtTrader: tokenYtTraderParam,
|
|
1346
|
+
tokenPtTrader: tokenPtTraderParam,
|
|
1305
1347
|
}: {
|
|
1306
1348
|
owner: web3.PublicKey
|
|
1307
1349
|
amount: bigint
|
|
@@ -1311,11 +1353,15 @@ export class Market {
|
|
|
1311
1353
|
tokenYtTrader?: web3.PublicKey
|
|
1312
1354
|
tokenPtTrader?: web3.PublicKey
|
|
1313
1355
|
}) {
|
|
1314
|
-
tokenBaseTrader =
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
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)
|
|
1319
1365
|
|
|
1320
1366
|
const tokenSyTraderAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1321
1367
|
owner,
|
|
@@ -1357,7 +1403,7 @@ export class Market {
|
|
|
1357
1403
|
...this.vault.cpiAccounts.depositSy,
|
|
1358
1404
|
])
|
|
1359
1405
|
|
|
1360
|
-
console.log(" remaining accounts length", remainingAccounts.length)
|
|
1406
|
+
// console.log(" remaining accounts length", remainingAccounts.length)
|
|
1361
1407
|
|
|
1362
1408
|
// remainingAccounts.push({
|
|
1363
1409
|
// pubkey: this.coreProgram.programId,
|
|
@@ -1470,11 +1516,11 @@ export class Market {
|
|
|
1470
1516
|
depositor,
|
|
1471
1517
|
amountBase,
|
|
1472
1518
|
minLpOut,
|
|
1473
|
-
tokenSyDepositor,
|
|
1474
|
-
tokenYtDepositor,
|
|
1475
|
-
tokenPtDepositor,
|
|
1476
|
-
tokenBaseDepositor,
|
|
1477
|
-
tokenLpDepositor,
|
|
1519
|
+
tokenSyDepositor: tokenSyDepositorParam,
|
|
1520
|
+
tokenYtDepositor: tokenYtDepositorParam,
|
|
1521
|
+
tokenPtDepositor: tokenPtDepositorParam,
|
|
1522
|
+
tokenBaseDepositor: tokenBaseDepositorParam,
|
|
1523
|
+
tokenLpDepositor: tokenLpDepositorParam,
|
|
1478
1524
|
}: {
|
|
1479
1525
|
depositor: web3.PublicKey
|
|
1480
1526
|
amountBase: bigint
|
|
@@ -1485,13 +1531,17 @@ export class Market {
|
|
|
1485
1531
|
tokenBaseDepositor?: web3.PublicKey
|
|
1486
1532
|
tokenLpDepositor?: web3.PublicKey
|
|
1487
1533
|
}) {
|
|
1488
|
-
tokenSyDepositor =
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
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 ||
|
|
1493
1542
|
getAssociatedTokenAddressSync(this.flavor.mintBase, depositor, true, this.flavor.baseTokenProgram)
|
|
1494
|
-
tokenLpDepositor =
|
|
1543
|
+
const tokenLpDepositor =
|
|
1544
|
+
tokenLpDepositorParam || getAssociatedTokenAddressSync(this.mintLp, depositor, true, TOKEN_PROGRAM_ID)
|
|
1495
1545
|
|
|
1496
1546
|
const tokenSyDepositorAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1497
1547
|
depositor,
|
|
@@ -1533,9 +1583,9 @@ export class Market {
|
|
|
1533
1583
|
|
|
1534
1584
|
const mintSyRemAccounts = mintSyIx.keys
|
|
1535
1585
|
|
|
1536
|
-
// Log vault accounts before filtering
|
|
1537
1586
|
const beforeVaultAccounts = [...this.vault.cpiAccounts.getSyState, ...this.vault.cpiAccounts.depositSy]
|
|
1538
|
-
|
|
1587
|
+
// Log vault accounts before filtering
|
|
1588
|
+
// console.log("Before filtering vault accounts:", beforeVaultAccounts.length, "accounts")
|
|
1539
1589
|
|
|
1540
1590
|
// Filter out TOKEN_PROGRAM_ID and SystemProgram.programId from vault remaining accounts
|
|
1541
1591
|
const vaultCpiRemainingAccounts = beforeVaultAccounts.filter(
|
|
@@ -1543,11 +1593,11 @@ export class Market {
|
|
|
1543
1593
|
)
|
|
1544
1594
|
|
|
1545
1595
|
// Log vault accounts after filtering
|
|
1546
|
-
console.log("After filtering vault accounts:", vaultCpiRemainingAccounts.length, "accounts")
|
|
1596
|
+
// console.log("After filtering vault accounts:", vaultCpiRemainingAccounts.length, "accounts")
|
|
1547
1597
|
|
|
1548
1598
|
// Log market accounts before filtering
|
|
1549
1599
|
const beforeMarketAccounts = [...this.cpiAccounts.depositSy]
|
|
1550
|
-
console.log("Before filtering market accounts:", beforeMarketAccounts.length, "accounts")
|
|
1600
|
+
// console.log("Before filtering market accounts:", beforeMarketAccounts.length, "accounts")
|
|
1551
1601
|
|
|
1552
1602
|
// Filter out TOKEN_PROGRAM_ID and SystemProgram.programId from market remaining accounts
|
|
1553
1603
|
const marketCpiRemainingAccounts = beforeMarketAccounts.filter(
|
|
@@ -1558,7 +1608,7 @@ export class Market {
|
|
|
1558
1608
|
)
|
|
1559
1609
|
|
|
1560
1610
|
// Log market accounts after filtering
|
|
1561
|
-
console.log("After filtering market accounts:", marketCpiRemainingAccounts.length, "accounts")
|
|
1611
|
+
// console.log("After filtering market accounts:", marketCpiRemainingAccounts.length, "accounts")
|
|
1562
1612
|
|
|
1563
1613
|
// These remaining accounts can be in any order, and they can be unique-ified
|
|
1564
1614
|
const unorderedRemainingAccounts = uniqueRemainingAccounts([
|
|
@@ -1603,7 +1653,7 @@ export class Market {
|
|
|
1603
1653
|
.remainingAccounts(allRemainingAccounts)
|
|
1604
1654
|
.instruction()
|
|
1605
1655
|
|
|
1606
|
-
console.log("ix accounts length", ix.keys.length)
|
|
1656
|
+
// console.log("ix accounts length", ix.keys.length)
|
|
1607
1657
|
|
|
1608
1658
|
return {
|
|
1609
1659
|
ixs: [
|
|
@@ -1621,11 +1671,11 @@ export class Market {
|
|
|
1621
1671
|
minLpOut,
|
|
1622
1672
|
externalPtToBuy,
|
|
1623
1673
|
externalSyConstraint,
|
|
1624
|
-
tokenSyDepositor,
|
|
1625
|
-
tokenYtDepositor,
|
|
1626
|
-
tokenPtDepositor,
|
|
1627
|
-
tokenBaseDepositor,
|
|
1628
|
-
tokenLpDepositor,
|
|
1674
|
+
tokenSyDepositor: tokenSyDepositorParam,
|
|
1675
|
+
tokenYtDepositor: tokenYtDepositorParam,
|
|
1676
|
+
tokenPtDepositor: tokenPtDepositorParam,
|
|
1677
|
+
tokenBaseDepositor: tokenBaseDepositorParam,
|
|
1678
|
+
tokenLpDepositor: tokenLpDepositorParam,
|
|
1629
1679
|
}: {
|
|
1630
1680
|
depositor: web3.PublicKey
|
|
1631
1681
|
amountBase: bigint
|
|
@@ -1638,13 +1688,17 @@ export class Market {
|
|
|
1638
1688
|
tokenBaseDepositor?: web3.PublicKey
|
|
1639
1689
|
tokenLpDepositor?: web3.PublicKey
|
|
1640
1690
|
}) {
|
|
1641
|
-
tokenSyDepositor =
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
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 ||
|
|
1646
1699
|
getAssociatedTokenAddressSync(this.flavor.mintBase, depositor, true, this.flavor.baseTokenProgram)
|
|
1647
|
-
tokenLpDepositor =
|
|
1700
|
+
const tokenLpDepositor =
|
|
1701
|
+
tokenLpDepositorParam || getAssociatedTokenAddressSync(this.mintLp, depositor, true, TOKEN_PROGRAM_ID)
|
|
1648
1702
|
|
|
1649
1703
|
const tokenSyAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1650
1704
|
depositor,
|
|
@@ -1739,11 +1793,11 @@ export class Market {
|
|
|
1739
1793
|
amountBase,
|
|
1740
1794
|
amountPt,
|
|
1741
1795
|
minLpOut,
|
|
1742
|
-
tokenSyDepositor,
|
|
1743
|
-
tokenYtDepositor,
|
|
1744
|
-
tokenPtDepositor,
|
|
1745
|
-
tokenBaseDepositor,
|
|
1746
|
-
tokenLpDepositor,
|
|
1796
|
+
tokenSyDepositor: tokenSyDepositorParam,
|
|
1797
|
+
tokenYtDepositor: tokenYtDepositorParam,
|
|
1798
|
+
tokenPtDepositor: tokenPtDepositorParam,
|
|
1799
|
+
tokenBaseDepositor: tokenBaseDepositorParam,
|
|
1800
|
+
tokenLpDepositor: tokenLpDepositorParam,
|
|
1747
1801
|
}: {
|
|
1748
1802
|
depositor: web3.PublicKey
|
|
1749
1803
|
amountBase: bigint
|
|
@@ -1755,13 +1809,17 @@ export class Market {
|
|
|
1755
1809
|
tokenBaseDepositor?: web3.PublicKey
|
|
1756
1810
|
tokenLpDepositor?: web3.PublicKey
|
|
1757
1811
|
}) {
|
|
1758
|
-
tokenSyDepositor =
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
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 ||
|
|
1763
1820
|
getAssociatedTokenAddressSync(this.flavor.mintBase, depositor, true, this.flavor.baseTokenProgram)
|
|
1764
|
-
tokenLpDepositor =
|
|
1821
|
+
const tokenLpDepositor =
|
|
1822
|
+
tokenLpDepositorParam || getAssociatedTokenAddressSync(this.mintLp, depositor, true, TOKEN_PROGRAM_ID)
|
|
1765
1823
|
|
|
1766
1824
|
const tokenSyAtaIx = createAssociatedTokenAccountIdempotentInstruction(
|
|
1767
1825
|
depositor,
|
|
@@ -1849,11 +1907,11 @@ export class Market {
|
|
|
1849
1907
|
owner,
|
|
1850
1908
|
amountLp,
|
|
1851
1909
|
minBaseOut,
|
|
1852
|
-
tokenSyWithdrawer,
|
|
1853
|
-
tokenYtWithdrawer,
|
|
1854
|
-
tokenPtWithdrawer,
|
|
1855
|
-
tokenBaseWithdrawer,
|
|
1856
|
-
tokenLpWithdrawer,
|
|
1910
|
+
tokenSyWithdrawer: tokenSyWithdrawerParam,
|
|
1911
|
+
tokenYtWithdrawer: tokenYtWithdrawerParam,
|
|
1912
|
+
tokenPtWithdrawer: tokenPtWithdrawerParam,
|
|
1913
|
+
tokenBaseWithdrawer: tokenBaseWithdrawerParam,
|
|
1914
|
+
tokenLpWithdrawer: tokenLpWithdrawerParam,
|
|
1857
1915
|
}: {
|
|
1858
1916
|
owner: web3.PublicKey
|
|
1859
1917
|
amountLp: bigint
|
|
@@ -1864,13 +1922,17 @@ export class Market {
|
|
|
1864
1922
|
tokenBaseWithdrawer?: web3.PublicKey
|
|
1865
1923
|
tokenLpWithdrawer?: web3.PublicKey
|
|
1866
1924
|
}) {
|
|
1867
|
-
tokenSyWithdrawer =
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
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 ||
|
|
1872
1933
|
getAssociatedTokenAddressSync(this.flavor.mintBase, owner, true, this.flavor.baseTokenProgram)
|
|
1873
|
-
tokenLpWithdrawer =
|
|
1934
|
+
const tokenLpWithdrawer =
|
|
1935
|
+
tokenLpWithdrawerParam || getAssociatedTokenAddressSync(this.mintLp, owner, true, TOKEN_PROGRAM_ID)
|
|
1874
1936
|
|
|
1875
1937
|
const tokenSyAtaIx = createAssociatedTokenAccountIdempotentInstruction(owner, tokenSyWithdrawer, owner, this.mintSy)
|
|
1876
1938
|
const tokenYtAtaIx = createAssociatedTokenAccountIdempotentInstruction(owner, tokenYtWithdrawer, owner, this.mintYt)
|
|
@@ -1939,29 +2001,31 @@ export class Market {
|
|
|
1939
2001
|
async ixWithdrawLiquidityClassic({
|
|
1940
2002
|
owner,
|
|
1941
2003
|
amountLp,
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
tokenLpWithdrawer,
|
|
2004
|
+
tokenSyWithdrawer: tokenSyWithdrawerParam,
|
|
2005
|
+
tokenYtWithdrawer: tokenYtWithdrawerParam,
|
|
2006
|
+
tokenPtWithdrawer: tokenPtWithdrawerParam,
|
|
2007
|
+
tokenBaseWithdrawer: tokenBaseWithdrawerParam,
|
|
2008
|
+
tokenLpWithdrawer: tokenLpWithdrawerParam,
|
|
1948
2009
|
}: {
|
|
1949
2010
|
owner: web3.PublicKey
|
|
1950
2011
|
amountLp: bigint
|
|
1951
|
-
minBaseOut: bigint
|
|
1952
2012
|
tokenSyWithdrawer?: web3.PublicKey
|
|
1953
2013
|
tokenYtWithdrawer?: web3.PublicKey
|
|
1954
2014
|
tokenPtWithdrawer?: web3.PublicKey
|
|
1955
2015
|
tokenBaseWithdrawer?: web3.PublicKey
|
|
1956
2016
|
tokenLpWithdrawer?: web3.PublicKey
|
|
1957
2017
|
}) {
|
|
1958
|
-
tokenSyWithdrawer =
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
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 ||
|
|
1963
2026
|
getAssociatedTokenAddressSync(this.flavor.mintBase, owner, true, this.flavor.baseTokenProgram)
|
|
1964
|
-
tokenLpWithdrawer =
|
|
2027
|
+
const tokenLpWithdrawer =
|
|
2028
|
+
tokenLpWithdrawerParam || getAssociatedTokenAddressSync(this.mintLp, owner, true, TOKEN_PROGRAM_ID)
|
|
1965
2029
|
|
|
1966
2030
|
const tokenSyAtaIx = createAssociatedTokenAccountIdempotentInstruction(owner, tokenSyWithdrawer, owner, this.mintSy)
|
|
1967
2031
|
const tokenYtAtaIx = createAssociatedTokenAccountIdempotentInstruction(owner, tokenYtWithdrawer, owner, this.mintYt)
|
|
@@ -2024,14 +2088,14 @@ export class Market {
|
|
|
2024
2088
|
owner,
|
|
2025
2089
|
mint,
|
|
2026
2090
|
tokenProgram,
|
|
2027
|
-
tokenDst,
|
|
2091
|
+
tokenDst: tokenDstParam,
|
|
2028
2092
|
}: {
|
|
2029
2093
|
owner: web3.PublicKey
|
|
2030
2094
|
mint: web3.PublicKey
|
|
2031
2095
|
tokenProgram: web3.PublicKey
|
|
2032
2096
|
tokenDst?: web3.PublicKey
|
|
2033
2097
|
}) {
|
|
2034
|
-
tokenDst =
|
|
2098
|
+
const tokenDst = tokenDstParam || getAssociatedTokenAddressSync(mint, owner, true, tokenProgram)
|
|
2035
2099
|
const lpPosition = this.xponPda.marketLpPosition({ market: this.selfAddress, owner })
|
|
2036
2100
|
|
|
2037
2101
|
const tokenFarm = getAssociatedTokenAddressSync(mint, this.selfAddress, true, tokenProgram)
|
|
@@ -2100,8 +2164,8 @@ export class Market {
|
|
|
2100
2164
|
farmRewardTokenProgram,
|
|
2101
2165
|
emissionsRate,
|
|
2102
2166
|
untilTimestamp,
|
|
2103
|
-
farmRewardSrc,
|
|
2104
|
-
feePayer,
|
|
2167
|
+
farmRewardSrc: farmRewardSrcParam,
|
|
2168
|
+
feePayer: feePayerParam,
|
|
2105
2169
|
}: {
|
|
2106
2170
|
signer: web3.PublicKey
|
|
2107
2171
|
farmMint: web3.PublicKey
|
|
@@ -2111,9 +2175,10 @@ export class Market {
|
|
|
2111
2175
|
farmRewardSrc?: web3.PublicKey
|
|
2112
2176
|
feePayer?: web3.PublicKey
|
|
2113
2177
|
}) {
|
|
2114
|
-
feePayer =
|
|
2178
|
+
const feePayer = feePayerParam || signer
|
|
2115
2179
|
const farmRewardEscrow = getAssociatedTokenAddressSync(farmMint, this.selfAddress, true, farmRewardTokenProgram)
|
|
2116
|
-
farmRewardSrc =
|
|
2180
|
+
const farmRewardSrc =
|
|
2181
|
+
farmRewardSrcParam || getAssociatedTokenAddressSync(farmMint, signer, true, farmRewardTokenProgram)
|
|
2117
2182
|
|
|
2118
2183
|
const ix = await this.coreProgram.methods
|
|
2119
2184
|
.addFarm(new BN(emissionsRate), untilTimestamp)
|
|
@@ -2136,22 +2201,22 @@ export class Market {
|
|
|
2136
2201
|
}
|
|
2137
2202
|
|
|
2138
2203
|
/** Calculate available liquidity for PT trades
|
|
2139
|
-
* @param
|
|
2140
|
-
* @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)
|
|
2141
2206
|
* @returns amount of PT that can be traded given current liquidity
|
|
2142
2207
|
*/
|
|
2143
|
-
liquidityAvailable(
|
|
2208
|
+
liquidityAvailable(isBuy: boolean, sizePt: bigint): bigint {
|
|
2144
2209
|
const calc = this.marketCalculator()
|
|
2145
2210
|
|
|
2146
|
-
if (
|
|
2211
|
+
if (isBuy) {
|
|
2147
2212
|
// When buying PT, we're limited by SY liquidity
|
|
2148
2213
|
// Convert PT amount to SY using current price
|
|
2149
|
-
const syNeeded = Number(
|
|
2214
|
+
const syNeeded = Number(sizePt) * calc.exchangeRate
|
|
2150
2215
|
// Available SY in the market
|
|
2151
2216
|
const syAvailable = Number(this.syBalance)
|
|
2152
2217
|
|
|
2153
2218
|
if (syNeeded <= syAvailable) {
|
|
2154
|
-
return
|
|
2219
|
+
return sizePt
|
|
2155
2220
|
} else {
|
|
2156
2221
|
// Calculate max PT that can be bought with available SY
|
|
2157
2222
|
return BigInt(Math.floor(syAvailable / calc.exchangeRate))
|
|
@@ -2160,8 +2225,8 @@ export class Market {
|
|
|
2160
2225
|
// When selling PT, we're limited by PT liquidity in the market
|
|
2161
2226
|
const ptAvailable = this.ptBalance
|
|
2162
2227
|
|
|
2163
|
-
if (
|
|
2164
|
-
return
|
|
2228
|
+
if (sizePt <= ptAvailable) {
|
|
2229
|
+
return sizePt
|
|
2165
2230
|
} else {
|
|
2166
2231
|
return ptAvailable
|
|
2167
2232
|
}
|
|
@@ -2284,59 +2349,61 @@ export type MarketJson = {
|
|
|
2284
2349
|
}
|
|
2285
2350
|
}
|
|
2286
2351
|
|
|
2287
|
-
function toJson(
|
|
2288
|
-
const
|
|
2352
|
+
function toJson(market: Market): MarketJson {
|
|
2353
|
+
const marketCalculator = market.marketCalculator()
|
|
2289
2354
|
return {
|
|
2290
|
-
treasurySyTokenAccount:
|
|
2291
|
-
address:
|
|
2292
|
-
addressLookupTable:
|
|
2293
|
-
tokenSyEscrow:
|
|
2294
|
-
tokenPtEscrow:
|
|
2295
|
-
tokenLpEscrow:
|
|
2296
|
-
lpSupply:
|
|
2297
|
-
syProgram:
|
|
2298
|
-
mintPt:
|
|
2299
|
-
mintSy:
|
|
2300
|
-
mintLp:
|
|
2301
|
-
feeTreasurySyBps:
|
|
2302
|
-
ptBalance:
|
|
2303
|
-
syBalance:
|
|
2304
|
-
currentSyRate:
|
|
2305
|
-
expirationTs:
|
|
2306
|
-
expirationDate: new Date(
|
|
2307
|
-
lnFeeRateRoot:
|
|
2308
|
-
rateScalarRoot:
|
|
2309
|
-
lastLnImpliedRate:
|
|
2310
|
-
currentPtPriceInSy:
|
|
2311
|
-
currentPtPriceInAsset:
|
|
2312
|
-
secondsRemaining:
|
|
2313
|
-
proportionPtInAsset:
|
|
2314
|
-
proportionPtInSy:
|
|
2315
|
-
currentFeeRate:
|
|
2316
|
-
currentRateScalar:
|
|
2317
|
-
currentRateAnchor:
|
|
2318
|
-
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),
|
|
2319
2384
|
emissions: {
|
|
2320
|
-
trackers:
|
|
2385
|
+
trackers: market.marketEmissions.trackers.map((e) => ({
|
|
2321
2386
|
tokenEscrow: e.tokenEscrow.toBase58(),
|
|
2322
2387
|
lpShareIndex: e.lpShareIndex,
|
|
2323
2388
|
lastSeenStaged: e.lastSeenStaged,
|
|
2324
2389
|
})),
|
|
2325
2390
|
},
|
|
2326
|
-
ptApr:
|
|
2327
|
-
ptApy:
|
|
2328
|
-
lpEscrowAmount:
|
|
2329
|
-
maxLpSupply:
|
|
2391
|
+
ptApr: market.ptApr,
|
|
2392
|
+
ptApy: market.ptApy,
|
|
2393
|
+
lpEscrowAmount: market.state.lpEscrowAmount.toString(),
|
|
2394
|
+
maxLpSupply: market.state.maxLpSupply.toString(),
|
|
2330
2395
|
liquidityNetBalanceLimits: {
|
|
2331
|
-
windowStartTimestamp:
|
|
2332
|
-
windowStartNetBalance:
|
|
2333
|
-
maxNetBalanceChangeNegativePercentage:
|
|
2334
|
-
|
|
2335
|
-
|
|
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,
|
|
2336
2403
|
},
|
|
2337
2404
|
lpFarm: {
|
|
2338
|
-
lastSeenTimestamp:
|
|
2339
|
-
farmEmissions:
|
|
2405
|
+
lastSeenTimestamp: market.state.lpFarm.lastSeenTimestamp,
|
|
2406
|
+
farmEmissions: market.state.lpFarm.farmEmissions.map((e) => ({
|
|
2340
2407
|
mint: e.mint.toBase58(),
|
|
2341
2408
|
tokenRate: e.tokenRate.toString(),
|
|
2342
2409
|
expiryTimestamp: e.expiryTimestamp,
|