@percolatorct/sdk 0.3.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.
@@ -0,0 +1,216 @@
1
+ /**
2
+ * @module stake
3
+ * Percolator Insurance LP Staking program — instruction encoders, PDA derivation, and account specs.
4
+ *
5
+ * Program: percolator-stake (dcccrypto/percolator-stake)
6
+ * Deployed devnet: 6aJb1F9CDCVWCNYFwj8aQsVb696YnW6J1FznteHq4Q6k
7
+ * Deployed mainnet: (pending deployment — DevOps must set STAKE_PROGRAM_ID env var or deploy and update STAKE_PROGRAM_IDS.mainnet)
8
+ */
9
+ import { PublicKey } from '@solana/web3.js';
10
+ /** Known stake program addresses per network. Mainnet is empty until deployed. */
11
+ export declare const STAKE_PROGRAM_IDS: {
12
+ readonly devnet: "6aJb1F9CDCVWCNYFwj8aQsVb696YnW6J1FznteHq4Q6k";
13
+ readonly mainnet: "";
14
+ };
15
+ /**
16
+ * Resolve the stake program ID for the given network.
17
+ *
18
+ * Priority:
19
+ * 1. STAKE_PROGRAM_ID env var (explicit override — DevOps sets this for mainnet until constant is filled)
20
+ * 2. Network-specific constant from STAKE_PROGRAM_IDS
21
+ *
22
+ * Throws a clear error on mainnet when no address is available so callers
23
+ * surface the gap instead of silently hitting the devnet program.
24
+ */
25
+ export declare function getStakeProgramId(network?: 'devnet' | 'mainnet'): PublicKey;
26
+ /**
27
+ * Default export — resolves for the current runtime network.
28
+ * Use getStakeProgramId() with an explicit network argument where possible.
29
+ *
30
+ * @deprecated Direct use of STAKE_PROGRAM_ID is being phased out in favour of
31
+ * getStakeProgramId() so mainnet callers get a clear error rather than silently
32
+ * resolving to the devnet address.
33
+ */
34
+ export declare const STAKE_PROGRAM_ID: PublicKey;
35
+ export declare const STAKE_IX: {
36
+ readonly InitPool: 0;
37
+ readonly Deposit: 1;
38
+ readonly Withdraw: 2;
39
+ readonly FlushToInsurance: 3;
40
+ readonly UpdateConfig: 4;
41
+ readonly TransferAdmin: 5;
42
+ readonly AdminSetOracleAuthority: 6;
43
+ readonly AdminSetRiskThreshold: 7;
44
+ readonly AdminSetMaintenanceFee: 8;
45
+ readonly AdminResolveMarket: 9;
46
+ readonly AdminWithdrawInsurance: 10;
47
+ readonly AdminSetInsurancePolicy: 11;
48
+ /** PERC-272: Accrue trading fees to LP vault */
49
+ readonly AccrueFees: 12;
50
+ /** PERC-272: Init pool in trading LP mode */
51
+ readonly InitTradingPool: 13;
52
+ /** PERC-313: Set HWM config (enable + floor bps) */
53
+ readonly AdminSetHwmConfig: 14;
54
+ /** PERC-303: Enable/configure senior-junior LP tranches */
55
+ readonly AdminSetTrancheConfig: 15;
56
+ /** PERC-303: Deposit into junior (first-loss) tranche */
57
+ readonly DepositJunior: 16;
58
+ };
59
+ /** Derive the stake pool PDA for a given slab (market). */
60
+ export declare function deriveStakePool(slab: PublicKey, programId?: PublicKey): [PublicKey, number];
61
+ /** Derive the vault authority PDA (signs CPI, owns LP mint + vault). */
62
+ export declare function deriveStakeVaultAuth(pool: PublicKey, programId?: PublicKey): [PublicKey, number];
63
+ /** Derive the per-user deposit PDA (tracks cooldown, deposit time). */
64
+ export declare function deriveDepositPda(pool: PublicKey, user: PublicKey, programId?: PublicKey): [PublicKey, number];
65
+ /** Tag 0: InitPool — create stake pool for a slab. */
66
+ export declare function encodeStakeInitPool(cooldownSlots: bigint | number, depositCap: bigint | number): Uint8Array;
67
+ /** Tag 1: Deposit — deposit collateral, receive LP tokens. */
68
+ export declare function encodeStakeDeposit(amount: bigint | number): Uint8Array;
69
+ /** Tag 2: Withdraw — burn LP tokens, receive collateral (subject to cooldown). */
70
+ export declare function encodeStakeWithdraw(lpAmount: bigint | number): Uint8Array;
71
+ /** Tag 3: FlushToInsurance — move collateral from stake vault to wrapper insurance. */
72
+ export declare function encodeStakeFlushToInsurance(amount: bigint | number): Uint8Array;
73
+ /** Tag 4: UpdateConfig — update cooldown and/or deposit cap. */
74
+ export declare function encodeStakeUpdateConfig(newCooldownSlots?: bigint | number, newDepositCap?: bigint | number): Uint8Array;
75
+ /** Tag 5: TransferAdmin — transfer wrapper admin to pool PDA. */
76
+ export declare function encodeStakeTransferAdmin(): Uint8Array;
77
+ /** Tag 6: AdminSetOracleAuthority — forward to wrapper via CPI. */
78
+ export declare function encodeStakeAdminSetOracleAuthority(newAuthority: PublicKey): Uint8Array;
79
+ /** Tag 7: AdminSetRiskThreshold — forward to wrapper via CPI. */
80
+ export declare function encodeStakeAdminSetRiskThreshold(newThreshold: bigint | number): Uint8Array;
81
+ /** Tag 8: AdminSetMaintenanceFee — forward to wrapper via CPI. */
82
+ export declare function encodeStakeAdminSetMaintenanceFee(newFee: bigint | number): Uint8Array;
83
+ /** Tag 9: AdminResolveMarket — forward to wrapper via CPI. */
84
+ export declare function encodeStakeAdminResolveMarket(): Uint8Array;
85
+ /** Tag 10: AdminWithdrawInsurance — withdraw insurance after market resolution. */
86
+ export declare function encodeStakeAdminWithdrawInsurance(amount: bigint | number): Uint8Array;
87
+ /** Tag 12: AccrueFees — permissionless: accrue trading fees to LP vault. */
88
+ export declare function encodeStakeAccrueFees(): Uint8Array;
89
+ /** Tag 13: InitTradingPool — create pool in trading LP mode (pool_mode = 1). */
90
+ export declare function encodeStakeInitTradingPool(cooldownSlots: bigint | number, depositCap: bigint | number): Uint8Array;
91
+ /** Tag 14 (PERC-313): AdminSetHwmConfig — enable HWM protection and set floor BPS. */
92
+ export declare function encodeStakeAdminSetHwmConfig(enabled: boolean, hwmFloorBps: number): Uint8Array;
93
+ /** Tag 15 (PERC-303): AdminSetTrancheConfig — enable senior/junior LP tranches. */
94
+ export declare function encodeStakeAdminSetTrancheConfig(juniorFeeMultBps: number): Uint8Array;
95
+ /** Tag 16 (PERC-303): DepositJunior — deposit into first-loss junior tranche. */
96
+ export declare function encodeStakeDepositJunior(amount: bigint | number): Uint8Array;
97
+ /** Tag 11: AdminSetInsurancePolicy — set withdrawal policy on wrapper. */
98
+ export declare function encodeStakeAdminSetInsurancePolicy(authority: PublicKey, minWithdrawBase: bigint | number, maxWithdrawBps: number, cooldownSlots: bigint | number): Uint8Array;
99
+ /**
100
+ * Decoded StakePool state (352 bytes on-chain).
101
+ * Includes PERC-272 (fee yield), PERC-313 (HWM), and PERC-303 (tranches).
102
+ */
103
+ export interface StakePoolState {
104
+ isInitialized: boolean;
105
+ bump: number;
106
+ vaultAuthorityBump: number;
107
+ adminTransferred: boolean;
108
+ slab: PublicKey;
109
+ admin: PublicKey;
110
+ collateralMint: PublicKey;
111
+ lpMint: PublicKey;
112
+ vault: PublicKey;
113
+ totalDeposited: bigint;
114
+ totalLpSupply: bigint;
115
+ cooldownSlots: bigint;
116
+ depositCap: bigint;
117
+ totalFlushed: bigint;
118
+ totalReturned: bigint;
119
+ totalWithdrawn: bigint;
120
+ percolatorProgram: PublicKey;
121
+ totalFeesEarned: bigint;
122
+ lastFeeAccrualSlot: bigint;
123
+ lastVaultSnapshot: bigint;
124
+ poolMode: number;
125
+ hwmEnabled: boolean;
126
+ epochHighWaterTvl: bigint;
127
+ hwmFloorBps: number;
128
+ trancheEnabled: boolean;
129
+ juniorBalance: bigint;
130
+ juniorTotalLp: bigint;
131
+ juniorFeeMultBps: number;
132
+ }
133
+ /** Size of StakePool on-chain (bytes). */
134
+ export declare const STAKE_POOL_SIZE = 352;
135
+ /**
136
+ * Decode a StakePool account from raw data buffer. * Uses DataView for all u64/u16 reads — browser-safe.
137
+ */
138
+ export declare function decodeStakePool(data: Uint8Array): StakePoolState;
139
+ export interface StakeAccounts {
140
+ /** InitPool accounts */
141
+ initPool: {
142
+ admin: PublicKey;
143
+ slab: PublicKey;
144
+ pool: PublicKey;
145
+ lpMint: PublicKey;
146
+ vault: PublicKey;
147
+ vaultAuth: PublicKey;
148
+ collateralMint: PublicKey;
149
+ percolatorProgram: PublicKey;
150
+ };
151
+ /** Deposit accounts */
152
+ deposit: {
153
+ user: PublicKey;
154
+ pool: PublicKey;
155
+ userCollateralAta: PublicKey;
156
+ vault: PublicKey;
157
+ lpMint: PublicKey;
158
+ userLpAta: PublicKey;
159
+ vaultAuth: PublicKey;
160
+ depositPda: PublicKey;
161
+ };
162
+ /** Withdraw accounts */
163
+ withdraw: {
164
+ user: PublicKey;
165
+ pool: PublicKey;
166
+ userLpAta: PublicKey;
167
+ lpMint: PublicKey;
168
+ vault: PublicKey;
169
+ userCollateralAta: PublicKey;
170
+ vaultAuth: PublicKey;
171
+ depositPda: PublicKey;
172
+ };
173
+ /** FlushToInsurance accounts (CPI from stake → percolator) */
174
+ flushToInsurance: {
175
+ caller: PublicKey;
176
+ pool: PublicKey;
177
+ vault: PublicKey;
178
+ vaultAuth: PublicKey;
179
+ slab: PublicKey;
180
+ wrapperVault: PublicKey;
181
+ percolatorProgram: PublicKey;
182
+ };
183
+ }
184
+ /**
185
+ * Build account keys for InitPool instruction.
186
+ * Returns array of {pubkey, isSigner, isWritable} in the order the program expects.
187
+ */
188
+ export declare function initPoolAccounts(a: StakeAccounts['initPool']): {
189
+ pubkey: PublicKey;
190
+ isSigner: boolean;
191
+ isWritable: boolean;
192
+ }[];
193
+ /**
194
+ * Build account keys for Deposit instruction.
195
+ */
196
+ export declare function depositAccounts(a: StakeAccounts['deposit']): {
197
+ pubkey: PublicKey;
198
+ isSigner: boolean;
199
+ isWritable: boolean;
200
+ }[];
201
+ /**
202
+ * Build account keys for Withdraw instruction.
203
+ */
204
+ export declare function withdrawAccounts(a: StakeAccounts['withdraw']): {
205
+ pubkey: PublicKey;
206
+ isSigner: boolean;
207
+ isWritable: boolean;
208
+ }[];
209
+ /**
210
+ * Build account keys for FlushToInsurance instruction.
211
+ */
212
+ export declare function flushToInsuranceAccounts(a: StakeAccounts['flushToInsurance']): {
213
+ pubkey: PublicKey;
214
+ isSigner: boolean;
215
+ isWritable: boolean;
216
+ }[];
@@ -0,0 +1,19 @@
1
+ import { Connection, PublicKey } from "@solana/web3.js";
2
+ /**
3
+ * Token2022 (Token Extensions) program ID.
4
+ */
5
+ export declare const TOKEN_2022_PROGRAM_ID: PublicKey;
6
+ /**
7
+ * Detect which token program owns a given mint account.
8
+ * Returns the owner program ID (TOKEN_PROGRAM_ID or TOKEN_2022_PROGRAM_ID).
9
+ * Throws if the mint account doesn't exist.
10
+ */
11
+ export declare function detectTokenProgram(connection: Connection, mint: PublicKey): Promise<PublicKey>;
12
+ /**
13
+ * Check if a given token program ID is Token2022.
14
+ */
15
+ export declare function isToken2022(tokenProgramId: PublicKey): boolean;
16
+ /**
17
+ * Check if a given token program ID is the standard SPL Token program.
18
+ */
19
+ export declare function isStandardToken(tokenProgramId: PublicKey): boolean;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Input validation utilities for CLI commands.
3
+ * Provides descriptive error messages for invalid input.
4
+ */
5
+ import { PublicKey } from "@solana/web3.js";
6
+ export declare class ValidationError extends Error {
7
+ readonly field: string;
8
+ constructor(field: string, message: string);
9
+ }
10
+ /**
11
+ * Validate a public key string.
12
+ */
13
+ export declare function validatePublicKey(value: string, field: string): PublicKey;
14
+ /**
15
+ * Validate a non-negative integer index (u16 range for accounts).
16
+ */
17
+ export declare function validateIndex(value: string, field: string): number;
18
+ /**
19
+ * Validate a non-negative amount (u64 range).
20
+ */
21
+ export declare function validateAmount(value: string, field: string): bigint;
22
+ /**
23
+ * Validate a u128 value.
24
+ */
25
+ export declare function validateU128(value: string, field: string): bigint;
26
+ /**
27
+ * Validate an i64 value.
28
+ */
29
+ export declare function validateI64(value: string, field: string): bigint;
30
+ /**
31
+ * Validate an i128 value (trade sizes).
32
+ */
33
+ export declare function validateI128(value: string, field: string): bigint;
34
+ /**
35
+ * Validate a basis points value (0-10000).
36
+ */
37
+ export declare function validateBps(value: string, field: string): number;
38
+ /**
39
+ * Validate a u64 value.
40
+ */
41
+ export declare function validateU64(value: string, field: string): bigint;
42
+ /**
43
+ * Validate a u16 value.
44
+ */
45
+ export declare function validateU16(value: string, field: string): number;
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@percolatorct/sdk",
3
+ "version": "0.3.0",
4
+ "license": "Apache-2.0",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=20.0.0"
8
+ },
9
+ "main": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.js",
15
+ "default": "./dist/index.js",
16
+ "types": "./dist/index.d.ts"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsup",
24
+ "test": "tsx test/abi.test.ts && tsx test/slab.test.ts && tsx test/validation.test.ts && tsx test/dex-oracle.test.ts && tsx test/trading.test.ts && tsx test/warmup-leverage-cap.test.ts && tsx test/dynamic-fees.test.ts && tsx test/oracle.test.ts && vitest run",
25
+ "lint": "tsc --noEmit"
26
+ },
27
+ "dependencies": {
28
+ "@solana/spl-token": "^0.4.14",
29
+ "@solana/web3.js": "^1.95.4"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^20.17.10",
33
+ "tsup": "^8.3.5",
34
+ "tsx": "^4.21.0",
35
+ "typescript": "^5.7.2",
36
+ "vitest": "^4.0.18"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/dcccrypto/percolator-sdk.git"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
44
+ }
45
+ }