@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.6

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.
@@ -11,6 +11,10 @@ import {
11
11
  BankAccount,
12
12
  UserBankBalance,
13
13
  MakerInfo,
14
+ TakerInfo,
15
+ OptionalOrderParams,
16
+ DefaultOrderParams,
17
+ OrderType,
14
18
  } from './types';
15
19
  import * as anchor from '@project-serum/anchor';
16
20
  import clearingHouseIDL from './idl/clearing_house.json';
@@ -52,7 +56,6 @@ import { WebSocketClearingHouseAccountSubscriber } from './accounts/webSocketCle
52
56
  import { RetryTxSender } from './tx/retryTxSender';
53
57
  import { ClearingHouseUser } from './clearingHouseUser';
54
58
  import { ClearingHouseUserAccountSubscriptionConfig } from './clearingHouseUserConfig';
55
- import { getMarketOrderParams } from './orderParams';
56
59
  import { getMarketsBanksAndOraclesForSubscription } from './config';
57
60
 
58
61
  /**
@@ -561,13 +564,19 @@ export class ClearingHouse {
561
564
  writableBankIndex: bankIndex,
562
565
  });
563
566
  } else {
564
- remainingAccounts = [
565
- {
566
- pubkey: this.getBankAccount(bankIndex).pubkey,
567
+ const bankAccount = this.getBankAccount(bankIndex);
568
+ if (!bankAccount.oracle.equals(PublicKey.default)) {
569
+ remainingAccounts.push({
570
+ pubkey: bankAccount.oracle,
567
571
  isSigner: false,
568
- isWritable: true,
569
- },
570
- ];
572
+ isWritable: false,
573
+ });
574
+ }
575
+ remainingAccounts.push({
576
+ pubkey: bankAccount.pubkey,
577
+ isSigner: false,
578
+ isWritable: true,
579
+ });
571
580
  }
572
581
 
573
582
  const bank = this.getBankAccount(bankIndex);
@@ -798,20 +807,17 @@ export class ClearingHouse {
798
807
  marketIndex: BN,
799
808
  limitPrice?: BN
800
809
  ): Promise<TransactionSignature> {
801
- return await this.placeAndTake(
802
- getMarketOrderParams(
803
- marketIndex,
804
- direction,
805
- ZERO,
806
- amount,
807
- false,
808
- limitPrice
809
- )
810
- );
810
+ return await this.placeAndTake({
811
+ orderType: OrderType.MARKET,
812
+ marketIndex,
813
+ direction,
814
+ baseAssetAmount: amount,
815
+ price: limitPrice,
816
+ });
811
817
  }
812
818
 
813
819
  public async placeOrder(
814
- orderParams: OrderParams
820
+ orderParams: OptionalOrderParams
815
821
  ): Promise<TransactionSignature> {
816
822
  const { txSig, slot } = await this.txSender.send(
817
823
  wrapInTx(await this.getPlaceOrderIx(orderParams)),
@@ -822,9 +828,14 @@ export class ClearingHouse {
822
828
  return txSig;
823
829
  }
824
830
 
831
+ getOrderParams(optionalOrderParams: OptionalOrderParams): OrderParams {
832
+ return Object.assign({}, DefaultOrderParams, optionalOrderParams);
833
+ }
834
+
825
835
  public async getPlaceOrderIx(
826
- orderParams: OrderParams
836
+ orderParams: OptionalOrderParams
827
837
  ): Promise<TransactionInstruction> {
838
+ orderParams = this.getOrderParams(orderParams);
828
839
  const userAccountPublicKey = await this.getUserAccountPublicKey();
829
840
 
830
841
  const remainingAccounts = this.getRemainingAccounts({
@@ -967,48 +978,64 @@ export class ClearingHouse {
967
978
  const marketIndex = order.marketIndex;
968
979
  const marketAccount = this.getMarketAccount(marketIndex);
969
980
 
970
- const bankAccountInfos = [
971
- {
972
- pubkey: this.getQuoteAssetBankAccount().pubkey,
973
- isSigner: false,
974
- isWritable: true,
975
- },
976
- ];
977
- const marketAccountInfos = [
978
- {
979
- pubkey: marketAccount.pubkey,
980
- isWritable: true,
981
- isSigner: false,
982
- },
983
- ];
984
- const oracleAccountInfos = [
985
- {
986
- pubkey: marketAccount.amm.oracle,
987
- isWritable: false,
988
- isSigner: false,
989
- },
990
- ];
981
+ const oracleAccountMap = new Map<string, AccountMeta>();
982
+ const bankAccountMap = new Map<number, AccountMeta>();
983
+ const marketAccountMap = new Map<number, AccountMeta>();
984
+
985
+ marketAccountMap.set(marketIndex.toNumber(), {
986
+ pubkey: marketAccount.pubkey,
987
+ isWritable: true,
988
+ isSigner: false,
989
+ });
990
+ oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
991
+ pubkey: marketAccount.amm.oracle,
992
+ isWritable: false,
993
+ isSigner: false,
994
+ });
995
+
996
+ for (const bankBalance of userAccount.bankBalances) {
997
+ if (!bankBalance.balance.eq(ZERO)) {
998
+ const bankAccount = this.getBankAccount(bankBalance.bankIndex);
999
+ bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
1000
+ pubkey: bankAccount.pubkey,
1001
+ isSigner: false,
1002
+ isWritable: false,
1003
+ });
1004
+
1005
+ if (!bankAccount.oracle.equals(PublicKey.default)) {
1006
+ oracleAccountMap.set(bankAccount.oracle.toString(), {
1007
+ pubkey: bankAccount.oracle,
1008
+ isSigner: false,
1009
+ isWritable: false,
1010
+ });
1011
+ }
1012
+ }
1013
+ }
1014
+
991
1015
  for (const position of userAccount.positions) {
992
1016
  if (
993
1017
  !positionIsAvailable(position) &&
994
1018
  !position.marketIndex.eq(order.marketIndex)
995
1019
  ) {
996
1020
  const market = this.getMarketAccount(position.marketIndex);
997
- marketAccountInfos.push({
1021
+ marketAccountMap.set(position.marketIndex.toNumber(), {
998
1022
  pubkey: market.pubkey,
999
- isWritable: false,
1023
+ isWritable: true,
1000
1024
  isSigner: false,
1001
1025
  });
1002
- oracleAccountInfos.push({
1026
+ oracleAccountMap.set(market.amm.oracle.toString(), {
1003
1027
  pubkey: market.amm.oracle,
1004
1028
  isWritable: false,
1005
1029
  isSigner: false,
1006
1030
  });
1007
1031
  }
1008
1032
  }
1009
- const remainingAccounts = oracleAccountInfos.concat(
1010
- bankAccountInfos.concat(marketAccountInfos)
1011
- );
1033
+
1034
+ const remainingAccounts = [
1035
+ ...oracleAccountMap.values(),
1036
+ ...bankAccountMap.values(),
1037
+ ...marketAccountMap.values(),
1038
+ ];
1012
1039
 
1013
1040
  if (makerInfo) {
1014
1041
  remainingAccounts.push({
@@ -1020,6 +1047,7 @@ export class ClearingHouse {
1020
1047
 
1021
1048
  const orderId = order.orderId;
1022
1049
  const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
1050
+
1023
1051
  return await this.program.instruction.fillOrder(orderId, makerOrderId, {
1024
1052
  accounts: {
1025
1053
  state: await this.getStatePublicKey(),
@@ -1110,7 +1138,7 @@ export class ClearingHouse {
1110
1138
  }
1111
1139
 
1112
1140
  public async placeAndTake(
1113
- orderParams: OrderParams,
1141
+ orderParams: OptionalOrderParams,
1114
1142
  makerInfo?: MakerInfo
1115
1143
  ): Promise<TransactionSignature> {
1116
1144
  const { txSig, slot } = await this.txSender.send(
@@ -1123,9 +1151,10 @@ export class ClearingHouse {
1123
1151
  }
1124
1152
 
1125
1153
  public async getPlaceAndTakeIx(
1126
- orderParams: OrderParams,
1154
+ orderParams: OptionalOrderParams,
1127
1155
  makerInfo?: MakerInfo
1128
1156
  ): Promise<TransactionInstruction> {
1157
+ orderParams = this.getOrderParams(orderParams);
1129
1158
  const userAccountPublicKey = await this.getUserAccountPublicKey();
1130
1159
 
1131
1160
  const remainingAccounts = this.getRemainingAccounts({
@@ -1157,6 +1186,55 @@ export class ClearingHouse {
1157
1186
  );
1158
1187
  }
1159
1188
 
1189
+ public async placeAndMake(
1190
+ orderParams: OptionalOrderParams,
1191
+ takerInfo: TakerInfo
1192
+ ): Promise<TransactionSignature> {
1193
+ const { txSig, slot } = await this.txSender.send(
1194
+ wrapInTx(await this.getPlaceAndMakeIx(orderParams, takerInfo)),
1195
+ [],
1196
+ this.opts
1197
+ );
1198
+
1199
+ this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
1200
+
1201
+ return txSig;
1202
+ }
1203
+
1204
+ public async getPlaceAndMakeIx(
1205
+ orderParams: OptionalOrderParams,
1206
+ takerInfo: TakerInfo
1207
+ ): Promise<TransactionInstruction> {
1208
+ orderParams = this.getOrderParams(orderParams);
1209
+ const userAccountPublicKey = await this.getUserAccountPublicKey();
1210
+
1211
+ const remainingAccounts = this.getRemainingAccounts({
1212
+ writableMarketIndex: orderParams.marketIndex,
1213
+ writableBankIndex: QUOTE_ASSET_BANK_INDEX,
1214
+ });
1215
+
1216
+ const takerOrderId = takerInfo!.order!.orderId;
1217
+ remainingAccounts.push({
1218
+ pubkey: takerInfo.taker,
1219
+ isSigner: false,
1220
+ isWritable: true,
1221
+ });
1222
+
1223
+ return await this.program.instruction.placeAndMake(
1224
+ orderParams,
1225
+ takerOrderId,
1226
+ {
1227
+ accounts: {
1228
+ state: await this.getStatePublicKey(),
1229
+ user: userAccountPublicKey,
1230
+ taker: takerInfo.taker,
1231
+ authority: this.wallet.publicKey,
1232
+ },
1233
+ remainingAccounts,
1234
+ }
1235
+ );
1236
+ }
1237
+
1160
1238
  /**
1161
1239
  * Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
1162
1240
  * @param marketIndex
@@ -1168,16 +1246,13 @@ export class ClearingHouse {
1168
1246
  throw Error(`No position in market ${marketIndex.toString()}`);
1169
1247
  }
1170
1248
 
1171
- return await this.placeAndTake(
1172
- getMarketOrderParams(
1173
- marketIndex,
1174
- findDirectionToClose(userPosition),
1175
- ZERO,
1176
- userPosition.baseAssetAmount,
1177
- true,
1178
- undefined
1179
- )
1180
- );
1249
+ return await this.placeAndTake({
1250
+ orderType: OrderType.MARKET,
1251
+ marketIndex,
1252
+ direction: findDirectionToClose(userPosition),
1253
+ baseAssetAmount: userPosition.baseAssetAmount,
1254
+ reduceOnly: true,
1255
+ });
1181
1256
  }
1182
1257
 
1183
1258
  public async settlePNLs(
package/src/config.ts CHANGED
@@ -28,7 +28,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
28
28
  devnet: {
29
29
  ENV: 'devnet',
30
30
  PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
31
- CLEARING_HOUSE_PROGRAM_ID: '9jwr5nC2f9yAraXrg4UzHXmCX3vi9FQkjD6p9e8bRqNa',
31
+ CLEARING_HOUSE_PROGRAM_ID: 'BMow898PH56jD8z4EaqxicoGXkR1HhN17qrER6Uc4AYq',
32
32
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
33
33
  MARKETS: DevnetMarkets,
34
34
  BANKS: DevnetBanks,
@@ -141,6 +141,10 @@
141
141
  {
142
142
  "name": "maintenanceLiabilityWeight",
143
143
  "type": "u128"
144
+ },
145
+ {
146
+ "name": "imfFactor",
147
+ "type": "u128"
144
148
  }
145
149
  ]
146
150
  },
@@ -534,7 +538,7 @@
534
538
  },
535
539
  {
536
540
  "name": "taker",
537
- "isMut": false,
541
+ "isMut": true,
538
542
  "isSigner": false
539
543
  },
540
544
  {
@@ -716,7 +720,7 @@
716
720
  ]
717
721
  },
718
722
  {
719
- "name": "withdrawFees",
723
+ "name": "withdrawFromMarketToInsuranceVault",
720
724
  "accounts": [
721
725
  {
722
726
  "name": "state",
@@ -730,7 +734,7 @@
730
734
  },
731
735
  {
732
736
  "name": "bank",
733
- "isMut": false,
737
+ "isMut": true,
734
738
  "isSigner": false
735
739
  },
736
740
  {
@@ -835,6 +839,11 @@
835
839
  "isMut": false,
836
840
  "isSigner": false
837
841
  },
842
+ {
843
+ "name": "bank",
844
+ "isMut": true,
845
+ "isSigner": false
846
+ },
838
847
  {
839
848
  "name": "bankVault",
840
849
  "isMut": true,
@@ -1093,6 +1102,62 @@
1093
1102
  }
1094
1103
  ]
1095
1104
  },
1105
+ {
1106
+ "name": "updateMarketImfFactor",
1107
+ "accounts": [
1108
+ {
1109
+ "name": "admin",
1110
+ "isMut": false,
1111
+ "isSigner": true
1112
+ },
1113
+ {
1114
+ "name": "state",
1115
+ "isMut": false,
1116
+ "isSigner": false
1117
+ },
1118
+ {
1119
+ "name": "market",
1120
+ "isMut": true,
1121
+ "isSigner": false
1122
+ }
1123
+ ],
1124
+ "args": [
1125
+ {
1126
+ "name": "imfFactor",
1127
+ "type": "u128"
1128
+ }
1129
+ ]
1130
+ },
1131
+ {
1132
+ "name": "updateMarketUnsettledAssetWeight",
1133
+ "accounts": [
1134
+ {
1135
+ "name": "admin",
1136
+ "isMut": false,
1137
+ "isSigner": true
1138
+ },
1139
+ {
1140
+ "name": "state",
1141
+ "isMut": false,
1142
+ "isSigner": false
1143
+ },
1144
+ {
1145
+ "name": "market",
1146
+ "isMut": true,
1147
+ "isSigner": false
1148
+ }
1149
+ ],
1150
+ "args": [
1151
+ {
1152
+ "name": "unsettledInitialAssetWeight",
1153
+ "type": "u8"
1154
+ },
1155
+ {
1156
+ "name": "unsettledMaintenanceAssetWeight",
1157
+ "type": "u8"
1158
+ }
1159
+ ]
1160
+ },
1096
1161
  {
1097
1162
  "name": "updateCurveUpdateIntensity",
1098
1163
  "accounts": [
@@ -1441,6 +1506,58 @@
1441
1506
  }
1442
1507
  ]
1443
1508
  },
1509
+ {
1510
+ "name": "updateMarketMaxSlippageRatio",
1511
+ "accounts": [
1512
+ {
1513
+ "name": "admin",
1514
+ "isMut": false,
1515
+ "isSigner": true
1516
+ },
1517
+ {
1518
+ "name": "state",
1519
+ "isMut": false,
1520
+ "isSigner": false
1521
+ },
1522
+ {
1523
+ "name": "market",
1524
+ "isMut": true,
1525
+ "isSigner": false
1526
+ }
1527
+ ],
1528
+ "args": [
1529
+ {
1530
+ "name": "maxSlippageRatio",
1531
+ "type": "u16"
1532
+ }
1533
+ ]
1534
+ },
1535
+ {
1536
+ "name": "updateMaxBaseAssetAmountRatio",
1537
+ "accounts": [
1538
+ {
1539
+ "name": "admin",
1540
+ "isMut": false,
1541
+ "isSigner": true
1542
+ },
1543
+ {
1544
+ "name": "state",
1545
+ "isMut": false,
1546
+ "isSigner": false
1547
+ },
1548
+ {
1549
+ "name": "market",
1550
+ "isMut": true,
1551
+ "isSigner": false
1552
+ }
1553
+ ],
1554
+ "args": [
1555
+ {
1556
+ "name": "maxBaseAssetAmountRatio",
1557
+ "type": "u16"
1558
+ }
1559
+ ]
1560
+ },
1444
1561
  {
1445
1562
  "name": "updateAdmin",
1446
1563
  "accounts": [
@@ -1563,7 +1680,7 @@
1563
1680
  ]
1564
1681
  },
1565
1682
  {
1566
- "name": "updateOrderAuctionTime",
1683
+ "name": "updateAuctionDuration",
1567
1684
  "accounts": [
1568
1685
  {
1569
1686
  "name": "admin",
@@ -1578,7 +1695,11 @@
1578
1695
  ],
1579
1696
  "args": [
1580
1697
  {
1581
- "name": "orderAuctionTime",
1698
+ "name": "minAuctionDuration",
1699
+ "type": "u8"
1700
+ },
1701
+ {
1702
+ "name": "maxAuctionDuration",
1582
1703
  "type": "u8"
1583
1704
  }
1584
1705
  ]
@@ -1675,6 +1796,10 @@
1675
1796
  {
1676
1797
  "name": "maintenanceLiabilityWeight",
1677
1798
  "type": "u128"
1799
+ },
1800
+ {
1801
+ "name": "imfFactor",
1802
+ "type": "u128"
1678
1803
  }
1679
1804
  ]
1680
1805
  }
@@ -1752,6 +1877,22 @@
1752
1877
  "name": "unsettledLoss",
1753
1878
  "type": "u128"
1754
1879
  },
1880
+ {
1881
+ "name": "imfFactor",
1882
+ "type": "u128"
1883
+ },
1884
+ {
1885
+ "name": "unsettledInitialAssetWeight",
1886
+ "type": "u8"
1887
+ },
1888
+ {
1889
+ "name": "unsettledMaintenanceAssetWeight",
1890
+ "type": "u8"
1891
+ },
1892
+ {
1893
+ "name": "unsettledImfFactor",
1894
+ "type": "u128"
1895
+ },
1755
1896
  {
1756
1897
  "name": "padding0",
1757
1898
  "type": "u32"
@@ -1885,7 +2026,11 @@
1885
2026
  "type": "u128"
1886
2027
  },
1887
2028
  {
1888
- "name": "orderAuctionDuration",
2029
+ "name": "minAuctionDuration",
2030
+ "type": "u8"
2031
+ },
2032
+ {
2033
+ "name": "maxAuctionDuration",
1889
2034
  "type": "u8"
1890
2035
  },
1891
2036
  {
@@ -1990,10 +2135,6 @@
1990
2135
  "name": "userOrderId",
1991
2136
  "type": "u8"
1992
2137
  },
1993
- {
1994
- "name": "quoteAssetAmount",
1995
- "type": "u128"
1996
- },
1997
2138
  {
1998
2139
  "name": "baseAssetAmount",
1999
2140
  "type": "u128"
@@ -2042,6 +2183,10 @@
2042
2183
  "name": "oraclePriceOffset",
2043
2184
  "type": "i128"
2044
2185
  },
2186
+ {
2187
+ "name": "auctionDuration",
2188
+ "type": "u8"
2189
+ },
2045
2190
  {
2046
2191
  "name": "padding0",
2047
2192
  "type": "bool"
@@ -2204,6 +2349,14 @@
2204
2349
  "name": "minimumQuoteAssetTradeSize",
2205
2350
  "type": "u128"
2206
2351
  },
2352
+ {
2353
+ "name": "maxBaseAssetAmountRatio",
2354
+ "type": "u16"
2355
+ },
2356
+ {
2357
+ "name": "maxSlippageRatio",
2358
+ "type": "u16"
2359
+ },
2207
2360
  {
2208
2361
  "name": "baseAssetAmountStepSize",
2209
2362
  "type": "u128"
@@ -2688,10 +2841,6 @@
2688
2841
  "defined": "PositionDirection"
2689
2842
  }
2690
2843
  },
2691
- {
2692
- "name": "quoteAssetAmount",
2693
- "type": "u128"
2694
- },
2695
2844
  {
2696
2845
  "name": "baseAssetAmount",
2697
2846
  "type": "u128"
@@ -2904,6 +3053,12 @@
2904
3053
  },
2905
3054
  {
2906
3055
  "name": "OraclePriceBreachedLimitPrice"
3056
+ },
3057
+ {
3058
+ "name": "MarketOrderFilledToLimitPrice"
3059
+ },
3060
+ {
3061
+ "name": "MarketOrderAuctionExpired"
2907
3062
  }
2908
3063
  ]
2909
3064
  }
@@ -3691,8 +3846,8 @@
3691
3846
  },
3692
3847
  {
3693
3848
  "code": 6044,
3694
- "name": "CouldNotFillOrder",
3695
- "msg": "CouldNotFillOrder"
3849
+ "name": "FillOrderDidNotUpdateState",
3850
+ "msg": "FillOrderDidNotUpdateState"
3696
3851
  },
3697
3852
  {
3698
3853
  "code": 6045,
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { BN } from '@project-serum/anchor';
2
2
  import { PublicKey } from '@solana/web3.js';
3
+ import pyth from '@pythnetwork/client';
3
4
 
4
5
  export * from './mockUSDCFaucet';
5
6
  export * from './oracles/types';
@@ -49,4 +50,4 @@ export * from './math/bankBalance';
49
50
  export * from './constants/banks';
50
51
  export * from './clearingHouseConfig';
51
52
 
52
- export { BN, PublicKey };
53
+ export { BN, PublicKey, pyth };