@drift-labs/sdk 2.49.0-beta.14 → 2.49.0-beta.16

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 CHANGED
@@ -1 +1 @@
1
- 2.49.0-beta.14
1
+ 2.49.0-beta.16
@@ -228,9 +228,12 @@ export declare class DriftClient {
228
228
  * @param subAccountId
229
229
  * @param name
230
230
  * @param fromSubAccountId
231
+ * @param referrerInfo
232
+ * @param donateAmount
233
+ * @param txParams
231
234
  * @returns
232
235
  */
233
- initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, marketIndex?: number, subAccountId?: number, name?: string, fromSubAccountId?: number, referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<[TransactionSignature, PublicKey]>;
236
+ initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, marketIndex?: number, subAccountId?: number, name?: string, fromSubAccountId?: number, referrerInfo?: ReferrerInfo, donateAmount?: BN, txParams?: TxParams): Promise<[TransactionSignature, PublicKey]>;
234
237
  initializeUserAccountForDevnet(subAccountId: number, name: string, marketIndex: number, tokenFaucet: TokenFaucet, amount: BN, referrerInfo?: ReferrerInfo): Promise<[TransactionSignature, PublicKey]>;
235
238
  /**
236
239
  * Withdraws from a user account. If deposit doesn't already exist, creates a borrow
@@ -619,6 +622,7 @@ export declare class DriftClient {
619
622
  settleRevenueToInsuranceFund(marketIndex: number): Promise<TransactionSignature>;
620
623
  resolvePerpPnlDeficit(spotMarketIndex: number, perpMarketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
621
624
  getResolvePerpPnlDeficitIx(spotMarketIndex: number, perpMarketIndex: number): Promise<TransactionInstruction>;
625
+ getDepositIntoSpotMarketRevenuePoolIx(marketIndex: number, amount: BN, userTokenAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
622
626
  depositIntoSpotMarketRevenuePool(marketIndex: number, amount: BN, userTokenAccountPublicKey: PublicKey): Promise<TransactionSignature>;
623
627
  getPerpMarketExtendedInfo(marketIndex: number): PerpMarketExtendedInfo;
624
628
  /**
@@ -1061,9 +1061,12 @@ class DriftClient {
1061
1061
  * @param subAccountId
1062
1062
  * @param name
1063
1063
  * @param fromSubAccountId
1064
+ * @param referrerInfo
1065
+ * @param donateAmount
1066
+ * @param txParams
1064
1067
  * @returns
1065
1068
  */
1066
- async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name, fromSubAccountId, referrerInfo, txParams) {
1069
+ async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name, fromSubAccountId, referrerInfo, donateAmount, txParams) {
1067
1070
  var _a;
1068
1071
  const ixs = [];
1069
1072
  const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
@@ -1083,10 +1086,19 @@ class DriftClient {
1083
1086
  const isFromSubaccount = fromSubAccountId !== null &&
1084
1087
  fromSubAccountId !== undefined &&
1085
1088
  !isNaN(fromSubAccountId);
1086
- const createWSOLTokenAccount = isSolMarket && userTokenAccount.equals(authority) && !isFromSubaccount;
1089
+ donateAmount = donateAmount ? donateAmount : numericConstants_1.ZERO;
1090
+ const createWSOLTokenAccount = (isSolMarket &&
1091
+ userTokenAccount.equals(authority) &&
1092
+ !isFromSubaccount) ||
1093
+ !donateAmount.eq(numericConstants_1.ZERO);
1094
+ const wSolAmount = isSolMarket ? amount.add(donateAmount) : donateAmount;
1095
+ let wsolTokenAccount;
1087
1096
  if (createWSOLTokenAccount) {
1088
- const { ixs: startIxs, pubkey } = await this.getWrappedSolAccountCreationIxs(amount, true);
1089
- userTokenAccount = pubkey;
1097
+ const { ixs: startIxs, pubkey } = await this.getWrappedSolAccountCreationIxs(wSolAmount, true);
1098
+ wsolTokenAccount = pubkey;
1099
+ if (isSolMarket) {
1100
+ userTokenAccount = pubkey;
1101
+ }
1090
1102
  ixs.push(...startIxs);
1091
1103
  }
1092
1104
  const depositCollateralIx = isFromSubaccount
@@ -1098,9 +1110,13 @@ class DriftClient {
1098
1110
  }
1099
1111
  }
1100
1112
  ixs.push(initializeUserAccountIx, depositCollateralIx);
1113
+ if (!donateAmount.eq(numericConstants_1.ZERO)) {
1114
+ const donateIx = await this.getDepositIntoSpotMarketRevenuePoolIx(1, donateAmount, wsolTokenAccount);
1115
+ ixs.push(donateIx);
1116
+ }
1101
1117
  // Close the wrapped sol account at the end of the transaction
1102
1118
  if (createWSOLTokenAccount) {
1103
- ixs.push((0, spl_token_1.createCloseAccountInstruction)(userTokenAccount, authority, authority, []));
1119
+ ixs.push((0, spl_token_1.createCloseAccountInstruction)(wsolTokenAccount, authority, authority, []));
1104
1120
  }
1105
1121
  const tx = await this.buildTransaction(ixs, params);
1106
1122
  const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
@@ -3237,9 +3253,9 @@ class DriftClient {
3237
3253
  remainingAccounts: remainingAccounts,
3238
3254
  });
3239
3255
  }
3240
- async depositIntoSpotMarketRevenuePool(marketIndex, amount, userTokenAccountPublicKey) {
3256
+ async getDepositIntoSpotMarketRevenuePoolIx(marketIndex, amount, userTokenAccountPublicKey) {
3241
3257
  const spotMarket = await this.getSpotMarketAccount(marketIndex);
3242
- const tx = await this.program.transaction.depositIntoSpotMarketRevenuePool(amount, {
3258
+ const ix = await this.program.instruction.depositIntoSpotMarketRevenuePool(amount, {
3243
3259
  accounts: {
3244
3260
  state: await this.getStatePublicKey(),
3245
3261
  spotMarket: spotMarket.pubkey,
@@ -3249,6 +3265,11 @@ class DriftClient {
3249
3265
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
3250
3266
  },
3251
3267
  });
3268
+ return ix;
3269
+ }
3270
+ async depositIntoSpotMarketRevenuePool(marketIndex, amount, userTokenAccountPublicKey) {
3271
+ const ix = await this.getDepositIntoSpotMarketRevenuePoolIx(marketIndex, amount, userTokenAccountPublicKey);
3272
+ const tx = await this.buildTransaction([ix]);
3252
3273
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
3253
3274
  return txSig;
3254
3275
  }
@@ -83,6 +83,7 @@ class OrderSubscriber {
83
83
  if (!this.mostRecentSlot || slot > this.mostRecentSlot) {
84
84
  this.mostRecentSlot = slot;
85
85
  }
86
+ this.eventEmitter.emit('updateReceived', new web3_js_1.PublicKey(key), slot, dataType);
86
87
  const slotAndUserAccount = this.usersAccounts.get(key);
87
88
  if (!slotAndUserAccount || slotAndUserAccount.slot <= slot) {
88
89
  let userAccount;
@@ -101,13 +102,14 @@ class OrderSubscriber {
101
102
  else {
102
103
  userAccount = data;
103
104
  }
105
+ this.eventEmitter.emit('userUpdated', userAccount, new web3_js_1.PublicKey(key), slot, dataType);
104
106
  const newOrders = userAccount.orders.filter((order) => {
105
107
  var _a;
106
108
  return order.slot.toNumber() > ((_a = slotAndUserAccount === null || slotAndUserAccount === void 0 ? void 0 : slotAndUserAccount.slot) !== null && _a !== void 0 ? _a : 0) &&
107
109
  order.slot.toNumber() <= slot;
108
110
  });
109
111
  if (newOrders.length > 0) {
110
- this.eventEmitter.emit('onUpdate', userAccount, newOrders, new web3_js_1.PublicKey(key), slot, dataType);
112
+ this.eventEmitter.emit('orderCreated', userAccount, newOrders, new web3_js_1.PublicKey(key), slot, dataType);
111
113
  }
112
114
  if (userAccount.hasOpenOrder) {
113
115
  this.usersAccounts.set(key, { slot, userAccount });
@@ -15,5 +15,7 @@ export type OrderSubscriberConfig = {
15
15
  };
16
16
  };
17
17
  export interface OrderSubscriberEvents {
18
- onUpdate: (account: UserAccount, updatedOrders: Order[], pubkey: PublicKey, slot: number, dataType: 'raw' | 'decoded') => void;
18
+ orderCreated: (account: UserAccount, updatedOrders: Order[], pubkey: PublicKey, slot: number, dataType: 'raw' | 'decoded') => void;
19
+ userUpdated: (account: UserAccount, pubkey: PublicKey, slot: number, dataType: 'raw' | 'decoded') => void;
20
+ updateReceived: (pubkey: PublicKey, slot: number, dataType: 'raw' | 'decoded') => void;
19
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.49.0-beta.14",
3
+ "version": "2.49.0-beta.16",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -1811,6 +1811,9 @@ export class DriftClient {
1811
1811
  * @param subAccountId
1812
1812
  * @param name
1813
1813
  * @param fromSubAccountId
1814
+ * @param referrerInfo
1815
+ * @param donateAmount
1816
+ * @param txParams
1814
1817
  * @returns
1815
1818
  */
1816
1819
  public async initializeUserAccountAndDepositCollateral(
@@ -1821,6 +1824,7 @@ export class DriftClient {
1821
1824
  name?: string,
1822
1825
  fromSubAccountId?: number,
1823
1826
  referrerInfo?: ReferrerInfo,
1827
+ donateAmount?: BN,
1824
1828
  txParams?: TxParams
1825
1829
  ): Promise<[TransactionSignature, PublicKey]> {
1826
1830
  const ixs = [];
@@ -1856,14 +1860,26 @@ export class DriftClient {
1856
1860
  fromSubAccountId !== undefined &&
1857
1861
  !isNaN(fromSubAccountId);
1858
1862
 
1863
+ donateAmount = donateAmount ? donateAmount : ZERO;
1864
+
1859
1865
  const createWSOLTokenAccount =
1860
- isSolMarket && userTokenAccount.equals(authority) && !isFromSubaccount;
1866
+ (isSolMarket &&
1867
+ userTokenAccount.equals(authority) &&
1868
+ !isFromSubaccount) ||
1869
+ !donateAmount.eq(ZERO);
1861
1870
 
1871
+ const wSolAmount = isSolMarket ? amount.add(donateAmount) : donateAmount;
1872
+
1873
+ let wsolTokenAccount: PublicKey;
1862
1874
  if (createWSOLTokenAccount) {
1863
1875
  const { ixs: startIxs, pubkey } =
1864
- await this.getWrappedSolAccountCreationIxs(amount, true);
1876
+ await this.getWrappedSolAccountCreationIxs(wSolAmount, true);
1865
1877
 
1866
- userTokenAccount = pubkey;
1878
+ wsolTokenAccount = pubkey;
1879
+
1880
+ if (isSolMarket) {
1881
+ userTokenAccount = pubkey;
1882
+ }
1867
1883
 
1868
1884
  ixs.push(...startIxs);
1869
1885
  }
@@ -1893,11 +1909,21 @@ export class DriftClient {
1893
1909
  }
1894
1910
  ixs.push(initializeUserAccountIx, depositCollateralIx);
1895
1911
 
1912
+ if (!donateAmount.eq(ZERO)) {
1913
+ const donateIx = await this.getDepositIntoSpotMarketRevenuePoolIx(
1914
+ 1,
1915
+ donateAmount,
1916
+ wsolTokenAccount
1917
+ );
1918
+
1919
+ ixs.push(donateIx);
1920
+ }
1921
+
1896
1922
  // Close the wrapped sol account at the end of the transaction
1897
1923
  if (createWSOLTokenAccount) {
1898
1924
  ixs.push(
1899
1925
  createCloseAccountInstruction(
1900
- userTokenAccount,
1926
+ wsolTokenAccount,
1901
1927
  authority,
1902
1928
  authority,
1903
1929
  []
@@ -5995,13 +6021,13 @@ export class DriftClient {
5995
6021
  );
5996
6022
  }
5997
6023
 
5998
- public async depositIntoSpotMarketRevenuePool(
6024
+ public async getDepositIntoSpotMarketRevenuePoolIx(
5999
6025
  marketIndex: number,
6000
6026
  amount: BN,
6001
6027
  userTokenAccountPublicKey: PublicKey
6002
- ): Promise<TransactionSignature> {
6028
+ ): Promise<TransactionInstruction> {
6003
6029
  const spotMarket = await this.getSpotMarketAccount(marketIndex);
6004
- const tx = await this.program.transaction.depositIntoSpotMarketRevenuePool(
6030
+ const ix = await this.program.instruction.depositIntoSpotMarketRevenuePool(
6005
6031
  amount,
6006
6032
  {
6007
6033
  accounts: {
@@ -6015,6 +6041,21 @@ export class DriftClient {
6015
6041
  }
6016
6042
  );
6017
6043
 
6044
+ return ix;
6045
+ }
6046
+
6047
+ public async depositIntoSpotMarketRevenuePool(
6048
+ marketIndex: number,
6049
+ amount: BN,
6050
+ userTokenAccountPublicKey: PublicKey
6051
+ ): Promise<TransactionSignature> {
6052
+ const ix = await this.getDepositIntoSpotMarketRevenuePoolIx(
6053
+ marketIndex,
6054
+ amount,
6055
+ userTokenAccountPublicKey
6056
+ );
6057
+ const tx = await this.buildTransaction([ix]);
6058
+
6018
6059
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
6019
6060
  return txSig;
6020
6061
  }
@@ -123,6 +123,13 @@ export class OrderSubscriber {
123
123
  this.mostRecentSlot = slot;
124
124
  }
125
125
 
126
+ this.eventEmitter.emit(
127
+ 'updateReceived',
128
+ new PublicKey(key),
129
+ slot,
130
+ dataType
131
+ );
132
+
126
133
  const slotAndUserAccount = this.usersAccounts.get(key);
127
134
  if (!slotAndUserAccount || slotAndUserAccount.slot <= slot) {
128
135
  let userAccount: UserAccount;
@@ -152,6 +159,14 @@ export class OrderSubscriber {
152
159
  userAccount = data as UserAccount;
153
160
  }
154
161
 
162
+ this.eventEmitter.emit(
163
+ 'userUpdated',
164
+ userAccount,
165
+ new PublicKey(key),
166
+ slot,
167
+ dataType
168
+ );
169
+
155
170
  const newOrders = userAccount.orders.filter(
156
171
  (order) =>
157
172
  order.slot.toNumber() > (slotAndUserAccount?.slot ?? 0) &&
@@ -159,7 +174,7 @@ export class OrderSubscriber {
159
174
  );
160
175
  if (newOrders.length > 0) {
161
176
  this.eventEmitter.emit(
162
- 'onUpdate',
177
+ 'orderCreated',
163
178
  userAccount,
164
179
  newOrders,
165
180
  new PublicKey(key),
@@ -19,11 +19,22 @@ export type OrderSubscriberConfig = {
19
19
  };
20
20
 
21
21
  export interface OrderSubscriberEvents {
22
- onUpdate: (
22
+ orderCreated: (
23
23
  account: UserAccount,
24
24
  updatedOrders: Order[],
25
25
  pubkey: PublicKey,
26
26
  slot: number,
27
27
  dataType: 'raw' | 'decoded'
28
28
  ) => void;
29
+ userUpdated: (
30
+ account: UserAccount,
31
+ pubkey: PublicKey,
32
+ slot: number,
33
+ dataType: 'raw' | 'decoded'
34
+ ) => void;
35
+ updateReceived: (
36
+ pubkey: PublicKey,
37
+ slot: number,
38
+ dataType: 'raw' | 'decoded'
39
+ ) => void;
29
40
  }