@lavarage/sdk 5.1.1 → 6.0.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/dist/index.js +45 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +46 -33
- package/dist/index.mjs.map +1 -1
- package/index.ts +54 -37
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Lavarage } from './idl/lavarage'
|
|
|
3
3
|
import { Lavarage as LavarageV2 } from './idl/lavaragev2'
|
|
4
4
|
import bs58 from 'bs58'
|
|
5
5
|
import { AddressLookupTableAccount, ComputeBudgetProgram, Keypair, PublicKey, SystemProgram, SYSVAR_CLOCK_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, Transaction, TransactionInstruction, TransactionMessage, VersionedTransaction } from '@solana/web3.js'
|
|
6
|
-
import { ASSOCIATED_TOKEN_PROGRAM_ID, createAssociatedTokenAccountInstruction, getAccount, getAssociatedTokenAddressSync,
|
|
6
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, createAssociatedTokenAccountInstruction, createTransferInstruction, getAccount, getAssociatedTokenAddressSync, TokenAccountNotFoundError, TokenInvalidAccountOwnerError } from '@solana/spl-token'
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
export function getPda(seed: Buffer | Buffer[], programId: PublicKey) {
|
|
@@ -16,8 +16,8 @@ export function getPositionAccountPDA(lavarageProgram: Program<Lavarage> | Progr
|
|
|
16
16
|
return getPda([Buffer.from('position'), lavarageProgram.provider.publicKey!.toBuffer(), offer.publicKey.toBuffer(), seed.toBuffer()], lavarageProgram.programId)
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
async function getTokenAccountOrCreateIfNotExists(lavarageProgram: Program<Lavarage> | Program<LavarageV2>, ownerPublicKey: PublicKey, tokenAddress: PublicKey) {
|
|
20
|
-
const associatedTokenAddress = getAssociatedTokenAddressSync(tokenAddress, ownerPublicKey, true,
|
|
19
|
+
async function getTokenAccountOrCreateIfNotExists(lavarageProgram: Program<Lavarage> | Program<LavarageV2>, ownerPublicKey: PublicKey, tokenAddress: PublicKey, tokenProgram?: PublicKey) {
|
|
20
|
+
const associatedTokenAddress = getAssociatedTokenAddressSync(tokenAddress, ownerPublicKey, true, tokenProgram, ASSOCIATED_TOKEN_PROGRAM_ID)
|
|
21
21
|
|
|
22
22
|
try {
|
|
23
23
|
const tokenAccount = await getAccount(lavarageProgram.provider.connection, associatedTokenAddress, 'finalized')
|
|
@@ -30,7 +30,7 @@ async function getTokenAccountOrCreateIfNotExists(lavarageProgram: Program<Lavar
|
|
|
30
30
|
associatedTokenAddress,
|
|
31
31
|
ownerPublicKey,
|
|
32
32
|
tokenAddress,
|
|
33
|
-
|
|
33
|
+
tokenProgram,
|
|
34
34
|
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
35
35
|
)
|
|
36
36
|
|
|
@@ -130,9 +130,13 @@ export const openTradeV1 = async (lavarageProgram: Program<Lavarage>, offer: Pro
|
|
|
130
130
|
}, marginSOL: BN, leverage: number, randomSeed: Keypair, partnerFeeRecipient?: PublicKey) => {
|
|
131
131
|
// assuming all token accounts are created prior
|
|
132
132
|
const positionAccount = getPositionAccountPDA(lavarageProgram, offer, randomSeed.publicKey)
|
|
133
|
-
const fromTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, lavarageProgram.provider.publicKey!, offer.account.collateralType)
|
|
134
133
|
|
|
135
|
-
const
|
|
134
|
+
const mintAccount = await lavarageProgram.provider.connection.getAccountInfo(offer.account.collateralType)
|
|
135
|
+
const tokenProgram = mintAccount?.owner
|
|
136
|
+
|
|
137
|
+
const fromTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, lavarageProgram.provider.publicKey!, offer.account.collateralType, tokenProgram)
|
|
138
|
+
|
|
139
|
+
const toTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, positionAccount, offer.account.collateralType, tokenProgram)
|
|
136
140
|
|
|
137
141
|
const tokenAccountCreationTx = new Transaction()
|
|
138
142
|
|
|
@@ -253,9 +257,16 @@ export const openTradeV2 = async (lavarageProgram: Program<LavarageV2>, offer: P
|
|
|
253
257
|
}, marginSOL: BN, leverage: number, randomSeed: Keypair, quoteToken: PublicKey, partnerFeeRecipient?: PublicKey) => {
|
|
254
258
|
// assuming all token accounts are created prior
|
|
255
259
|
const positionAccount = getPositionAccountPDA(lavarageProgram, offer, randomSeed.publicKey)
|
|
256
|
-
const fromTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, lavarageProgram.provider.publicKey!, offer.account.collateralType)
|
|
257
260
|
|
|
258
|
-
const
|
|
261
|
+
const mintAccount = await lavarageProgram.provider.connection.getAccountInfo(offer.account.collateralType)
|
|
262
|
+
const tokenProgram = mintAccount?.owner
|
|
263
|
+
|
|
264
|
+
const quoteMintAccount = await lavarageProgram.provider.connection.getAccountInfo(quoteToken)
|
|
265
|
+
const quoteTokenProgram = quoteMintAccount?.owner
|
|
266
|
+
|
|
267
|
+
const fromTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, lavarageProgram.provider.publicKey!, offer.account.collateralType, tokenProgram)
|
|
268
|
+
|
|
269
|
+
const toTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, positionAccount, offer.account.collateralType, tokenProgram)
|
|
259
270
|
|
|
260
271
|
const tokenAccountCreationTx = new Transaction()
|
|
261
272
|
|
|
@@ -319,9 +330,9 @@ export const openTradeV2 = async (lavarageProgram: Program<LavarageV2>, offer: P
|
|
|
319
330
|
clock: SYSVAR_CLOCK_PUBKEY,
|
|
320
331
|
randomAccountAsId: randomSeed.publicKey.toBase58(),
|
|
321
332
|
feeTokenAccount: getAssociatedTokenAddressSync(quoteToken, new PublicKey('6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF')),
|
|
322
|
-
toTokenAccount: getAssociatedTokenAddressSync(quoteToken, lavarageProgram.provider.publicKey
|
|
323
|
-
tokenProgram:
|
|
324
|
-
fromTokenAccount: getAssociatedTokenAddressSync(quoteToken, offer.account.nodeWallet, true),
|
|
333
|
+
toTokenAccount: getAssociatedTokenAddressSync(quoteToken, lavarageProgram.provider.publicKey!, true, quoteTokenProgram),
|
|
334
|
+
tokenProgram: tokenProgram!,
|
|
335
|
+
fromTokenAccount: getAssociatedTokenAddressSync(quoteToken, offer.account.nodeWallet, true, tokenProgram),
|
|
325
336
|
}).remainingAccounts(partnerFeeRecipient ? [{
|
|
326
337
|
pubkey: partnerFeeRecipient,
|
|
327
338
|
isSigner: false,
|
|
@@ -501,11 +512,14 @@ export const closeTradeV1 = async (lavarageProgram: Program<Lavarage>, position:
|
|
|
501
512
|
|
|
502
513
|
const tokenAddressPubKey = new PublicKey(offer.account.collateralType)
|
|
503
514
|
|
|
515
|
+
const mintAccount = await lavarageProgram.provider.connection.getAccountInfo(offer.account.collateralType)
|
|
516
|
+
const tokenProgram = mintAccount?.owner
|
|
517
|
+
|
|
504
518
|
const positionAccountPDA = position.publicKey
|
|
505
519
|
|
|
506
|
-
const fromTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, positionAccountPDA, tokenAddressPubKey)
|
|
520
|
+
const fromTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, positionAccountPDA, tokenAddressPubKey, tokenProgram)
|
|
507
521
|
|
|
508
|
-
const toTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, lavarageProgram.provider.publicKey!, tokenAddressPubKey)
|
|
522
|
+
const toTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, lavarageProgram.provider.publicKey!, tokenAddressPubKey, tokenProgram)
|
|
509
523
|
|
|
510
524
|
const jupiterSellIx = jupInstruction!.instructions
|
|
511
525
|
|
|
@@ -560,14 +574,14 @@ export const closeTradeV1 = async (lavarageProgram: Program<Lavarage>, position:
|
|
|
560
574
|
clock: SYSVAR_CLOCK_PUBKEY,
|
|
561
575
|
systemProgram: SystemProgram.programId,
|
|
562
576
|
trader: lavarageProgram.provider.publicKey!,
|
|
563
|
-
tokenProgram:
|
|
577
|
+
tokenProgram: tokenProgram!,
|
|
564
578
|
randomAccountAsId: position.account.seed,
|
|
565
579
|
})
|
|
566
580
|
.instruction()
|
|
567
581
|
|
|
568
582
|
let repaySolIx: TransactionInstruction | null = null
|
|
569
583
|
let jupiterIxs: TransactionInstruction[] = []
|
|
570
|
-
if (jupInstruction.instructions ==
|
|
584
|
+
if (jupInstruction.instructions == undefined ) {
|
|
571
585
|
repaySolIx = await lavarageProgram.methods
|
|
572
586
|
.tradingCloseRepaySol(new BN(jupInstruction.quoteResponse.outAmount), new BN(9997))
|
|
573
587
|
.accountsStrict({
|
|
@@ -661,11 +675,17 @@ export const closeTradeV2 = async (lavarageProgram: Program<LavarageV2>, positio
|
|
|
661
675
|
|
|
662
676
|
const tokenAddressPubKey = new PublicKey(offer.account.collateralType)
|
|
663
677
|
|
|
678
|
+
const mintAccount = await lavarageProgram.provider.connection.getAccountInfo(offer.account.collateralType)
|
|
679
|
+
const tokenProgram = mintAccount?.owner
|
|
680
|
+
|
|
681
|
+
const quoteMintAccount = await lavarageProgram.provider.connection.getAccountInfo(quoteToken)
|
|
682
|
+
const quoteTokenProgram = quoteMintAccount?.owner
|
|
683
|
+
|
|
664
684
|
const positionAccountPDA = position.publicKey
|
|
665
685
|
|
|
666
|
-
const fromTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, positionAccountPDA, tokenAddressPubKey)
|
|
686
|
+
const fromTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, positionAccountPDA, tokenAddressPubKey, tokenProgram)
|
|
667
687
|
|
|
668
|
-
const toTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, lavarageProgram.provider.publicKey!, tokenAddressPubKey)
|
|
688
|
+
const toTokenAccount = await getTokenAccountOrCreateIfNotExists(lavarageProgram, lavarageProgram.provider.publicKey!, tokenAddressPubKey, tokenProgram)
|
|
669
689
|
|
|
670
690
|
const jupiterSellIx = jupInstruction!.instructions
|
|
671
691
|
|
|
@@ -702,12 +722,8 @@ export const closeTradeV2 = async (lavarageProgram: Program<LavarageV2>, positio
|
|
|
702
722
|
|
|
703
723
|
const addressLookupTableAccounts: AddressLookupTableAccount[] = []
|
|
704
724
|
|
|
705
|
-
|
|
706
|
-
|
|
707
725
|
const { blockhash } = await lavarageProgram.provider.connection.getLatestBlockhash('finalized')
|
|
708
726
|
|
|
709
|
-
|
|
710
|
-
|
|
711
727
|
const closePositionIx = await lavarageProgram.methods
|
|
712
728
|
.tradingCloseBorrowCollateral()
|
|
713
729
|
.accountsStrict({
|
|
@@ -720,14 +736,14 @@ export const closeTradeV2 = async (lavarageProgram: Program<LavarageV2>, positio
|
|
|
720
736
|
clock: SYSVAR_CLOCK_PUBKEY,
|
|
721
737
|
systemProgram: SystemProgram.programId,
|
|
722
738
|
trader: lavarageProgram.provider.publicKey!,
|
|
723
|
-
tokenProgram:
|
|
739
|
+
tokenProgram: tokenProgram!,
|
|
724
740
|
randomAccountAsId: position.account.seed,
|
|
725
741
|
})
|
|
726
742
|
.instruction()
|
|
727
743
|
|
|
728
744
|
let repaySolIx: TransactionInstruction | null = null
|
|
729
745
|
let jupiterIxs: TransactionInstruction[] = []
|
|
730
|
-
if (jupInstruction.instructions ==
|
|
746
|
+
if (jupInstruction.instructions == undefined ) {
|
|
731
747
|
repaySolIx = await lavarageProgram.methods
|
|
732
748
|
.tradingCloseRepaySol(new BN(jupInstruction.quoteResponse.outAmount), new BN(9997))
|
|
733
749
|
.accountsStrict({
|
|
@@ -738,10 +754,10 @@ export const closeTradeV2 = async (lavarageProgram: Program<LavarageV2>, positio
|
|
|
738
754
|
systemProgram: SystemProgram.programId,
|
|
739
755
|
clock: SYSVAR_CLOCK_PUBKEY,
|
|
740
756
|
randomAccountAsId: position.account.seed,
|
|
741
|
-
feeTokenAccount: getAssociatedTokenAddressSync(quoteToken, new PublicKey('6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF')),
|
|
742
|
-
fromTokenAccount: getAssociatedTokenAddressSync(quoteToken, lavarageProgram.provider.publicKey
|
|
743
|
-
tokenProgram:
|
|
744
|
-
toTokenAccount: getAssociatedTokenAddressSync(quoteToken, pool.account.nodeWallet, true),
|
|
757
|
+
feeTokenAccount: getAssociatedTokenAddressSync(quoteToken, new PublicKey('6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF'), false, quoteTokenProgram),
|
|
758
|
+
fromTokenAccount: getAssociatedTokenAddressSync(quoteToken, lavarageProgram.provider.publicKey!, false, quoteTokenProgram),
|
|
759
|
+
tokenProgram: quoteTokenProgram!,
|
|
760
|
+
toTokenAccount: getAssociatedTokenAddressSync(quoteToken, pool.account.nodeWallet, true, quoteTokenProgram),
|
|
745
761
|
mint: quoteToken,
|
|
746
762
|
}).remainingAccounts(partnerFeeRecipient ? [{
|
|
747
763
|
pubkey: partnerFeeRecipient,
|
|
@@ -760,10 +776,10 @@ export const closeTradeV2 = async (lavarageProgram: Program<LavarageV2>, positio
|
|
|
760
776
|
systemProgram: SystemProgram.programId,
|
|
761
777
|
clock: SYSVAR_CLOCK_PUBKEY,
|
|
762
778
|
randomAccountAsId: position.account.seed,
|
|
763
|
-
feeTokenAccount: getAssociatedTokenAddressSync(quoteToken, new PublicKey('6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF')),
|
|
764
|
-
fromTokenAccount: getAssociatedTokenAddressSync(quoteToken, lavarageProgram.provider.publicKey
|
|
765
|
-
tokenProgram:
|
|
766
|
-
toTokenAccount: getAssociatedTokenAddressSync(quoteToken, pool.account.nodeWallet, true),
|
|
779
|
+
feeTokenAccount: getAssociatedTokenAddressSync(quoteToken, new PublicKey('6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF'), false, quoteTokenProgram),
|
|
780
|
+
fromTokenAccount: getAssociatedTokenAddressSync(quoteToken, lavarageProgram.provider.publicKey!, false, quoteTokenProgram),
|
|
781
|
+
tokenProgram: quoteTokenProgram!,
|
|
782
|
+
toTokenAccount: getAssociatedTokenAddressSync(quoteToken, pool.account.nodeWallet, true, quoteTokenProgram),
|
|
767
783
|
mint: quoteToken,
|
|
768
784
|
}).remainingAccounts(partnerFeeRecipient ? [{
|
|
769
785
|
pubkey: partnerFeeRecipient,
|
|
@@ -785,12 +801,13 @@ export const closeTradeV2 = async (lavarageProgram: Program<LavarageV2>, positio
|
|
|
785
801
|
closePositionIx,
|
|
786
802
|
...jupiterIxs,
|
|
787
803
|
repaySolIx,
|
|
788
|
-
profitFeeMarkup && partnerFeeRecipient ?
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
804
|
+
profitFeeMarkup && partnerFeeRecipient ? createTransferInstruction(
|
|
805
|
+
getAssociatedTokenAddressSync(quoteToken, lavarageProgram.provider.publicKey!, false, quoteTokenProgram),
|
|
806
|
+
partnerFeeRecipient,
|
|
807
|
+
lavarageProgram.provider.publicKey!,
|
|
808
|
+
profit.toNumber() > 0 ? profit.mul(new BN(profitFeeMarkup * 1000)).div(new BN(1000)).toNumber() : 0,
|
|
809
|
+
[],
|
|
810
|
+
quoteTokenProgram!,
|
|
794
811
|
) : null,
|
|
795
812
|
].filter(i => !!i)
|
|
796
813
|
|