@parqxchange/sdk 0.1.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.
- package/LICENSE +21 -0
- package/README.md +122 -0
- package/dist/accounts/marketOracle.d.ts +21 -0
- package/dist/accounts/marketOracle.js +114 -0
- package/dist/accounts/resolve.d.ts +75 -0
- package/dist/accounts/resolve.js +116 -0
- package/dist/actions/cranks.d.ts +8 -0
- package/dist/actions/cranks.js +10 -0
- package/dist/actions/liquidity.d.ts +23 -0
- package/dist/actions/liquidity.js +21 -0
- package/dist/actions/trading.d.ts +39 -0
- package/dist/actions/trading.js +95 -0
- package/dist/auth/tradingKey.d.ts +27 -0
- package/dist/auth/tradingKey.js +71 -0
- package/dist/decode/index.d.ts +39 -0
- package/dist/decode/index.js +445 -0
- package/dist/events/decoder.d.ts +60 -0
- package/dist/events/decoder.js +369 -0
- package/dist/events/emitter.d.ts +49 -0
- package/dist/events/emitter.js +153 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +93 -0
- package/dist/indexer/client.d.ts +13 -0
- package/dist/indexer/client.js +36 -0
- package/dist/indexer/types.d.ts +9 -0
- package/dist/indexer/types.js +2 -0
- package/dist/minimal.d.ts +3 -0
- package/dist/minimal.js +13 -0
- package/dist/node.d.ts +1 -0
- package/dist/node.js +5 -0
- package/dist/programs/feeDistributor.d.ts +42 -0
- package/dist/programs/feeDistributor.js +42 -0
- package/dist/programs/oracle.d.ts +31 -0
- package/dist/programs/oracle.js +49 -0
- package/dist/programs/perp.d.ts +283 -0
- package/dist/programs/perp.js +346 -0
- package/dist/programs/pool.d.ts +113 -0
- package/dist/programs/pool.js +126 -0
- package/dist/programs/priceFeed.d.ts +13 -0
- package/dist/programs/priceFeed.js +21 -0
- package/dist/programs/staking.d.ts +76 -0
- package/dist/programs/staking.js +135 -0
- package/dist/types/index.d.ts +337 -0
- package/dist/types/index.js +9 -0
- package/dist/utils/computeBudget.d.ts +5 -0
- package/dist/utils/computeBudget.js +14 -0
- package/dist/utils/errors.d.ts +56 -0
- package/dist/utils/errors.js +138 -0
- package/dist/utils/pda.d.ts +28 -0
- package/dist/utils/pda.js +131 -0
- package/dist/utils/priceMath.d.ts +5 -0
- package/dist/utils/priceMath.js +46 -0
- package/dist/utils/tokenProgram.d.ts +2 -0
- package/dist/utils/tokenProgram.js +16 -0
- package/dist/utils/transaction.d.ts +2 -0
- package/dist/utils/transaction.js +12 -0
- package/dist/utils/version.d.ts +11 -0
- package/dist/utils/version.js +29 -0
- package/idl/fee_distributor.json +779 -0
- package/idl/oracle_adapter.json +767 -0
- package/idl/perp_engine.json +3908 -0
- package/idl/pool_program.json +2956 -0
- package/idl/price_feed.json +478 -0
- package/idl/staking.json +1336 -0
- package/package.json +98 -0
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodeAnchorEvent = decodeAnchorEvent;
|
|
4
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
+
const node_crypto_1 = require("node:crypto");
|
|
6
|
+
function sha256Disc(name) {
|
|
7
|
+
return (0, node_crypto_1.createHash)("sha256").update(name).digest().subarray(0, 8);
|
|
8
|
+
}
|
|
9
|
+
const EVENT_DISCS = {
|
|
10
|
+
PositionOpened: sha256Disc("event:PositionOpened"),
|
|
11
|
+
PositionClosed: sha256Disc("event:PositionClosed"),
|
|
12
|
+
Liquidated: sha256Disc("event:Liquidated"),
|
|
13
|
+
BadDebt: sha256Disc("event:BadDebt"),
|
|
14
|
+
FundingUpdated: sha256Disc("event:FundingUpdated"),
|
|
15
|
+
Enqueued: sha256Disc("event:Enqueued"),
|
|
16
|
+
Harvested: sha256Disc("event:Harvested"),
|
|
17
|
+
EntryVoided: sha256Disc("event:EntryVoided"),
|
|
18
|
+
SideBucketCredited: sha256Disc("event:SideBucketCredited"),
|
|
19
|
+
QueueDrained: sha256Disc("event:QueueDrained"),
|
|
20
|
+
PhantomCreditDrained: sha256Disc("event:PhantomCreditDrained"),
|
|
21
|
+
FeesSwept: sha256Disc("event:FeesSwept"),
|
|
22
|
+
FeesDistributed: sha256Disc("event:FeesDistributed"),
|
|
23
|
+
TreasuryWithdrawal: sha256Disc("event:TreasuryWithdrawal"),
|
|
24
|
+
Staked: sha256Disc("event:Staked"),
|
|
25
|
+
Unstaked: sha256Disc("event:Unstaked"),
|
|
26
|
+
RewardClaimed: sha256Disc("event:RewardClaimed"),
|
|
27
|
+
RewardIndexUpdated: sha256Disc("event:RewardIndexUpdated"),
|
|
28
|
+
CompoundedReward: sha256Disc("event:CompoundedReward"),
|
|
29
|
+
};
|
|
30
|
+
function discMatches(data, disc) {
|
|
31
|
+
for (let i = 0; i < 8; i++) {
|
|
32
|
+
if (data[i] !== disc[i])
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
function makeView(data) {
|
|
38
|
+
return new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
39
|
+
}
|
|
40
|
+
function readPubkey(view, offset) {
|
|
41
|
+
const bytes = new Uint8Array(view.buffer, view.byteOffset + offset, 32);
|
|
42
|
+
return new web3_js_1.PublicKey(bytes);
|
|
43
|
+
}
|
|
44
|
+
function readBytes(view, offset, len) {
|
|
45
|
+
return new Uint8Array(view.buffer.slice(view.byteOffset + offset, view.byteOffset + offset + len));
|
|
46
|
+
}
|
|
47
|
+
function readU64LE(view, offset) {
|
|
48
|
+
return view.getBigUint64(offset, true);
|
|
49
|
+
}
|
|
50
|
+
function readU128LE(view, offset) {
|
|
51
|
+
const lo = view.getBigUint64(offset, true);
|
|
52
|
+
const hi = view.getBigUint64(offset + 8, true);
|
|
53
|
+
return lo + (hi << 64n);
|
|
54
|
+
}
|
|
55
|
+
function readI64LE(view, offset) {
|
|
56
|
+
return view.getBigInt64(offset, true);
|
|
57
|
+
}
|
|
58
|
+
function readI128LE(view, offset) {
|
|
59
|
+
const lo = view.getBigUint64(offset, true);
|
|
60
|
+
const hi = view.getBigInt64(offset + 8, true);
|
|
61
|
+
return lo | (hi << 64n);
|
|
62
|
+
}
|
|
63
|
+
function readU8(view, offset) {
|
|
64
|
+
return view.getUint8(offset);
|
|
65
|
+
}
|
|
66
|
+
function readSide(view, offset) {
|
|
67
|
+
const v = view.getUint8(offset);
|
|
68
|
+
if (v === 0)
|
|
69
|
+
return "long";
|
|
70
|
+
if (v === 1)
|
|
71
|
+
return "short";
|
|
72
|
+
throw new Error(`invalid Side byte: ${v}`);
|
|
73
|
+
}
|
|
74
|
+
function decodePositionOpened(data) {
|
|
75
|
+
const v = makeView(data);
|
|
76
|
+
return {
|
|
77
|
+
owner: readPubkey(v, 8),
|
|
78
|
+
marketId: readBytes(v, 40, 32),
|
|
79
|
+
side: readSide(v, 72),
|
|
80
|
+
sizeUsdc: readU64LE(v, 73),
|
|
81
|
+
collateral: readU64LE(v, 81),
|
|
82
|
+
entryPrice: readU64LE(v, 89),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function decodePositionClosed(data) {
|
|
86
|
+
const v = makeView(data);
|
|
87
|
+
return {
|
|
88
|
+
owner: readPubkey(v, 8),
|
|
89
|
+
marketId: readBytes(v, 40, 32),
|
|
90
|
+
pnl: readI64LE(v, 72),
|
|
91
|
+
netReturn: readU64LE(v, 80),
|
|
92
|
+
exitPrice: readU64LE(v, 88),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function decodeLiquidated(data) {
|
|
96
|
+
const v = makeView(data);
|
|
97
|
+
return {
|
|
98
|
+
owner: readPubkey(v, 8),
|
|
99
|
+
marketId: readBytes(v, 40, 32),
|
|
100
|
+
badDebt: readU64LE(v, 72),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function decodeBadDebt(data) {
|
|
104
|
+
const v = makeView(data);
|
|
105
|
+
return {
|
|
106
|
+
marketId: readBytes(v, 8, 32),
|
|
107
|
+
amount: readU64LE(v, 40),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function decodeFundingUpdated(data) {
|
|
111
|
+
const v = makeView(data);
|
|
112
|
+
return {
|
|
113
|
+
marketId: readBytes(v, 8, 32),
|
|
114
|
+
fundingIndex: readI128LE(v, 40),
|
|
115
|
+
longOi: readU64LE(v, 56),
|
|
116
|
+
shortOi: readU64LE(v, 64),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function decodeEnqueued(data) {
|
|
120
|
+
const v = makeView(data);
|
|
121
|
+
return {
|
|
122
|
+
idx: readU64LE(v, 8),
|
|
123
|
+
owner: readPubkey(v, 16),
|
|
124
|
+
marketId: readBytes(v, 48, 32),
|
|
125
|
+
amount: readU64LE(v, 80),
|
|
126
|
+
queueTotalOwedAfter: readU64LE(v, 88),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function decodeHarvested(data) {
|
|
130
|
+
const v = makeView(data);
|
|
131
|
+
return {
|
|
132
|
+
idx: readU64LE(v, 8),
|
|
133
|
+
owner: readPubkey(v, 16),
|
|
134
|
+
marketId: readBytes(v, 48, 32),
|
|
135
|
+
amount: readU64LE(v, 80),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function decodeEntryVoided(data) {
|
|
139
|
+
const v = makeView(data);
|
|
140
|
+
return {
|
|
141
|
+
idx: readU64LE(v, 8),
|
|
142
|
+
owner: readPubkey(v, 16),
|
|
143
|
+
marketId: readBytes(v, 48, 32),
|
|
144
|
+
amount: readU64LE(v, 80),
|
|
145
|
+
reason: readU8(v, 88),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function decodeSideBucketCredited(data) {
|
|
149
|
+
const v = makeView(data);
|
|
150
|
+
return {
|
|
151
|
+
marketId: readBytes(v, 8, 32),
|
|
152
|
+
amount: readU64LE(v, 40),
|
|
153
|
+
source: readU8(v, 48),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function decodeQueueDrained(data) {
|
|
157
|
+
const v = makeView(data);
|
|
158
|
+
return {
|
|
159
|
+
marketId: readBytes(v, 8, 32),
|
|
160
|
+
residualToTreasury: readU64LE(v, 40),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function decodePhantomCreditDrained(data) {
|
|
164
|
+
const v = makeView(data);
|
|
165
|
+
return {
|
|
166
|
+
owner: readPubkey(v, 8),
|
|
167
|
+
marketId: readBytes(v, 40, 32),
|
|
168
|
+
amount: readU64LE(v, 72),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function decodeFeesSwept(data) {
|
|
172
|
+
const v = makeView(data);
|
|
173
|
+
return {
|
|
174
|
+
marketId: readBytes(v, 8, 32),
|
|
175
|
+
amount: readU64LE(v, 40),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function decodeFeesDistributed(data) {
|
|
179
|
+
const v = makeView(data);
|
|
180
|
+
return {
|
|
181
|
+
total: readU64LE(v, 8),
|
|
182
|
+
stakerShare: readU64LE(v, 16),
|
|
183
|
+
treasuryShare: readU64LE(v, 24),
|
|
184
|
+
referralShare: readU64LE(v, 32),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function decodeTreasuryWithdrawal(data) {
|
|
188
|
+
const v = makeView(data);
|
|
189
|
+
return {
|
|
190
|
+
amount: readU64LE(v, 8),
|
|
191
|
+
destination: readPubkey(v, 16),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function decodeStaked(data) {
|
|
195
|
+
const v = makeView(data);
|
|
196
|
+
return {
|
|
197
|
+
owner: readPubkey(v, 8),
|
|
198
|
+
amount: readU64LE(v, 40),
|
|
199
|
+
tier: readU8(v, 48),
|
|
200
|
+
lockupExpiry: readI64LE(v, 49),
|
|
201
|
+
weightedAmount: readU64LE(v, 57),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function decodeUnstaked(data) {
|
|
205
|
+
const v = makeView(data);
|
|
206
|
+
return {
|
|
207
|
+
owner: readPubkey(v, 8),
|
|
208
|
+
amount: readU64LE(v, 40),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function decodeRewardClaimed(data) {
|
|
212
|
+
const v = makeView(data);
|
|
213
|
+
return {
|
|
214
|
+
owner: readPubkey(v, 8),
|
|
215
|
+
amountUsdc: readU64LE(v, 40),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function decodeRewardIndexUpdated(data) {
|
|
219
|
+
if (data.length < 32) {
|
|
220
|
+
throw new Error(`RewardIndexUpdated: truncated payload (${data.length} < 32)`);
|
|
221
|
+
}
|
|
222
|
+
const v = makeView(data);
|
|
223
|
+
return {
|
|
224
|
+
rewardIndex: readU128LE(v, 8),
|
|
225
|
+
newRewards: readU64LE(v, 24),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function decodeCompoundedReward(data) {
|
|
229
|
+
const v = makeView(data);
|
|
230
|
+
return {
|
|
231
|
+
owner: readPubkey(v, 8),
|
|
232
|
+
amount: readU64LE(v, 40),
|
|
233
|
+
tier: readU8(v, 48),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
function tryDecodePayload(fn) {
|
|
237
|
+
try {
|
|
238
|
+
return fn();
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function decodeAnchorEvent(base64Data) {
|
|
245
|
+
let raw;
|
|
246
|
+
try {
|
|
247
|
+
raw = Buffer.from(base64Data, "base64");
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
if (raw.length < 8)
|
|
253
|
+
return null;
|
|
254
|
+
if (discMatches(raw, EVENT_DISCS.PositionOpened)) {
|
|
255
|
+
const data = tryDecodePayload(() => decodePositionOpened(raw));
|
|
256
|
+
if (data)
|
|
257
|
+
return { type: "positionOpened", data };
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
if (discMatches(raw, EVENT_DISCS.PositionClosed)) {
|
|
261
|
+
const data = tryDecodePayload(() => decodePositionClosed(raw));
|
|
262
|
+
if (data)
|
|
263
|
+
return { type: "positionClosed", data };
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
if (discMatches(raw, EVENT_DISCS.Liquidated)) {
|
|
267
|
+
const data = tryDecodePayload(() => decodeLiquidated(raw));
|
|
268
|
+
if (data)
|
|
269
|
+
return { type: "liquidated", data };
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
if (discMatches(raw, EVENT_DISCS.BadDebt)) {
|
|
273
|
+
const data = tryDecodePayload(() => decodeBadDebt(raw));
|
|
274
|
+
if (data)
|
|
275
|
+
return { type: "badDebt", data };
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
if (discMatches(raw, EVENT_DISCS.FundingUpdated)) {
|
|
279
|
+
const data = tryDecodePayload(() => decodeFundingUpdated(raw));
|
|
280
|
+
if (data)
|
|
281
|
+
return { type: "fundingUpdated", data };
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
if (discMatches(raw, EVENT_DISCS.Enqueued)) {
|
|
285
|
+
const data = tryDecodePayload(() => decodeEnqueued(raw));
|
|
286
|
+
if (data)
|
|
287
|
+
return { type: "enqueued", data };
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
if (discMatches(raw, EVENT_DISCS.Harvested)) {
|
|
291
|
+
const data = tryDecodePayload(() => decodeHarvested(raw));
|
|
292
|
+
if (data)
|
|
293
|
+
return { type: "harvested", data };
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
if (discMatches(raw, EVENT_DISCS.EntryVoided)) {
|
|
297
|
+
const data = tryDecodePayload(() => decodeEntryVoided(raw));
|
|
298
|
+
if (data)
|
|
299
|
+
return { type: "entryVoided", data };
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
if (discMatches(raw, EVENT_DISCS.SideBucketCredited)) {
|
|
303
|
+
const data = tryDecodePayload(() => decodeSideBucketCredited(raw));
|
|
304
|
+
if (data)
|
|
305
|
+
return { type: "sideBucketCredited", data };
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
if (discMatches(raw, EVENT_DISCS.QueueDrained)) {
|
|
309
|
+
const data = tryDecodePayload(() => decodeQueueDrained(raw));
|
|
310
|
+
if (data)
|
|
311
|
+
return { type: "queueDrained", data };
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
if (discMatches(raw, EVENT_DISCS.PhantomCreditDrained)) {
|
|
315
|
+
const data = tryDecodePayload(() => decodePhantomCreditDrained(raw));
|
|
316
|
+
if (data)
|
|
317
|
+
return { type: "phantomCreditDrained", data };
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
if (discMatches(raw, EVENT_DISCS.FeesSwept)) {
|
|
321
|
+
const data = tryDecodePayload(() => decodeFeesSwept(raw));
|
|
322
|
+
if (data)
|
|
323
|
+
return { type: "feesSwept", data };
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
if (discMatches(raw, EVENT_DISCS.FeesDistributed)) {
|
|
327
|
+
const data = tryDecodePayload(() => decodeFeesDistributed(raw));
|
|
328
|
+
if (data)
|
|
329
|
+
return { type: "feesDistributed", data };
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
if (discMatches(raw, EVENT_DISCS.TreasuryWithdrawal)) {
|
|
333
|
+
const data = tryDecodePayload(() => decodeTreasuryWithdrawal(raw));
|
|
334
|
+
if (data)
|
|
335
|
+
return { type: "treasuryWithdrawal", data };
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
if (discMatches(raw, EVENT_DISCS.Staked)) {
|
|
339
|
+
const data = tryDecodePayload(() => decodeStaked(raw));
|
|
340
|
+
if (data)
|
|
341
|
+
return { type: "staked", data };
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
if (discMatches(raw, EVENT_DISCS.Unstaked)) {
|
|
345
|
+
const data = tryDecodePayload(() => decodeUnstaked(raw));
|
|
346
|
+
if (data)
|
|
347
|
+
return { type: "unstaked", data };
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
if (discMatches(raw, EVENT_DISCS.RewardClaimed)) {
|
|
351
|
+
const data = tryDecodePayload(() => decodeRewardClaimed(raw));
|
|
352
|
+
if (data)
|
|
353
|
+
return { type: "rewardClaimed", data };
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
if (discMatches(raw, EVENT_DISCS.RewardIndexUpdated)) {
|
|
357
|
+
const data = tryDecodePayload(() => decodeRewardIndexUpdated(raw));
|
|
358
|
+
if (data)
|
|
359
|
+
return { type: "rewardIndexUpdated", data };
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
if (discMatches(raw, EVENT_DISCS.CompoundedReward)) {
|
|
363
|
+
const data = tryDecodePayload(() => decodeCompoundedReward(raw));
|
|
364
|
+
if (data)
|
|
365
|
+
return { type: "compoundedReward", data };
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Connection, PublicKey } from "@solana/web3.js";
|
|
2
|
+
import EventEmitter from "events";
|
|
3
|
+
import type { Position, MarketState, Order, PositionOpenedEvent, PositionClosedEvent, LiquidatedEvent, BadDebtEvent, FundingUpdatedEvent, EnqueuedEvent, HarvestedEvent, EntryVoidedEvent, SideBucketCreditedEvent, QueueDrainedEvent, PhantomCreditDrainedEvent, FeesSweptEvent, FeesDistributedEvent, TreasuryWithdrawalEvent, StakedEvent, UnstakedEvent, RewardClaimedEvent, RewardIndexUpdatedEvent, CompoundedRewardEvent } from "../types";
|
|
4
|
+
export type ParquetEventType = "positionOpened" | "positionClosed" | "liquidated" | "badDebt" | "fundingUpdated" | "enqueued" | "harvested" | "entryVoided" | "sideBucketCredited" | "queueDrained" | "phantomCreditDrained" | "feesSwept" | "feesDistributed" | "treasuryWithdrawal" | "staked" | "unstaked" | "rewardClaimed" | "rewardIndexUpdated" | "compoundedReward" | "positionChanged" | "marketStateChanged" | "orderChanged";
|
|
5
|
+
export interface ParquetEventEmitterEvents {
|
|
6
|
+
positionOpened: (data: PositionOpenedEvent) => void;
|
|
7
|
+
positionClosed: (data: PositionClosedEvent) => void;
|
|
8
|
+
liquidated: (data: LiquidatedEvent) => void;
|
|
9
|
+
badDebt: (data: BadDebtEvent) => void;
|
|
10
|
+
fundingUpdated: (data: FundingUpdatedEvent) => void;
|
|
11
|
+
enqueued: (data: EnqueuedEvent) => void;
|
|
12
|
+
harvested: (data: HarvestedEvent) => void;
|
|
13
|
+
entryVoided: (data: EntryVoidedEvent) => void;
|
|
14
|
+
sideBucketCredited: (data: SideBucketCreditedEvent) => void;
|
|
15
|
+
queueDrained: (data: QueueDrainedEvent) => void;
|
|
16
|
+
phantomCreditDrained: (data: PhantomCreditDrainedEvent) => void;
|
|
17
|
+
feesSwept: (data: FeesSweptEvent) => void;
|
|
18
|
+
feesDistributed: (data: FeesDistributedEvent) => void;
|
|
19
|
+
treasuryWithdrawal: (data: TreasuryWithdrawalEvent) => void;
|
|
20
|
+
staked: (data: StakedEvent) => void;
|
|
21
|
+
unstaked: (data: UnstakedEvent) => void;
|
|
22
|
+
rewardClaimed: (data: RewardClaimedEvent) => void;
|
|
23
|
+
rewardIndexUpdated: (data: RewardIndexUpdatedEvent) => void;
|
|
24
|
+
compoundedReward: (data: CompoundedRewardEvent) => void;
|
|
25
|
+
positionChanged: (payload: {
|
|
26
|
+
pubkey: PublicKey;
|
|
27
|
+
account: Position;
|
|
28
|
+
}) => void;
|
|
29
|
+
marketStateChanged: (payload: {
|
|
30
|
+
pubkey: PublicKey;
|
|
31
|
+
account: MarketState;
|
|
32
|
+
}) => void;
|
|
33
|
+
orderChanged: (payload: {
|
|
34
|
+
pubkey: PublicKey;
|
|
35
|
+
account: Order;
|
|
36
|
+
}) => void;
|
|
37
|
+
error: (err: Error) => void;
|
|
38
|
+
}
|
|
39
|
+
export declare class ParquetEventEmitter extends EventEmitter {
|
|
40
|
+
private readonly connection;
|
|
41
|
+
private readonly perpEngineId;
|
|
42
|
+
private logsSubId;
|
|
43
|
+
private accountSubId;
|
|
44
|
+
constructor(connection: Connection, perpEngineId: PublicKey);
|
|
45
|
+
startEvents(): void;
|
|
46
|
+
startAccounts(): void;
|
|
47
|
+
startAll(): void;
|
|
48
|
+
stop(): Promise<void>;
|
|
49
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ParquetEventEmitter = void 0;
|
|
7
|
+
const events_1 = __importDefault(require("events"));
|
|
8
|
+
const decoder_1 = require("./decoder");
|
|
9
|
+
const decode_1 = require("../decode");
|
|
10
|
+
const LOG_PREFIX = "Program log: ";
|
|
11
|
+
class ParquetEventEmitter extends events_1.default {
|
|
12
|
+
connection;
|
|
13
|
+
perpEngineId;
|
|
14
|
+
logsSubId = null;
|
|
15
|
+
accountSubId = null;
|
|
16
|
+
constructor(connection, perpEngineId) {
|
|
17
|
+
super();
|
|
18
|
+
this.connection = connection;
|
|
19
|
+
this.perpEngineId = perpEngineId;
|
|
20
|
+
}
|
|
21
|
+
startEvents() {
|
|
22
|
+
if (this.logsSubId !== null)
|
|
23
|
+
return;
|
|
24
|
+
this.logsSubId = this.connection.onLogs(this.perpEngineId, (logs, _ctx) => {
|
|
25
|
+
try {
|
|
26
|
+
for (const line of logs.logs) {
|
|
27
|
+
if (!line.startsWith(LOG_PREFIX))
|
|
28
|
+
continue;
|
|
29
|
+
const base64 = line.slice(LOG_PREFIX.length);
|
|
30
|
+
const decoded = (0, decoder_1.decodeAnchorEvent)(base64);
|
|
31
|
+
if (decoded === null)
|
|
32
|
+
continue;
|
|
33
|
+
switch (decoded.type) {
|
|
34
|
+
case "positionOpened":
|
|
35
|
+
this.emit("positionOpened", decoded.data);
|
|
36
|
+
break;
|
|
37
|
+
case "positionClosed":
|
|
38
|
+
this.emit("positionClosed", decoded.data);
|
|
39
|
+
break;
|
|
40
|
+
case "liquidated":
|
|
41
|
+
this.emit("liquidated", decoded.data);
|
|
42
|
+
break;
|
|
43
|
+
case "badDebt":
|
|
44
|
+
this.emit("badDebt", decoded.data);
|
|
45
|
+
break;
|
|
46
|
+
case "fundingUpdated":
|
|
47
|
+
this.emit("fundingUpdated", decoded.data);
|
|
48
|
+
break;
|
|
49
|
+
case "enqueued":
|
|
50
|
+
this.emit("enqueued", decoded.data);
|
|
51
|
+
break;
|
|
52
|
+
case "harvested":
|
|
53
|
+
this.emit("harvested", decoded.data);
|
|
54
|
+
break;
|
|
55
|
+
case "entryVoided":
|
|
56
|
+
this.emit("entryVoided", decoded.data);
|
|
57
|
+
break;
|
|
58
|
+
case "sideBucketCredited":
|
|
59
|
+
this.emit("sideBucketCredited", decoded.data);
|
|
60
|
+
break;
|
|
61
|
+
case "queueDrained":
|
|
62
|
+
this.emit("queueDrained", decoded.data);
|
|
63
|
+
break;
|
|
64
|
+
case "phantomCreditDrained":
|
|
65
|
+
this.emit("phantomCreditDrained", decoded.data);
|
|
66
|
+
break;
|
|
67
|
+
case "feesSwept":
|
|
68
|
+
this.emit("feesSwept", decoded.data);
|
|
69
|
+
break;
|
|
70
|
+
case "feesDistributed":
|
|
71
|
+
this.emit("feesDistributed", decoded.data);
|
|
72
|
+
break;
|
|
73
|
+
case "treasuryWithdrawal":
|
|
74
|
+
this.emit("treasuryWithdrawal", decoded.data);
|
|
75
|
+
break;
|
|
76
|
+
case "staked":
|
|
77
|
+
this.emit("staked", decoded.data);
|
|
78
|
+
break;
|
|
79
|
+
case "unstaked":
|
|
80
|
+
this.emit("unstaked", decoded.data);
|
|
81
|
+
break;
|
|
82
|
+
case "rewardClaimed":
|
|
83
|
+
this.emit("rewardClaimed", decoded.data);
|
|
84
|
+
break;
|
|
85
|
+
case "rewardIndexUpdated":
|
|
86
|
+
this.emit("rewardIndexUpdated", decoded.data);
|
|
87
|
+
break;
|
|
88
|
+
case "compoundedReward":
|
|
89
|
+
this.emit("compoundedReward", decoded.data);
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
this.emit("error", err instanceof Error ? err : new Error(String(err)));
|
|
96
|
+
}
|
|
97
|
+
}, "confirmed");
|
|
98
|
+
}
|
|
99
|
+
startAccounts() {
|
|
100
|
+
if (this.accountSubId !== null)
|
|
101
|
+
return;
|
|
102
|
+
this.accountSubId = this.connection.onProgramAccountChange(this.perpEngineId, (keyedAccountInfo, _ctx) => {
|
|
103
|
+
try {
|
|
104
|
+
const pubkey = keyedAccountInfo.accountId;
|
|
105
|
+
const data = Buffer.from(keyedAccountInfo.accountInfo.data);
|
|
106
|
+
if (data.length < 8)
|
|
107
|
+
return;
|
|
108
|
+
const accountType = (0, decode_1.identifyAccountType)(data);
|
|
109
|
+
if (accountType === null)
|
|
110
|
+
return;
|
|
111
|
+
switch (accountType) {
|
|
112
|
+
case "Position": {
|
|
113
|
+
const account = (0, decode_1.decodePosition)(data);
|
|
114
|
+
this.emit("positionChanged", { pubkey, account });
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case "MarketState": {
|
|
118
|
+
const account = (0, decode_1.decodeMarketState)(data);
|
|
119
|
+
this.emit("marketStateChanged", { pubkey, account });
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case "Order": {
|
|
123
|
+
const account = (0, decode_1.decodeOrder)(data);
|
|
124
|
+
this.emit("orderChanged", { pubkey, account });
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
default:
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
this.emit("error", err instanceof Error ? err : new Error(String(err)));
|
|
133
|
+
}
|
|
134
|
+
}, "confirmed");
|
|
135
|
+
}
|
|
136
|
+
startAll() {
|
|
137
|
+
this.startEvents();
|
|
138
|
+
this.startAccounts();
|
|
139
|
+
}
|
|
140
|
+
async stop() {
|
|
141
|
+
const removals = [];
|
|
142
|
+
if (this.logsSubId !== null) {
|
|
143
|
+
removals.push(this.connection.removeOnLogsListener(this.logsSubId));
|
|
144
|
+
this.logsSubId = null;
|
|
145
|
+
}
|
|
146
|
+
if (this.accountSubId !== null) {
|
|
147
|
+
removals.push(this.connection.removeProgramAccountChangeListener(this.accountSubId));
|
|
148
|
+
this.accountSubId = null;
|
|
149
|
+
}
|
|
150
|
+
await Promise.all(removals);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
exports.ParquetEventEmitter = ParquetEventEmitter;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export * from "./utils/pda";
|
|
2
|
+
export * from "./utils/errors";
|
|
3
|
+
export * from "./utils/priceMath";
|
|
4
|
+
export { withComputeBudget } from "./utils/computeBudget";
|
|
5
|
+
export { buildVersionedTransaction } from "./utils/transaction";
|
|
6
|
+
export { detectProgramVersion, PROGRAM_IDS_V4, type MarketStateDataLayout, } from "./utils/version";
|
|
7
|
+
export { probeTokenProgram } from "./utils/tokenProgram";
|
|
8
|
+
export * from "./types";
|
|
9
|
+
export { decodePosition, decodeMarketState, decodePoolState, decodeOrder, decodeOrderNonce, decodeTradingKey, decodeReferralConfig, decodeReferralCode, decodeTraderReferral, decodeAffiliateReward, decodeFeePool, decodeStakingPool, decodeStakePosition, decodePayoutQueueEntry, decodeUserQueueClaims, identifyAccountType, DecoderError, DISCRIMINATORS, type AccountTypeName, } from "./decode";
|
|
10
|
+
export { PoolClient } from "./programs/pool";
|
|
11
|
+
export { PerpClient, type OpenPositionArgs } from "./programs/perp";
|
|
12
|
+
export { OracleClient } from "./programs/oracle";
|
|
13
|
+
export { PriceFeedClient, PRICE_FEED_PROGRAM_ID } from "./programs/priceFeed";
|
|
14
|
+
export { FeeDistributorClient } from "./programs/feeDistributor";
|
|
15
|
+
export { StakingClient } from "./programs/staking";
|
|
16
|
+
export { addLiquidity, removeLiquidity, type LiquidityOpts } from "./actions/liquidity";
|
|
17
|
+
export { openPosition, closePosition, updateMargin, type TradingOpts } from "./actions/trading";
|
|
18
|
+
export { crankFundingRate } from "./actions/cranks";
|
|
19
|
+
export { resolveOpenPositionAccounts, resolveClosePositionAccounts, resolveUpdateMarginAccounts, resolveLiquidityAccounts, type ProgramIds, } from "./accounts/resolve";
|
|
20
|
+
export { getMarketOracleFeeds, clearMarketOracleFeedsCache, MARKET_ORACLE_V2_LEN, type MarketOracleFeeds, } from "./accounts/marketOracle";
|
|
21
|
+
export { registerTradingKey, revokeTradingKey, buildRegisterTradingKeyIx, buildRevokeTradingKeyIx, } from "./auth/tradingKey";
|
|
22
|
+
export { ParquetEventEmitter, type ParquetEventType, type ParquetEventEmitterEvents } from "./events/emitter";
|
|
23
|
+
export { decodeAnchorEvent, type DecodedEvent } from "./events/decoder";
|
|
24
|
+
export { IndexerClient, IndexerError } from "./indexer/client";
|
|
25
|
+
export type { StatsResponse } from "./indexer/types";
|