@drift-labs/sdk 2.142.0-beta.9 → 2.142.0

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.
Files changed (58) hide show
  1. package/VERSION +1 -1
  2. package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.d.ts +46 -5
  3. package/lib/browser/accounts/grpcDriftClientAccountSubscriberV2.js +242 -41
  4. package/lib/browser/accounts/grpcMultiAccountSubscriber.d.ts +6 -3
  5. package/lib/browser/accounts/grpcMultiAccountSubscriber.js +112 -19
  6. package/lib/browser/adminClient.d.ts +4 -0
  7. package/lib/browser/adminClient.js +34 -0
  8. package/lib/browser/constants/perpMarkets.js +35 -0
  9. package/lib/browser/constants/spotMarkets.js +4 -4
  10. package/lib/browser/driftClient.d.ts +35 -5
  11. package/lib/browser/driftClient.js +41 -14
  12. package/lib/browser/events/parse.d.ts +2 -0
  13. package/lib/browser/events/parse.js +94 -1
  14. package/lib/browser/events/types.d.ts +22 -3
  15. package/lib/browser/idl/drift.json +105 -6
  16. package/lib/browser/math/amm.d.ts +1 -0
  17. package/lib/browser/math/amm.js +28 -4
  18. package/lib/browser/types.d.ts +20 -0
  19. package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts +46 -5
  20. package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.d.ts.map +1 -1
  21. package/lib/node/accounts/grpcDriftClientAccountSubscriberV2.js +242 -41
  22. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts +6 -3
  23. package/lib/node/accounts/grpcMultiAccountSubscriber.d.ts.map +1 -1
  24. package/lib/node/accounts/grpcMultiAccountSubscriber.js +112 -19
  25. package/lib/node/adminClient.d.ts +4 -0
  26. package/lib/node/adminClient.d.ts.map +1 -1
  27. package/lib/node/adminClient.js +34 -0
  28. package/lib/node/constants/perpMarkets.d.ts.map +1 -1
  29. package/lib/node/constants/perpMarkets.js +35 -0
  30. package/lib/node/constants/spotMarkets.js +4 -4
  31. package/lib/node/driftClient.d.ts +35 -5
  32. package/lib/node/driftClient.d.ts.map +1 -1
  33. package/lib/node/driftClient.js +41 -14
  34. package/lib/node/events/parse.d.ts +2 -0
  35. package/lib/node/events/parse.d.ts.map +1 -1
  36. package/lib/node/events/parse.js +94 -1
  37. package/lib/node/events/types.d.ts +22 -3
  38. package/lib/node/events/types.d.ts.map +1 -1
  39. package/lib/node/idl/drift.json +105 -6
  40. package/lib/node/math/amm.d.ts +1 -0
  41. package/lib/node/math/amm.d.ts.map +1 -1
  42. package/lib/node/math/amm.js +28 -4
  43. package/lib/node/types.d.ts +20 -0
  44. package/lib/node/types.d.ts.map +1 -1
  45. package/package.json +2 -1
  46. package/scripts/client-test.ts +294 -135
  47. package/src/accounts/grpcDriftClientAccountSubscriberV2.ts +398 -72
  48. package/src/accounts/grpcMultiAccountSubscriber.ts +163 -31
  49. package/src/adminClient.ts +74 -0
  50. package/src/constants/perpMarkets.ts +37 -0
  51. package/src/constants/spotMarkets.ts +4 -4
  52. package/src/driftClient.ts +65 -14
  53. package/src/events/parse.ts +115 -0
  54. package/src/events/types.ts +26 -2
  55. package/src/idl/drift.json +105 -6
  56. package/src/math/amm.ts +52 -8
  57. package/src/types.ts +22 -0
  58. package/tests/events/parseLogsForCuUsage.ts +139 -0
@@ -1,10 +1,13 @@
1
1
  import { Program, Event } from '@coral-xyz/anchor';
2
+ import { CuUsageEvent } from './types';
2
3
 
3
4
  const driftProgramId = 'dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH';
4
5
  const PROGRAM_LOG = 'Program log: ';
6
+ const PROGRAM_INSTRUCTION = 'Program log: Instruction: ';
5
7
  const PROGRAM_DATA = 'Program data: ';
6
8
  const PROGRAM_LOG_START_INDEX = PROGRAM_LOG.length;
7
9
  const PROGRAM_DATA_START_INDEX = PROGRAM_DATA.length;
10
+ const PROGRAM_INSTRUCTION_START_INDEX = PROGRAM_INSTRUCTION.length;
8
11
 
9
12
  export function parseLogs(
10
13
  program: Program,
@@ -112,6 +115,7 @@ function handleSystemLog(
112
115
  // executing for a given log.
113
116
  class ExecutionContext {
114
117
  stack: string[] = [];
118
+ ixStack: string[] = [];
115
119
 
116
120
  program(): string {
117
121
  if (!this.stack.length) {
@@ -130,4 +134,115 @@ class ExecutionContext {
130
134
  }
131
135
  this.stack.pop();
132
136
  }
137
+
138
+ ix(): string {
139
+ if (!this.ixStack.length) {
140
+ throw new Error('Expected the ix stack to have elements');
141
+ }
142
+ return this.ixStack[this.ixStack.length - 1];
143
+ }
144
+
145
+ pushIx(newIx: string) {
146
+ this.ixStack.push(newIx);
147
+ }
148
+
149
+ popIx() {
150
+ this.ixStack.pop();
151
+ }
152
+ }
153
+
154
+ export function parseLogsForCuUsage(
155
+ logs: string[],
156
+ programId = driftProgramId
157
+ ): Event<CuUsageEvent>[] {
158
+ const cuUsageEvents: Event<CuUsageEvent>[] = [];
159
+
160
+ const execution = new ExecutionContext();
161
+ for (const log of logs) {
162
+ if (log.startsWith('Log truncated')) {
163
+ break;
164
+ }
165
+
166
+ const [newProgram, newIx, didPopProgram, didPopIx] = handleLogForCuUsage(
167
+ execution,
168
+ log,
169
+ programId
170
+ );
171
+ if (newProgram) {
172
+ execution.push(newProgram);
173
+ }
174
+ if (newIx) {
175
+ execution.pushIx(newIx);
176
+ }
177
+ if (didPopProgram) {
178
+ execution.pop();
179
+ }
180
+ if (didPopIx !== null) {
181
+ cuUsageEvents.push({
182
+ name: 'CuUsage',
183
+ data: {
184
+ instruction: execution.ix(),
185
+ cuUsage: didPopIx!,
186
+ },
187
+ } as any);
188
+ execution.popIx();
189
+ }
190
+ }
191
+ return cuUsageEvents;
192
+ }
193
+
194
+ function handleLogForCuUsage(
195
+ execution: ExecutionContext,
196
+ log: string,
197
+ programId = driftProgramId
198
+ ): [string | null, string | null, boolean, number | null] {
199
+ if (execution.stack.length > 0 && execution.program() === programId) {
200
+ return handleProgramLogForCuUsage(log, programId);
201
+ } else {
202
+ return handleSystemLogForCuUsage(log, programId);
203
+ }
204
+ }
205
+
206
+ function handleProgramLogForCuUsage(
207
+ log: string,
208
+ programId = driftProgramId
209
+ ): [string | null, string | null, boolean, number | null] {
210
+ if (log.startsWith(PROGRAM_INSTRUCTION)) {
211
+ const ixStr = log.slice(PROGRAM_INSTRUCTION_START_INDEX);
212
+ return [null, ixStr, false, null];
213
+ } else {
214
+ return handleSystemLogForCuUsage(log, programId);
215
+ }
216
+ }
217
+
218
+ function handleSystemLogForCuUsage(
219
+ log: string,
220
+ programId = driftProgramId
221
+ ): [string | null, string | null, boolean, number | null] {
222
+ // System component.
223
+ const logStart = log.split(':')[0];
224
+ const programStart = `Program ${programId} invoke`;
225
+
226
+ // Did the program finish executing?
227
+ if (logStart.match(/^Program (.*) success/g) !== null) {
228
+ return [null, null, true, null];
229
+ // Recursive call.
230
+ } else if (logStart.startsWith(programStart)) {
231
+ return [programId, null, false, null];
232
+ // Consumed CU log.
233
+ } else if (log.startsWith(`Program ${programId} consumed `)) {
234
+ // Extract CU usage, e.g. 'Program ... consumed 29242 of 199700 compute units'
235
+ // We need to extract the consumed value (29242)
236
+ const matches = log.match(/consumed (\d+) of \d+ compute units/);
237
+ if (matches) {
238
+ return [null, null, false, Number(matches[1])];
239
+ }
240
+ return [null, null, false, null];
241
+ }
242
+ // CPI call.
243
+ else if (logStart.includes('invoke')) {
244
+ return ['cpi', null, false, null]; // Any string will do.
245
+ } else {
246
+ return [null, null, false, null];
247
+ }
133
248
  }
@@ -24,6 +24,7 @@ import {
24
24
  LPMintRedeemRecord,
25
25
  LPSettleRecord,
26
26
  LPSwapRecord,
27
+ LPBorrowLendDepositRecord,
27
28
  } from '../types';
28
29
  import { EventEmitter } from 'events';
29
30
 
@@ -116,8 +117,8 @@ export type EventMap = {
116
117
  FuelSeasonRecord: Event<FuelSeasonRecord>;
117
118
  InsuranceFundSwapRecord: Event<InsuranceFundSwapRecord>;
118
119
  TransferProtocolIfSharesToRevenuePoolRecord: Event<TransferProtocolIfSharesToRevenuePoolRecord>;
119
- LPMintRedeemRecord: Event<LPMintRedeemRecord>;
120
120
  LPSettleRecord: Event<LPSettleRecord>;
121
+ LPMintRedeemRecord: Event<LPMintRedeemRecord>;
121
122
  LPSwapRecord: Event<LPSwapRecord>;
122
123
  };
123
124
 
@@ -146,8 +147,10 @@ export type DriftEvent =
146
147
  | Event<InsuranceFundSwapRecord>
147
148
  | Event<TransferProtocolIfSharesToRevenuePoolRecord>
148
149
  | Event<LPSettleRecord>
150
+ | Event<LPMintRedeemRecord>
149
151
  | Event<LPSwapRecord>
150
- | Event<LPMintRedeemRecord>;
152
+ | Event<LPBorrowLendDepositRecord>
153
+ | Event<CuUsage>;
151
154
 
152
155
  export interface EventSubscriberEvents {
153
156
  newEvent: (event: WrappedEvent<EventType>) => void;
@@ -211,3 +214,24 @@ export type LogProviderConfig =
211
214
  | WebSocketLogProviderConfig
212
215
  | PollingLogProviderConfig
213
216
  | EventsServerLogProviderConfig;
217
+
218
+ export type CuUsageEvent = {
219
+ name: 'CuUsage';
220
+ fields: [
221
+ {
222
+ name: 'instruction';
223
+ type: 'string';
224
+ index: false;
225
+ },
226
+ {
227
+ name: 'cuUsage';
228
+ type: 'u32';
229
+ index: false;
230
+ },
231
+ ];
232
+ };
233
+
234
+ export type CuUsage = {
235
+ instruction: string;
236
+ cuUsage: number;
237
+ };
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.141.0",
2
+ "version": "2.142.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -5874,6 +5874,32 @@
5874
5874
  }
5875
5875
  ]
5876
5876
  },
5877
+ {
5878
+ "name": "updatePerpMarketReferencePriceOffsetDeadbandPct",
5879
+ "accounts": [
5880
+ {
5881
+ "name": "admin",
5882
+ "isMut": false,
5883
+ "isSigner": true
5884
+ },
5885
+ {
5886
+ "name": "state",
5887
+ "isMut": false,
5888
+ "isSigner": false
5889
+ },
5890
+ {
5891
+ "name": "perpMarket",
5892
+ "isMut": true,
5893
+ "isSigner": false
5894
+ }
5895
+ ],
5896
+ "args": [
5897
+ {
5898
+ "name": "referencePriceOffsetDeadbandPct",
5899
+ "type": "u8"
5900
+ }
5901
+ ]
5902
+ },
5877
5903
  {
5878
5904
  "name": "updateLpCooldownTime",
5879
5905
  "accounts": [
@@ -11209,12 +11235,16 @@
11209
11235
  ],
11210
11236
  "type": "i8"
11211
11237
  },
11238
+ {
11239
+ "name": "referencePriceOffsetDeadbandPct",
11240
+ "type": "u8"
11241
+ },
11212
11242
  {
11213
11243
  "name": "padding",
11214
11244
  "type": {
11215
11245
  "array": [
11216
11246
  "u8",
11217
- 3
11247
+ 2
11218
11248
  ]
11219
11249
  }
11220
11250
  },
@@ -14875,6 +14905,11 @@
14875
14905
  "name": "lpPrice",
14876
14906
  "type": "u128",
14877
14907
  "index": false
14908
+ },
14909
+ {
14910
+ "name": "lpPool",
14911
+ "type": "publicKey",
14912
+ "index": false
14878
14913
  }
14879
14914
  ]
14880
14915
  },
@@ -14985,6 +15020,11 @@
14985
15020
  "name": "outSwapId",
14986
15021
  "type": "u64",
14987
15022
  "index": false
15023
+ },
15024
+ {
15025
+ "name": "lpPool",
15026
+ "type": "publicKey",
15027
+ "index": false
14988
15028
  }
14989
15029
  ]
14990
15030
  },
@@ -15080,6 +15120,68 @@
15080
15120
  "name": "inMarketTargetWeight",
15081
15121
  "type": "i64",
15082
15122
  "index": false
15123
+ },
15124
+ {
15125
+ "name": "lpPool",
15126
+ "type": "publicKey",
15127
+ "index": false
15128
+ }
15129
+ ]
15130
+ },
15131
+ {
15132
+ "name": "LPBorrowLendDepositRecord",
15133
+ "fields": [
15134
+ {
15135
+ "name": "ts",
15136
+ "type": "i64",
15137
+ "index": false
15138
+ },
15139
+ {
15140
+ "name": "slot",
15141
+ "type": "u64",
15142
+ "index": false
15143
+ },
15144
+ {
15145
+ "name": "spotMarketIndex",
15146
+ "type": "u16",
15147
+ "index": false
15148
+ },
15149
+ {
15150
+ "name": "constituentIndex",
15151
+ "type": "u16",
15152
+ "index": false
15153
+ },
15154
+ {
15155
+ "name": "direction",
15156
+ "type": {
15157
+ "defined": "DepositDirection"
15158
+ },
15159
+ "index": false
15160
+ },
15161
+ {
15162
+ "name": "tokenBalance",
15163
+ "type": "i64",
15164
+ "index": false
15165
+ },
15166
+ {
15167
+ "name": "lastTokenBalance",
15168
+ "type": "i64",
15169
+ "index": false
15170
+ },
15171
+ {
15172
+ "name": "interestAccruedTokenAmount",
15173
+ "type": "i64",
15174
+ "index": false
15175
+ },
15176
+ {
15177
+ "name": "amountDepositWithdraw",
15178
+ "type": "u64",
15179
+ "index": false
15180
+ },
15181
+ {
15182
+ "name": "lpPool",
15183
+ "type": "publicKey",
15184
+ "index": false
15083
15185
  }
15084
15186
  ]
15085
15187
  }
@@ -16710,8 +16812,5 @@
16710
16812
  "name": "UnableToLoadRevenueShareAccount",
16711
16813
  "msg": "Unable to load builder account"
16712
16814
  }
16713
- ],
16714
- "metadata": {
16715
- "address": "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH"
16716
- }
16815
+ ]
16717
16816
  }
package/src/math/amm.ts CHANGED
@@ -389,6 +389,31 @@ export function calculateInventoryLiquidityRatio(
389
389
  return inventoryScaleBN;
390
390
  }
391
391
 
392
+ export function calculateInventoryLiquidityRatioForReferencePriceOffset(
393
+ baseAssetAmountWithAmm: BN,
394
+ baseAssetReserve: BN,
395
+ minBaseAssetReserve: BN,
396
+ maxBaseAssetReserve: BN
397
+ ): BN {
398
+ // inventory skew
399
+ const [openBids, openAsks] = calculateMarketOpenBidAsk(
400
+ baseAssetReserve,
401
+ minBaseAssetReserve,
402
+ maxBaseAssetReserve
403
+ );
404
+
405
+ const avgSideLiquidity = openBids.abs().add(openAsks.abs()).div(TWO);
406
+
407
+ const inventoryScaleBN = BN.min(
408
+ baseAssetAmountWithAmm
409
+ .mul(PERCENTAGE_PRECISION)
410
+ .div(BN.max(avgSideLiquidity, ONE))
411
+ .abs(),
412
+ PERCENTAGE_PRECISION
413
+ );
414
+ return inventoryScaleBN;
415
+ }
416
+
392
417
  export function calculateInventoryScale(
393
418
  baseAssetAmountWithAmm: BN,
394
419
  baseAssetReserve: BN,
@@ -440,7 +465,7 @@ export function calculateReferencePriceOffset(
440
465
  markTwapSlow: BN,
441
466
  maxOffsetPct: number
442
467
  ): BN {
443
- if (last24hAvgFundingRate.eq(ZERO)) {
468
+ if (last24hAvgFundingRate.eq(ZERO) || liquidityFraction.eq(ZERO)) {
444
469
  return ZERO;
445
470
  }
446
471
 
@@ -1013,19 +1038,38 @@ export function calculateSpreadReserves(
1013
1038
  (amm.curveUpdateIntensity - 100)
1014
1039
  );
1015
1040
 
1016
- const liquidityFraction = calculateInventoryLiquidityRatio(
1017
- amm.baseAssetAmountWithAmm,
1018
- amm.baseAssetReserve,
1019
- amm.minBaseAssetReserve,
1020
- amm.maxBaseAssetReserve
1021
- );
1041
+ const liquidityFraction =
1042
+ calculateInventoryLiquidityRatioForReferencePriceOffset(
1043
+ amm.baseAssetAmountWithAmm,
1044
+ amm.baseAssetReserve,
1045
+ amm.minBaseAssetReserve,
1046
+ amm.maxBaseAssetReserve
1047
+ );
1022
1048
  const liquidityFractionSigned = liquidityFraction.mul(
1023
1049
  sigNum(amm.baseAssetAmountWithAmm.add(amm.baseAssetAmountWithUnsettledLp))
1024
1050
  );
1051
+
1052
+ let liquidityFractionAfterDeadband = liquidityFractionSigned;
1053
+ const deadbandPct = amm.referencePriceOffsetDeadbandPct
1054
+ ? PERCENTAGE_PRECISION.mul(
1055
+ new BN(amm.referencePriceOffsetDeadbandPct as number)
1056
+ ).divn(100)
1057
+ : ZERO;
1058
+ if (!liquidityFractionAfterDeadband.eq(ZERO) && deadbandPct.gt(ZERO)) {
1059
+ const abs = liquidityFractionAfterDeadband.abs();
1060
+ if (abs.lte(deadbandPct)) {
1061
+ liquidityFractionAfterDeadband = ZERO;
1062
+ } else {
1063
+ liquidityFractionAfterDeadband = liquidityFractionAfterDeadband.sub(
1064
+ deadbandPct.mul(sigNum(liquidityFractionAfterDeadband))
1065
+ );
1066
+ }
1067
+ }
1068
+
1025
1069
  referencePriceOffset = calculateReferencePriceOffset(
1026
1070
  reservePrice,
1027
1071
  amm.last24HAvgFundingRate,
1028
- liquidityFractionSigned,
1072
+ liquidityFractionAfterDeadband,
1029
1073
  amm.historicalOracleData.lastOraclePriceTwap5Min,
1030
1074
  amm.lastMarkPriceTwap5Min,
1031
1075
  amm.historicalOracleData.lastOraclePriceTwap,
package/src/types.ts CHANGED
@@ -767,6 +767,7 @@ export type LPSwapRecord = {
767
767
  outMarketTargetWeight: BN;
768
768
  inSwapId: BN;
769
769
  outSwapId: BN;
770
+ lpPool: PublicKey;
770
771
  };
771
772
 
772
773
  export type LPMintRedeemRecord = {
@@ -789,6 +790,7 @@ export type LPMintRedeemRecord = {
789
790
  lastAumSlot: BN;
790
791
  inMarketCurrentWeight: BN;
791
792
  inMarketTargetWeight: BN;
793
+ lpPool: PublicKey;
792
794
  };
793
795
 
794
796
  export type LPSettleRecord = {
@@ -803,6 +805,20 @@ export type LPSettleRecord = {
803
805
  perpAmmExFeeDelta: BN;
804
806
  lpAum: BN;
805
807
  lpPrice: BN;
808
+ lpPool: PublicKey;
809
+ };
810
+
811
+ export type LPBorrowLendDepositRecord = {
812
+ ts: BN;
813
+ slot: BN;
814
+ spotMarketIndex: number;
815
+ constituentIndex: number;
816
+ direction: DepositDirection;
817
+ tokenBalance: BN;
818
+ lastTokenBalance: BN;
819
+ interestAccruedTokenAmount: BN;
820
+ amountDepositWithdraw: BN;
821
+ lpPool: PublicKey;
806
822
  };
807
823
 
808
824
  export type StateAccount = {
@@ -878,6 +894,11 @@ export type PerpMarketAccount = {
878
894
  protectedMakerLimitPriceDivisor: number;
879
895
  protectedMakerDynamicDivisor: number;
880
896
  lastFillPrice: BN;
897
+
898
+ lpFeeTransferScalar: number;
899
+ lpExchangeFeeExcluscionScalar: number;
900
+ lpStatus: number;
901
+ lpPausedOperations: number;
881
902
  };
882
903
 
883
904
  export type HistoricalOracleData = {
@@ -1091,6 +1112,7 @@ export type AMM = {
1091
1112
  ammInventorySpreadAdjustment: number;
1092
1113
 
1093
1114
  lastFundingOracleTwap: BN;
1115
+ referencePriceOffsetDeadbandPct: number;
1094
1116
  };
1095
1117
 
1096
1118
  // # User Account Types
@@ -0,0 +1,139 @@
1
+ import { expect } from 'chai';
2
+ import { parseLogsForCuUsage } from '../../src/events/parse';
3
+
4
+ // if you used the '@types/mocha' method to install mocha type definitions, uncomment the following line
5
+ // import 'mocha';
6
+
7
+ describe('parseLogsForCuUsage Tests', () => {
8
+ it('can parse single ix', () => {
9
+ const logs = [
10
+ 'Program ComputeBudget111111111111111111111111111111 invoke [1]',
11
+ 'Program ComputeBudget111111111111111111111111111111 success',
12
+ 'Program ComputeBudget111111111111111111111111111111 invoke [1]',
13
+ 'Program ComputeBudget111111111111111111111111111111 success',
14
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH invoke [1]',
15
+ 'Program log: Instruction: UpdateFundingRate',
16
+ 'Program log: correcting mark twap update (oracle previously invalid for 1625 seconds)',
17
+ 'Program data: Ze4o5EYuPXWjSrNoAAAAADkUAAAAAAAAZTIKAAAAAAAAAAAAAAAAAGwIAGYvdqgAAAAAAAAAAAAxpQahVMKdAAAAAAAAAAAAK0cfnMcFowAAAAAAAAAAAGUyCgAAAAAAAAAAAAAAAAAbXpy4n6WoAAAAAAAAAAAAGAAXbsHunQAAAAAAAAAAAObum9evM6MAAAAAAAAAAAAA9PA5+UYKAAAAAAAAAAAAAKhuDZ0RCgAAAAAAAAAAAABMgixcNQAAAAAAAAAAAAA9NqYKSgAAAAAAAAAAAAAA4nylcy0AAAAAAAAAAAAAAMvCAAAAAAAAAAAAAAAAAACOjAkAAAAAAET3AgAAAAAAAAAAAAAAAAAMAQAAHgA=',
18
+ 'Program data: RAP/GoVbk/6jSrNoAAAAAA0rAAAAAAAAHgBxcgEAAAAAAHFyAQAAAAAAAAAAAAAAAABxcgEAAAAAAAAAAAAAAAAAZRuYAQAAAAAAAAAAAAAAAJMNmAEAAAAAAAAAAAAAAAC0eAkAAAAAAByBCQAAAAAAWUbv4v////8ATIIsXDUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==',
19
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH consumed 102636 of 143817 compute units',
20
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH success',
21
+ ];
22
+ const cuUsage = parseLogsForCuUsage(logs);
23
+ expect(cuUsage).to.deep.equal([
24
+ {
25
+ name: 'CuUsage',
26
+ data: {
27
+ instruction: 'UpdateFundingRate',
28
+ cuUsage: 102636,
29
+ },
30
+ },
31
+ ]);
32
+ });
33
+
34
+ it('can parse multiple ixs', () => {
35
+ const logs = [
36
+ 'Program ComputeBudget111111111111111111111111111111 invoke [1]',
37
+ 'Program ComputeBudget111111111111111111111111111111 success',
38
+ 'Program ComputeBudget111111111111111111111111111111 invoke [1]',
39
+ 'Program ComputeBudget111111111111111111111111111111 success',
40
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH invoke [1]',
41
+ 'Program log: Instruction: PostPythLazerOracleUpdate',
42
+ 'Program log: Skipping new lazer update. current ts 1756622092550000 >= next ts 1756622092000000',
43
+ 'Program log: Skipping new lazer update. current ts 1756622092550000 >= next ts 1756622092000000',
44
+ 'Program log: Skipping new lazer update. current ts 1756622092550000 >= next ts 1756622092000000',
45
+ 'Program log: Price updated to 433158894',
46
+ 'Program log: Posting new lazer update. current ts 1756622079000000 < next ts 1756622092000000',
47
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH consumed 29242 of 199700 compute units',
48
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH success',
49
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH invoke [1]',
50
+ 'Program log: Instruction: UpdatePerpBidAskTwap',
51
+ 'Program log: estimated_bid = None estimated_ask = None',
52
+ 'Program log: after amm bid twap = 204332308 -> 204328128 \n ask twap = 204350474 -> 204347149 \n ts = 1756622080 -> 1756622092',
53
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH consumed 71006 of 170458 compute units',
54
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH success',
55
+ ];
56
+ const cuUsage = parseLogsForCuUsage(logs);
57
+ expect(cuUsage).to.deep.equal([
58
+ {
59
+ name: 'CuUsage',
60
+ data: {
61
+ instruction: 'PostPythLazerOracleUpdate',
62
+ cuUsage: 29242,
63
+ },
64
+ },
65
+ {
66
+ name: 'CuUsage',
67
+ data: {
68
+ instruction: 'UpdatePerpBidAskTwap',
69
+ cuUsage: 71006,
70
+ },
71
+ },
72
+ ]);
73
+ });
74
+
75
+ it('can parse ixs with CPI (swaps)', () => {
76
+ const logs = [
77
+ 'Program ComputeBudget111111111111111111111111111111 invoke [1]',
78
+ 'Program ComputeBudget111111111111111111111111111111 success',
79
+ 'Program ComputeBudget111111111111111111111111111111 invoke [1]',
80
+ 'Program ComputeBudget111111111111111111111111111111 success',
81
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH invoke [1]',
82
+ 'Program log: Instruction: BeginSwap',
83
+ 'Program data: t7rLuuG7X4K7X+loAAAAAAAAoDz72UK0JwQAAAAAAAAAAMS7r8ACAAAAAAAAAAAAAAB36Aiv26cZAwAAAAAAAAAArDDOLAMAAAAAAAAAAAAAAAA1DAAUzQAAoLsNAA==',
84
+ 'Program data: t7rLuuG7X4K7X+loAAAAAAEASQcRhBUVAQAAAAAAAAAAAG3WGn8CAAAAAAAAAAAAAADBBakRTIoAAAAAAAAAAAAAFfFDwgIAAAAAAAAAAAAAAAA1DACghgEAYOMWAA==',
85
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
86
+ 'Program log: Instruction: Transfer',
87
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4645 of 1336324 compute units',
88
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
89
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH consumed 79071 of 1399700 compute units',
90
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH success',
91
+ 'Program JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4 invoke [1]',
92
+ 'Program log: Instruction: Route',
93
+ 'Program SV2EYYJyRz2YhfXwXnhNAevDEui5Q6yrfyo13WtupPF invoke [2]',
94
+ 'Program data: S3VCwUhV8CXSyrcV3EtPUNCsQJvXpBqCGUobEJZVRnl5bVAAAAAAAFUFNBYAAAAAAAAAAAAAAAAAAAAAAAAAAA==',
95
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [3]',
96
+ 'Program log: Instruction: Transfer',
97
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4645 of 1255262 compute units',
98
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
99
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [3]',
100
+ 'Program log: Instruction: Transfer',
101
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4736 of 1249195 compute units',
102
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
103
+ 'Program SV2EYYJyRz2YhfXwXnhNAevDEui5Q6yrfyo13WtupPF consumed 69257 of 1311915 compute units',
104
+ 'Program SV2EYYJyRz2YhfXwXnhNAevDEui5Q6yrfyo13WtupPF success',
105
+ 'Program JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4 invoke [2]',
106
+ 'Program JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4 consumed 199 of 1241147 compute units',
107
+ 'Program JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4 success',
108
+ 'Program JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4 consumed 81059 of 1320629 compute units',
109
+ 'Program return: JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4 Z/tXxXEAAAA=',
110
+ 'Program JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4 success',
111
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH invoke [1]',
112
+ 'Program log: Instruction: EndSwap',
113
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
114
+ 'Program log: Instruction: Transfer',
115
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4736 of 1187840 compute units',
116
+ 'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
117
+ 'Program data: ort7woo4+vG7X+loAAAAAJ1Bg8Gp9WhWrw9VRm1UiC0KW6LRC2am2mjhfd3lzm6WZ/tXxXEAAAAA6HZIFwAAAAEAAAAJdDEMAAAAANFBDwAAAAAAAAAAAAAAAAA=',
118
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH consumed 156076 of 1239570 compute units',
119
+ 'Program dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH success',
120
+ ];
121
+ const cuUsage = parseLogsForCuUsage(logs);
122
+ expect(cuUsage).to.deep.equal([
123
+ {
124
+ name: 'CuUsage',
125
+ data: {
126
+ instruction: 'BeginSwap',
127
+ cuUsage: 79071,
128
+ },
129
+ },
130
+ {
131
+ name: 'CuUsage',
132
+ data: {
133
+ instruction: 'EndSwap',
134
+ cuUsage: 156076,
135
+ },
136
+ },
137
+ ]);
138
+ });
139
+ });