@drift-labs/vaults-sdk 0.1.191 → 0.1.192
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/lib/vaultClient.js +21 -10
- package/package.json +1 -1
- package/src/vaultClient.ts +35 -14
package/lib/vaultClient.js
CHANGED
|
@@ -515,6 +515,12 @@ class VaultClient {
|
|
|
515
515
|
if (!spotMarket) {
|
|
516
516
|
throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
|
|
517
517
|
}
|
|
518
|
+
const userAta = (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey, true);
|
|
519
|
+
let createAtaIx = undefined;
|
|
520
|
+
const userAtaExists = await this.driftClient.connection.getAccountInfo(userAta);
|
|
521
|
+
if (userAtaExists === null) {
|
|
522
|
+
createAtaIx = (0, spl_token_1.createAssociatedTokenAccountInstruction)(this.driftClient.wallet.publicKey, userAta, this.driftClient.wallet.publicKey, spotMarket.mint);
|
|
523
|
+
}
|
|
518
524
|
const accounts = {
|
|
519
525
|
vault: vaultDepositorAccount.vault,
|
|
520
526
|
vaultDepositor,
|
|
@@ -524,7 +530,7 @@ class VaultClient {
|
|
|
524
530
|
driftState: driftStateKey,
|
|
525
531
|
driftSpotMarketVault: spotMarket.vault,
|
|
526
532
|
driftSigner: this.driftClient.getStateAccount().signer,
|
|
527
|
-
userTokenAccount:
|
|
533
|
+
userTokenAccount: userAta,
|
|
528
534
|
driftProgram: this.driftClient.program.programId,
|
|
529
535
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
530
536
|
};
|
|
@@ -536,15 +542,20 @@ class VaultClient {
|
|
|
536
542
|
.rpc();
|
|
537
543
|
}
|
|
538
544
|
else {
|
|
539
|
-
const
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
545
|
+
const ixs = [
|
|
546
|
+
this.program.instruction.withdraw({
|
|
547
|
+
accounts: {
|
|
548
|
+
authority: this.driftClient.wallet.publicKey,
|
|
549
|
+
...accounts,
|
|
550
|
+
},
|
|
551
|
+
remainingAccounts,
|
|
552
|
+
}),
|
|
553
|
+
];
|
|
554
|
+
if (createAtaIx) {
|
|
555
|
+
ixs.unshift(createAtaIx);
|
|
556
|
+
}
|
|
557
|
+
return await this.createAndSendTxn(ixs, {
|
|
558
|
+
cuLimit: ((_a = txParams === null || txParams === void 0 ? void 0 : txParams.cuLimit) !== null && _a !== void 0 ? _a : 650000) + (createAtaIx ? 100000 : 0),
|
|
548
559
|
...txParams,
|
|
549
560
|
});
|
|
550
561
|
}
|
package/package.json
CHANGED
package/src/vaultClient.ts
CHANGED
|
@@ -29,6 +29,7 @@ import {
|
|
|
29
29
|
TransactionSignature,
|
|
30
30
|
} from '@solana/web3.js';
|
|
31
31
|
import {
|
|
32
|
+
createAssociatedTokenAccountInstruction,
|
|
32
33
|
getAssociatedTokenAddressSync,
|
|
33
34
|
TOKEN_PROGRAM_ID,
|
|
34
35
|
} from '@solana/spl-token';
|
|
@@ -825,6 +826,25 @@ export class VaultClient {
|
|
|
825
826
|
);
|
|
826
827
|
}
|
|
827
828
|
|
|
829
|
+
const userAta = getAssociatedTokenAddressSync(
|
|
830
|
+
spotMarket.mint,
|
|
831
|
+
this.driftClient.wallet.publicKey,
|
|
832
|
+
true
|
|
833
|
+
);
|
|
834
|
+
|
|
835
|
+
let createAtaIx: TransactionInstruction | undefined = undefined;
|
|
836
|
+
const userAtaExists = await this.driftClient.connection.getAccountInfo(
|
|
837
|
+
userAta
|
|
838
|
+
);
|
|
839
|
+
if (userAtaExists === null) {
|
|
840
|
+
createAtaIx = createAssociatedTokenAccountInstruction(
|
|
841
|
+
this.driftClient.wallet.publicKey,
|
|
842
|
+
userAta,
|
|
843
|
+
this.driftClient.wallet.publicKey,
|
|
844
|
+
spotMarket.mint
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
|
|
828
848
|
const accounts = {
|
|
829
849
|
vault: vaultDepositorAccount.vault,
|
|
830
850
|
vaultDepositor,
|
|
@@ -834,11 +854,7 @@ export class VaultClient {
|
|
|
834
854
|
driftState: driftStateKey,
|
|
835
855
|
driftSpotMarketVault: spotMarket.vault,
|
|
836
856
|
driftSigner: this.driftClient.getStateAccount().signer,
|
|
837
|
-
userTokenAccount:
|
|
838
|
-
spotMarket.mint,
|
|
839
|
-
this.driftClient.wallet.publicKey,
|
|
840
|
-
true
|
|
841
|
-
),
|
|
857
|
+
userTokenAccount: userAta,
|
|
842
858
|
driftProgram: this.driftClient.program.programId,
|
|
843
859
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
844
860
|
};
|
|
@@ -850,16 +866,21 @@ export class VaultClient {
|
|
|
850
866
|
.remainingAccounts(remainingAccounts)
|
|
851
867
|
.rpc();
|
|
852
868
|
} else {
|
|
853
|
-
const
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
869
|
+
const ixs = [
|
|
870
|
+
this.program.instruction.withdraw({
|
|
871
|
+
accounts: {
|
|
872
|
+
authority: this.driftClient.wallet.publicKey,
|
|
873
|
+
...accounts,
|
|
874
|
+
},
|
|
875
|
+
remainingAccounts,
|
|
876
|
+
}),
|
|
877
|
+
];
|
|
878
|
+
if (createAtaIx) {
|
|
879
|
+
ixs.unshift(createAtaIx);
|
|
880
|
+
}
|
|
860
881
|
|
|
861
|
-
return await this.createAndSendTxn(
|
|
862
|
-
cuLimit: txParams?.cuLimit ?? 650_000,
|
|
882
|
+
return await this.createAndSendTxn(ixs, {
|
|
883
|
+
cuLimit: (txParams?.cuLimit ?? 650_000) + (createAtaIx ? 100_000 : 0),
|
|
863
884
|
...txParams,
|
|
864
885
|
});
|
|
865
886
|
}
|