Skip to content

Commit ecf25d9

Browse files
committed
Merge branch 'master' into wphan/whitelist-titan-swap-program
2 parents e52a2c7 + 4e1f214 commit ecf25d9

File tree

7 files changed

+347
-11
lines changed

7 files changed

+347
-11
lines changed

sdk/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.142.0-beta.20
1+
2.142.0-beta.23

sdk/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@drift-labs/sdk",
3-
"version": "2.142.0-beta.20",
3+
"version": "2.142.0-beta.23",
44
"main": "lib/node/index.js",
55
"types": "lib/node/index.d.ts",
66
"module": "./lib/browser/index.js",
@@ -20,6 +20,7 @@
2020
"test:bignum": "mocha -r ts-node/register tests/bn/**/*.ts",
2121
"test:ci": "mocha -r ts-node/register tests/ci/**/*.ts",
2222
"test:dlob": "mocha -r ts-node/register tests/dlob/**/*.ts",
23+
"test:events": "mocha -r ts-node/register tests/events/**/*.ts",
2324
"patch-and-pub": "npm version patch --force && npm publish",
2425
"prettify": "prettier --check './src/***/*.ts'",
2526
"prettify:fix": "prettier --write './{src,tests}/***/*.ts'",

sdk/src/constants/perpMarkets.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,43 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
13511351
'0xf2b3ab1c49e35e881003c3c0482d18b181a1560b697b844c24c8f85aba1cab95',
13521352
pythLazerId: 2316,
13531353
},
1354+
{
1355+
fullName: 'ZCash',
1356+
category: ['Privacy'],
1357+
symbol: 'ZEC-PERP',
1358+
baseAssetSymbol: 'ZEC',
1359+
marketIndex: 79,
1360+
oracle: new PublicKey('BXunfRSyiQWJHv88qMvE42mpMpksWEC8Bf13p2msnRms'),
1361+
launchTs: 1760366017000,
1362+
oracleSource: OracleSource.PYTH_LAZER,
1363+
pythFeedId:
1364+
'0xbe9b59d178f0d6a97ab4c343bff2aa69caa1eaae3e9048a65788c529b125bb24',
1365+
pythLazerId: 66,
1366+
},
1367+
{
1368+
fullName: 'Mantle',
1369+
category: ['L1'],
1370+
symbol: 'MNT-PERP',
1371+
baseAssetSymbol: 'MNT',
1372+
marketIndex: 80,
1373+
oracle: new PublicKey('Gy7cJ4U1nxMA44XXC3hwqkpcxEB1mZTYiwJVkaqZfU7u'),
1374+
launchTs: 1760366017000,
1375+
oracleSource: OracleSource.PYTH_LAZER,
1376+
pythFeedId:
1377+
'0x4e3037c822d852d79af3ac80e35eb420ee3b870dca49f9344a38ef4773fb0585',
1378+
pythLazerId: 199,
1379+
},
1380+
{
1381+
fullName: '1KPUMP',
1382+
category: ['Launchpad'],
1383+
symbol: '1KPUMP-PERP',
1384+
baseAssetSymbol: '1KPUMP',
1385+
marketIndex: 81,
1386+
oracle: new PublicKey('5r8RWTaRiMgr9Lph3FTUE3sGb1vymhpCrm83Bovjfcps'),
1387+
launchTs: 1760366017000,
1388+
oracleSource: OracleSource.PYTH_LAZER_1K,
1389+
pythLazerId: 1578,
1390+
},
13541391
];
13551392

13561393
export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {

sdk/src/driftClient.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4761,11 +4761,19 @@ export class DriftClient {
47614761
orderIds?: number[],
47624762
txParams?: TxParams,
47634763
subAccountId?: number,
4764-
user?: User
4764+
user?: User,
4765+
overrides?: {
4766+
authority?: PublicKey;
4767+
}
47654768
): Promise<TransactionSignature> {
47664769
const { txSig } = await this.sendTransaction(
47674770
await this.buildTransaction(
4768-
await this.getCancelOrdersByIdsIx(orderIds, subAccountId, user),
4771+
await this.getCancelOrdersByIdsIx(
4772+
orderIds,
4773+
subAccountId,
4774+
user,
4775+
overrides
4776+
),
47694777
txParams
47704778
),
47714779
[],
@@ -4785,7 +4793,10 @@ export class DriftClient {
47854793
public async getCancelOrdersByIdsIx(
47864794
orderIds?: number[],
47874795
subAccountId?: number,
4788-
user?: User
4796+
user?: User,
4797+
overrides?: {
4798+
authority?: PublicKey;
4799+
}
47894800
): Promise<TransactionInstruction> {
47904801
const userAccountPubKey =
47914802
user?.userAccountPublicKey ??
@@ -4798,11 +4809,13 @@ export class DriftClient {
47984809
useMarketLastSlotCache: true,
47994810
});
48004811

4812+
const authority = overrides?.authority ?? this.wallet.publicKey;
4813+
48014814
return await this.program.instruction.cancelOrdersByIds(orderIds, {
48024815
accounts: {
48034816
state: await this.getStatePublicKey(),
48044817
user: userAccountPubKey,
4805-
authority: this.wallet.publicKey,
4818+
authority,
48064819
},
48074820
remainingAccounts,
48084821
});
@@ -4943,7 +4956,10 @@ export class DriftClient {
49434956

49444957
public async getPlaceOrdersIx(
49454958
params: OptionalOrderParams[],
4946-
subAccountId?: number
4959+
subAccountId?: number,
4960+
overrides?: {
4961+
authority?: PublicKey;
4962+
}
49474963
): Promise<TransactionInstruction> {
49484964
const user = await this.getUserAccountPublicKey(subAccountId);
49494965

@@ -4978,13 +4994,14 @@ export class DriftClient {
49784994
}
49794995

49804996
const formattedParams = params.map((item) => getOrderParams(item));
4997+
const authority = overrides?.authority ?? this.wallet.publicKey;
49814998

49824999
return await this.program.instruction.placeOrders(formattedParams, {
49835000
accounts: {
49845001
state: await this.getStatePublicKey(),
49855002
user,
49865003
userStats: this.getUserStatsAccountPublicKey(),
4987-
authority: this.wallet.publicKey,
5004+
authority,
49885005
},
49895006
remainingAccounts,
49905007
});
@@ -6688,7 +6705,10 @@ export class DriftClient {
66886705
referrerInfo?: ReferrerInfo,
66896706
successCondition?: PlaceAndTakeOrderSuccessCondition,
66906707
auctionDurationPercentage?: number,
6691-
subAccountId?: number
6708+
subAccountId?: number,
6709+
overrides?: {
6710+
authority?: PublicKey;
6711+
}
66926712
): Promise<TransactionInstruction> {
66936713
orderParams = getOrderParams(orderParams, { marketType: MarketType.PERP });
66946714
const userStatsPublicKey = await this.getUserStatsAccountPublicKey();
@@ -6756,6 +6776,8 @@ export class DriftClient {
67566776
((auctionDurationPercentage ?? 100) << 8) | (successCondition ?? 0);
67576777
}
67586778

6779+
const authority = overrides?.authority ?? this.wallet.publicKey;
6780+
67596781
return await this.program.instruction.placeAndTakePerpOrder(
67606782
orderParams,
67616783
optionalParams,
@@ -6764,7 +6786,7 @@ export class DriftClient {
67646786
state: await this.getStatePublicKey(),
67656787
user,
67666788
userStats: userStatsPublicKey,
6767-
authority: this.wallet.publicKey,
6789+
authority,
67686790
},
67696791
remainingAccounts,
67706792
}

sdk/src/events/parse.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Program, Event } from '@coral-xyz/anchor';
2+
import { CuUsageEvent } from './types';
23

34
const driftProgramId = 'dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH';
45
const PROGRAM_LOG = 'Program log: ';
6+
const PROGRAM_INSTRUCTION = 'Program log: Instruction: ';
57
const PROGRAM_DATA = 'Program data: ';
68
const PROGRAM_LOG_START_INDEX = PROGRAM_LOG.length;
79
const PROGRAM_DATA_START_INDEX = PROGRAM_DATA.length;
10+
const PROGRAM_INSTRUCTION_START_INDEX = PROGRAM_INSTRUCTION.length;
811

912
export function parseLogs(
1013
program: Program,
@@ -112,6 +115,7 @@ function handleSystemLog(
112115
// executing for a given log.
113116
class ExecutionContext {
114117
stack: string[] = [];
118+
ixStack: string[] = [];
115119

116120
program(): string {
117121
if (!this.stack.length) {
@@ -130,4 +134,115 @@ class ExecutionContext {
130134
}
131135
this.stack.pop();
132136
}
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+
}
133248
}

sdk/src/events/types.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ export type DriftEvent =
149149
| Event<LPSettleRecord>
150150
| Event<LPMintRedeemRecord>
151151
| Event<LPSwapRecord>
152-
| Event<LPBorrowLendDepositRecord>;
152+
| Event<LPBorrowLendDepositRecord>
153+
| Event<CuUsage>;
153154

154155
export interface EventSubscriberEvents {
155156
newEvent: (event: WrappedEvent<EventType>) => void;
@@ -213,3 +214,24 @@ export type LogProviderConfig =
213214
| WebSocketLogProviderConfig
214215
| PollingLogProviderConfig
215216
| 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+
};

0 commit comments

Comments
 (0)