@drift-labs/sdk 2.31.1-beta.10 → 2.31.1-beta.12
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/VERSION +1 -1
- package/lib/driftClient.d.ts +24 -1
- package/lib/driftClient.js +134 -173
- package/lib/idl/drift.json +31 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/marinade/index.d.ts +11 -0
- package/lib/marinade/index.js +36 -0
- package/lib/marinade/types.d.ts +1963 -0
- package/lib/marinade/types.js +1965 -0
- package/package.json +1 -1
- package/src/driftClient.ts +255 -190
- package/src/idl/drift.json +31 -1
- package/src/index.ts +1 -0
- package/src/marinade/idl/idl.json +1962 -0
- package/src/marinade/index.ts +64 -0
- package/src/marinade/types.ts +3925 -0
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.31.1-beta.
|
|
1
|
+
2.31.1-beta.12
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ type RemainingAccountParams = {
|
|
|
19
19
|
userAccounts: UserAccount[];
|
|
20
20
|
writablePerpMarketIndexes?: number[];
|
|
21
21
|
writableSpotMarketIndexes?: number[];
|
|
22
|
-
readablePerpMarketIndex?: number;
|
|
22
|
+
readablePerpMarketIndex?: number | number[];
|
|
23
23
|
readableSpotMarketIndexes?: number[];
|
|
24
24
|
useMarketLastSlotCache?: boolean;
|
|
25
25
|
};
|
|
@@ -44,6 +44,8 @@ export declare class DriftClient {
|
|
|
44
44
|
txSender: TxSender;
|
|
45
45
|
perpMarketLastSlotCache: Map<number, number>;
|
|
46
46
|
spotMarketLastSlotCache: Map<number, number>;
|
|
47
|
+
mustIncludePerpMarketIndexes: Set<number>;
|
|
48
|
+
mustIncludeSpotMarketIndexes: Set<number>;
|
|
47
49
|
authority: PublicKey;
|
|
48
50
|
marketLookupTable: PublicKey;
|
|
49
51
|
lookupTableAccount: AddressLookupTableAccount;
|
|
@@ -162,7 +164,20 @@ export declare class DriftClient {
|
|
|
162
164
|
* @param amount
|
|
163
165
|
*/
|
|
164
166
|
convertToPricePrecision(amount: BN | number): BN;
|
|
167
|
+
/**
|
|
168
|
+
* Each drift instruction must include perp and sport market accounts in the ix remaining accounts.
|
|
169
|
+
* Use this function to force a subset of markets to be included in the remaining accounts for every ix
|
|
170
|
+
*
|
|
171
|
+
* @param perpMarketIndexes
|
|
172
|
+
* @param spotMarketIndexes
|
|
173
|
+
*/
|
|
174
|
+
mustIncludeMarketsInIx({ perpMarketIndexes, spotMarketIndexes, }: {
|
|
175
|
+
perpMarketIndexes: number[];
|
|
176
|
+
spotMarketIndexes: number[];
|
|
177
|
+
}): void;
|
|
165
178
|
getRemainingAccounts(params: RemainingAccountParams): AccountMeta[];
|
|
179
|
+
addPerpMarketToRemainingAccountMaps(marketIndex: number, writable: boolean, oracleAccountMap: Map<string, AccountMeta>, spotMarketAccountMap: Map<number, AccountMeta>, perpMarketAccountMap: Map<number, AccountMeta>): void;
|
|
180
|
+
addSpotMarketToRemainingAccountMaps(marketIndex: number, writable: boolean, oracleAccountMap: Map<string, AccountMeta>, spotMarketAccountMap: Map<number, AccountMeta>): void;
|
|
166
181
|
getRemainingAccountMapsForUsers(userAccounts: UserAccount[]): {
|
|
167
182
|
oracleAccountMap: Map<string, AccountMeta>;
|
|
168
183
|
spotMarketAccountMap: Map<number, AccountMeta>;
|
|
@@ -271,6 +286,8 @@ export declare class DriftClient {
|
|
|
271
286
|
marketIndex?: number;
|
|
272
287
|
direction?: PositionDirection;
|
|
273
288
|
}, placeOrderParams: OrderParams[], txParams?: TxParams): Promise<TransactionSignature>;
|
|
289
|
+
placeOrders(params: OrderParams[], txParams?: TxParams): Promise<TransactionSignature>;
|
|
290
|
+
getPlaceOrdersIx(params: OrderParams[]): Promise<TransactionInstruction>;
|
|
274
291
|
fillPerpOrder(userAccountPublicKey: PublicKey, user: UserAccount, order?: Pick<Order, 'marketIndex' | 'orderId'>, makerInfo?: MakerInfo | MakerInfo[], referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<TransactionSignature>;
|
|
275
292
|
getFillPerpOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Pick<Order, 'marketIndex' | 'orderId'>, makerInfo?: MakerInfo | MakerInfo[], referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
276
293
|
getRevertFillIx(): Promise<TransactionInstruction>;
|
|
@@ -328,6 +345,12 @@ export declare class DriftClient {
|
|
|
328
345
|
beginSwapIx: TransactionInstruction;
|
|
329
346
|
endSwapIx: TransactionInstruction;
|
|
330
347
|
}>;
|
|
348
|
+
stakeForMSOL({ amount }: {
|
|
349
|
+
amount: BN;
|
|
350
|
+
}): Promise<TxSigAndSlot>;
|
|
351
|
+
getStakeForMSOLIx({ amount, }: {
|
|
352
|
+
amount: BN;
|
|
353
|
+
}): Promise<TransactionInstruction[]>;
|
|
331
354
|
triggerOrder(userAccountPublicKey: PublicKey, user: UserAccount, order: Order, txParams?: TxParams): Promise<TransactionSignature>;
|
|
332
355
|
getTriggerOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order): Promise<TransactionInstruction>;
|
|
333
356
|
forceCancelOrders(userAccountPublicKey: PublicKey, user: UserAccount, txParams?: TxParams): Promise<TransactionSignature>;
|
package/lib/driftClient.js
CHANGED
|
@@ -52,6 +52,7 @@ const market_1 = require("./math/market");
|
|
|
52
52
|
const fetch_1 = require("./accounts/fetch");
|
|
53
53
|
const spotMarket_1 = require("./math/spotMarket");
|
|
54
54
|
const memcmp_1 = require("./memcmp");
|
|
55
|
+
const marinade_1 = require("./marinade");
|
|
55
56
|
/**
|
|
56
57
|
* # DriftClient
|
|
57
58
|
* This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
|
|
@@ -69,6 +70,8 @@ class DriftClient {
|
|
|
69
70
|
this._isSubscribed = false;
|
|
70
71
|
this.perpMarketLastSlotCache = new Map();
|
|
71
72
|
this.spotMarketLastSlotCache = new Map();
|
|
73
|
+
this.mustIncludePerpMarketIndexes = new Set();
|
|
74
|
+
this.mustIncludeSpotMarketIndexes = new Set();
|
|
72
75
|
this.connection = config.connection;
|
|
73
76
|
this.wallet = config.wallet;
|
|
74
77
|
this.opts = config.opts || anchor_1.AnchorProvider.defaultOptions();
|
|
@@ -716,6 +719,21 @@ class DriftClient {
|
|
|
716
719
|
amount = typeof amount === 'number' ? new anchor_1.BN(amount) : amount;
|
|
717
720
|
return amount.mul(numericConstants_1.PRICE_PRECISION);
|
|
718
721
|
}
|
|
722
|
+
/**
|
|
723
|
+
* Each drift instruction must include perp and sport market accounts in the ix remaining accounts.
|
|
724
|
+
* Use this function to force a subset of markets to be included in the remaining accounts for every ix
|
|
725
|
+
*
|
|
726
|
+
* @param perpMarketIndexes
|
|
727
|
+
* @param spotMarketIndexes
|
|
728
|
+
*/
|
|
729
|
+
mustIncludeMarketsInIx({ perpMarketIndexes, spotMarketIndexes, }) {
|
|
730
|
+
perpMarketIndexes.forEach((perpMarketIndex) => {
|
|
731
|
+
this.mustIncludePerpMarketIndexes.add(perpMarketIndex);
|
|
732
|
+
});
|
|
733
|
+
spotMarketIndexes.forEach((spotMarketIndex) => {
|
|
734
|
+
this.mustIncludeSpotMarketIndexes.add(spotMarketIndex);
|
|
735
|
+
});
|
|
736
|
+
}
|
|
719
737
|
getRemainingAccounts(params) {
|
|
720
738
|
var _a;
|
|
721
739
|
const { oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap } = this.getRemainingAccountMapsForUsers(params.userAccounts);
|
|
@@ -725,30 +743,7 @@ class DriftClient {
|
|
|
725
743
|
// if cache has more recent slot than user positions account slot, add market to remaining accounts
|
|
726
744
|
// otherwise remove from slot
|
|
727
745
|
if (slot > lastUserSlot) {
|
|
728
|
-
|
|
729
|
-
perpMarketAccountMap.set(marketIndex, {
|
|
730
|
-
pubkey: perpMarketAccount.pubkey,
|
|
731
|
-
isSigner: false,
|
|
732
|
-
isWritable: false,
|
|
733
|
-
});
|
|
734
|
-
oracleAccountMap.set(perpMarketAccount.amm.oracle.toString(), {
|
|
735
|
-
pubkey: perpMarketAccount.amm.oracle,
|
|
736
|
-
isSigner: false,
|
|
737
|
-
isWritable: false,
|
|
738
|
-
});
|
|
739
|
-
const spotMarketAccount = this.getSpotMarketAccount(perpMarketAccount.quoteSpotMarketIndex);
|
|
740
|
-
spotMarketAccountMap.set(perpMarketAccount.quoteSpotMarketIndex, {
|
|
741
|
-
pubkey: spotMarketAccount.pubkey,
|
|
742
|
-
isSigner: false,
|
|
743
|
-
isWritable: false,
|
|
744
|
-
});
|
|
745
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
746
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
747
|
-
pubkey: spotMarketAccount.oracle,
|
|
748
|
-
isSigner: false,
|
|
749
|
-
isWritable: false,
|
|
750
|
-
});
|
|
751
|
-
}
|
|
746
|
+
this.addPerpMarketToRemainingAccountMaps(marketIndex, false, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap);
|
|
752
747
|
}
|
|
753
748
|
else {
|
|
754
749
|
this.perpMarketLastSlotCache.delete(marketIndex);
|
|
@@ -758,19 +753,7 @@ class DriftClient {
|
|
|
758
753
|
// if cache has more recent slot than user positions account slot, add market to remaining accounts
|
|
759
754
|
// otherwise remove from slot
|
|
760
755
|
if (slot > lastUserSlot) {
|
|
761
|
-
|
|
762
|
-
spotMarketAccountMap.set(marketIndex, {
|
|
763
|
-
pubkey: spotMarketAccount.pubkey,
|
|
764
|
-
isSigner: false,
|
|
765
|
-
isWritable: false,
|
|
766
|
-
});
|
|
767
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
768
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
769
|
-
pubkey: spotMarketAccount.oracle,
|
|
770
|
-
isSigner: false,
|
|
771
|
-
isWritable: false,
|
|
772
|
-
});
|
|
773
|
-
}
|
|
756
|
+
this.addSpotMarketToRemainingAccountMaps(marketIndex, false, oracleAccountMap, spotMarketAccountMap);
|
|
774
757
|
}
|
|
775
758
|
else {
|
|
776
759
|
this.spotMarketLastSlotCache.delete(marketIndex);
|
|
@@ -778,91 +761,32 @@ class DriftClient {
|
|
|
778
761
|
}
|
|
779
762
|
}
|
|
780
763
|
if (params.readablePerpMarketIndex !== undefined) {
|
|
781
|
-
const
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
});
|
|
787
|
-
oracleAccountMap.set(perpMarketAccount.amm.oracle.toString(), {
|
|
788
|
-
pubkey: perpMarketAccount.amm.oracle,
|
|
789
|
-
isSigner: false,
|
|
790
|
-
isWritable: false,
|
|
791
|
-
});
|
|
792
|
-
const spotMarketAccount = this.getSpotMarketAccount(perpMarketAccount.quoteSpotMarketIndex);
|
|
793
|
-
spotMarketAccountMap.set(perpMarketAccount.quoteSpotMarketIndex, {
|
|
794
|
-
pubkey: spotMarketAccount.pubkey,
|
|
795
|
-
isSigner: false,
|
|
796
|
-
isWritable: false,
|
|
797
|
-
});
|
|
798
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
799
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
800
|
-
pubkey: spotMarketAccount.oracle,
|
|
801
|
-
isSigner: false,
|
|
802
|
-
isWritable: false,
|
|
803
|
-
});
|
|
764
|
+
const readablePerpMarketIndexes = Array.isArray(params.readablePerpMarketIndex)
|
|
765
|
+
? params.readablePerpMarketIndex
|
|
766
|
+
: [params.readablePerpMarketIndex];
|
|
767
|
+
for (const marketIndex of readablePerpMarketIndexes) {
|
|
768
|
+
this.addPerpMarketToRemainingAccountMaps(marketIndex, false, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap);
|
|
804
769
|
}
|
|
805
770
|
}
|
|
771
|
+
for (const perpMarketIndex of this.mustIncludePerpMarketIndexes.values()) {
|
|
772
|
+
this.addPerpMarketToRemainingAccountMaps(perpMarketIndex, false, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap);
|
|
773
|
+
}
|
|
806
774
|
if (params.readableSpotMarketIndexes !== undefined) {
|
|
807
775
|
for (const readableSpotMarketIndex of params.readableSpotMarketIndexes) {
|
|
808
|
-
|
|
809
|
-
spotMarketAccountMap.set(readableSpotMarketIndex, {
|
|
810
|
-
pubkey: spotMarketAccount.pubkey,
|
|
811
|
-
isSigner: false,
|
|
812
|
-
isWritable: false,
|
|
813
|
-
});
|
|
814
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
815
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
816
|
-
pubkey: spotMarketAccount.oracle,
|
|
817
|
-
isSigner: false,
|
|
818
|
-
isWritable: false,
|
|
819
|
-
});
|
|
820
|
-
}
|
|
776
|
+
this.addSpotMarketToRemainingAccountMaps(readableSpotMarketIndex, false, oracleAccountMap, spotMarketAccountMap);
|
|
821
777
|
}
|
|
822
778
|
}
|
|
779
|
+
for (const spotMarketIndex of this.mustIncludeSpotMarketIndexes.values()) {
|
|
780
|
+
this.addSpotMarketToRemainingAccountMaps(spotMarketIndex, false, oracleAccountMap, spotMarketAccountMap);
|
|
781
|
+
}
|
|
823
782
|
if (params.writablePerpMarketIndexes !== undefined) {
|
|
824
783
|
for (const writablePerpMarketIndex of params.writablePerpMarketIndexes) {
|
|
825
|
-
|
|
826
|
-
perpMarketAccountMap.set(writablePerpMarketIndex, {
|
|
827
|
-
pubkey: perpMarketAccount.pubkey,
|
|
828
|
-
isSigner: false,
|
|
829
|
-
isWritable: true,
|
|
830
|
-
});
|
|
831
|
-
oracleAccountMap.set(perpMarketAccount.amm.oracle.toString(), {
|
|
832
|
-
pubkey: perpMarketAccount.amm.oracle,
|
|
833
|
-
isSigner: false,
|
|
834
|
-
isWritable: false,
|
|
835
|
-
});
|
|
836
|
-
const spotMarketAccount = this.getSpotMarketAccount(perpMarketAccount.quoteSpotMarketIndex);
|
|
837
|
-
spotMarketAccountMap.set(perpMarketAccount.quoteSpotMarketIndex, {
|
|
838
|
-
pubkey: spotMarketAccount.pubkey,
|
|
839
|
-
isSigner: false,
|
|
840
|
-
isWritable: false,
|
|
841
|
-
});
|
|
842
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
843
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
844
|
-
pubkey: spotMarketAccount.oracle,
|
|
845
|
-
isSigner: false,
|
|
846
|
-
isWritable: false,
|
|
847
|
-
});
|
|
848
|
-
}
|
|
784
|
+
this.addPerpMarketToRemainingAccountMaps(writablePerpMarketIndex, true, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap);
|
|
849
785
|
}
|
|
850
786
|
}
|
|
851
787
|
if (params.writableSpotMarketIndexes !== undefined) {
|
|
852
788
|
for (const writableSpotMarketIndex of params.writableSpotMarketIndexes) {
|
|
853
|
-
|
|
854
|
-
spotMarketAccountMap.set(spotMarketAccount.marketIndex, {
|
|
855
|
-
pubkey: spotMarketAccount.pubkey,
|
|
856
|
-
isSigner: false,
|
|
857
|
-
isWritable: true,
|
|
858
|
-
});
|
|
859
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
860
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
861
|
-
pubkey: spotMarketAccount.oracle,
|
|
862
|
-
isSigner: false,
|
|
863
|
-
isWritable: false,
|
|
864
|
-
});
|
|
865
|
-
}
|
|
789
|
+
this.addSpotMarketToRemainingAccountMaps(writableSpotMarketIndex, true, oracleAccountMap, spotMarketAccountMap);
|
|
866
790
|
}
|
|
867
791
|
}
|
|
868
792
|
return [
|
|
@@ -871,6 +795,35 @@ class DriftClient {
|
|
|
871
795
|
...perpMarketAccountMap.values(),
|
|
872
796
|
];
|
|
873
797
|
}
|
|
798
|
+
addPerpMarketToRemainingAccountMaps(marketIndex, writable, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap) {
|
|
799
|
+
const perpMarketAccount = this.getPerpMarketAccount(marketIndex);
|
|
800
|
+
perpMarketAccountMap.set(marketIndex, {
|
|
801
|
+
pubkey: perpMarketAccount.pubkey,
|
|
802
|
+
isSigner: false,
|
|
803
|
+
isWritable: writable,
|
|
804
|
+
});
|
|
805
|
+
oracleAccountMap.set(perpMarketAccount.amm.oracle.toString(), {
|
|
806
|
+
pubkey: perpMarketAccount.amm.oracle,
|
|
807
|
+
isSigner: false,
|
|
808
|
+
isWritable: false,
|
|
809
|
+
});
|
|
810
|
+
this.addSpotMarketToRemainingAccountMaps(perpMarketAccount.quoteSpotMarketIndex, false, oracleAccountMap, spotMarketAccountMap);
|
|
811
|
+
}
|
|
812
|
+
addSpotMarketToRemainingAccountMaps(marketIndex, writable, oracleAccountMap, spotMarketAccountMap) {
|
|
813
|
+
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
814
|
+
spotMarketAccountMap.set(spotMarketAccount.marketIndex, {
|
|
815
|
+
pubkey: spotMarketAccount.pubkey,
|
|
816
|
+
isSigner: false,
|
|
817
|
+
isWritable: writable,
|
|
818
|
+
});
|
|
819
|
+
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
820
|
+
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
821
|
+
pubkey: spotMarketAccount.oracle,
|
|
822
|
+
isSigner: false,
|
|
823
|
+
isWritable: false,
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
}
|
|
874
827
|
getRemainingAccountMapsForUsers(userAccounts) {
|
|
875
828
|
const oracleAccountMap = new Map();
|
|
876
829
|
const spotMarketAccountMap = new Map();
|
|
@@ -878,63 +831,16 @@ class DriftClient {
|
|
|
878
831
|
for (const userAccount of userAccounts) {
|
|
879
832
|
for (const spotPosition of userAccount.spotPositions) {
|
|
880
833
|
if (!(0, spotPosition_1.isSpotPositionAvailable)(spotPosition)) {
|
|
881
|
-
|
|
882
|
-
spotMarketAccountMap.set(spotPosition.marketIndex, {
|
|
883
|
-
pubkey: spotMarket.pubkey,
|
|
884
|
-
isSigner: false,
|
|
885
|
-
isWritable: false,
|
|
886
|
-
});
|
|
887
|
-
if (!spotMarket.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
888
|
-
oracleAccountMap.set(spotMarket.oracle.toString(), {
|
|
889
|
-
pubkey: spotMarket.oracle,
|
|
890
|
-
isSigner: false,
|
|
891
|
-
isWritable: false,
|
|
892
|
-
});
|
|
893
|
-
}
|
|
834
|
+
this.addSpotMarketToRemainingAccountMaps(spotPosition.marketIndex, false, oracleAccountMap, spotMarketAccountMap);
|
|
894
835
|
if (!spotPosition.openAsks.eq(numericConstants_1.ZERO) ||
|
|
895
836
|
!spotPosition.openBids.eq(numericConstants_1.ZERO)) {
|
|
896
|
-
|
|
897
|
-
spotMarketAccountMap.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX, {
|
|
898
|
-
pubkey: quoteSpotMarket.pubkey,
|
|
899
|
-
isSigner: false,
|
|
900
|
-
isWritable: false,
|
|
901
|
-
});
|
|
902
|
-
if (!quoteSpotMarket.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
903
|
-
oracleAccountMap.set(quoteSpotMarket.oracle.toString(), {
|
|
904
|
-
pubkey: quoteSpotMarket.oracle,
|
|
905
|
-
isSigner: false,
|
|
906
|
-
isWritable: false,
|
|
907
|
-
});
|
|
908
|
-
}
|
|
837
|
+
this.addSpotMarketToRemainingAccountMaps(numericConstants_1.QUOTE_SPOT_MARKET_INDEX, false, oracleAccountMap, spotMarketAccountMap);
|
|
909
838
|
}
|
|
910
839
|
}
|
|
911
840
|
}
|
|
912
841
|
for (const position of userAccount.perpPositions) {
|
|
913
842
|
if (!(0, position_1.positionIsAvailable)(position)) {
|
|
914
|
-
|
|
915
|
-
perpMarketAccountMap.set(position.marketIndex, {
|
|
916
|
-
pubkey: perpMarketAccount.pubkey,
|
|
917
|
-
isWritable: false,
|
|
918
|
-
isSigner: false,
|
|
919
|
-
});
|
|
920
|
-
oracleAccountMap.set(perpMarketAccount.amm.oracle.toString(), {
|
|
921
|
-
pubkey: perpMarketAccount.amm.oracle,
|
|
922
|
-
isWritable: false,
|
|
923
|
-
isSigner: false,
|
|
924
|
-
});
|
|
925
|
-
const spotMarketAccount = this.getSpotMarketAccount(perpMarketAccount.quoteSpotMarketIndex);
|
|
926
|
-
spotMarketAccountMap.set(perpMarketAccount.quoteSpotMarketIndex, {
|
|
927
|
-
pubkey: spotMarketAccount.pubkey,
|
|
928
|
-
isSigner: false,
|
|
929
|
-
isWritable: false,
|
|
930
|
-
});
|
|
931
|
-
if (!spotMarketAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
932
|
-
oracleAccountMap.set(spotMarketAccount.oracle.toString(), {
|
|
933
|
-
pubkey: spotMarketAccount.oracle,
|
|
934
|
-
isSigner: false,
|
|
935
|
-
isWritable: false,
|
|
936
|
-
});
|
|
937
|
-
}
|
|
843
|
+
this.addPerpMarketToRemainingAccountMaps(position.marketIndex, false, oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap);
|
|
938
844
|
}
|
|
939
845
|
}
|
|
940
846
|
}
|
|
@@ -1645,23 +1551,45 @@ class DriftClient {
|
|
|
1645
1551
|
});
|
|
1646
1552
|
}
|
|
1647
1553
|
async cancelAndPlaceOrders(cancelOrderParams, placeOrderParams, txParams) {
|
|
1648
|
-
const
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1554
|
+
const ixs = [
|
|
1555
|
+
await this.getCancelOrdersIx(cancelOrderParams.marketType, cancelOrderParams.marketIndex, cancelOrderParams.direction),
|
|
1556
|
+
await this.getPlaceOrdersIx(placeOrderParams),
|
|
1557
|
+
];
|
|
1558
|
+
const tx = await this.buildTransaction(ixs, txParams);
|
|
1559
|
+
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
1560
|
+
return txSig;
|
|
1561
|
+
}
|
|
1562
|
+
async placeOrders(params, txParams) {
|
|
1563
|
+
const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceOrdersIx(params), txParams), [], this.opts);
|
|
1564
|
+
return txSig;
|
|
1565
|
+
}
|
|
1566
|
+
async getPlaceOrdersIx(params) {
|
|
1567
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1568
|
+
const readablePerpMarketIndex = [];
|
|
1569
|
+
const readableSpotMarketIndexes = [];
|
|
1570
|
+
for (const param of params) {
|
|
1571
|
+
if ((0, types_1.isVariant)(param.marketType, 'perp')) {
|
|
1572
|
+
readablePerpMarketIndex.push(param.marketIndex);
|
|
1657
1573
|
}
|
|
1658
1574
|
else {
|
|
1659
|
-
|
|
1575
|
+
readableSpotMarketIndexes.push(param.marketIndex);
|
|
1660
1576
|
}
|
|
1661
|
-
tx.add(ix);
|
|
1662
1577
|
}
|
|
1663
|
-
const
|
|
1664
|
-
|
|
1578
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1579
|
+
userAccounts: [this.getUserAccount()],
|
|
1580
|
+
readablePerpMarketIndex,
|
|
1581
|
+
readableSpotMarketIndexes,
|
|
1582
|
+
useMarketLastSlotCache: true,
|
|
1583
|
+
});
|
|
1584
|
+
return await this.program.instruction.placeOrders(params, {
|
|
1585
|
+
accounts: {
|
|
1586
|
+
state: await this.getStatePublicKey(),
|
|
1587
|
+
user: userAccountPublicKey,
|
|
1588
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
1589
|
+
authority: this.wallet.publicKey,
|
|
1590
|
+
},
|
|
1591
|
+
remainingAccounts,
|
|
1592
|
+
});
|
|
1665
1593
|
}
|
|
1666
1594
|
async fillPerpOrder(userAccountPublicKey, user, order, makerInfo, referrerInfo, txParams) {
|
|
1667
1595
|
const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getFillPerpOrderIx(userAccountPublicKey, user, order, makerInfo, referrerInfo), txParams), [], this.opts);
|
|
@@ -2112,6 +2040,39 @@ class DriftClient {
|
|
|
2112
2040
|
});
|
|
2113
2041
|
return { beginSwapIx, endSwapIx };
|
|
2114
2042
|
}
|
|
2043
|
+
async stakeForMSOL({ amount }) {
|
|
2044
|
+
const ixs = await this.getStakeForMSOLIx({ amount });
|
|
2045
|
+
const tx = await this.buildTransaction(ixs);
|
|
2046
|
+
return this.sendTransaction(tx);
|
|
2047
|
+
}
|
|
2048
|
+
async getStakeForMSOLIx({ amount, }) {
|
|
2049
|
+
const wSOLMint = this.getSpotMarketAccount(1).mint;
|
|
2050
|
+
const mSOLAccount = await this.getAssociatedTokenAccount(2);
|
|
2051
|
+
const wSOLAccount = await this.getAssociatedTokenAccount(1, false);
|
|
2052
|
+
const wSOLAccountExists = await this.checkIfAccountExists(wSOLAccount);
|
|
2053
|
+
const closeWSOLIx = (0, spl_token_1.createCloseAccountInstruction)(wSOLAccount, this.wallet.publicKey, this.wallet.publicKey);
|
|
2054
|
+
const createWSOLIx = await this.createAssociatedTokenAccountIdempotentInstruction(wSOLAccount, this.wallet.publicKey, this.wallet.publicKey, wSOLMint);
|
|
2055
|
+
const { beginSwapIx, endSwapIx } = await this.getSwapIx({
|
|
2056
|
+
inMarketIndex: 1,
|
|
2057
|
+
outMarketIndex: 2,
|
|
2058
|
+
amountIn: amount,
|
|
2059
|
+
inTokenAccount: wSOLAccount,
|
|
2060
|
+
outTokenAccount: mSOLAccount,
|
|
2061
|
+
});
|
|
2062
|
+
const program = (0, marinade_1.getMarinadeFinanceProgram)(this.provider);
|
|
2063
|
+
const depositIx = await (0, marinade_1.getMarinadeDepositIx)({
|
|
2064
|
+
program,
|
|
2065
|
+
mSOLAccount: mSOLAccount,
|
|
2066
|
+
transferFrom: this.wallet.publicKey,
|
|
2067
|
+
amount,
|
|
2068
|
+
});
|
|
2069
|
+
const ixs = [];
|
|
2070
|
+
if (!wSOLAccountExists) {
|
|
2071
|
+
ixs.push(createWSOLIx);
|
|
2072
|
+
}
|
|
2073
|
+
ixs.push(beginSwapIx, closeWSOLIx, depositIx, createWSOLIx, endSwapIx);
|
|
2074
|
+
return ixs;
|
|
2075
|
+
}
|
|
2115
2076
|
async triggerOrder(userAccountPublicKey, user, order, txParams) {
|
|
2116
2077
|
const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getTriggerOrderIx(userAccountPublicKey, user, order), txParams), [], this.opts);
|
|
2117
2078
|
return txSig;
|
package/lib/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.31.1-beta.
|
|
2
|
+
"version": "2.31.1-beta.11",
|
|
3
3
|
"name": "drift",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
@@ -710,6 +710,36 @@
|
|
|
710
710
|
}
|
|
711
711
|
]
|
|
712
712
|
},
|
|
713
|
+
{
|
|
714
|
+
"name": "placeOrders",
|
|
715
|
+
"accounts": [
|
|
716
|
+
{
|
|
717
|
+
"name": "state",
|
|
718
|
+
"isMut": false,
|
|
719
|
+
"isSigner": false
|
|
720
|
+
},
|
|
721
|
+
{
|
|
722
|
+
"name": "user",
|
|
723
|
+
"isMut": true,
|
|
724
|
+
"isSigner": false
|
|
725
|
+
},
|
|
726
|
+
{
|
|
727
|
+
"name": "authority",
|
|
728
|
+
"isMut": false,
|
|
729
|
+
"isSigner": true
|
|
730
|
+
}
|
|
731
|
+
],
|
|
732
|
+
"args": [
|
|
733
|
+
{
|
|
734
|
+
"name": "params",
|
|
735
|
+
"type": {
|
|
736
|
+
"vec": {
|
|
737
|
+
"defined": "OrderParams"
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
]
|
|
742
|
+
},
|
|
713
743
|
{
|
|
714
744
|
"name": "beginSwap",
|
|
715
745
|
"accounts": [
|
package/lib/index.d.ts
CHANGED
|
@@ -45,6 +45,7 @@ export * from './math/orders';
|
|
|
45
45
|
export * from './math/repeg';
|
|
46
46
|
export * from './math/margin';
|
|
47
47
|
export * from './math/insurance';
|
|
48
|
+
export * from './marinade';
|
|
48
49
|
export * from './orderParams';
|
|
49
50
|
export * from './slot/SlotSubscriber';
|
|
50
51
|
export * from './wallet';
|
package/lib/index.js
CHANGED
|
@@ -68,6 +68,7 @@ __exportStar(require("./math/orders"), exports);
|
|
|
68
68
|
__exportStar(require("./math/repeg"), exports);
|
|
69
69
|
__exportStar(require("./math/margin"), exports);
|
|
70
70
|
__exportStar(require("./math/insurance"), exports);
|
|
71
|
+
__exportStar(require("./marinade"), exports);
|
|
71
72
|
__exportStar(require("./orderParams"), exports);
|
|
72
73
|
__exportStar(require("./slot/SlotSubscriber"), exports);
|
|
73
74
|
__exportStar(require("./wallet"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AnchorProvider, BN, Program } from '@coral-xyz/anchor';
|
|
2
|
+
import { MarinadeFinance } from './types';
|
|
3
|
+
import { PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
4
|
+
export declare function getMarinadeFinanceProgram(provider: AnchorProvider): Program<MarinadeFinance>;
|
|
5
|
+
export declare function getMarinadeDepositIx({ program, amount, mSOLAccount, transferFrom, }: {
|
|
6
|
+
amount: BN;
|
|
7
|
+
mSOLAccount: PublicKey;
|
|
8
|
+
transferFrom: PublicKey;
|
|
9
|
+
program: Program<MarinadeFinance>;
|
|
10
|
+
}): Promise<TransactionInstruction>;
|
|
11
|
+
export declare function getMarinadeMSolPrice(program: Program<MarinadeFinance>): Promise<number>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMarinadeMSolPrice = exports.getMarinadeDepositIx = exports.getMarinadeFinanceProgram = void 0;
|
|
4
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
7
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
8
|
+
const marinadeFinanceProgramId = new web3_js_1.PublicKey('MarBmsSgKXdrN1egZf5sqe1TMai9K1rChYNDJgjq7aD');
|
|
9
|
+
function getMarinadeFinanceProgram(provider) {
|
|
10
|
+
return new anchor_1.Program(types_1.IDL, marinadeFinanceProgramId, provider);
|
|
11
|
+
}
|
|
12
|
+
exports.getMarinadeFinanceProgram = getMarinadeFinanceProgram;
|
|
13
|
+
function getMarinadeDepositIx({ program, amount, mSOLAccount, transferFrom, }) {
|
|
14
|
+
return program.methods
|
|
15
|
+
.deposit(amount)
|
|
16
|
+
.accountsStrict({
|
|
17
|
+
reservePda: new web3_js_1.PublicKey('Du3Ysj1wKbxPKkuPPnvzQLQh8oMSVifs3jGZjJWXFmHN'),
|
|
18
|
+
state: new web3_js_1.PublicKey('8szGkuLTAux9XMgZ2vtY39jVSowEcpBfFfD8hXSEqdGC'),
|
|
19
|
+
msolMint: new web3_js_1.PublicKey('mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So'),
|
|
20
|
+
msolMintAuthority: new web3_js_1.PublicKey('3JLPCS1qM2zRw3Dp6V4hZnYHd4toMNPkNesXdX9tg6KM'),
|
|
21
|
+
liqPoolMsolLegAuthority: new web3_js_1.PublicKey('EyaSjUtSgo9aRD1f8LWXwdvkpDTmXAW54yoSHZRF14WL'),
|
|
22
|
+
liqPoolMsolLeg: new web3_js_1.PublicKey('7GgPYjS5Dza89wV6FpZ23kUJRG5vbQ1GM25ezspYFSoE'),
|
|
23
|
+
liqPoolSolLegPda: new web3_js_1.PublicKey('UefNb6z6yvArqe4cJHTXCqStRsKmWhGxnZzuHbikP5Q'),
|
|
24
|
+
mintTo: mSOLAccount,
|
|
25
|
+
transferFrom,
|
|
26
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
27
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
28
|
+
})
|
|
29
|
+
.instruction();
|
|
30
|
+
}
|
|
31
|
+
exports.getMarinadeDepositIx = getMarinadeDepositIx;
|
|
32
|
+
async function getMarinadeMSolPrice(program) {
|
|
33
|
+
const state = await program.account.state.fetch(new web3_js_1.PublicKey('8szGkuLTAux9XMgZ2vtY39jVSowEcpBfFfD8hXSEqdGC'));
|
|
34
|
+
return state.msolPrice.toNumber() / 4294967296;
|
|
35
|
+
}
|
|
36
|
+
exports.getMarinadeMSolPrice = getMarinadeMSolPrice;
|