@elemental-stv-core/sdk 0.9.2 → 0.11.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.
Files changed (38) hide show
  1. package/dist/common/buffer.d.ts +3 -0
  2. package/dist/common/buffer.js +19 -0
  3. package/dist/elemental-lend/instructions.d.ts +14 -0
  4. package/dist/elemental-lend/instructions.js +18 -6
  5. package/dist/elemental-lend/protocol-actions.d.ts +6 -0
  6. package/dist/elemental-lend/protocol-actions.js +21 -10
  7. package/dist/jlpd-strategy/accounts.d.ts +4 -1
  8. package/dist/jlpd-strategy/accounts.js +6 -1
  9. package/dist/jlpd-strategy/adapter.d.ts +16 -6
  10. package/dist/jlpd-strategy/adapter.js +21 -9
  11. package/dist/jlpd-strategy/base-to-base-swap.d.ts +74 -0
  12. package/dist/jlpd-strategy/base-to-base-swap.js +199 -0
  13. package/dist/jlpd-strategy/constants.d.ts +11 -3
  14. package/dist/jlpd-strategy/constants.js +19 -10
  15. package/dist/jlpd-strategy/index.d.ts +1 -0
  16. package/dist/jlpd-strategy/index.js +1 -0
  17. package/dist/jlpd-strategy/instructions.d.ts +49 -0
  18. package/dist/jlpd-strategy/instructions.js +55 -0
  19. package/dist/jlpd-strategy/jlp-data.d.ts +11 -0
  20. package/dist/jlpd-strategy/jlp-data.js +15 -0
  21. package/dist/jlpd-strategy/settle-yield.d.ts +20 -1
  22. package/dist/jlpd-strategy/settle-yield.js +9 -6
  23. package/dist/jlpd-strategy/swap-jlp.d.ts +26 -0
  24. package/dist/jlpd-strategy/swap-jlp.js +74 -10
  25. package/dist/jlpd-strategy/types.d.ts +3 -1
  26. package/dist/p-stv-core/accounts.d.ts +40 -4
  27. package/dist/p-stv-core/accounts.js +97 -4
  28. package/dist/p-stv-core/constants.d.ts +39 -3
  29. package/dist/p-stv-core/constants.js +42 -4
  30. package/dist/p-stv-core/events.js +50 -7
  31. package/dist/p-stv-core/instructions.d.ts +245 -2
  32. package/dist/p-stv-core/instructions.js +226 -13
  33. package/dist/p-stv-core/pda.d.ts +15 -0
  34. package/dist/p-stv-core/pda.js +25 -0
  35. package/dist/p-stv-core/remaining-accounts.d.ts +4 -1
  36. package/dist/p-stv-core/remaining-accounts.js +17 -2
  37. package/dist/p-stv-core/types.d.ts +78 -1
  38. package/package.json +1 -1
@@ -2,6 +2,8 @@ import { PublicKey } from "@solana/web3.js";
2
2
  import BN from "bn.js";
3
3
  export declare function readPubkey(data: Buffer, offset: number): PublicKey;
4
4
  export declare function readU64(data: Buffer, offset: number): BN;
5
+ /** Read a signed little-endian i64 as a BN (two's-complement aware). */
6
+ export declare function readI64(data: Buffer, offset: number): BN;
5
7
  export declare function readU32(data: Buffer, offset: number): number;
6
8
  export declare function readU16(data: Buffer, offset: number): number;
7
9
  export declare function readU8(data: Buffer, offset: number): number;
@@ -10,3 +12,4 @@ export declare function writeOptionalPubkey(parts: number[], value: PublicKey |
10
12
  export declare function writeOptionalU64(parts: number[], value: BN | number | undefined | null): void;
11
13
  export declare function writeOptionalU32(parts: number[], value: number | undefined | null): void;
12
14
  export declare function writeOptionalU16(parts: number[], value: number | undefined | null): void;
15
+ export declare function writeOptionalU8(parts: number[], value: number | undefined | null): void;
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.readPubkey = readPubkey;
7
7
  exports.readU64 = readU64;
8
+ exports.readI64 = readI64;
8
9
  exports.readU32 = readU32;
9
10
  exports.readU16 = readU16;
10
11
  exports.readU8 = readU8;
@@ -13,6 +14,7 @@ exports.writeOptionalPubkey = writeOptionalPubkey;
13
14
  exports.writeOptionalU64 = writeOptionalU64;
14
15
  exports.writeOptionalU32 = writeOptionalU32;
15
16
  exports.writeOptionalU16 = writeOptionalU16;
17
+ exports.writeOptionalU8 = writeOptionalU8;
16
18
  const web3_js_1 = require("@solana/web3.js");
17
19
  const bn_js_1 = __importDefault(require("bn.js"));
18
20
  // ---------------------------------------------------------------------------
@@ -37,6 +39,11 @@ function readU64(data, offset) {
37
39
  checkBounds(data, offset, 8, "readU64");
38
40
  return new bn_js_1.default(data.subarray(offset, offset + 8), "le");
39
41
  }
42
+ /** Read a signed little-endian i64 as a BN (two's-complement aware). */
43
+ function readI64(data, offset) {
44
+ checkBounds(data, offset, 8, "readI64");
45
+ return new bn_js_1.default(data.readBigInt64LE(offset).toString());
46
+ }
40
47
  function readU32(data, offset) {
41
48
  checkBounds(data, offset, 4, "readU32");
42
49
  return data.readUInt32LE(offset);
@@ -97,3 +104,15 @@ function writeOptionalU16(parts, value) {
97
104
  parts.push(0x00);
98
105
  }
99
106
  }
107
+ function writeOptionalU8(parts, value) {
108
+ if (value != null) {
109
+ if (!Number.isInteger(value) || value < 0 || value > 0xff) {
110
+ throw new Error(`writeOptionalU8: value out of range (0..255): ${value}`);
111
+ }
112
+ parts.push(0x01);
113
+ parts.push(value);
114
+ }
115
+ else {
116
+ parts.push(0x00);
117
+ }
118
+ }
@@ -13,6 +13,12 @@ export interface InitOrUpdateStrategyArgs {
13
13
  addProtocol?: PublicKey | null;
14
14
  removeProtocol?: PublicKey | null;
15
15
  newAdmin?: PublicKey | null;
16
+ /**
17
+ * One-shot legacy backfill of `manager_count` (update path only) — set to the true
18
+ * live ManagerRole count on a strategy that predates the counter, so the
19
+ * close_strategy `manager_count == 0` gate is effective. Omit on normal calls.
20
+ */
21
+ managerCount?: number | null;
16
22
  remainingAccounts?: AccountMeta[];
17
23
  }
18
24
  export declare function createInitOrUpdateStrategyIx(args: InitOrUpdateStrategyArgs, programId?: PublicKey): TransactionInstruction;
@@ -23,6 +29,8 @@ export interface KvaultDepositArgs {
23
29
  depositorBaseAta: PublicKey;
24
30
  depositorSharesAta: PublicKey;
25
31
  amount: BN | number;
32
+ /** Minimum protocol-receipt out (deposit: shares/fTokens; withdraw: base). 0 = non-zero only. */
33
+ minOut?: BN | number;
26
34
  remainingAccounts: AccountMeta[];
27
35
  }
28
36
  export declare function createKvaultDepositIx(args: KvaultDepositArgs, programId?: PublicKey): TransactionInstruction;
@@ -33,6 +41,8 @@ export interface KvaultWithdrawArgs {
33
41
  depositorBaseAta: PublicKey;
34
42
  depositorSharesAta: PublicKey;
35
43
  shares: BN | number;
44
+ /** Minimum protocol-receipt out (deposit: shares/fTokens; withdraw: base). 0 = non-zero only. */
45
+ minOut?: BN | number;
36
46
  remainingAccounts: AccountMeta[];
37
47
  }
38
48
  export declare function createKvaultWithdrawIx(args: KvaultWithdrawArgs, programId?: PublicKey): TransactionInstruction;
@@ -43,6 +53,8 @@ export interface JupLendDepositArgs {
43
53
  depositorBaseAta: PublicKey;
44
54
  depositorFtokenAta: PublicKey;
45
55
  amount: BN | number;
56
+ /** Minimum protocol-receipt out (deposit: shares/fTokens; withdraw: base). 0 = non-zero only. */
57
+ minOut?: BN | number;
46
58
  remainingAccounts: AccountMeta[];
47
59
  }
48
60
  export declare function createJupLendDepositIx(args: JupLendDepositArgs, programId?: PublicKey): TransactionInstruction;
@@ -53,6 +65,8 @@ export interface JupLendWithdrawArgs {
53
65
  depositorBaseAta: PublicKey;
54
66
  depositorFtokenAta: PublicKey;
55
67
  amount: BN | number;
68
+ /** Minimum protocol-receipt out (deposit: shares/fTokens; withdraw: base). 0 = non-zero only. */
69
+ minOut?: BN | number;
56
70
  remainingAccounts: AccountMeta[];
57
71
  }
58
72
  export declare function createJupLendWithdrawIx(args: JupLendWithdrawArgs, programId?: PublicKey): TransactionInstruction;
@@ -58,12 +58,20 @@ function serializeInitOrUpdateStrategyParams(args) {
58
58
  else {
59
59
  parts.push(0x00);
60
60
  }
61
+ // manager_count: Option<u8> (last field; update-only backfill)
62
+ if (args.managerCount != null) {
63
+ parts.push(0x01);
64
+ parts.push(args.managerCount & 0xff);
65
+ }
66
+ else {
67
+ parts.push(0x00);
68
+ }
61
69
  return Buffer.from(parts);
62
70
  }
63
71
  function createInitOrUpdateStrategyIx(args, programId = constants_1.PROGRAM_ID) {
64
- const { admin, strategyState, baseMint, strategyBaseAta, tokenProgram, systemProgram = web3_js_1.SystemProgram.programId, associatedTokenProgram = spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, authority, flags, addProtocol, removeProtocol, newAdmin, remainingAccounts = [], } = args;
72
+ const { admin, strategyState, baseMint, strategyBaseAta, tokenProgram, systemProgram = web3_js_1.SystemProgram.programId, associatedTokenProgram = spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, authority, flags, addProtocol, removeProtocol, newAdmin, managerCount, remainingAccounts = [], } = args;
65
73
  const params = serializeInitOrUpdateStrategyParams({
66
- newAdmin, authority, flags, addProtocol, removeProtocol,
74
+ newAdmin, authority, flags, addProtocol, removeProtocol, managerCount,
67
75
  });
68
76
  const data = Buffer.concat([constants_1.IX_INIT_OR_UPDATE_STRATEGY, params]);
69
77
  return new web3_js_1.TransactionInstruction({
@@ -82,9 +90,10 @@ function createInitOrUpdateStrategyIx(args, programId = constants_1.PROGRAM_ID)
82
90
  });
83
91
  }
84
92
  function createKvaultDepositIx(args, programId = constants_1.PROGRAM_ID) {
85
- const { manager, strategyState, managerRole, depositorBaseAta, depositorSharesAta, amount, remainingAccounts } = args;
93
+ const { manager, strategyState, managerRole, depositorBaseAta, depositorSharesAta, amount, minOut = 0, remainingAccounts } = args;
86
94
  const data = [...constants_1.IX_KVAULT_DEPOSIT];
87
95
  (0, buffer_1.writeU64)(data, amount);
96
+ (0, buffer_1.writeU64)(data, minOut); // min_shares_out
88
97
  return new web3_js_1.TransactionInstruction({
89
98
  keys: [
90
99
  { pubkey: manager, isSigner: true, isWritable: true },
@@ -99,9 +108,10 @@ function createKvaultDepositIx(args, programId = constants_1.PROGRAM_ID) {
99
108
  });
100
109
  }
101
110
  function createKvaultWithdrawIx(args, programId = constants_1.PROGRAM_ID) {
102
- const { manager, strategyState, managerRole, depositorBaseAta, depositorSharesAta, shares, remainingAccounts } = args;
111
+ const { manager, strategyState, managerRole, depositorBaseAta, depositorSharesAta, shares, minOut = 0, remainingAccounts } = args;
103
112
  const data = [...constants_1.IX_KVAULT_WITHDRAW];
104
113
  (0, buffer_1.writeU64)(data, shares);
114
+ (0, buffer_1.writeU64)(data, minOut); // min_base_out
105
115
  return new web3_js_1.TransactionInstruction({
106
116
  keys: [
107
117
  { pubkey: manager, isSigner: true, isWritable: true },
@@ -116,9 +126,10 @@ function createKvaultWithdrawIx(args, programId = constants_1.PROGRAM_ID) {
116
126
  });
117
127
  }
118
128
  function createJupLendDepositIx(args, programId = constants_1.PROGRAM_ID) {
119
- const { manager, strategyState, managerRole, depositorBaseAta, depositorFtokenAta, amount, remainingAccounts } = args;
129
+ const { manager, strategyState, managerRole, depositorBaseAta, depositorFtokenAta, amount, minOut = 0, remainingAccounts } = args;
120
130
  const data = [...constants_1.IX_JUP_LEND_DEPOSIT];
121
131
  (0, buffer_1.writeU64)(data, amount);
132
+ (0, buffer_1.writeU64)(data, minOut); // min_ftokens_out
122
133
  return new web3_js_1.TransactionInstruction({
123
134
  keys: [
124
135
  { pubkey: manager, isSigner: true, isWritable: true },
@@ -133,9 +144,10 @@ function createJupLendDepositIx(args, programId = constants_1.PROGRAM_ID) {
133
144
  });
134
145
  }
135
146
  function createJupLendWithdrawIx(args, programId = constants_1.PROGRAM_ID) {
136
- const { manager, strategyState, managerRole, depositorBaseAta, depositorFtokenAta, amount, remainingAccounts } = args;
147
+ const { manager, strategyState, managerRole, depositorBaseAta, depositorFtokenAta, amount, minOut = 0, remainingAccounts } = args;
137
148
  const data = [...constants_1.IX_JUP_LEND_WITHDRAW];
138
149
  (0, buffer_1.writeU64)(data, amount);
150
+ (0, buffer_1.writeU64)(data, minOut); // min_base_out
139
151
  return new web3_js_1.TransactionInstruction({
140
152
  keys: [
141
153
  { pubkey: manager, isSigner: true, isWritable: true },
@@ -27,6 +27,12 @@ export interface ProtocolActionArgs {
27
27
  action: ProtocolAction;
28
28
  /** Amount in base token units (e.g. USDC lamports) */
29
29
  amount: bigint;
30
+ /**
31
+ * Minimum protocol-receipt out (deposit: min shares/fTokens; withdraw: min base).
32
+ * On-chain the handler reverts if the CPI yields less (and always requires > 0).
33
+ * Defaults to 0n (non-zero still enforced; 0 opts out of slippage protection).
34
+ */
35
+ minOut?: bigint;
30
36
  baseMint: PublicKey;
31
37
  /** Token program for the base mint (defaults to SPL Token) */
32
38
  tokenProgram?: PublicKey;
@@ -30,7 +30,7 @@ const jupiter_lend_1 = require("./jupiter-lend");
30
30
  * @returns TransactionInstruction[] ready for sendSmartTx
31
31
  */
32
32
  async function buildProtocolActionIxs(args) {
33
- const { connection, manager, protocol, protocolType, action, amount, baseMint, tokenProgram = spl_token_1.TOKEN_PROGRAM_ID, jupLendPool, programId = constants_1.PROGRAM_ID, } = args;
33
+ const { connection, manager, protocol, protocolType, action, amount, minOut = 0n, baseMint, tokenProgram = spl_token_1.TOKEN_PROGRAM_ID, jupLendPool, programId = constants_1.PROGRAM_ID, } = args;
34
34
  const [strategyStatePda] = args.strategyState
35
35
  ? [args.strategyState]
36
36
  : (0, pda_1.findStrategyStatePda)(baseMint, programId);
@@ -46,6 +46,7 @@ async function buildProtocolActionIxs(args) {
46
46
  protocol,
47
47
  action,
48
48
  amount,
49
+ minOut,
49
50
  baseMint,
50
51
  tokenProgram,
51
52
  pool: jupLendPool ?? jupiter_lend_1.JUP_LEND_POOLS[baseMint.toBase58()] ?? jupiter_lend_1.USDC_POOL,
@@ -62,6 +63,7 @@ async function buildProtocolActionIxs(args) {
62
63
  vaultState: protocol,
63
64
  action,
64
65
  amount,
66
+ minOut,
65
67
  baseMint,
66
68
  tokenProgram,
67
69
  programId,
@@ -69,7 +71,7 @@ async function buildProtocolActionIxs(args) {
69
71
  }
70
72
  }
71
73
  function buildJupLendActionIxs(params) {
72
- const { manager, strategyStatePda, strategyBaseAta, managerRolePda, action, amount, tokenProgram, pool, programId, } = params;
74
+ const { manager, strategyStatePda, strategyBaseAta, managerRolePda, action, amount, minOut, tokenProgram, pool, programId, } = params;
73
75
  const strategyFtokenAta = (0, ata_1.findAta)(pool.fTokenMint, strategyStatePda, tokenProgram);
74
76
  const JUP_LEND_PROG = new web3_js_1.PublicKey("jup3YeL8QhtSx1e253b2FDvsMNC87fDrgQZivbrndc9");
75
77
  // Fixed accounts for the Anchor instruction
@@ -96,9 +98,12 @@ function buildJupLendActionIxs(params) {
96
98
  { pubkey: JUP_LEND_PROG, isSigner: false, isWritable: false },
97
99
  ];
98
100
  }
101
+ // Data: disc(8) + amount(8) + min_out(8) [deposit: min_ftokens_out, withdraw: min_base_out].
99
102
  const amountBuf = Buffer.alloc(8);
100
103
  amountBuf.writeBigUInt64LE(amount, 0);
101
- const ixData = Buffer.concat([disc, amountBuf]);
104
+ const minOutBuf = Buffer.alloc(8);
105
+ minOutBuf.writeBigUInt64LE(minOut, 0);
106
+ const ixData = Buffer.concat([disc, amountBuf, minOutBuf]);
102
107
  const ix = new web3_js_1.TransactionInstruction({
103
108
  keys: [...fixedAccounts, ...remainingAccounts],
104
109
  programId,
@@ -107,7 +112,7 @@ function buildJupLendActionIxs(params) {
107
112
  return Promise.resolve([ix]);
108
113
  }
109
114
  async function buildKvaultActionIxs(params) {
110
- const { connection, manager, strategyStatePda, strategyBaseAta, managerRolePda, vaultState, action, amount, baseMint, tokenProgram, programId, } = params;
115
+ const { connection, manager, strategyStatePda, strategyBaseAta, managerRolePda, vaultState, action, amount, minOut, baseMint, tokenProgram, programId, } = params;
111
116
  // Read vault state for reserve data (needed for both deposit and withdraw)
112
117
  const vaultStateInfo = await connection.getAccountInfo(vaultState);
113
118
  if (!vaultStateInfo)
@@ -154,18 +159,21 @@ async function buildKvaultActionIxs(params) {
154
159
  { pubkey: strategySharesAta, isSigner: false, isWritable: true },
155
160
  ];
156
161
  if (action === "deposit") {
157
- return buildKvaultDepositIxs(fixedAccounts, vaultState, strategyStatePda, strategyBaseAta, strategySharesAta, baseMint, klendReserves, amount, programId);
162
+ return buildKvaultDepositIxs(fixedAccounts, vaultState, strategyStatePda, strategyBaseAta, strategySharesAta, baseMint, klendReserves, amount, minOut, programId);
158
163
  }
159
164
  else {
160
- return buildKvaultWithdrawIxs(connection, fixedAccounts, vaultState, vaultData, strategyStatePda, strategyBaseAta, strategySharesAta, baseMint, klendReserves, amount, programId);
165
+ return buildKvaultWithdrawIxs(connection, fixedAccounts, vaultState, vaultData, strategyStatePda, strategyBaseAta, strategySharesAta, baseMint, klendReserves, amount, minOut, programId);
161
166
  }
162
167
  }
163
- function buildKvaultDepositIxs(fixedAccounts, vaultState, strategyStatePda, strategyBaseAta, strategySharesAta, baseMint, klendReserves, amount, programId) {
168
+ function buildKvaultDepositIxs(fixedAccounts, vaultState, strategyStatePda, strategyBaseAta, strategySharesAta, baseMint, klendReserves, amount, minOut, programId) {
164
169
  const disc = Buffer.from(constants_1.IX_KVAULT_DEPOSIT);
165
170
  const remainingAccounts = (0, kamino_vault_1.buildKvaultDepositAccounts)(vaultState, strategyStatePda, strategyBaseAta, strategySharesAta, baseMint, klendReserves).map((a) => ({ ...a, isSigner: false }));
171
+ // Data: disc(8) + amount(8) + min_shares_out(8).
166
172
  const amountBuf = Buffer.alloc(8);
167
173
  amountBuf.writeBigUInt64LE(amount, 0);
168
- const ixData = Buffer.concat([disc, amountBuf]);
174
+ const minOutBuf = Buffer.alloc(8);
175
+ minOutBuf.writeBigUInt64LE(minOut, 0);
176
+ const ixData = Buffer.concat([disc, amountBuf, minOutBuf]);
169
177
  const ix = new web3_js_1.TransactionInstruction({
170
178
  keys: [...fixedAccounts, ...remainingAccounts],
171
179
  programId,
@@ -173,7 +181,7 @@ function buildKvaultDepositIxs(fixedAccounts, vaultState, strategyStatePda, stra
173
181
  });
174
182
  return Promise.resolve([ix]);
175
183
  }
176
- async function buildKvaultWithdrawIxs(connection, fixedAccounts, vaultState, vaultData, strategyStatePda, strategyBaseAta, strategySharesAta, baseMint, klendReserves, amount, programId) {
184
+ async function buildKvaultWithdrawIxs(connection, fixedAccounts, vaultState, vaultData, strategyStatePda, strategyBaseAta, strategySharesAta, baseMint, klendReserves, amount, minOut, programId) {
177
185
  const disc = Buffer.from(constants_1.IX_KVAULT_WITHDRAW);
178
186
  // Convert base amount to kvToken shares
179
187
  const sharesIssued = vaultData.readBigUInt64LE(232);
@@ -221,9 +229,12 @@ async function buildKvaultWithdrawIxs(connection, fixedAccounts, vaultState, vau
221
229
  remainingAccounts.push({ pubkey: r.lendingMarket, isSigner: false, isWritable: false });
222
230
  }
223
231
  }
232
+ // Data: disc(8) + shares(8) + min_base_out(8). minOut is the base-out floor.
224
233
  const sharesBuf = Buffer.alloc(8);
225
234
  sharesBuf.writeBigUInt64LE(withdrawShares, 0);
226
- const ixData = Buffer.concat([disc, sharesBuf]);
235
+ const minOutBuf = Buffer.alloc(8);
236
+ minOutBuf.writeBigUInt64LE(minOut, 0);
237
+ const ixData = Buffer.concat([disc, sharesBuf, minOutBuf]);
227
238
  const ix = new web3_js_1.TransactionInstruction({
228
239
  keys: [...fixedAccounts, ...remainingAccounts],
229
240
  programId,
@@ -50,7 +50,10 @@ export declare function deserializeManagerRole(data: Buffer): ManagerRole;
50
50
  * [108..112] _headerPadding [u8; 4]
51
51
  * --- Custom JLPD fields (112 bytes) ---
52
52
  * [112..120] baseLoaned u64 (base assets swapped into JLP)
53
- * [120..224] _reserved [u8; 104]
53
+ * [120..128] swapArb i64 (signed arb accumulator; read as BN)
54
+ * [128..160] priceOracle Pubkey (per-strategy Doves price feed)
55
+ * [160..192] _reserved [u8; 32]
56
+ * [192..224] _reserved2 [u8; 32]
54
57
  */
55
58
  export declare function deserializeJlpStrategyState(data: Buffer): JlpStrategyState;
56
59
  /**
@@ -107,7 +107,10 @@ function deserializeManagerRole(data) {
107
107
  * [108..112] _headerPadding [u8; 4]
108
108
  * --- Custom JLPD fields (112 bytes) ---
109
109
  * [112..120] baseLoaned u64 (base assets swapped into JLP)
110
- * [120..224] _reserved [u8; 104]
110
+ * [120..128] swapArb i64 (signed arb accumulator; read as BN)
111
+ * [128..160] priceOracle Pubkey (per-strategy Doves price feed)
112
+ * [160..192] _reserved [u8; 32]
113
+ * [192..224] _reserved2 [u8; 32]
111
114
  */
112
115
  function deserializeJlpStrategyState(data) {
113
116
  if (data.length < constants_1.STRATEGY_STATE_SIZE) {
@@ -130,6 +133,8 @@ function deserializeJlpStrategyState(data) {
130
133
  bump: (0, buffer_1.readU8)(data, 107),
131
134
  // Custom
132
135
  baseLoaned: (0, buffer_1.readU64)(data, 112),
136
+ swapArb: (0, buffer_1.readI64)(data, 120),
137
+ priceOracle: (0, buffer_1.readPubkey)(data, 128),
133
138
  };
134
139
  }
135
140
  /**
@@ -45,16 +45,26 @@ export interface AdapterManageArgs {
45
45
  managerRole: PublicKey;
46
46
  adapterState: PublicKey;
47
47
  adapterProgram: PublicKey;
48
- /** Adapter instruction discriminator (e.g. ADAPTER_DISC_BORROW) */
48
+ /** Adapter instruction discriminator (>= 0x03; e.g. ADAPTER_DISC_BORROW) */
49
49
  adapterDisc: number;
50
- amount: BN | number;
51
- /** Protocol-specific remaining accounts for the adapter CPI */
50
+ /**
51
+ * Opaque payload forwarded verbatim after the discriminator. Provide either
52
+ * `data` (raw bytes) or `amount` (convenience: packed as u64 LE, which is
53
+ * what jlpd-lend-adapter's verbs expect).
54
+ */
55
+ data?: Buffer;
56
+ amount?: BN | number;
57
+ /**
58
+ * Protocol-specific remaining accounts for the adapter CPI. NOTE: the adapter
59
+ * authorizes on its `authority` (the config PDA, propagated as signer) — do
60
+ * NOT pass an adapter ManagerRole here; that system was retired.
61
+ */
52
62
  remainingAccounts?: AccountMeta[];
53
63
  }
54
64
  /**
55
- * Build `adapter_manage` instruction.
65
+ * Build `adapter_manage` instruction (Adapter Interface v1, opaque passthrough).
56
66
  *
57
- * Routes borrow/repay/earn operations through JLPD for config PDA signer propagation.
58
- * Anchor data: [8-byte disc, AdapterManageParams(u8 + u64)]
67
+ * Routes a management op (disc >= 0x03) through JLPD for config PDA signer
68
+ * propagation. Anchor data: [8-byte ix disc, adapter_disc: u8, data: Vec<u8>].
59
69
  */
60
70
  export declare function createAdapterManageIx(args: AdapterManageArgs, programId?: PublicKey): TransactionInstruction;
@@ -61,26 +61,38 @@ exports.ADAPTER_DISC_DEPOSIT_EARN = 0x04;
61
61
  exports.ADAPTER_DISC_WITHDRAW_EARN = 0x05;
62
62
  exports.ADAPTER_DISC_REPAY = 0x06;
63
63
  /**
64
- * Build `adapter_manage` instruction.
64
+ * Build `adapter_manage` instruction (Adapter Interface v1, opaque passthrough).
65
65
  *
66
- * Routes borrow/repay/earn operations through JLPD for config PDA signer propagation.
67
- * Anchor data: [8-byte disc, AdapterManageParams(u8 + u64)]
66
+ * Routes a management op (disc >= 0x03) through JLPD for config PDA signer
67
+ * propagation. Anchor data: [8-byte ix disc, adapter_disc: u8, data: Vec<u8>].
68
68
  */
69
69
  function createAdapterManageIx(args, programId = constants_1.PROGRAM_ID) {
70
- const { manager, config, managerRole, adapterState, adapterProgram, adapterDisc, amount, remainingAccounts = [], } = args;
71
- // Anchor data: [8-byte disc, adapter_disc: u8, amount: u64]
72
- const data = Buffer.alloc(17);
70
+ const { manager, config, managerRole, adapterState, adapterProgram, adapterDisc, data: rawData, amount, remainingAccounts = [], } = args;
71
+ // Resolve the opaque payload: explicit bytes, or a u64-LE amount.
72
+ let payload;
73
+ if (rawData !== undefined) {
74
+ payload = rawData;
75
+ }
76
+ else if (amount !== undefined) {
77
+ const amtBn = typeof amount === "number" ? new bn_js_1.default(amount) : amount;
78
+ payload = amtBn.toArrayLike(Buffer, "le", 8);
79
+ }
80
+ else {
81
+ throw new Error("createAdapterManageIx: provide either `data` or `amount`");
82
+ }
83
+ // Anchor data: [8-byte ix disc][adapter_disc: u8][Vec<u8>: u32 LE len + bytes]
84
+ const data = Buffer.alloc(8 + 1 + 4 + payload.length);
73
85
  constants_1.IX_ADAPTER_MANAGE.copy(data, 0);
74
86
  data[8] = adapterDisc;
75
- const amtBuf = typeof amount === "number" ? new bn_js_1.default(amount) : amount;
76
- amtBuf.toArrayLike(Buffer, "le", 8).copy(data, 9);
87
+ data.writeUInt32LE(payload.length, 9);
88
+ payload.copy(data, 13);
77
89
  return new web3_js_1.TransactionInstruction({
78
90
  programId,
79
91
  keys: [
80
92
  { pubkey: manager, isSigner: true, isWritable: false },
81
93
  { pubkey: config, isSigner: false, isWritable: true },
82
94
  { pubkey: managerRole, isSigner: false, isWritable: false },
83
- // remaining_accounts: [adapter_state, adapter_program, ...jupiter accounts]
95
+ // remaining_accounts: [adapter_state, adapter_program, ...protocol accounts]
84
96
  { pubkey: adapterState, isSigner: false, isWritable: true },
85
97
  { pubkey: adapterProgram, isSigner: false, isWritable: false },
86
98
  ...remainingAccounts,
@@ -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
+ }>;