@elmntl/jlpd-sdk 0.1.11 → 0.13.4

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 (115) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +185 -404
  3. package/dist/common/ata.d.ts +26 -0
  4. package/dist/common/ata.js +48 -0
  5. package/dist/common/buffer.d.ts +15 -0
  6. package/dist/common/buffer.js +118 -0
  7. package/dist/common/connection.d.ts +13 -0
  8. package/dist/common/connection.js +2 -0
  9. package/dist/common/constants.d.ts +14 -0
  10. package/dist/common/constants.js +31 -0
  11. package/dist/common/index.d.ts +6 -0
  12. package/dist/common/index.js +28 -0
  13. package/dist/common/strategy-interface.d.ts +59 -0
  14. package/dist/common/strategy-interface.js +42 -0
  15. package/dist/elemental-lend/accounts.d.ts +103 -0
  16. package/dist/elemental-lend/accounts.js +354 -0
  17. package/dist/elemental-lend/constants.d.ts +25 -0
  18. package/dist/elemental-lend/constants.js +48 -0
  19. package/dist/elemental-lend/index.d.ts +8 -0
  20. package/dist/elemental-lend/index.js +24 -0
  21. package/dist/elemental-lend/instructions.d.ts +104 -0
  22. package/dist/elemental-lend/instructions.js +266 -0
  23. package/dist/elemental-lend/jupiter-lend.d.ts +91 -0
  24. package/dist/elemental-lend/jupiter-lend.js +189 -0
  25. package/dist/elemental-lend/kamino-vault.d.ts +173 -0
  26. package/dist/elemental-lend/kamino-vault.js +483 -0
  27. package/dist/elemental-lend/pda.d.ts +12 -0
  28. package/dist/elemental-lend/pda.js +24 -0
  29. package/dist/elemental-lend/protocol-actions.d.ts +56 -0
  30. package/dist/elemental-lend/protocol-actions.js +244 -0
  31. package/dist/elemental-lend/types.d.ts +113 -0
  32. package/dist/elemental-lend/types.js +2 -0
  33. package/dist/elemental-lend-v2/accounts.d.ts +14 -0
  34. package/dist/elemental-lend-v2/accounts.js +136 -0
  35. package/dist/elemental-lend-v2/adapters.d.ts +22 -0
  36. package/dist/elemental-lend-v2/adapters.js +50 -0
  37. package/dist/elemental-lend-v2/constants.d.ts +48 -0
  38. package/dist/elemental-lend-v2/constants.js +104 -0
  39. package/dist/elemental-lend-v2/index.d.ts +8 -0
  40. package/dist/elemental-lend-v2/index.js +24 -0
  41. package/dist/elemental-lend-v2/instructions.d.ts +96 -0
  42. package/dist/elemental-lend-v2/instructions.js +190 -0
  43. package/dist/elemental-lend-v2/lut.d.ts +20 -0
  44. package/dist/elemental-lend-v2/lut.js +65 -0
  45. package/dist/elemental-lend-v2/pda.d.ts +9 -0
  46. package/dist/elemental-lend-v2/pda.js +27 -0
  47. package/dist/elemental-lend-v2/types.d.ts +105 -0
  48. package/dist/elemental-lend-v2/types.js +2 -0
  49. package/dist/elemental-lend-v2/update-aum.d.ts +29 -0
  50. package/dist/elemental-lend-v2/update-aum.js +82 -0
  51. package/dist/index.d.ts +5 -3552
  52. package/dist/index.js +37 -4454
  53. package/dist/jlpd-strategy/accounts.d.ts +83 -0
  54. package/dist/jlpd-strategy/accounts.js +216 -0
  55. package/dist/jlpd-strategy/adapter.d.ts +81 -0
  56. package/dist/jlpd-strategy/adapter.js +118 -0
  57. package/dist/jlpd-strategy/base-to-base-swap.d.ts +74 -0
  58. package/dist/jlpd-strategy/base-to-base-swap.js +205 -0
  59. package/dist/jlpd-strategy/constants.d.ts +127 -0
  60. package/dist/jlpd-strategy/constants.js +174 -0
  61. package/dist/jlpd-strategy/fluid-view.d.ts +199 -0
  62. package/dist/jlpd-strategy/fluid-view.js +799 -0
  63. package/dist/jlpd-strategy/hedge-derived.d.ts +135 -0
  64. package/dist/jlpd-strategy/hedge-derived.js +231 -0
  65. package/dist/jlpd-strategy/hedge-instructions.d.ts +210 -0
  66. package/dist/jlpd-strategy/hedge-instructions.js +300 -0
  67. package/dist/jlpd-strategy/hedge-state.d.ts +88 -0
  68. package/dist/jlpd-strategy/hedge-state.js +110 -0
  69. package/dist/jlpd-strategy/index.d.ts +17 -0
  70. package/dist/jlpd-strategy/index.js +33 -0
  71. package/dist/jlpd-strategy/instructions.d.ts +159 -0
  72. package/dist/jlpd-strategy/instructions.js +234 -0
  73. package/dist/jlpd-strategy/jlp-borrow.d.ts +63 -0
  74. package/dist/jlpd-strategy/jlp-borrow.js +87 -0
  75. package/dist/jlpd-strategy/jlp-data.d.ts +166 -0
  76. package/dist/jlpd-strategy/jlp-data.js +611 -0
  77. package/dist/jlpd-strategy/jupusd-earn.d.ts +90 -0
  78. package/dist/jlpd-strategy/jupusd-earn.js +166 -0
  79. package/dist/jlpd-strategy/live-jlp-price.d.ts +46 -0
  80. package/dist/jlpd-strategy/live-jlp-price.js +267 -0
  81. package/dist/jlpd-strategy/pda.d.ts +13 -0
  82. package/dist/jlpd-strategy/pda.js +28 -0
  83. package/dist/jlpd-strategy/settle-yield.d.ts +45 -0
  84. package/dist/jlpd-strategy/settle-yield.js +113 -0
  85. package/dist/jlpd-strategy/swap-jlp.d.ts +198 -0
  86. package/dist/jlpd-strategy/swap-jlp.js +586 -0
  87. package/dist/jlpd-strategy/types.d.ts +101 -0
  88. package/dist/jlpd-strategy/types.js +29 -0
  89. package/dist/p-stv-core/accounts.d.ts +152 -0
  90. package/dist/p-stv-core/accounts.js +407 -0
  91. package/dist/p-stv-core/constants.d.ts +138 -0
  92. package/dist/p-stv-core/constants.js +181 -0
  93. package/dist/p-stv-core/events.d.ts +7 -0
  94. package/dist/p-stv-core/events.js +246 -0
  95. package/dist/p-stv-core/index.d.ts +11 -0
  96. package/dist/p-stv-core/index.js +27 -0
  97. package/dist/p-stv-core/instructions.d.ts +637 -0
  98. package/dist/p-stv-core/instructions.js +670 -0
  99. package/dist/p-stv-core/lut.d.ts +41 -0
  100. package/dist/p-stv-core/lut.js +81 -0
  101. package/dist/p-stv-core/pda.d.ts +30 -0
  102. package/dist/p-stv-core/pda.js +65 -0
  103. package/dist/p-stv-core/prices.d.ts +20 -0
  104. package/dist/p-stv-core/prices.js +77 -0
  105. package/dist/p-stv-core/remaining-accounts.d.ts +107 -0
  106. package/dist/p-stv-core/remaining-accounts.js +326 -0
  107. package/dist/p-stv-core/send-tx.d.ts +72 -0
  108. package/dist/p-stv-core/send-tx.js +290 -0
  109. package/dist/p-stv-core/sol-wrap.d.ts +34 -0
  110. package/dist/p-stv-core/sol-wrap.js +74 -0
  111. package/dist/p-stv-core/types.d.ts +361 -0
  112. package/dist/p-stv-core/types.js +2 -0
  113. package/package.json +56 -15
  114. package/dist/index.d.mts +0 -3552
  115. package/dist/index.mjs +0 -4363
@@ -0,0 +1,83 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import type { StvPosition } from "../common/strategy-interface";
3
+ import type { SolanaConnection } from "../common/connection";
4
+ import { JlpdConfig, ManagerRole, JlpStrategyState } from "./types";
5
+ /**
6
+ * JlpdConfig — singleton config shared across all five base-asset strategies.
7
+ * Total 392 bytes.
8
+ *
9
+ * Layout (offsets include the 8-byte discriminator). Matches the CURRENT
10
+ * `jlpd_config.rs::JlpdConfig` struct byte-for-byte (verified against that
11
+ * file's own `test_jlpd_config_field_offsets`) — the retired adapter-era
12
+ * `jlp_earn_amount`/`jlp_adapter_program` fields are GONE from the account;
13
+ * that byte range is now split between the divergence-breaker fields and
14
+ * `_reserved`/`_reserved_hi`:
15
+ * [ 0.. 8] discriminator (sha256("account:JlpdConfig")[..8])
16
+ * [ 8.. 40] admin Pubkey
17
+ * [ 40.. 72] jlpMint Pubkey
18
+ * [ 72..232] baseAssetMints[5] [Pubkey; 5] (BTC, ETH, SOL, USDC, JupUSD)
19
+ * [232..240] oracleStalenessTh u64 (max age in seconds)
20
+ * [240..242] flags u16
21
+ * [242..244] jlpSlippageBps u16 (max slippage on swap_jlp)
22
+ * [244..245] version u8
23
+ * [245..246] bump u8
24
+ * [246..248] softBreakerTolBps u16 (repurposed `_padding`; 0 ⇒ 1% default)
25
+ * [248..250] aumHardBreakerBps u16 (retired `jlp_earn_amount` head; 0 ⇒ 3% default)
26
+ * [250..288] _reserved / _reserved_hi [u8; 6] + [u8; 32] (retired earn-adapter bytes, 38 total)
27
+ * [288..320] vaultJlpAta Pubkey (canonical vault JLP ATA; write-once at init)
28
+ * [320..328] maxSwapArbUsdPerAsset u64 (per-StrategyState swap_arb cap, USD 6-dec)
29
+ * [328..392] _reserved2 / _reserved3 / _reserved4 [u8; 24] + [u8; 32] + [u8; 8]
30
+ */
31
+ export declare function deserializeJlpdConfig(data: Buffer): JlpdConfig;
32
+ /**
33
+ * ManagerRole — per-(config, manager) authorization. Total 80 bytes.
34
+ *
35
+ * Layout (offsets include the 8-byte discriminator):
36
+ * [ 0.. 8] discriminator (sha256("account:ManagerRole")[..8] —
37
+ * shared with p-stv-core + elemental-lend)
38
+ * [ 8.. 40] config Pubkey
39
+ * [ 40.. 72] manager Pubkey
40
+ * [ 72.. 73] bump u8
41
+ * [ 73.. 80] _padding [u8; 7]
42
+ */
43
+ export declare function deserializeManagerRole(data: Buffer): ManagerRole;
44
+ /**
45
+ * JlpStrategyState — per-base-asset JLPD strategy account. Total 224 bytes.
46
+ *
47
+ * Layout (offsets include the 8-byte discriminator):
48
+ * [ 0.. 8] discriminator (sha256("account:StrategyState")[..8] —
49
+ * shared with elemental-lend)
50
+ * --- StrategyStateHeader (104 bytes, identical across all strategy programs) ---
51
+ * [ 8.. 40] baseMint Pubkey
52
+ * [ 40.. 72] authority Pubkey (p-stv-core program)
53
+ * [ 72.. 80] pps u64 (scaled by PPS_DECIMALS = 10^9)
54
+ * [ 80.. 88] totalAum u64
55
+ * [ 88.. 96] totalShares u64
56
+ * [ 96..104] lastUpdated u64 (unix seconds)
57
+ * [104..106] flags u16
58
+ * [106..107] version u8
59
+ * [107..108] bump u8
60
+ * [108..112] _headerPadding [u8; 4]
61
+ * --- Custom JLPD fields (112 bytes) ---
62
+ * [112..120] baseLoaned u64 (base assets swapped into JLP)
63
+ * [120..128] swapArb i64 (signed arb accumulator; read as BN)
64
+ * [128..160] priceOracle Pubkey (per-strategy Doves price feed)
65
+ * [160..192] _reserved [u8; 32]
66
+ * [192..224] _reserved2 [u8; 32]
67
+ */
68
+ export declare function deserializeJlpStrategyState(data: Buffer): JlpStrategyState;
69
+ /**
70
+ * StvPosition — per-STV virtual share record. Total 120 bytes.
71
+ *
72
+ * The on-disk layout is identical across elemental-lend and jlpd-strategy
73
+ * (`StvPosition` from `common/strategy-interface.ts`). The 8-byte
74
+ * discriminator validation here distinguishes the two so a `LendStvPosition`
75
+ * can't be parsed as a JLPD position by mistake. See
76
+ * `common/strategy-interface.ts` for the field-by-field layout.
77
+ */
78
+ export declare function deserializeJlpStvPosition(data: Buffer): StvPosition;
79
+ export declare function fetchJlpdConfig(connection: SolanaConnection, programId?: PublicKey): Promise<JlpdConfig>;
80
+ export declare function fetchJlpStrategyState(connection: SolanaConnection, baseMint: PublicKey, programId?: PublicKey): Promise<JlpStrategyState>;
81
+ export declare function fetchJlpStvPosition(connection: SolanaConnection, stv: PublicKey, programId?: PublicKey): Promise<StvPosition>;
82
+ export declare function fetchAllJlpStrategyStates(connection: SolanaConnection, programId?: PublicKey): Promise<[PublicKey, JlpStrategyState][]>;
83
+ export declare function fetchManagersForConfig(connection: SolanaConnection, config: PublicKey, programId?: PublicKey): Promise<[PublicKey, ManagerRole][]>;
@@ -0,0 +1,216 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deserializeJlpdConfig = deserializeJlpdConfig;
4
+ exports.deserializeManagerRole = deserializeManagerRole;
5
+ exports.deserializeJlpStrategyState = deserializeJlpStrategyState;
6
+ exports.deserializeJlpStvPosition = deserializeJlpStvPosition;
7
+ exports.fetchJlpdConfig = fetchJlpdConfig;
8
+ exports.fetchJlpStrategyState = fetchJlpStrategyState;
9
+ exports.fetchJlpStvPosition = fetchJlpStvPosition;
10
+ exports.fetchAllJlpStrategyStates = fetchAllJlpStrategyStates;
11
+ exports.fetchManagersForConfig = fetchManagersForConfig;
12
+ const buffer_1 = require("../common/buffer");
13
+ const strategy_interface_1 = require("../common/strategy-interface");
14
+ const constants_1 = require("./constants");
15
+ const pda_1 = require("./pda");
16
+ // ---------------------------------------------------------------------------
17
+ // Deserialization
18
+ //
19
+ // All offsets below INCLUDE the 8-byte Anchor account discriminator. Layout
20
+ // blocks above each parser show the field map; the helpers in
21
+ // `common/buffer.ts` validate that `offset + width <= data.length` so a
22
+ // drift between this file and the on-chain layout fails loudly with a
23
+ // `RangeError` rather than returning garbage.
24
+ // ---------------------------------------------------------------------------
25
+ /**
26
+ * JlpdConfig — singleton config shared across all five base-asset strategies.
27
+ * Total 392 bytes.
28
+ *
29
+ * Layout (offsets include the 8-byte discriminator). Matches the CURRENT
30
+ * `jlpd_config.rs::JlpdConfig` struct byte-for-byte (verified against that
31
+ * file's own `test_jlpd_config_field_offsets`) — the retired adapter-era
32
+ * `jlp_earn_amount`/`jlp_adapter_program` fields are GONE from the account;
33
+ * that byte range is now split between the divergence-breaker fields and
34
+ * `_reserved`/`_reserved_hi`:
35
+ * [ 0.. 8] discriminator (sha256("account:JlpdConfig")[..8])
36
+ * [ 8.. 40] admin Pubkey
37
+ * [ 40.. 72] jlpMint Pubkey
38
+ * [ 72..232] baseAssetMints[5] [Pubkey; 5] (BTC, ETH, SOL, USDC, JupUSD)
39
+ * [232..240] oracleStalenessTh u64 (max age in seconds)
40
+ * [240..242] flags u16
41
+ * [242..244] jlpSlippageBps u16 (max slippage on swap_jlp)
42
+ * [244..245] version u8
43
+ * [245..246] bump u8
44
+ * [246..248] softBreakerTolBps u16 (repurposed `_padding`; 0 ⇒ 1% default)
45
+ * [248..250] aumHardBreakerBps u16 (retired `jlp_earn_amount` head; 0 ⇒ 3% default)
46
+ * [250..288] _reserved / _reserved_hi [u8; 6] + [u8; 32] (retired earn-adapter bytes, 38 total)
47
+ * [288..320] vaultJlpAta Pubkey (canonical vault JLP ATA; write-once at init)
48
+ * [320..328] maxSwapArbUsdPerAsset u64 (per-StrategyState swap_arb cap, USD 6-dec)
49
+ * [328..392] _reserved2 / _reserved3 / _reserved4 [u8; 24] + [u8; 32] + [u8; 8]
50
+ */
51
+ function deserializeJlpdConfig(data) {
52
+ if (data.length < constants_1.JLPD_CONFIG_SIZE) {
53
+ throw new Error(`JlpdConfig data too short: ${data.length} < ${constants_1.JLPD_CONFIG_SIZE}`);
54
+ }
55
+ const disc = data.subarray(0, 8);
56
+ if (!disc.equals(constants_1.DISC_JLPD_CONFIG)) {
57
+ throw new Error(`Invalid JlpdConfig discriminator`);
58
+ }
59
+ const baseAssetMints = [];
60
+ for (let i = 0; i < constants_1.NUM_ASSETS; i++) {
61
+ baseAssetMints.push((0, buffer_1.readPubkey)(data, 72 + i * 32));
62
+ }
63
+ return {
64
+ admin: (0, buffer_1.readPubkey)(data, 8),
65
+ jlpMint: (0, buffer_1.readPubkey)(data, 40),
66
+ baseAssetMints,
67
+ oracleStalenessTh: (0, buffer_1.readU64)(data, 232),
68
+ flags: (0, buffer_1.readU16)(data, 240),
69
+ jlpSlippageBps: (0, buffer_1.readU16)(data, 242),
70
+ version: (0, buffer_1.readU8)(data, 244),
71
+ bump: (0, buffer_1.readU8)(data, 245),
72
+ softBreakerTolBps: (0, buffer_1.readU16)(data, 246),
73
+ aumHardBreakerBps: (0, buffer_1.readU16)(data, 248),
74
+ vaultJlpAta: (0, buffer_1.readPubkey)(data, 288),
75
+ maxSwapArbUsdPerAsset: (0, buffer_1.readU64)(data, 320),
76
+ };
77
+ }
78
+ /**
79
+ * ManagerRole — per-(config, manager) authorization. Total 80 bytes.
80
+ *
81
+ * Layout (offsets include the 8-byte discriminator):
82
+ * [ 0.. 8] discriminator (sha256("account:ManagerRole")[..8] —
83
+ * shared with p-stv-core + elemental-lend)
84
+ * [ 8.. 40] config Pubkey
85
+ * [ 40.. 72] manager Pubkey
86
+ * [ 72.. 73] bump u8
87
+ * [ 73.. 80] _padding [u8; 7]
88
+ */
89
+ function deserializeManagerRole(data) {
90
+ if (data.length < constants_1.MANAGER_ROLE_SIZE) {
91
+ throw new Error(`ManagerRole data too short: ${data.length} < ${constants_1.MANAGER_ROLE_SIZE}`);
92
+ }
93
+ const disc = data.subarray(0, 8);
94
+ if (!disc.equals(constants_1.DISC_MANAGER_ROLE)) {
95
+ throw new Error(`Invalid ManagerRole discriminator`);
96
+ }
97
+ return {
98
+ config: (0, buffer_1.readPubkey)(data, 8),
99
+ manager: (0, buffer_1.readPubkey)(data, 40),
100
+ bump: (0, buffer_1.readU8)(data, 72),
101
+ };
102
+ }
103
+ /**
104
+ * JlpStrategyState — per-base-asset JLPD strategy account. Total 224 bytes.
105
+ *
106
+ * Layout (offsets include the 8-byte discriminator):
107
+ * [ 0.. 8] discriminator (sha256("account:StrategyState")[..8] —
108
+ * shared with elemental-lend)
109
+ * --- StrategyStateHeader (104 bytes, identical across all strategy programs) ---
110
+ * [ 8.. 40] baseMint Pubkey
111
+ * [ 40.. 72] authority Pubkey (p-stv-core program)
112
+ * [ 72.. 80] pps u64 (scaled by PPS_DECIMALS = 10^9)
113
+ * [ 80.. 88] totalAum u64
114
+ * [ 88.. 96] totalShares u64
115
+ * [ 96..104] lastUpdated u64 (unix seconds)
116
+ * [104..106] flags u16
117
+ * [106..107] version u8
118
+ * [107..108] bump u8
119
+ * [108..112] _headerPadding [u8; 4]
120
+ * --- Custom JLPD fields (112 bytes) ---
121
+ * [112..120] baseLoaned u64 (base assets swapped into JLP)
122
+ * [120..128] swapArb i64 (signed arb accumulator; read as BN)
123
+ * [128..160] priceOracle Pubkey (per-strategy Doves price feed)
124
+ * [160..192] _reserved [u8; 32]
125
+ * [192..224] _reserved2 [u8; 32]
126
+ */
127
+ function deserializeJlpStrategyState(data) {
128
+ if (data.length < constants_1.STRATEGY_STATE_SIZE) {
129
+ throw new Error(`JlpStrategyState data too short: ${data.length} < ${constants_1.STRATEGY_STATE_SIZE}`);
130
+ }
131
+ const disc = data.subarray(0, 8);
132
+ if (!disc.equals(constants_1.DISC_STRATEGY_STATE)) {
133
+ throw new Error(`Invalid JlpStrategyState discriminator`);
134
+ }
135
+ return {
136
+ // Header
137
+ baseMint: (0, buffer_1.readPubkey)(data, 8),
138
+ authority: (0, buffer_1.readPubkey)(data, 40),
139
+ pps: (0, buffer_1.readU64)(data, 72),
140
+ totalAum: (0, buffer_1.readU64)(data, 80),
141
+ totalShares: (0, buffer_1.readU64)(data, 88),
142
+ lastUpdated: (0, buffer_1.readU64)(data, 96),
143
+ flags: (0, buffer_1.readU16)(data, 104),
144
+ version: (0, buffer_1.readU8)(data, 106),
145
+ bump: (0, buffer_1.readU8)(data, 107),
146
+ // Custom
147
+ baseLoaned: (0, buffer_1.readU64)(data, 112),
148
+ swapArb: (0, buffer_1.readI64)(data, 120),
149
+ priceOracle: (0, buffer_1.readPubkey)(data, 128),
150
+ };
151
+ }
152
+ /**
153
+ * StvPosition — per-STV virtual share record. Total 120 bytes.
154
+ *
155
+ * The on-disk layout is identical across elemental-lend and jlpd-strategy
156
+ * (`StvPosition` from `common/strategy-interface.ts`). The 8-byte
157
+ * discriminator validation here distinguishes the two so a `LendStvPosition`
158
+ * can't be parsed as a JLPD position by mistake. See
159
+ * `common/strategy-interface.ts` for the field-by-field layout.
160
+ */
161
+ function deserializeJlpStvPosition(data) {
162
+ if (data.length < constants_1.STV_POSITION_SIZE) {
163
+ throw new Error(`StvPosition data too short: ${data.length} < ${constants_1.STV_POSITION_SIZE}`);
164
+ }
165
+ const disc = data.subarray(0, 8);
166
+ if (!disc.equals(constants_1.DISC_STV_POSITION)) {
167
+ throw new Error(`Invalid StvPosition discriminator`);
168
+ }
169
+ return (0, strategy_interface_1.deserializeStvPosition)(data);
170
+ }
171
+ // ---------------------------------------------------------------------------
172
+ // Fetch Helpers
173
+ // ---------------------------------------------------------------------------
174
+ async function fetchJlpdConfig(connection, programId = constants_1.PROGRAM_ID) {
175
+ const [pda] = (0, pda_1.findJlpdConfigPda)(programId);
176
+ const info = await connection.getAccountInfo(pda);
177
+ if (!info)
178
+ throw new Error("JlpdConfig account not found");
179
+ return deserializeJlpdConfig(info.data);
180
+ }
181
+ async function fetchJlpStrategyState(connection, baseMint, programId = constants_1.PROGRAM_ID) {
182
+ const [pda] = (0, pda_1.findStrategyStatePda)(baseMint, programId);
183
+ const info = await connection.getAccountInfo(pda);
184
+ if (!info)
185
+ throw new Error(`JlpStrategyState not found for mint ${baseMint.toBase58()}`);
186
+ return deserializeJlpStrategyState(info.data);
187
+ }
188
+ async function fetchJlpStvPosition(connection, stv, programId = constants_1.PROGRAM_ID) {
189
+ const [pda] = (0, pda_1.findStvPositionPda)(stv, programId);
190
+ const info = await connection.getAccountInfo(pda);
191
+ if (!info)
192
+ throw new Error(`StvPosition not found for stv ${stv.toBase58()}`);
193
+ return deserializeJlpStvPosition(info.data);
194
+ }
195
+ // ---------------------------------------------------------------------------
196
+ // GPA Helpers
197
+ // ---------------------------------------------------------------------------
198
+ async function fetchAllJlpStrategyStates(connection, programId = constants_1.PROGRAM_ID) {
199
+ const accounts = await connection.getProgramAccounts(programId, {
200
+ filters: [
201
+ { dataSize: constants_1.STRATEGY_STATE_SIZE },
202
+ { memcmp: { offset: 0, bytes: constants_1.DISC_STRATEGY_STATE.toString("base64"), encoding: "base64" } },
203
+ ],
204
+ });
205
+ return accounts.map((a) => [a.pubkey, deserializeJlpStrategyState(a.account.data)]);
206
+ }
207
+ async function fetchManagersForConfig(connection, config, programId = constants_1.PROGRAM_ID) {
208
+ const accounts = await connection.getProgramAccounts(programId, {
209
+ filters: [
210
+ { dataSize: constants_1.MANAGER_ROLE_SIZE },
211
+ { memcmp: { offset: 0, bytes: constants_1.DISC_MANAGER_ROLE.toString("base64"), encoding: "base64" } },
212
+ { memcmp: { offset: 8, bytes: config.toBuffer().toString("base64"), encoding: "base64" } },
213
+ ],
214
+ });
215
+ return accounts.map((a) => [a.pubkey, deserializeManagerRole(a.account.data)]);
216
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * JLPD Adapter instruction builders.
3
+ *
4
+ * These build JLPD-side instructions (deposit_to_adapter, withdraw_from_adapter)
5
+ * that CPI into the JLPD Lend Adapter program.
6
+ */
7
+ import { PublicKey, TransactionInstruction, AccountMeta } from "@solana/web3.js";
8
+ import BN from "bn.js";
9
+ export interface DepositToAdapterArgs {
10
+ manager: PublicKey;
11
+ config: PublicKey;
12
+ managerRole: PublicKey;
13
+ vaultJlpAta: PublicKey;
14
+ adapterState: PublicKey;
15
+ adapterProgram: PublicKey;
16
+ /**
17
+ * Token amount as a BN (u64). Must be BN — plain `number` silently truncates
18
+ * above 2^53 and would corrupt u64 token amounts on large balances.
19
+ */
20
+ amount: BN;
21
+ /** Protocol-specific remaining accounts for the adapter's deposit CPI */
22
+ remainingAccounts?: AccountMeta[];
23
+ }
24
+ /**
25
+ * Build `deposit_to_adapter` instruction.
26
+ *
27
+ * Anchor data: [8-byte disc, DepositToAdapterParams(u64 + bool + u16 + u32)]
28
+ */
29
+ export declare function createDepositToAdapterIx(args: DepositToAdapterArgs, programId?: PublicKey): TransactionInstruction;
30
+ export interface WithdrawFromAdapterArgs {
31
+ manager: PublicKey;
32
+ config: PublicKey;
33
+ managerRole: PublicKey;
34
+ vaultJlpAta: PublicKey;
35
+ adapterState: PublicKey;
36
+ adapterProgram: PublicKey;
37
+ /**
38
+ * Token amount as a BN (u64). Must be BN — plain `number` silently truncates
39
+ * above 2^53 and would corrupt u64 token amounts on large balances.
40
+ */
41
+ amount: BN;
42
+ remainingAccounts?: AccountMeta[];
43
+ }
44
+ export declare function createWithdrawFromAdapterIx(args: WithdrawFromAdapterArgs, programId?: PublicKey): TransactionInstruction;
45
+ /** Adapter instruction discriminators for adapter_manage */
46
+ export declare const ADAPTER_DISC_BORROW = 3;
47
+ export declare const ADAPTER_DISC_DEPOSIT_EARN = 4;
48
+ export declare const ADAPTER_DISC_WITHDRAW_EARN = 5;
49
+ export declare const ADAPTER_DISC_REPAY = 6;
50
+ export interface AdapterManageArgs {
51
+ manager: PublicKey;
52
+ config: PublicKey;
53
+ managerRole: PublicKey;
54
+ adapterState: PublicKey;
55
+ adapterProgram: PublicKey;
56
+ /**
57
+ * Adapter instruction discriminator. Must be an integer in [0x03, 0xFF].
58
+ * 0x00/0x01/0x02 are reserved for deposit/withdraw/update_yield and are
59
+ * rejected by the on-chain handler (`ReservedAdapterDiscriminator`).
60
+ */
61
+ adapterDisc: number;
62
+ /**
63
+ * Opaque payload forwarded verbatim after the discriminator. Provide either
64
+ * `data` (raw bytes) or `amount` (convenience: packed as u64 LE, which is
65
+ * what jlpd-lend-adapter's verbs expect). Total payload length must not
66
+ * exceed 128 bytes (on-chain limit at adapter_manage.rs:72).
67
+ */
68
+ data?: Buffer;
69
+ /**
70
+ * Convenience u64 amount, encoded as u64 LE. Must be a BN instance —
71
+ * plain `number` silently truncates above 2^53.
72
+ */
73
+ amount?: BN;
74
+ /**
75
+ * Protocol-specific remaining accounts for the adapter CPI. NOTE: the adapter
76
+ * authorizes on its `authority` (the config PDA, propagated as signer) — do
77
+ * NOT pass an adapter ManagerRole here; that system was retired.
78
+ */
79
+ remainingAccounts?: AccountMeta[];
80
+ }
81
+ export declare function createAdapterManageIx(args: AdapterManageArgs, programId?: PublicKey): TransactionInstruction;
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ /**
3
+ * JLPD Adapter instruction builders.
4
+ *
5
+ * These build JLPD-side instructions (deposit_to_adapter, withdraw_from_adapter)
6
+ * that CPI into the JLPD Lend Adapter program.
7
+ */
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ADAPTER_DISC_REPAY = exports.ADAPTER_DISC_WITHDRAW_EARN = exports.ADAPTER_DISC_DEPOSIT_EARN = exports.ADAPTER_DISC_BORROW = void 0;
13
+ exports.createDepositToAdapterIx = createDepositToAdapterIx;
14
+ exports.createWithdrawFromAdapterIx = createWithdrawFromAdapterIx;
15
+ exports.createAdapterManageIx = createAdapterManageIx;
16
+ const web3_js_1 = require("@solana/web3.js");
17
+ const bn_js_1 = __importDefault(require("bn.js"));
18
+ const constants_1 = require("./constants");
19
+ /**
20
+ * Internal helper — builds the instruction for deposit_to_adapter or
21
+ * withdraw_from_adapter. Both share the same account layout and data
22
+ * format (8-byte disc + u64 amount); only the discriminator differs.
23
+ */
24
+ function buildAdapterIx(disc, args, programId) {
25
+ const { manager, config, managerRole, vaultJlpAta, adapterState, adapterProgram, amount, remainingAccounts = [], } = args;
26
+ if (!(amount instanceof bn_js_1.default)) {
27
+ throw new TypeError("adapter amount must be a BN instance (not number) to avoid u64 truncation");
28
+ }
29
+ const data = Buffer.alloc(16);
30
+ disc.copy(data, 0);
31
+ amount.toArrayLike(Buffer, "le", 8).copy(data, 8);
32
+ return new web3_js_1.TransactionInstruction({
33
+ programId,
34
+ keys: [
35
+ { pubkey: manager, isSigner: true, isWritable: false },
36
+ { pubkey: config, isSigner: false, isWritable: true },
37
+ { pubkey: managerRole, isSigner: false, isWritable: false },
38
+ { pubkey: vaultJlpAta, isSigner: false, isWritable: true },
39
+ { pubkey: adapterState, isSigner: false, isWritable: true },
40
+ { pubkey: adapterProgram, isSigner: false, isWritable: false },
41
+ ...remainingAccounts,
42
+ ],
43
+ data,
44
+ });
45
+ }
46
+ /**
47
+ * Build `deposit_to_adapter` instruction.
48
+ *
49
+ * Anchor data: [8-byte disc, DepositToAdapterParams(u64 + bool + u16 + u32)]
50
+ */
51
+ function createDepositToAdapterIx(args, programId = constants_1.PROGRAM_ID) {
52
+ return buildAdapterIx(constants_1.IX_DEPOSIT_TO_ADAPTER, args, programId);
53
+ }
54
+ function createWithdrawFromAdapterIx(args, programId = constants_1.PROGRAM_ID) {
55
+ return buildAdapterIx(constants_1.IX_WITHDRAW_FROM_ADAPTER, args, programId);
56
+ }
57
+ // ============================================================================
58
+ // adapter_manage (generic: borrow, repay, deposit_earn, withdraw_earn)
59
+ // ============================================================================
60
+ /** Adapter instruction discriminators for adapter_manage */
61
+ exports.ADAPTER_DISC_BORROW = 0x03;
62
+ exports.ADAPTER_DISC_DEPOSIT_EARN = 0x04;
63
+ exports.ADAPTER_DISC_WITHDRAW_EARN = 0x05;
64
+ exports.ADAPTER_DISC_REPAY = 0x06;
65
+ /**
66
+ * Build `adapter_manage` instruction (Adapter Interface v1, opaque passthrough).
67
+ *
68
+ * Routes a management op (disc >= 0x03) through JLPD for config PDA signer
69
+ * propagation. Anchor data: [8-byte ix disc, adapter_disc: u8, data: Vec<u8>].
70
+ */
71
+ /** Maximum opaque payload length accepted by the on-chain handler (adapter_manage.rs:49). */
72
+ const MAX_MANAGE_PAYLOAD_LEN = 128;
73
+ function createAdapterManageIx(args, programId = constants_1.PROGRAM_ID) {
74
+ const { manager, config, managerRole, adapterState, adapterProgram, adapterDisc, data: rawData, amount, remainingAccounts = [], } = args;
75
+ // Validate adapterDisc: must be an integer in [0x03, 0xFF].
76
+ // 0x00/0x01/0x02 are reserved — on-chain handler rejects them with
77
+ // ReservedAdapterDiscriminator (adapter_manage.rs:67-70).
78
+ if (!Number.isInteger(adapterDisc) || adapterDisc < 0x03 || adapterDisc > 0xff) {
79
+ throw new RangeError(`createAdapterManageIx: adapterDisc must be an integer in [0x03, 0xFF], got ${adapterDisc}`);
80
+ }
81
+ // Resolve the opaque payload: explicit bytes, or a u64-LE amount.
82
+ let payload;
83
+ if (rawData !== undefined) {
84
+ payload = rawData;
85
+ }
86
+ else if (amount !== undefined) {
87
+ if (!(amount instanceof bn_js_1.default)) {
88
+ throw new TypeError("createAdapterManageIx: amount must be a BN instance (not number) to avoid u64 truncation");
89
+ }
90
+ payload = amount.toArrayLike(Buffer, "le", 8);
91
+ }
92
+ else {
93
+ throw new Error("createAdapterManageIx: provide either `data` or `amount`");
94
+ }
95
+ // Validate payload length against on-chain limit (adapter_manage.rs:71-74).
96
+ if (payload.length > MAX_MANAGE_PAYLOAD_LEN) {
97
+ throw new RangeError(`createAdapterManageIx: payload length ${payload.length} exceeds on-chain maximum of ${MAX_MANAGE_PAYLOAD_LEN} bytes`);
98
+ }
99
+ // Anchor data: [8-byte ix disc][adapter_disc: u8][Vec<u8>: u32 LE len + bytes]
100
+ const data = Buffer.alloc(8 + 1 + 4 + payload.length);
101
+ constants_1.IX_ADAPTER_MANAGE.copy(data, 0);
102
+ data[8] = adapterDisc;
103
+ data.writeUInt32LE(payload.length, 9);
104
+ payload.copy(data, 13);
105
+ return new web3_js_1.TransactionInstruction({
106
+ programId,
107
+ keys: [
108
+ { pubkey: manager, isSigner: true, isWritable: false },
109
+ { pubkey: config, isSigner: false, isWritable: true },
110
+ { pubkey: managerRole, isSigner: false, isWritable: false },
111
+ // remaining_accounts: [adapter_state, adapter_program, ...protocol accounts]
112
+ { pubkey: adapterState, isSigner: false, isWritable: true },
113
+ { pubkey: adapterProgram, isSigner: false, isWritable: false },
114
+ ...remainingAccounts,
115
+ ],
116
+ data,
117
+ });
118
+ }
@@ -0,0 +1,74 @@
1
+ import { PublicKey, TransactionInstruction, AddressLookupTableAccount, AccountMeta } from "@solana/web3.js";
2
+ import type { SolanaConnection } from "../common/connection";
3
+ import BN from "bn.js";
4
+ import { type JupiterQuote } from "./swap-jlp";
5
+ /** Arguments for building a base_to_base_swap instruction. */
6
+ export interface CreateBaseToBaseSwapIxArgs {
7
+ manager: PublicKey;
8
+ config: PublicKey;
9
+ managerRole: PublicKey;
10
+ strategyStateIn: PublicKey;
11
+ strategyStateOut: PublicKey;
12
+ baseInAta: PublicKey;
13
+ baseOutAta: PublicKey;
14
+ baseInMint: PublicKey;
15
+ baseOutMint: PublicKey;
16
+ tokenProgram: PublicKey;
17
+ jupiterProgram: PublicKey;
18
+ priceOracleIn: PublicKey;
19
+ priceOracleOut: PublicKey;
20
+ amountIn: bigint | BN;
21
+ minOut: bigint | BN;
22
+ jupiterData: Buffer | Uint8Array;
23
+ remainingAccounts?: AccountMeta[];
24
+ }
25
+ /**
26
+ * Build the on-chain `base_to_base_swap` instruction.
27
+ *
28
+ * Instruction data layout (Anchor Borsh):
29
+ * [8-byte disc, 8-byte amount_in (LE), 8-byte min_out (LE),
30
+ * 4-byte jupiter_data_len (LE), ...jupiter_data_bytes]
31
+ */
32
+ export declare function createBaseToBaseSwapIx(args: CreateBaseToBaseSwapIxArgs, programId?: PublicKey): TransactionInstruction;
33
+ /**
34
+ * Build a complete base_to_base_swap transaction:
35
+ * 1. Derive PDAs for both strategies
36
+ * 2. Resolve oracle accounts (from strategy_state.price_oracle on-chain, or overrides)
37
+ * 3. Get Jupiter quote (base_in → base_out)
38
+ * 4. Get Jupiter swap instructions (strategy_state_in PDA signs)
39
+ * 5. Build instruction + return ALTs
40
+ *
41
+ * Oracle resolution:
42
+ * - Stablecoins (USDC/JupUSD): always PublicKey.default (program ignores oracle for $1 assets).
43
+ * - Volatile assets: use caller-supplied override, or fetch strategy_state on-chain
44
+ * to read the stored price_oracle field. To avoid two extra RPC calls, pass
45
+ * priceOracleIn/priceOracleOut when you already have the strategy states.
46
+ */
47
+ export declare function buildBaseToBaseSwapTransaction(args: {
48
+ connection: SolanaConnection;
49
+ manager: PublicKey;
50
+ baseInMint: PublicKey;
51
+ baseOutMint: PublicKey;
52
+ /** Amount of base_in to swap, raw u64. */
53
+ amountIn: bigint;
54
+ /**
55
+ * Override oracle for base_in. If omitted and base_in is not a stable,
56
+ * the strategy_state_in account is fetched to read price_oracle.
57
+ */
58
+ priceOracleIn?: PublicKey;
59
+ /**
60
+ * Override oracle for base_out. If omitted and base_out is not a stable,
61
+ * the strategy_state_out account is fetched to read price_oracle.
62
+ */
63
+ priceOracleOut?: PublicKey;
64
+ slippageBps?: number;
65
+ tokenProgram?: PublicKey;
66
+ programId?: PublicKey;
67
+ }): Promise<{
68
+ instructions: TransactionInstruction[];
69
+ addressLookupTables: AddressLookupTableAccount[];
70
+ quoteInAmount: bigint;
71
+ quoteOutAmount: bigint;
72
+ minOut: bigint;
73
+ quote: JupiterQuote;
74
+ }>;