@percolatortool/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/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # @percolatortool/sdk
2
+
3
+ TypeScript types and instruction builders for building **Percolator wrapper** programs and frontends.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @percolatortool/sdk
9
+ # or from repo
10
+ npm install ./path/to/percolator-tools/sdk
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```ts
16
+ import {
17
+ buildDepositInstructionData,
18
+ buildWithdrawInstructionData,
19
+ buildExecuteTradeInstructionData,
20
+ buildKeeperCrankInstructionData,
21
+ type DepositArgs,
22
+ type KeeperCrankArgs,
23
+ } from '@percolatortool/sdk';
24
+
25
+ // Deposit
26
+ const depositData = buildDepositInstructionData({
27
+ accountIndex: 0,
28
+ amount: BigInt(1_000_000),
29
+ nowSlot: 12345,
30
+ });
31
+
32
+ // Keeper crank (permissionless)
33
+ const crankData = buildKeeperCrankInstructionData({
34
+ callerIndex: 0,
35
+ nowSlot: 12345,
36
+ oraclePrice: 1_000_000,
37
+ fundingRateBpsPerSlot: 1,
38
+ allowPanic: false,
39
+ });
40
+ ```
41
+
42
+ Then pass `depositData` / `crankData` as the `data` for a `TransactionInstruction` to your wrapper program ID.
43
+
44
+ ## Note
45
+
46
+ Instruction **layout** (discriminators, field order) is defined by your **wrapper** program. This SDK uses a minimal layout; if your wrapper uses Anchor or a different layout, adapt the builders or use this as reference.
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Percolator SDK — types and instruction builders for wrapper programs.
3
+ * Engine does not move tokens; wrapper must do SPL transfers and call engine.
4
+ *
5
+ * @see https://github.com/aeyakovenko/percolator
6
+ */
7
+ export type U128 = {
8
+ lo: bigint;
9
+ hi: bigint;
10
+ } | bigint;
11
+ export type I128 = {
12
+ lo: bigint;
13
+ hi: bigint;
14
+ } | bigint;
15
+ export interface RiskEngineState {
16
+ vault: bigint;
17
+ insuranceBalance: bigint;
18
+ cTot: bigint;
19
+ pnlPosTot: bigint;
20
+ totalOpenInterest: bigint;
21
+ currentSlot: number;
22
+ lastCrankSlot: number;
23
+ fundingIndexQpbE6: bigint;
24
+ lastFundingSlot: number;
25
+ fundingRateBpsPerSlotLast: number;
26
+ lifetimeLiquidations: number;
27
+ numUsedAccounts: number;
28
+ }
29
+ export interface AccountSummary {
30
+ accountIndex: number;
31
+ accountId: number;
32
+ kind: 'user' | 'lp';
33
+ capital: bigint;
34
+ pnl: bigint;
35
+ positionSize: bigint;
36
+ entryPrice: number;
37
+ owner: string;
38
+ }
39
+ export interface DepositArgs {
40
+ accountIndex: number;
41
+ amount: bigint;
42
+ nowSlot: number;
43
+ }
44
+ export interface WithdrawArgs {
45
+ accountIndex: number;
46
+ amount: bigint;
47
+ nowSlot: number;
48
+ oraclePrice: number;
49
+ }
50
+ export interface ExecuteTradeArgs {
51
+ lpIndex: number;
52
+ userIndex: number;
53
+ nowSlot: number;
54
+ oraclePrice: number;
55
+ size: bigint;
56
+ }
57
+ export interface KeeperCrankArgs {
58
+ callerIndex: number;
59
+ nowSlot: number;
60
+ oraclePrice: number;
61
+ fundingRateBpsPerSlot: number;
62
+ allowPanic: boolean;
63
+ }
64
+ export declare const INSTRUCTION_NAMES: {
65
+ readonly deposit: 0;
66
+ readonly withdraw: 1;
67
+ readonly executeTrade: 2;
68
+ readonly keeperCrank: 3;
69
+ readonly addUser: 4;
70
+ readonly addLp: 5;
71
+ };
72
+ /**
73
+ * Build deposit instruction data.
74
+ * Wrapper must: transfer tokens to vault, then call engine.deposit(idx, amount, now_slot).
75
+ */
76
+ export declare function buildDepositInstructionData(args: DepositArgs): Buffer;
77
+ /**
78
+ * Build withdraw instruction data.
79
+ * Wrapper must: call engine.withdraw(...); if Ok, transfer tokens out of vault.
80
+ */
81
+ export declare function buildWithdrawInstructionData(args: WithdrawArgs): Buffer;
82
+ /**
83
+ * Build execute_trade instruction data.
84
+ * Wrapper must validate signers and oracle, then call engine.execute_trade(...).
85
+ */
86
+ export declare function buildExecuteTradeInstructionData(args: ExecuteTradeArgs): Buffer;
87
+ /**
88
+ * Build keeper_crank instruction data.
89
+ * Permissionless; anyone can call. Wrapper forwards to engine.keeper_crank(...).
90
+ */
91
+ export declare function buildKeeperCrankInstructionData(args: KeeperCrankArgs): Buffer;
package/dist/index.js ADDED
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ /**
3
+ * Percolator SDK — types and instruction builders for wrapper programs.
4
+ * Engine does not move tokens; wrapper must do SPL transfers and call engine.
5
+ *
6
+ * @see https://github.com/aeyakovenko/percolator
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.INSTRUCTION_NAMES = void 0;
10
+ exports.buildDepositInstructionData = buildDepositInstructionData;
11
+ exports.buildWithdrawInstructionData = buildWithdrawInstructionData;
12
+ exports.buildExecuteTradeInstructionData = buildExecuteTradeInstructionData;
13
+ exports.buildKeeperCrankInstructionData = buildKeeperCrankInstructionData;
14
+ // -----------------------------------------------------------------------------
15
+ // Instruction discriminators (wrapper-specific; replace with your program's)
16
+ // -----------------------------------------------------------------------------
17
+ exports.INSTRUCTION_NAMES = {
18
+ deposit: 0x00,
19
+ withdraw: 0x01,
20
+ executeTrade: 0x02,
21
+ keeperCrank: 0x03,
22
+ addUser: 0x04,
23
+ addLp: 0x05,
24
+ };
25
+ // -----------------------------------------------------------------------------
26
+ // Instruction builders (serialize args for CPI; layout is wrapper-defined)
27
+ // -----------------------------------------------------------------------------
28
+ function encodeU64(n) {
29
+ const b = Buffer.alloc(8);
30
+ b.writeBigUInt64LE(BigInt(n));
31
+ return b;
32
+ }
33
+ function encodeU128(n) {
34
+ const b = Buffer.alloc(16);
35
+ b.writeBigUInt64LE(n & BigInt('0xffffffffffffffff'), 0);
36
+ b.writeBigUInt64LE(n >> BigInt(64), 8);
37
+ return b;
38
+ }
39
+ function encodeI128(n) {
40
+ const b = Buffer.alloc(16);
41
+ const lo = n & BigInt('0xffffffffffffffff');
42
+ const hi = n >> BigInt(64);
43
+ b.writeBigUInt64LE(lo >= 0 ? lo : lo + BigInt(1) << BigInt(64), 0);
44
+ b.writeBigInt64LE(hi, 8);
45
+ return b;
46
+ }
47
+ /**
48
+ * Build deposit instruction data.
49
+ * Wrapper must: transfer tokens to vault, then call engine.deposit(idx, amount, now_slot).
50
+ */
51
+ function buildDepositInstructionData(args) {
52
+ const parts = [
53
+ Buffer.from([exports.INSTRUCTION_NAMES.deposit]),
54
+ encodeU64(args.accountIndex),
55
+ encodeU128(args.amount),
56
+ encodeU64(args.nowSlot),
57
+ ];
58
+ return Buffer.concat(parts);
59
+ }
60
+ /**
61
+ * Build withdraw instruction data.
62
+ * Wrapper must: call engine.withdraw(...); if Ok, transfer tokens out of vault.
63
+ */
64
+ function buildWithdrawInstructionData(args) {
65
+ const parts = [
66
+ Buffer.from([exports.INSTRUCTION_NAMES.withdraw]),
67
+ encodeU64(args.accountIndex),
68
+ encodeU128(args.amount),
69
+ encodeU64(args.nowSlot),
70
+ encodeU64(args.oraclePrice),
71
+ ];
72
+ return Buffer.concat(parts);
73
+ }
74
+ /**
75
+ * Build execute_trade instruction data.
76
+ * Wrapper must validate signers and oracle, then call engine.execute_trade(...).
77
+ */
78
+ function buildExecuteTradeInstructionData(args) {
79
+ const size = BigInt(args.size);
80
+ const parts = [
81
+ Buffer.from([exports.INSTRUCTION_NAMES.executeTrade]),
82
+ encodeU64(args.lpIndex),
83
+ encodeU64(args.userIndex),
84
+ encodeU64(args.nowSlot),
85
+ encodeU64(args.oraclePrice),
86
+ encodeI128(size),
87
+ ];
88
+ return Buffer.concat(parts);
89
+ }
90
+ /**
91
+ * Build keeper_crank instruction data.
92
+ * Permissionless; anyone can call. Wrapper forwards to engine.keeper_crank(...).
93
+ */
94
+ function buildKeeperCrankInstructionData(args) {
95
+ const parts = [
96
+ Buffer.from([exports.INSTRUCTION_NAMES.keeperCrank]),
97
+ encodeU64(args.callerIndex),
98
+ encodeU64(args.nowSlot),
99
+ encodeU64(args.oraclePrice),
100
+ encodeI128(BigInt(args.fundingRateBpsPerSlot)),
101
+ Buffer.from([args.allowPanic ? 1 : 0]),
102
+ ];
103
+ return Buffer.concat(parts);
104
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@percolatortool/sdk",
3
+ "version": "0.1.0",
4
+ "description": "TypeScript SDK for Percolator wrapper builders",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "devDependencies": {
8
+ "@types/node": "^20.10.0",
9
+ "typescript": "^5.0.0"
10
+ },
11
+ "peerDependencies": {
12
+ "@solana/web3.js": "^1.87.0"
13
+ },
14
+ "scripts": {
15
+ "build": "tsc"
16
+ }
17
+ }
package/src/index.ts ADDED
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Percolator SDK — types and instruction builders for wrapper programs.
3
+ * Engine does not move tokens; wrapper must do SPL transfers and call engine.
4
+ *
5
+ * @see https://github.com/aeyakovenko/percolator
6
+ */
7
+
8
+ // -----------------------------------------------------------------------------
9
+ // Types (align with engine state where useful for builders)
10
+ // -----------------------------------------------------------------------------
11
+
12
+ export type U128 = { lo: bigint; hi: bigint } | bigint;
13
+ export type I128 = { lo: bigint; hi: bigint } | bigint;
14
+
15
+ export interface RiskEngineState {
16
+ vault: bigint;
17
+ insuranceBalance: bigint;
18
+ cTot: bigint;
19
+ pnlPosTot: bigint;
20
+ totalOpenInterest: bigint;
21
+ currentSlot: number;
22
+ lastCrankSlot: number;
23
+ fundingIndexQpbE6: bigint;
24
+ lastFundingSlot: number;
25
+ fundingRateBpsPerSlotLast: number;
26
+ lifetimeLiquidations: number;
27
+ numUsedAccounts: number;
28
+ }
29
+
30
+ export interface AccountSummary {
31
+ accountIndex: number;
32
+ accountId: number;
33
+ kind: 'user' | 'lp';
34
+ capital: bigint;
35
+ pnl: bigint;
36
+ positionSize: bigint;
37
+ entryPrice: number;
38
+ owner: string;
39
+ }
40
+
41
+ export interface DepositArgs {
42
+ accountIndex: number;
43
+ amount: bigint;
44
+ nowSlot: number;
45
+ }
46
+
47
+ export interface WithdrawArgs {
48
+ accountIndex: number;
49
+ amount: bigint;
50
+ nowSlot: number;
51
+ oraclePrice: number;
52
+ }
53
+
54
+ export interface ExecuteTradeArgs {
55
+ lpIndex: number;
56
+ userIndex: number;
57
+ nowSlot: number;
58
+ oraclePrice: number;
59
+ size: bigint;
60
+ }
61
+
62
+ export interface KeeperCrankArgs {
63
+ callerIndex: number;
64
+ nowSlot: number;
65
+ oraclePrice: number;
66
+ fundingRateBpsPerSlot: number;
67
+ allowPanic: boolean;
68
+ }
69
+
70
+ // -----------------------------------------------------------------------------
71
+ // Instruction discriminators (wrapper-specific; replace with your program's)
72
+ // -----------------------------------------------------------------------------
73
+
74
+ export const INSTRUCTION_NAMES = {
75
+ deposit: 0x00,
76
+ withdraw: 0x01,
77
+ executeTrade: 0x02,
78
+ keeperCrank: 0x03,
79
+ addUser: 0x04,
80
+ addLp: 0x05,
81
+ } as const;
82
+
83
+ // -----------------------------------------------------------------------------
84
+ // Instruction builders (serialize args for CPI; layout is wrapper-defined)
85
+ // -----------------------------------------------------------------------------
86
+
87
+ function encodeU64(n: number): Buffer {
88
+ const b = Buffer.alloc(8);
89
+ b.writeBigUInt64LE(BigInt(n));
90
+ return b;
91
+ }
92
+
93
+ function encodeU128(n: bigint): Buffer {
94
+ const b = Buffer.alloc(16);
95
+ b.writeBigUInt64LE(n & BigInt('0xffffffffffffffff'), 0);
96
+ b.writeBigUInt64LE(n >> BigInt(64), 8);
97
+ return b;
98
+ }
99
+
100
+ function encodeI128(n: bigint): Buffer {
101
+ const b = Buffer.alloc(16);
102
+ const lo = n & BigInt('0xffffffffffffffff');
103
+ const hi = n >> BigInt(64);
104
+ b.writeBigUInt64LE(lo >= 0 ? lo : lo + BigInt(1) << BigInt(64), 0);
105
+ b.writeBigInt64LE(hi, 8);
106
+ return b;
107
+ }
108
+
109
+ /**
110
+ * Build deposit instruction data.
111
+ * Wrapper must: transfer tokens to vault, then call engine.deposit(idx, amount, now_slot).
112
+ */
113
+ export function buildDepositInstructionData(args: DepositArgs): Buffer {
114
+ const parts = [
115
+ Buffer.from([INSTRUCTION_NAMES.deposit]),
116
+ encodeU64(args.accountIndex),
117
+ encodeU128(args.amount),
118
+ encodeU64(args.nowSlot),
119
+ ];
120
+ return Buffer.concat(parts);
121
+ }
122
+
123
+ /**
124
+ * Build withdraw instruction data.
125
+ * Wrapper must: call engine.withdraw(...); if Ok, transfer tokens out of vault.
126
+ */
127
+ export function buildWithdrawInstructionData(args: WithdrawArgs): Buffer {
128
+ const parts = [
129
+ Buffer.from([INSTRUCTION_NAMES.withdraw]),
130
+ encodeU64(args.accountIndex),
131
+ encodeU128(args.amount),
132
+ encodeU64(args.nowSlot),
133
+ encodeU64(args.oraclePrice),
134
+ ];
135
+ return Buffer.concat(parts);
136
+ }
137
+
138
+ /**
139
+ * Build execute_trade instruction data.
140
+ * Wrapper must validate signers and oracle, then call engine.execute_trade(...).
141
+ */
142
+ export function buildExecuteTradeInstructionData(args: ExecuteTradeArgs): Buffer {
143
+ const size = BigInt(args.size);
144
+ const parts = [
145
+ Buffer.from([INSTRUCTION_NAMES.executeTrade]),
146
+ encodeU64(args.lpIndex),
147
+ encodeU64(args.userIndex),
148
+ encodeU64(args.nowSlot),
149
+ encodeU64(args.oraclePrice),
150
+ encodeI128(size),
151
+ ];
152
+ return Buffer.concat(parts);
153
+ }
154
+
155
+ /**
156
+ * Build keeper_crank instruction data.
157
+ * Permissionless; anyone can call. Wrapper forwards to engine.keeper_crank(...).
158
+ */
159
+ export function buildKeeperCrankInstructionData(args: KeeperCrankArgs): Buffer {
160
+ const parts = [
161
+ Buffer.from([INSTRUCTION_NAMES.keeperCrank]),
162
+ encodeU64(args.callerIndex),
163
+ encodeU64(args.nowSlot),
164
+ encodeU64(args.oraclePrice),
165
+ encodeI128(BigInt(args.fundingRateBpsPerSlot)),
166
+ Buffer.from([args.allowPanic ? 1 : 0]),
167
+ ];
168
+ return Buffer.concat(parts);
169
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "commonjs",
5
+ "lib": ["ES2020"],
6
+ "declaration": true,
7
+ "outDir": "dist",
8
+ "rootDir": "src",
9
+ "strict": true,
10
+ "esModuleInterop": true,
11
+ "skipLibCheck": true
12
+ },
13
+ "include": ["src/**/*"]
14
+ }