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

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.
@@ -827,9 +827,6 @@ export class ClearingHouse {
827
827
  ): Promise<TransactionInstruction> {
828
828
  const userAccountPublicKey = await this.getUserAccountPublicKey();
829
829
 
830
- const priceOracle = this.getMarketAccount(orderParams.marketIndex).amm
831
- .oracle;
832
-
833
830
  const remainingAccounts = this.getRemainingAccounts({
834
831
  writableMarketIndex: orderParams.marketIndex,
835
832
  });
@@ -839,38 +836,11 @@ export class ClearingHouse {
839
836
  state: await this.getStatePublicKey(),
840
837
  user: userAccountPublicKey,
841
838
  authority: this.wallet.publicKey,
842
- oracle: priceOracle,
843
839
  },
844
840
  remainingAccounts,
845
841
  });
846
842
  }
847
843
 
848
- public async expireOrders(
849
- userAccountPublicKey: PublicKey
850
- ): Promise<TransactionSignature> {
851
- const { txSig } = await this.txSender.send(
852
- wrapInTx(await this.getExpireOrdersIx(userAccountPublicKey)),
853
- [],
854
- this.opts
855
- );
856
- return txSig;
857
- }
858
-
859
- public async getExpireOrdersIx(
860
- userAccountPublicKey: PublicKey
861
- ): Promise<TransactionInstruction> {
862
- const fillerPublicKey = await this.getUserAccountPublicKey();
863
-
864
- return await this.program.instruction.expireOrders({
865
- accounts: {
866
- state: await this.getStatePublicKey(),
867
- filler: fillerPublicKey,
868
- user: userAccountPublicKey,
869
- authority: this.wallet.publicKey,
870
- },
871
- });
872
- }
873
-
874
844
  public async updateAMMs(marketIndexes: BN[]): Promise<TransactionSignature> {
875
845
  const { txSig } = await this.txSender.send(
876
846
  wrapInTx(await this.getUpdateAMMsIx(marketIndexes)),
@@ -914,7 +884,7 @@ export class ClearingHouse {
914
884
  });
915
885
  }
916
886
 
917
- public async cancelOrder(orderId: BN): Promise<TransactionSignature> {
887
+ public async cancelOrder(orderId?: BN): Promise<TransactionSignature> {
918
888
  const { txSig } = await this.txSender.send(
919
889
  wrapInTx(await this.getCancelOrderIx(orderId)),
920
890
  [],
@@ -923,20 +893,16 @@ export class ClearingHouse {
923
893
  return txSig;
924
894
  }
925
895
 
926
- public async getCancelOrderIx(orderId: BN): Promise<TransactionInstruction> {
896
+ public async getCancelOrderIx(orderId?: BN): Promise<TransactionInstruction> {
927
897
  const userAccountPublicKey = await this.getUserAccountPublicKey();
928
898
 
929
- const order = this.getOrder(orderId);
930
- const oracle = this.getMarketAccount(order.marketIndex).amm.oracle;
931
-
932
899
  const remainingAccounts = this.getRemainingAccounts({});
933
900
 
934
- return await this.program.instruction.cancelOrder(orderId, {
901
+ return await this.program.instruction.cancelOrder(orderId ?? null, {
935
902
  accounts: {
936
903
  state: await this.getStatePublicKey(),
937
904
  user: userAccountPublicKey,
938
905
  authority: this.wallet.publicKey,
939
- oracle,
940
906
  },
941
907
  remainingAccounts,
942
908
  });
@@ -974,36 +940,90 @@ export class ClearingHouse {
974
940
  });
975
941
  }
976
942
 
977
- public async cancelAllOrders(
978
- bestEffort?: boolean
943
+ public async fillOrder(
944
+ userAccountPublicKey: PublicKey,
945
+ user: UserAccount,
946
+ order?: Order,
947
+ makerInfo?: MakerInfo
979
948
  ): Promise<TransactionSignature> {
980
949
  const { txSig } = await this.txSender.send(
981
- wrapInTx(await this.getCancelAllOrdersIx(bestEffort)),
950
+ wrapInTx(
951
+ await this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo)
952
+ ),
982
953
  [],
983
954
  this.opts
984
955
  );
985
956
  return txSig;
986
957
  }
987
958
 
988
- public async getCancelAllOrdersIx(
989
- bestEffort?: boolean
959
+ public async getFillOrderIx(
960
+ userAccountPublicKey: PublicKey,
961
+ userAccount: UserAccount,
962
+ order: Order,
963
+ makerInfo?: MakerInfo
990
964
  ): Promise<TransactionInstruction> {
991
- const userAccountPublicKey = await this.getUserAccountPublicKey();
965
+ const fillerPublicKey = await this.getUserAccountPublicKey();
992
966
 
993
- const remainingAccounts = this.getRemainingAccounts({});
967
+ const marketIndex = order.marketIndex;
968
+ const marketAccount = this.getMarketAccount(marketIndex);
994
969
 
995
- for (const order of this.getUserAccount().orders) {
996
- const oracle = this.getMarketAccount(order.marketIndex).amm.oracle;
997
- remainingAccounts.push({
998
- pubkey: oracle,
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,
999
987
  isWritable: false,
1000
988
  isSigner: false,
989
+ },
990
+ ];
991
+ for (const position of userAccount.positions) {
992
+ if (
993
+ !positionIsAvailable(position) &&
994
+ !position.marketIndex.eq(order.marketIndex)
995
+ ) {
996
+ const market = this.getMarketAccount(position.marketIndex);
997
+ marketAccountInfos.push({
998
+ pubkey: market.pubkey,
999
+ isWritable: false,
1000
+ isSigner: false,
1001
+ });
1002
+ oracleAccountInfos.push({
1003
+ pubkey: market.amm.oracle,
1004
+ isWritable: false,
1005
+ isSigner: false,
1006
+ });
1007
+ }
1008
+ }
1009
+ const remainingAccounts = oracleAccountInfos.concat(
1010
+ bankAccountInfos.concat(marketAccountInfos)
1011
+ );
1012
+
1013
+ if (makerInfo) {
1014
+ remainingAccounts.push({
1015
+ pubkey: makerInfo.maker,
1016
+ isWritable: true,
1017
+ isSigner: false,
1001
1018
  });
1002
1019
  }
1003
1020
 
1004
- return await this.program.instruction.cancelAllOrders(bestEffort, {
1021
+ const orderId = order.orderId;
1022
+ const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
1023
+ return await this.program.instruction.fillOrder(orderId, makerOrderId, {
1005
1024
  accounts: {
1006
1025
  state: await this.getStatePublicKey(),
1026
+ filler: fillerPublicKey,
1007
1027
  user: userAccountPublicKey,
1008
1028
  authority: this.wallet.publicKey,
1009
1029
  },
@@ -1011,85 +1031,28 @@ export class ClearingHouse {
1011
1031
  });
1012
1032
  }
1013
1033
 
1014
- public async cancelOrdersByMarketAndSide(
1015
- bestEffort?: boolean,
1016
- marketIndexOnly?: BN,
1017
- directionOnly?: PositionDirection
1018
- ): Promise<TransactionSignature> {
1019
- const { txSig } = await this.txSender.send(
1020
- wrapInTx(
1021
- await this.getCancelOrdersByMarketAndSideIx(
1022
- bestEffort,
1023
- marketIndexOnly,
1024
- directionOnly
1025
- )
1026
- ),
1027
- [],
1028
- this.opts
1029
- );
1030
- return txSig;
1031
- }
1032
-
1033
- public async getCancelOrdersByMarketAndSideIx(
1034
- bestEffort?: boolean,
1035
- marketIndexOnly?: BN,
1036
- directionOnly?: PositionDirection
1037
- ): Promise<TransactionInstruction> {
1038
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1039
-
1040
- const remainingAccounts = this.getRemainingAccounts({});
1041
-
1042
- for (const order of this.getUserAccount().orders) {
1043
- const oracle = this.getMarketAccount(order.marketIndex).amm.oracle;
1044
- remainingAccounts.push({
1045
- pubkey: oracle,
1046
- isWritable: false,
1047
- isSigner: false,
1048
- });
1049
- }
1050
-
1051
- return await this.program.instruction.cancelOrdersByMarketAndSide(
1052
- bestEffort,
1053
- marketIndexOnly,
1054
- directionOnly,
1055
- {
1056
- accounts: {
1057
- state: await this.getStatePublicKey(),
1058
- user: userAccountPublicKey,
1059
- authority: this.wallet.publicKey,
1060
- },
1061
- remainingAccounts,
1062
- }
1063
- );
1064
- }
1065
-
1066
- public async fillOrder(
1034
+ public async triggerOrder(
1067
1035
  userAccountPublicKey: PublicKey,
1068
1036
  user: UserAccount,
1069
- order: Order,
1070
- makerInfo?: MakerInfo
1037
+ order: Order
1071
1038
  ): Promise<TransactionSignature> {
1072
1039
  const { txSig } = await this.txSender.send(
1073
- wrapInTx(
1074
- await this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo)
1075
- ),
1040
+ wrapInTx(await this.getTriggerOrderIx(userAccountPublicKey, user, order)),
1076
1041
  [],
1077
1042
  this.opts
1078
1043
  );
1079
1044
  return txSig;
1080
1045
  }
1081
1046
 
1082
- public async getFillOrderIx(
1047
+ public async getTriggerOrderIx(
1083
1048
  userAccountPublicKey: PublicKey,
1084
1049
  userAccount: UserAccount,
1085
- order: Order,
1086
- makerInfo?: MakerInfo
1050
+ order: Order
1087
1051
  ): Promise<TransactionInstruction> {
1088
1052
  const fillerPublicKey = await this.getUserAccountPublicKey();
1089
1053
 
1090
1054
  const marketIndex = order.marketIndex;
1091
1055
  const marketAccount = this.getMarketAccount(marketIndex);
1092
- const oracle = marketAccount.amm.oracle;
1093
1056
 
1094
1057
  const bankAccountInfos = [
1095
1058
  {
@@ -1134,23 +1097,13 @@ export class ClearingHouse {
1134
1097
  bankAccountInfos.concat(marketAccountInfos)
1135
1098
  );
1136
1099
 
1137
- if (makerInfo) {
1138
- remainingAccounts.push({
1139
- pubkey: makerInfo.maker,
1140
- isWritable: true,
1141
- isSigner: false,
1142
- });
1143
- }
1144
-
1145
1100
  const orderId = order.orderId;
1146
- const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
1147
- return await this.program.instruction.fillOrder(orderId, makerOrderId, {
1101
+ return await this.program.instruction.triggerOrder(orderId, {
1148
1102
  accounts: {
1149
1103
  state: await this.getStatePublicKey(),
1150
1104
  filler: fillerPublicKey,
1151
1105
  user: userAccountPublicKey,
1152
1106
  authority: this.wallet.publicKey,
1153
- oracle: oracle,
1154
1107
  },
1155
1108
  remainingAccounts,
1156
1109
  });
@@ -1175,9 +1128,6 @@ export class ClearingHouse {
1175
1128
  ): Promise<TransactionInstruction> {
1176
1129
  const userAccountPublicKey = await this.getUserAccountPublicKey();
1177
1130
 
1178
- const priceOracle = this.getMarketAccount(orderParams.marketIndex).amm
1179
- .oracle;
1180
-
1181
1131
  const remainingAccounts = this.getRemainingAccounts({
1182
1132
  writableMarketIndex: orderParams.marketIndex,
1183
1133
  writableBankIndex: QUOTE_ASSET_BANK_INDEX,
@@ -1201,7 +1151,6 @@ export class ClearingHouse {
1201
1151
  state: await this.getStatePublicKey(),
1202
1152
  user: userAccountPublicKey,
1203
1153
  authority: this.wallet.publicKey,
1204
- oracle: priceOracle,
1205
1154
  },
1206
1155
  remainingAccounts,
1207
1156
  }
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: 'GCuH76fb1rXc7bjFXNcnNvWSekLgzpsnMbc1Ng4FsGSs',
31
+ CLEARING_HOUSE_PROGRAM_ID: '9jwr5nC2f9yAraXrg4UzHXmCX3vi9FQkjD6p9e8bRqNa',
32
32
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
33
33
  MARKETS: DevnetMarkets,
34
34
  BANKS: DevnetBanks,
@@ -43,6 +43,7 @@ export const BASE_PRECISION_EXP = AMM_RESERVE_PRECISION_EXP;
43
43
 
44
44
  export const AMM_TO_QUOTE_PRECISION_RATIO =
45
45
  AMM_RESERVE_PRECISION.div(QUOTE_PRECISION); // 10^7
46
+ export const PRICE_DIV_PEG = MARK_PRICE_PRECISION.div(PEG_PRECISION); //10^7
46
47
  export const PRICE_TO_QUOTE_PRECISION =
47
48
  MARK_PRICE_PRECISION.div(QUOTE_PRECISION);
48
49
  export const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO =
@@ -234,8 +234,14 @@ export class BigNum {
234
234
  return printString;
235
235
  }
236
236
 
237
- public prettyPrint(): string {
238
- const [leftSide, rightSide] = this.printShort().split(BigNum.delim);
237
+ public prettyPrint(
238
+ useTradePrecision?: boolean,
239
+ precisionOverride?: number
240
+ ): string {
241
+ const [leftSide, rightSide] = this.printShort(
242
+ useTradePrecision,
243
+ precisionOverride
244
+ ).split(BigNum.delim);
239
245
 
240
246
  let formattedLeftSide = leftSide;
241
247
 
@@ -374,13 +380,24 @@ export class BigNum {
374
380
  return this.toPrecision(6, true);
375
381
  }
376
382
 
377
- public toNotional(): string {
378
- return `${this.lt(BigNum.zero()) ? `-` : ``}$${BigNum.fromPrint(
379
- this.toFixed(2),
380
- new BN(2)
381
- )
382
- .prettyPrint()
383
- .replace('-', '')}`;
383
+ /**
384
+ * Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
385
+ * @param useTradePrecision
386
+ * @param precisionOverride
387
+ * @returns
388
+ */
389
+ public toNotional(
390
+ useTradePrecision?: boolean,
391
+ precisionOverride?: number
392
+ ): string {
393
+ const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
394
+
395
+ const val =
396
+ useTradePrecision || precisionOverride
397
+ ? this.prettyPrint(useTradePrecision, precisionOverride)
398
+ : BigNum.fromPrint(this.toFixed(2), new BN(2)).prettyPrint();
399
+
400
+ return `${prefix}${val.replace('-', '')}`;
384
401
  }
385
402
 
386
403
  public toMillified(precision = 3): string {
@@ -381,11 +381,6 @@
381
381
  "name": "authority",
382
382
  "isMut": false,
383
383
  "isSigner": true
384
- },
385
- {
386
- "name": "oracle",
387
- "isMut": false,
388
- "isSigner": false
389
384
  }
390
385
  ],
391
386
  "args": [
@@ -414,17 +409,14 @@
414
409
  "name": "authority",
415
410
  "isMut": false,
416
411
  "isSigner": true
417
- },
418
- {
419
- "name": "oracle",
420
- "isMut": false,
421
- "isSigner": false
422
412
  }
423
413
  ],
424
414
  "args": [
425
415
  {
426
416
  "name": "orderId",
427
- "type": "u64"
417
+ "type": {
418
+ "option": "u64"
419
+ }
428
420
  }
429
421
  ]
430
422
  },
@@ -445,11 +437,6 @@
445
437
  "name": "authority",
446
438
  "isMut": false,
447
439
  "isSigner": true
448
- },
449
- {
450
- "name": "oracle",
451
- "isMut": false,
452
- "isSigner": false
453
440
  }
454
441
  ],
455
442
  "args": [
@@ -481,17 +468,14 @@
481
468
  "name": "user",
482
469
  "isMut": true,
483
470
  "isSigner": false
484
- },
485
- {
486
- "name": "oracle",
487
- "isMut": false,
488
- "isSigner": false
489
471
  }
490
472
  ],
491
473
  "args": [
492
474
  {
493
475
  "name": "orderId",
494
- "type": "u64"
476
+ "type": {
477
+ "option": "u64"
478
+ }
495
479
  },
496
480
  {
497
481
  "name": "makerOrderId",
@@ -518,11 +502,6 @@
518
502
  "name": "authority",
519
503
  "isMut": false,
520
504
  "isSigner": true
521
- },
522
- {
523
- "name": "oracle",
524
- "isMut": false,
525
- "isSigner": false
526
505
  }
527
506
  ],
528
507
  "args": [
@@ -562,11 +541,6 @@
562
541
  "name": "authority",
563
542
  "isMut": false,
564
543
  "isSigner": true
565
- },
566
- {
567
- "name": "oracle",
568
- "isMut": false,
569
- "isSigner": false
570
544
  }
571
545
  ],
572
546
  "args": [
@@ -582,6 +556,37 @@
582
556
  }
583
557
  ]
584
558
  },
559
+ {
560
+ "name": "triggerOrder",
561
+ "accounts": [
562
+ {
563
+ "name": "state",
564
+ "isMut": false,
565
+ "isSigner": false
566
+ },
567
+ {
568
+ "name": "authority",
569
+ "isMut": false,
570
+ "isSigner": true
571
+ },
572
+ {
573
+ "name": "filler",
574
+ "isMut": true,
575
+ "isSigner": false
576
+ },
577
+ {
578
+ "name": "user",
579
+ "isMut": true,
580
+ "isSigner": false
581
+ }
582
+ ],
583
+ "args": [
584
+ {
585
+ "name": "orderId",
586
+ "type": "u64"
587
+ }
588
+ ]
589
+ },
585
590
  {
586
591
  "name": "updateAmms",
587
592
  "accounts": [
@@ -993,11 +998,6 @@
993
998
  "name": "user",
994
999
  "isMut": true,
995
1000
  "isSigner": false
996
- },
997
- {
998
- "name": "market",
999
- "isMut": false,
1000
- "isSigner": false
1001
1001
  }
1002
1002
  ],
1003
1003
  "args": []
@@ -1389,6 +1389,32 @@
1389
1389
  }
1390
1390
  ]
1391
1391
  },
1392
+ {
1393
+ "name": "updateMarketMaxSpread",
1394
+ "accounts": [
1395
+ {
1396
+ "name": "admin",
1397
+ "isMut": false,
1398
+ "isSigner": true
1399
+ },
1400
+ {
1401
+ "name": "state",
1402
+ "isMut": false,
1403
+ "isSigner": false
1404
+ },
1405
+ {
1406
+ "name": "market",
1407
+ "isMut": true,
1408
+ "isSigner": false
1409
+ }
1410
+ ],
1411
+ "args": [
1412
+ {
1413
+ "name": "maxSpread",
1414
+ "type": "u32"
1415
+ }
1416
+ ]
1417
+ },
1392
1418
  {
1393
1419
  "name": "updateMarketBaseAssetAmountStepSize",
1394
1420
  "accounts": [
@@ -2194,6 +2220,10 @@
2194
2220
  "name": "shortSpread",
2195
2221
  "type": "u128"
2196
2222
  },
2223
+ {
2224
+ "name": "maxSpread",
2225
+ "type": "u32"
2226
+ },
2197
2227
  {
2198
2228
  "name": "askBaseAssetReserve",
2199
2229
  "type": "u128"
@@ -2252,7 +2282,7 @@
2252
2282
  },
2253
2283
  {
2254
2284
  "name": "totalFeeMinusDistributions",
2255
- "type": "u128"
2285
+ "type": "i128"
2256
2286
  },
2257
2287
  {
2258
2288
  "name": "totalFeeWithdrawn",
@@ -2712,6 +2742,10 @@
2712
2742
  "defined": "OrderTriggerCondition"
2713
2743
  }
2714
2744
  },
2745
+ {
2746
+ "name": "triggered",
2747
+ "type": "bool"
2748
+ },
2715
2749
  {
2716
2750
  "name": "referrer",
2717
2751
  "type": "publicKey"
@@ -2848,6 +2882,9 @@
2848
2882
  {
2849
2883
  "name": "Fill"
2850
2884
  },
2885
+ {
2886
+ "name": "Trigger"
2887
+ },
2851
2888
  {
2852
2889
  "name": "Expire"
2853
2890
  }
@@ -3016,6 +3053,11 @@
3016
3053
  "type": "u64",
3017
3054
  "index": false
3018
3055
  },
3056
+ {
3057
+ "name": "oraclePrice",
3058
+ "type": "i128",
3059
+ "index": false
3060
+ },
3019
3061
  {
3020
3062
  "name": "from",
3021
3063
  "type": {
@@ -3217,7 +3259,7 @@
3217
3259
  },
3218
3260
  {
3219
3261
  "name": "totalFeeMinusDistributions",
3220
- "type": "u128",
3262
+ "type": "i128",
3221
3263
  "index": false
3222
3264
  },
3223
3265
  {
@@ -3349,6 +3391,16 @@
3349
3391
  },
3350
3392
  "index": false
3351
3393
  },
3394
+ {
3395
+ "name": "makerUnsettledPnl",
3396
+ "type": "i128",
3397
+ "index": false
3398
+ },
3399
+ {
3400
+ "name": "takerUnsettledPnl",
3401
+ "type": "i128",
3402
+ "index": false
3403
+ },
3352
3404
  {
3353
3405
  "name": "action",
3354
3406
  "type": {
@@ -3854,6 +3906,21 @@
3854
3906
  },
3855
3907
  {
3856
3908
  "code": 6087,
3909
+ "name": "OrderMustBeTriggeredFirst",
3910
+ "msg": "OrderMustBeTriggeredFirst"
3911
+ },
3912
+ {
3913
+ "code": 6088,
3914
+ "name": "OrderNotTriggerable",
3915
+ "msg": "OrderNotTriggerable"
3916
+ },
3917
+ {
3918
+ "code": 6089,
3919
+ "name": "OrderDidNotSatisfyTriggerCondition",
3920
+ "msg": "OrderDidNotSatisfyTriggerCondition"
3921
+ },
3922
+ {
3923
+ "code": 6090,
3857
3924
  "name": "DefaultError",
3858
3925
  "msg": "DefaultError"
3859
3926
  }