@continuumdao/ctm-mpc-defi 0.1.3 → 0.2.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 (68) hide show
  1. package/README.md +16 -1
  2. package/dist/agent/catalog.cjs +913 -142
  3. package/dist/agent/catalog.cjs.map +1 -1
  4. package/dist/agent/catalog.d.cts +834 -12
  5. package/dist/agent/catalog.d.ts +834 -12
  6. package/dist/agent/catalog.js +863 -143
  7. package/dist/agent/catalog.js.map +1 -1
  8. package/dist/chains/evm/index.cjs +13 -0
  9. package/dist/chains/evm/index.cjs.map +1 -1
  10. package/dist/chains/evm/index.d.cts +3 -1
  11. package/dist/chains/evm/index.d.ts +3 -1
  12. package/dist/chains/evm/index.js +13 -1
  13. package/dist/chains/evm/index.js.map +1 -1
  14. package/dist/core/index.cjs +76 -0
  15. package/dist/core/index.cjs.map +1 -1
  16. package/dist/core/index.d.cts +35 -2
  17. package/dist/core/index.d.ts +35 -2
  18. package/dist/core/index.js +70 -1
  19. package/dist/core/index.js.map +1 -1
  20. package/dist/index.cjs +934 -141
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +3 -2
  23. package/dist/index.d.ts +3 -2
  24. package/dist/index.js +927 -142
  25. package/dist/index.js.map +1 -1
  26. package/dist/protocols/evm/aave-v4/index.cjs +1987 -0
  27. package/dist/protocols/evm/aave-v4/index.cjs.map +1 -0
  28. package/dist/protocols/evm/aave-v4/index.d.cts +500 -0
  29. package/dist/protocols/evm/aave-v4/index.d.ts +500 -0
  30. package/dist/protocols/evm/aave-v4/index.js +1943 -0
  31. package/dist/protocols/evm/aave-v4/index.js.map +1 -0
  32. package/dist/protocols/evm/ethena/index.cjs +965 -0
  33. package/dist/protocols/evm/ethena/index.cjs.map +1 -0
  34. package/dist/protocols/evm/ethena/index.d.cts +161 -0
  35. package/dist/protocols/evm/ethena/index.d.ts +161 -0
  36. package/dist/protocols/evm/ethena/index.js +943 -0
  37. package/dist/protocols/evm/ethena/index.js.map +1 -0
  38. package/dist/protocols/evm/euler-v2/index.cjs +2263 -0
  39. package/dist/protocols/evm/euler-v2/index.cjs.map +1 -0
  40. package/dist/protocols/evm/euler-v2/index.d.cts +317 -0
  41. package/dist/protocols/evm/euler-v2/index.d.ts +317 -0
  42. package/dist/protocols/evm/euler-v2/index.js +2238 -0
  43. package/dist/protocols/evm/euler-v2/index.js.map +1 -0
  44. package/dist/protocols/evm/lido/index.cjs +834 -0
  45. package/dist/protocols/evm/lido/index.cjs.map +1 -0
  46. package/dist/protocols/evm/lido/index.d.cts +120 -0
  47. package/dist/protocols/evm/lido/index.d.ts +120 -0
  48. package/dist/protocols/evm/lido/index.js +809 -0
  49. package/dist/protocols/evm/lido/index.js.map +1 -0
  50. package/dist/protocols/evm/maple/index.cjs +707 -0
  51. package/dist/protocols/evm/maple/index.cjs.map +1 -0
  52. package/dist/protocols/evm/maple/index.d.cts +109 -0
  53. package/dist/protocols/evm/maple/index.d.ts +109 -0
  54. package/dist/protocols/evm/maple/index.js +693 -0
  55. package/dist/protocols/evm/maple/index.js.map +1 -0
  56. package/dist/protocols/evm/sky/index.cjs +1254 -0
  57. package/dist/protocols/evm/sky/index.cjs.map +1 -0
  58. package/dist/protocols/evm/sky/index.d.cts +218 -0
  59. package/dist/protocols/evm/sky/index.d.ts +218 -0
  60. package/dist/protocols/evm/sky/index.js +1229 -0
  61. package/dist/protocols/evm/sky/index.js.map +1 -0
  62. package/dist/protocols/evm/uniswap-v4/index.cjs +31 -11
  63. package/dist/protocols/evm/uniswap-v4/index.cjs.map +1 -1
  64. package/dist/protocols/evm/uniswap-v4/index.d.cts +2 -1
  65. package/dist/protocols/evm/uniswap-v4/index.d.ts +2 -1
  66. package/dist/protocols/evm/uniswap-v4/index.js +31 -11
  67. package/dist/protocols/evm/uniswap-v4/index.js.map +1 -1
  68. package/package.json +37 -3
@@ -0,0 +1,943 @@
1
+ import { parseAbi, getAddress, defineChain, createPublicClient, http, parseUnits, encodeFunctionData, parseGwei, serializeTransaction, keccak256, formatUnits } from 'viem';
2
+
3
+ // src/core/registry.ts
4
+ var modules = [];
5
+ function registerProtocolModule(mod) {
6
+ const existing = modules.findIndex((m) => m.id === mod.id);
7
+ if (existing >= 0) {
8
+ modules[existing] = mod;
9
+ } else {
10
+ modules.push(mod);
11
+ }
12
+ }
13
+
14
+ // src/chains/evm/chainIdParse.ts
15
+ function parseEvmChainIdToNumber(chainId) {
16
+ if (chainId == null) return Number.NaN;
17
+ if (typeof chainId === "bigint") {
18
+ const n = Number(chainId);
19
+ return Number.isSafeInteger(n) && n >= 0 ? n : Number.NaN;
20
+ }
21
+ if (typeof chainId === "number") {
22
+ return Number.isInteger(chainId) && chainId >= 0 ? chainId : Number.NaN;
23
+ }
24
+ const t = String(chainId).trim();
25
+ if (!t) return Number.NaN;
26
+ const low = t.toLowerCase();
27
+ if (low.startsWith("eip155:")) {
28
+ const rest = t.slice("eip155:".length).trim();
29
+ const n = Number.parseInt(rest, 10);
30
+ return Number.isNaN(n) || n < 0 ? Number.NaN : n;
31
+ }
32
+ if (low.startsWith("0x")) {
33
+ return Number.parseInt(t, 16);
34
+ }
35
+ return Number.parseInt(t, 10);
36
+ }
37
+
38
+ // src/protocols/evm/ethena/constants.ts
39
+ var ETHENA_LABS_GITHUB = "https://github.com/ethena-labs";
40
+ var ETHENA_KEY_ADDRESSES_DOC = "https://docs.ethena.fi/solution-design/key-addresses";
41
+ var USDE_ETHEREUM_MAINNET = "0x4c9edd5852cd905f086c759e8383e09bff1e68b3";
42
+ var SUSDE_ETHEREUM_MAINNET = "0x9d39a5de30e57443bff2a8307a4256c8797a3497";
43
+ var USDE_MOST_L2S = "0x5d3a1Ff2b6BAb83b63cd9AD0787074081a52ef34";
44
+ var USDE_ZKSYNC_ERA = "0x39Fe7a0DACcE31Bd90418e3e659fb0b5f0B3Db0d";
45
+ var L2_SAME_ADDRESS_CHAIN_IDS = /* @__PURE__ */ new Set([
46
+ 42161,
47
+ // Arbitrum One
48
+ 10,
49
+ // Optimism
50
+ 8453,
51
+ // Base
52
+ 56,
53
+ // BNB Chain
54
+ 59144,
55
+ // Linea
56
+ 5e3,
57
+ // Mantle
58
+ 81457,
59
+ // Blast
60
+ 169,
61
+ // Manta Pacific
62
+ 534352,
63
+ // Scroll
64
+ 252,
65
+ // Fraxtal
66
+ 34443,
67
+ // Mode
68
+ 196,
69
+ // X Layer
70
+ 1088,
71
+ // Metis
72
+ 80084,
73
+ // Berachain
74
+ 2222,
75
+ // Kava
76
+ 2818,
77
+ // Morph
78
+ 1923,
79
+ // Swell
80
+ 48900
81
+ // Zircuit
82
+ ]);
83
+ var FALLBACK_NAME_BY_ID = {
84
+ 1: "Ethereum",
85
+ 42161: "Arbitrum One",
86
+ 10: "Optimism",
87
+ 8453: "Base",
88
+ 56: "BNB Chain",
89
+ 59144: "Linea",
90
+ 5e3: "Mantle",
91
+ 81457: "Blast",
92
+ 169: "Manta Pacific",
93
+ 534352: "Scroll",
94
+ 252: "Fraxtal",
95
+ 34443: "Mode",
96
+ 196: "X Layer",
97
+ 1088: "Metis",
98
+ 80084: "Berachain",
99
+ 2222: "Kava",
100
+ 2818: "Morph",
101
+ 1923: "Swell",
102
+ 48900: "Zircuit",
103
+ 324: "ZKSync Era"
104
+ };
105
+ function usdeTokenAddressOnEvmChain(chainId) {
106
+ if (chainId === 1) return USDE_ETHEREUM_MAINNET;
107
+ if (chainId === 324) return USDE_ZKSYNC_ERA;
108
+ if (L2_SAME_ADDRESS_CHAIN_IDS.has(chainId)) return USDE_MOST_L2S;
109
+ return null;
110
+ }
111
+ function isEthenaUsdeOnAssetsChain(assetsChainId, contractAddress) {
112
+ const n = parseEvmChainIdToNumber(assetsChainId);
113
+ if (Number.isNaN(n) || n < 0) return false;
114
+ const expected = usdeTokenAddressOnEvmChain(n);
115
+ if (!expected) return false;
116
+ try {
117
+ return getAddress(contractAddress) === getAddress(expected);
118
+ } catch {
119
+ return false;
120
+ }
121
+ }
122
+ function listEthenaUsdeEvmNetworkRows() {
123
+ const rows = [
124
+ { chainId: 1, label: FALLBACK_NAME_BY_ID[1], usde: USDE_ETHEREUM_MAINNET }
125
+ ];
126
+ const l2 = [...L2_SAME_ADDRESS_CHAIN_IDS].sort((a, b) => a - b);
127
+ for (const id of l2) {
128
+ rows.push({
129
+ chainId: id,
130
+ label: FALLBACK_NAME_BY_ID[id] ?? `Chain ${id}`,
131
+ usde: USDE_MOST_L2S
132
+ });
133
+ }
134
+ rows.push({ chainId: 324, label: FALLBACK_NAME_BY_ID[324], usde: USDE_ZKSYNC_ERA });
135
+ return rows.sort((a, b) => {
136
+ if (a.chainId === 1) return -1;
137
+ if (b.chainId === 1) return 1;
138
+ return a.chainId - b.chainId;
139
+ });
140
+ }
141
+ function isEvmChainInEthenaUsdeList(chainId) {
142
+ return usdeTokenAddressOnEvmChain(chainId) != null;
143
+ }
144
+
145
+ // src/core/keygen.ts
146
+ function firstClientIdFromKeyGen(data) {
147
+ const map = data?.ClientKeys;
148
+ if (!map || typeof map !== "object") return null;
149
+ for (const v of Object.values(map)) {
150
+ if (typeof v === "string" && v.trim()) return v.trim();
151
+ }
152
+ return null;
153
+ }
154
+
155
+ // src/chains/evm/txParams.ts
156
+ function gasLimitFromEstimateAndChainConfig(estimatedGas, chainGasLimit) {
157
+ if (chainGasLimit == null || !Number.isFinite(chainGasLimit) || chainGasLimit <= 0) {
158
+ return estimatedGas;
159
+ }
160
+ const cfg = BigInt(Math.floor(chainGasLimit));
161
+ return cfg > estimatedGas ? cfg : estimatedGas;
162
+ }
163
+ async function fetchChainFeeParams(rpcUrl, chainId) {
164
+ const url = rpcUrl.trim();
165
+ if (!url) return { isEip1559: false };
166
+ const chainIdNum = chainId;
167
+ if (Number.isNaN(chainIdNum)) return { isEip1559: false };
168
+ const chain = defineChain({
169
+ id: chainIdNum,
170
+ name: "Discovery",
171
+ nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
172
+ rpcUrls: { default: { http: [url] } }
173
+ });
174
+ const publicClient = createPublicClient({
175
+ chain,
176
+ transport: http(url)
177
+ });
178
+ const getGasPriceGwei = async () => {
179
+ const gasPriceWei = await publicClient.getGasPrice();
180
+ return parseFloat(formatUnits(gasPriceWei, 9));
181
+ };
182
+ try {
183
+ const block = await publicClient.getBlock({ blockTag: "latest" });
184
+ const baseFeePerGas = block?.baseFeePerGas;
185
+ if (baseFeePerGas == null || baseFeePerGas === void 0) {
186
+ const gasPriceGwei2 = await getGasPriceGwei();
187
+ return { isEip1559: false, gasPriceGwei: gasPriceGwei2 };
188
+ }
189
+ const baseFeeGwei = parseFloat(formatUnits(baseFeePerGas, 9));
190
+ let priorityFeeGwei;
191
+ try {
192
+ const priorityWei = await publicClient.estimateMaxPriorityFeePerGas();
193
+ priorityFeeGwei = parseFloat(formatUnits(priorityWei, 9));
194
+ } catch {
195
+ }
196
+ const gasPriceGwei = await getGasPriceGwei();
197
+ return {
198
+ isEip1559: true,
199
+ baseFeeGwei,
200
+ priorityFeeGwei,
201
+ gasPriceGwei
202
+ };
203
+ } catch {
204
+ try {
205
+ const gasPriceWei = await publicClient.getGasPrice();
206
+ const gasPriceGwei = parseFloat(formatUnits(gasPriceWei, 9));
207
+ return { isEip1559: false, gasPriceGwei };
208
+ } catch {
209
+ return { isEip1559: false };
210
+ }
211
+ }
212
+ }
213
+ function finalizeEip1559Fees(maxFeePerGas, maxPriorityFeePerGas, floor, baseWei) {
214
+ let maxP = maxPriorityFeePerGas;
215
+ let maxF = maxFeePerGas;
216
+ if (baseWei > 0n && maxF < baseWei + maxP) {
217
+ maxF = baseWei + maxP + parseGwei("0.001");
218
+ }
219
+ if (maxF < maxP) {
220
+ maxF = baseWei > 0n ? baseWei + maxP + parseGwei("0.001") : maxP * 2n;
221
+ }
222
+ return { maxFeePerGas: maxF, maxPriorityFeePerGas: maxP };
223
+ }
224
+ function alignEip1559FeesWithLatestBase(maxFeePerGas, maxPriorityFeePerGas, latestBlockBaseFeeWei) {
225
+ return finalizeEip1559Fees(maxFeePerGas, maxPriorityFeePerGas, null, latestBlockBaseFeeWei);
226
+ }
227
+
228
+ // src/protocols/evm/ethena/multisign.ts
229
+ var AAVE_ERC20_APPROVE_FALLBACK = 100000n;
230
+ var ETHENA_SUSDE_DEPOSIT_GET_SIG_GAS_FALLBACK = 1200000n;
231
+ var ETHENA_SUSDE_DEPOSIT_FALLBACK = ETHENA_SUSDE_DEPOSIT_GET_SIG_GAS_FALLBACK;
232
+ var ETHENA_SUSDE_REDEEM_FALLBACK = 1200000n;
233
+ function gweiToDecimalString(n) {
234
+ if (!Number.isFinite(n)) return "0";
235
+ if (n === 0) return "0";
236
+ const s = String(n);
237
+ if (s.indexOf("e") !== -1 || s.indexOf("E") !== -1) return n.toFixed(9).replace(/\.?0+$/, "") || "0";
238
+ return s;
239
+ }
240
+ var erc20AllowanceAbi = parseAbi([
241
+ "function allowance(address owner, address spender) view returns (uint256)",
242
+ "function decimals() view returns (uint8)"
243
+ ]);
244
+ var erc20ApproveAbi = parseAbi(["function approve(address spender, uint256 amount) returns (bool)"]);
245
+ var susdeErc4626Abi = parseAbi([
246
+ "function deposit(uint256 assets, address receiver) returns (uint256 shares)",
247
+ "function previewDeposit(uint256 assets) view returns (uint256 shares)",
248
+ "function previewRedeem(uint256 shares) view returns (uint256 assets)",
249
+ "function redeem(uint256 shares, address receiver, address owner) returns (uint256 assets)",
250
+ "function decimals() view returns (uint8)"
251
+ ]);
252
+ var stakedUsdeV2UnstakeAbi = parseAbi([
253
+ "function cooldownShares(uint256 shares, address owner) returns (uint256 assets)",
254
+ "function unstake(address receiver)"
255
+ ]);
256
+ async function readEthenaStakePreviewShares(args) {
257
+ const { rpcUrl, usdeAmountWei } = args;
258
+ if (usdeAmountWei <= 0n) return 0n;
259
+ const ch = defineChain({
260
+ id: 1,
261
+ name: "Ethereum",
262
+ nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
263
+ rpcUrls: { default: { http: [rpcUrl.trim()] } }
264
+ });
265
+ const client = createPublicClient({ chain: ch, transport: http(rpcUrl.trim()) });
266
+ const vault = getAddress(SUSDE_ETHEREUM_MAINNET);
267
+ return client.readContract({
268
+ address: vault,
269
+ abi: susdeErc4626Abi,
270
+ functionName: "previewDeposit",
271
+ args: [usdeAmountWei]
272
+ });
273
+ }
274
+ async function readEthenaUnstakePreviewUsde(args) {
275
+ const { rpcUrl, susdeSharesWei } = args;
276
+ if (susdeSharesWei <= 0n) return 0n;
277
+ const ch = defineChain({
278
+ id: 1,
279
+ name: "Ethereum",
280
+ nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
281
+ rpcUrls: { default: { http: [rpcUrl.trim()] } }
282
+ });
283
+ const client = createPublicClient({ chain: ch, transport: http(rpcUrl.trim()) });
284
+ const vault = getAddress(SUSDE_ETHEREUM_MAINNET);
285
+ return client.readContract({
286
+ address: vault,
287
+ abi: susdeErc4626Abi,
288
+ functionName: "previewRedeem",
289
+ args: [susdeSharesWei]
290
+ });
291
+ }
292
+ async function buildEvmMultisignBodyEthenaUsdeStakeToSusde(args) {
293
+ if (args.chainId !== 1) {
294
+ throw new Error("Ethena USDe \u2192 sUSDe stake is only supported on Ethereum mainnet (chain id 1).");
295
+ }
296
+ const ph = (args.keyGen.pubkeyhex ?? "").trim();
297
+ if (!ph) throw new Error("keyGen pubKey (pubkeyhex) is required");
298
+ const keyList = args.keyGen.keylist ?? [];
299
+ const clientId = firstClientIdFromKeyGen(args.keyGen);
300
+ const usde = getAddress(args.usde);
301
+ const mainnetUsde = getAddress(USDE_ETHEREUM_MAINNET);
302
+ if (usde.toLowerCase() !== mainnetUsde.toLowerCase()) {
303
+ throw new Error("Token in must be mainnet USDe for this flow.");
304
+ }
305
+ const susde = getAddress(args.susdeVault ?? SUSDE_ETHEREUM_MAINNET);
306
+ const executor = getAddress(args.executorAddress);
307
+ const receiver = getAddress(args.receiver);
308
+ if (getAddress(SUSDE_ETHEREUM_MAINNET).toLowerCase() !== susde.toLowerCase()) {
309
+ throw new Error("sUSDe vault address mismatch.");
310
+ }
311
+ const ch = defineChain({
312
+ id: 1,
313
+ name: "Ethereum",
314
+ nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
315
+ rpcUrls: { default: { http: [args.rpcUrl.trim()] } }
316
+ });
317
+ const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl.trim()) });
318
+ const dec = await publicClient.readContract({
319
+ address: usde,
320
+ abi: erc20AllowanceAbi,
321
+ functionName: "decimals"
322
+ });
323
+ const amountWei = parseUnits(args.amountHuman, Number(dec));
324
+ if (amountWei === 0n) throw new Error("Amount is zero after converting with token decimals.");
325
+ const steps = [];
326
+ const currentAllowance = await publicClient.readContract({
327
+ address: usde,
328
+ abi: erc20AllowanceAbi,
329
+ functionName: "allowance",
330
+ args: [executor, susde]
331
+ });
332
+ if (currentAllowance < amountWei) {
333
+ if (currentAllowance > 0n) {
334
+ const dataReset = encodeFunctionData({
335
+ abi: erc20ApproveAbi,
336
+ functionName: "approve",
337
+ args: [susde, 0n]
338
+ });
339
+ steps.push({ kind: "approve", to: usde, data: dataReset, value: 0n });
340
+ }
341
+ const dataApprove = encodeFunctionData({
342
+ abi: erc20ApproveAbi,
343
+ functionName: "approve",
344
+ args: [susde, amountWei]
345
+ });
346
+ steps.push({ kind: "approve", to: usde, data: dataApprove, value: 0n });
347
+ }
348
+ const depositData = encodeFunctionData({
349
+ abi: susdeErc4626Abi,
350
+ functionName: "deposit",
351
+ args: [amountWei, receiver]
352
+ });
353
+ steps.push({ kind: "deposit", to: susde, data: depositData, value: 0n });
354
+ const feeParams = await fetchChainFeeParams(args.rpcUrl, 1);
355
+ const legacy = Boolean(args.chainDetail?.legacy) || !feeParams.isEip1559;
356
+ const latestBaseFeeWei = !legacy ? (await publicClient.getBlock({ blockTag: "latest" })).baseFeePerGas ?? 0n : 0n;
357
+ const useCustomGas = args.useCustomGas;
358
+ const gasLimitConfig = useCustomGas && args.chainDetail?.gasLimit != null ? Number(args.chainDetail.gasLimit) : void 0;
359
+ const gasFeeMultiplier = useCustomGas && args.chainDetail?.gasMultiplier != null ? Number(args.chainDetail.gasMultiplier) : void 0;
360
+ const baseNonce = await publicClient.getTransactionCount({ address: executor, blockTag: "pending" });
361
+ const messageHashes = [];
362
+ const messageRawBatch = [];
363
+ const proposalTxParamsBatch = [];
364
+ const batchMeta = [];
365
+ let firstTxFeePayload = {};
366
+ let firstDataNo0x = "";
367
+ for (let i = 0; i < steps.length; i++) {
368
+ const s = steps[i];
369
+ const currentNonce = baseNonce + i;
370
+ let estimatedGas;
371
+ const skipDepositEstimateBecausePriorApproves = s.kind === "deposit" && i > 0;
372
+ if (skipDepositEstimateBecausePriorApproves) {
373
+ estimatedGas = ETHENA_SUSDE_DEPOSIT_FALLBACK;
374
+ } else {
375
+ try {
376
+ estimatedGas = await publicClient.estimateGas({
377
+ to: s.to,
378
+ data: s.data,
379
+ value: s.value,
380
+ account: executor
381
+ });
382
+ } catch {
383
+ estimatedGas = s.kind === "approve" ? AAVE_ERC20_APPROVE_FALLBACK : ETHENA_SUSDE_DEPOSIT_FALLBACK;
384
+ }
385
+ }
386
+ const gasLimitI = useCustomGas ? gasLimitFromEstimateAndChainConfig(estimatedGas, gasLimitConfig) : estimatedGas;
387
+ if (legacy) {
388
+ let gasPriceWei = await publicClient.getGasPrice();
389
+ if (useCustomGas && gasFeeMultiplier != null && gasFeeMultiplier > 0) {
390
+ gasPriceWei = gasPriceWei * BigInt(100 + gasFeeMultiplier) / 100n;
391
+ }
392
+ if (useCustomGas && args.chainDetail?.gasPrice != null && args.chainDetail.gasPrice > 0) {
393
+ const configured = parseGwei(gweiToDecimalString(Number(args.chainDetail.gasPrice)));
394
+ if (configured > gasPriceWei) gasPriceWei = configured;
395
+ }
396
+ const ser = serializeTransaction({
397
+ type: "legacy",
398
+ to: s.to,
399
+ data: s.data,
400
+ value: s.value,
401
+ gas: gasLimitI,
402
+ gasPrice: gasPriceWei,
403
+ nonce: currentNonce,
404
+ chainId: 1
405
+ });
406
+ const h = keccak256(ser);
407
+ messageHashes.push(h.startsWith("0x") ? h.slice(2) : h);
408
+ messageRawBatch.push(ser);
409
+ proposalTxParamsBatch.push({
410
+ nonce: currentNonce,
411
+ gasLimit: gasLimitI.toString(),
412
+ txType: "legacy",
413
+ gasPrice: gasPriceWei.toString()
414
+ });
415
+ if (i === 0) {
416
+ firstTxFeePayload = { txNonce: currentNonce, txGasLimit: gasLimitI.toString(), txGasPrice: gasPriceWei.toString() };
417
+ firstDataNo0x = s.data.startsWith("0x") ? s.data.slice(2) : s.data;
418
+ }
419
+ } else {
420
+ const fetchedBase = feeParams.baseFeeGwei ?? 0;
421
+ const fetchedPriority = feeParams.priorityFeeGwei ?? 0;
422
+ const configuredBase = useCustomGas && args.chainDetail?.baseFee != null ? Number(args.chainDetail.baseFee) : 0;
423
+ const configuredPriority = useCustomGas && args.chainDetail?.priorityFee != null ? Number(args.chainDetail.priorityFee) : 0;
424
+ const effectiveBaseFeeGwei = Math.max(fetchedBase, configuredBase);
425
+ const effectivePriorityFeeGwei = Math.max(fetchedPriority, configuredPriority);
426
+ const baseFeeMultiplierPct = useCustomGas && args.chainDetail?.baseFeeMultiplier != null ? Math.max(100, Number(args.chainDetail.baseFeeMultiplier)) : 100;
427
+ const baseComponentGwei = effectiveBaseFeeGwei * baseFeeMultiplierPct / 100;
428
+ const maxFeePerGasGwei = baseComponentGwei + effectivePriorityFeeGwei;
429
+ let maxPriorityFeePerGas = effectivePriorityFeeGwei > 0 ? parseGwei(gweiToDecimalString(effectivePriorityFeeGwei)) : parseGwei("1");
430
+ let maxFeePerGas = parseGwei(gweiToDecimalString(maxFeePerGasGwei));
431
+ if (useCustomGas && gasFeeMultiplier != null && gasFeeMultiplier > 0) {
432
+ maxPriorityFeePerGas = maxPriorityFeePerGas * BigInt(100 + gasFeeMultiplier) / 100n;
433
+ maxFeePerGas = maxFeePerGas * BigInt(100 + gasFeeMultiplier) / 100n;
434
+ }
435
+ ({ maxFeePerGas, maxPriorityFeePerGas } = alignEip1559FeesWithLatestBase(
436
+ maxFeePerGas,
437
+ maxPriorityFeePerGas,
438
+ latestBaseFeeWei
439
+ ));
440
+ const ser = serializeTransaction({
441
+ type: "eip1559",
442
+ to: s.to,
443
+ data: s.data,
444
+ value: s.value,
445
+ gas: gasLimitI,
446
+ maxFeePerGas,
447
+ maxPriorityFeePerGas,
448
+ nonce: currentNonce,
449
+ chainId: 1
450
+ });
451
+ const h = keccak256(ser);
452
+ messageHashes.push(h.startsWith("0x") ? h.slice(2) : h);
453
+ messageRawBatch.push(ser);
454
+ proposalTxParamsBatch.push({
455
+ nonce: currentNonce,
456
+ gasLimit: gasLimitI.toString(),
457
+ txType: "eip1559",
458
+ maxFeePerGas: maxFeePerGas.toString(),
459
+ maxPriorityFeePerGas: maxPriorityFeePerGas.toString()
460
+ });
461
+ if (i === 0) {
462
+ firstTxFeePayload = {
463
+ txNonce: currentNonce,
464
+ txGasLimit: gasLimitI.toString(),
465
+ txMaxFeePerGas: maxFeePerGas.toString(),
466
+ txMaxPriorityFeePerGas: maxPriorityFeePerGas.toString()
467
+ };
468
+ firstDataNo0x = s.data.startsWith("0x") ? s.data.slice(2) : s.data;
469
+ }
470
+ }
471
+ if (s.kind === "approve") {
472
+ batchMeta.push({
473
+ destinationAddress: usde,
474
+ signatureText: JSON.stringify({
475
+ kind: "Ethena",
476
+ name: "ERC20.approve",
477
+ to: "sUSDe vault",
478
+ function: "approve(address spender, uint256 amount)",
479
+ spender: susde,
480
+ amountHuman: args.amountHuman,
481
+ note: "Allowance to stake this USDe amount (not unlimited)."
482
+ }),
483
+ evm: { type: "ethena_erc20_approve", version: 1, chainId: "1" },
484
+ ethena: { step: "approve_usde", susde }
485
+ });
486
+ } else {
487
+ batchMeta.push({
488
+ destinationAddress: susde,
489
+ signatureText: JSON.stringify({
490
+ kind: "Ethena",
491
+ name: "StakedUSDe (ERC-4626).deposit",
492
+ function: "deposit(uint256 assets, address receiver)",
493
+ assets: amountWei.toString(),
494
+ receiver,
495
+ amountHuman: args.amountHuman
496
+ }),
497
+ evm: { type: "ethena_susde_deposit", version: 1, chainId: "1" },
498
+ ethena: { step: "deposit", vault: susde, gasBuildDeposit: { baseGasUnits: gasLimitI.toString() } }
499
+ });
500
+ }
501
+ }
502
+ const extraPayload = { batchMeta };
503
+ if (useCustomGas && args.customGasChainDetails && Object.keys(args.customGasChainDetails).length > 0) {
504
+ extraPayload.customGasChainDetails = args.customGasChainDetails;
505
+ }
506
+ const extraJSON = JSON.stringify(extraPayload);
507
+ const firstSigText = batchMeta[0].signatureText;
508
+ const n = steps.length;
509
+ const purposeSuffix = n === 1 ? "Ethena: 1-tx \u2014 deposit USDe to sUSDe (allowance already set)." : `Ethena: ${n}-tx batch \u2014 approve USDe to sUSDe vault, then deposit.`;
510
+ const bodyForSign = {
511
+ keyList,
512
+ pubKey: ph,
513
+ msgHash: messageHashes[0],
514
+ msgRaw: firstDataNo0x,
515
+ messageHashes,
516
+ messageRawBatch,
517
+ destinationChainID: "1",
518
+ destinationAddress: steps[0].to,
519
+ extraJSON,
520
+ signatureText: firstSigText,
521
+ purpose: (() => {
522
+ const t = args.purposeText.trim();
523
+ return (t ? `${t}
524
+
525
+ ` : "") + purposeSuffix;
526
+ })(),
527
+ ...firstTxFeePayload,
528
+ proposalTxParams: proposalTxParamsBatch
529
+ };
530
+ if (clientId) bodyForSign.clientId = clientId;
531
+ return { bodyForSign, messageToSign: JSON.stringify(bodyForSign) };
532
+ }
533
+ var ETHENA_SUSDE_COOLDOWN_SHARES_FALLBACK = 1200000n;
534
+ var ETHENA_SUSDE_UNSTAKE_CLAIM_FALLBACK = 500000n;
535
+ async function buildEthenaStakedUsdeOneTxMultisignBody(args) {
536
+ if (args.chainId !== 1) {
537
+ throw new Error("Ethena sUSDe exit is only supported on Ethereum mainnet (chain id 1).");
538
+ }
539
+ const ph = (args.keyGen.pubkeyhex ?? "").trim();
540
+ if (!ph) throw new Error("keyGen pubKey (pubkeyhex) is required");
541
+ const keyList = args.keyGen.keylist ?? [];
542
+ const clientId = firstClientIdFromKeyGen(args.keyGen);
543
+ const { executor } = args;
544
+ const steps = [args.step];
545
+ const feeParams = await fetchChainFeeParams(args.rpcUrl, 1);
546
+ const legacy = Boolean(args.chainDetail?.legacy) || !feeParams.isEip1559;
547
+ const useCustomGas = args.useCustomGas;
548
+ const gasLimitConfig = useCustomGas && args.chainDetail?.gasLimit != null ? Number(args.chainDetail.gasLimit) : void 0;
549
+ const gasFeeMultiplier = useCustomGas && args.chainDetail?.gasMultiplier != null ? Number(args.chainDetail.gasMultiplier) : void 0;
550
+ const ch = defineChain({
551
+ id: 1,
552
+ name: "Ethereum",
553
+ nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
554
+ rpcUrls: { default: { http: [args.rpcUrl.trim()] } }
555
+ });
556
+ const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl.trim()) });
557
+ const latestBaseFeeWei = !legacy ? (await publicClient.getBlock({ blockTag: "latest" })).baseFeePerGas ?? 0n : 0n;
558
+ const baseNonce = await publicClient.getTransactionCount({ address: executor, blockTag: "pending" });
559
+ const messageHashes = [];
560
+ const messageRawBatch = [];
561
+ const proposalTxParamsBatch = [];
562
+ const batchMeta = [];
563
+ let firstTxFeePayload = {};
564
+ let firstDataNo0x = "";
565
+ for (let i = 0; i < steps.length; i++) {
566
+ const s = steps[i];
567
+ const currentNonce = baseNonce + i;
568
+ let estimatedGas;
569
+ try {
570
+ estimatedGas = await publicClient.estimateGas({
571
+ to: s.to,
572
+ data: s.data,
573
+ value: s.value,
574
+ account: executor
575
+ });
576
+ } catch {
577
+ estimatedGas = args.gasFallback;
578
+ }
579
+ const gasLimitI = useCustomGas ? gasLimitFromEstimateAndChainConfig(estimatedGas, gasLimitConfig) : estimatedGas;
580
+ if (legacy) {
581
+ let gasPriceWei = await publicClient.getGasPrice();
582
+ if (useCustomGas && gasFeeMultiplier != null && gasFeeMultiplier > 0) {
583
+ gasPriceWei = gasPriceWei * BigInt(100 + gasFeeMultiplier) / 100n;
584
+ }
585
+ if (useCustomGas && args.chainDetail?.gasPrice != null && args.chainDetail.gasPrice > 0) {
586
+ const configured = parseGwei(gweiToDecimalString(Number(args.chainDetail.gasPrice)));
587
+ if (configured > gasPriceWei) gasPriceWei = configured;
588
+ }
589
+ const ser = serializeTransaction({
590
+ type: "legacy",
591
+ to: s.to,
592
+ data: s.data,
593
+ value: s.value,
594
+ gas: gasLimitI,
595
+ gasPrice: gasPriceWei,
596
+ nonce: currentNonce,
597
+ chainId: 1
598
+ });
599
+ const h = keccak256(ser);
600
+ messageHashes.push(h.startsWith("0x") ? h.slice(2) : h);
601
+ messageRawBatch.push(ser);
602
+ proposalTxParamsBatch.push({
603
+ nonce: currentNonce,
604
+ gasLimit: gasLimitI.toString(),
605
+ txType: "legacy",
606
+ gasPrice: gasPriceWei.toString()
607
+ });
608
+ if (i === 0) {
609
+ firstTxFeePayload = { txNonce: currentNonce, txGasLimit: gasLimitI.toString(), txGasPrice: gasPriceWei.toString() };
610
+ firstDataNo0x = s.data.startsWith("0x") ? s.data.slice(2) : s.data;
611
+ }
612
+ } else {
613
+ const fetchedBase = feeParams.baseFeeGwei ?? 0;
614
+ const fetchedPriority = feeParams.priorityFeeGwei ?? 0;
615
+ const configuredBase = useCustomGas && args.chainDetail?.baseFee != null ? Number(args.chainDetail.baseFee) : 0;
616
+ const configuredPriority = useCustomGas && args.chainDetail?.priorityFee != null ? Number(args.chainDetail.priorityFee) : 0;
617
+ const effectiveBaseFeeGwei = Math.max(fetchedBase, configuredBase);
618
+ const effectivePriorityFeeGwei = Math.max(fetchedPriority, configuredPriority);
619
+ const baseFeeMultiplierPct = useCustomGas && args.chainDetail?.baseFeeMultiplier != null ? Math.max(100, Number(args.chainDetail.baseFeeMultiplier)) : 100;
620
+ const baseComponentGwei = effectiveBaseFeeGwei * baseFeeMultiplierPct / 100;
621
+ const maxFeePerGasGwei = baseComponentGwei + effectivePriorityFeeGwei;
622
+ let maxPriorityFeePerGas = effectivePriorityFeeGwei > 0 ? parseGwei(gweiToDecimalString(effectivePriorityFeeGwei)) : parseGwei("1");
623
+ let maxFeePerGas = parseGwei(gweiToDecimalString(maxFeePerGasGwei));
624
+ if (useCustomGas && gasFeeMultiplier != null && gasFeeMultiplier > 0) {
625
+ maxPriorityFeePerGas = maxPriorityFeePerGas * BigInt(100 + gasFeeMultiplier) / 100n;
626
+ maxFeePerGas = maxFeePerGas * BigInt(100 + gasFeeMultiplier) / 100n;
627
+ }
628
+ ({ maxFeePerGas, maxPriorityFeePerGas } = alignEip1559FeesWithLatestBase(
629
+ maxFeePerGas,
630
+ maxPriorityFeePerGas,
631
+ latestBaseFeeWei
632
+ ));
633
+ const ser = serializeTransaction({
634
+ type: "eip1559",
635
+ to: s.to,
636
+ data: s.data,
637
+ value: s.value,
638
+ gas: gasLimitI,
639
+ maxFeePerGas,
640
+ maxPriorityFeePerGas,
641
+ nonce: currentNonce,
642
+ chainId: 1
643
+ });
644
+ const h = keccak256(ser);
645
+ messageHashes.push(h.startsWith("0x") ? h.slice(2) : h);
646
+ messageRawBatch.push(ser);
647
+ proposalTxParamsBatch.push({
648
+ nonce: currentNonce,
649
+ gasLimit: gasLimitI.toString(),
650
+ txType: "eip1559",
651
+ maxFeePerGas: maxFeePerGas.toString(),
652
+ maxPriorityFeePerGas: maxPriorityFeePerGas.toString()
653
+ });
654
+ if (i === 0) {
655
+ firstTxFeePayload = {
656
+ txNonce: currentNonce,
657
+ txGasLimit: gasLimitI.toString(),
658
+ txMaxFeePerGas: maxFeePerGas.toString(),
659
+ txMaxPriorityFeePerGas: maxPriorityFeePerGas.toString()
660
+ };
661
+ firstDataNo0x = s.data.startsWith("0x") ? s.data.slice(2) : s.data;
662
+ }
663
+ }
664
+ batchMeta.push(args.makeBatchMeta({ gasLimitI }));
665
+ }
666
+ const extraPayload = { batchMeta };
667
+ if (useCustomGas && args.customGasChainDetails && Object.keys(args.customGasChainDetails).length > 0) {
668
+ extraPayload.customGasChainDetails = args.customGasChainDetails;
669
+ }
670
+ const extraJSON = JSON.stringify(extraPayload);
671
+ const firstSigText = batchMeta[0].signatureText;
672
+ const bodyForSign = {
673
+ keyList,
674
+ pubKey: ph,
675
+ msgHash: messageHashes[0],
676
+ msgRaw: firstDataNo0x,
677
+ messageHashes,
678
+ messageRawBatch,
679
+ destinationChainID: "1",
680
+ destinationAddress: steps[0].to,
681
+ extraJSON,
682
+ signatureText: firstSigText,
683
+ purpose: (() => {
684
+ const t = args.purposeText.trim();
685
+ return (t ? `${t}
686
+
687
+ ` : "") + args.purposeSuffix;
688
+ })(),
689
+ ...firstTxFeePayload,
690
+ proposalTxParams: proposalTxParamsBatch
691
+ };
692
+ if (clientId) bodyForSign.clientId = clientId;
693
+ return { bodyForSign, messageToSign: JSON.stringify(bodyForSign) };
694
+ }
695
+ async function buildEvmMultisignBodyEthenaSusdeRedeemToUsde(args) {
696
+ if (args.chainId !== 1) {
697
+ throw new Error("Ethena sUSDe \u2192 USDe redeem is only supported on Ethereum mainnet (chain id 1).");
698
+ }
699
+ const susde = getAddress(args.susdeVault ?? SUSDE_ETHEREUM_MAINNET);
700
+ if (getAddress(SUSDE_ETHEREUM_MAINNET).toLowerCase() !== susde.toLowerCase()) {
701
+ throw new Error("sUSDe vault address mismatch.");
702
+ }
703
+ const executor = getAddress(args.executorAddress);
704
+ const receiver = getAddress(args.receiver);
705
+ const ch = defineChain({
706
+ id: 1,
707
+ name: "Ethereum",
708
+ nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
709
+ rpcUrls: { default: { http: [args.rpcUrl.trim()] } }
710
+ });
711
+ const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl.trim()) });
712
+ const dec = await publicClient.readContract({
713
+ address: susde,
714
+ abi: susdeErc4626Abi,
715
+ functionName: "decimals"
716
+ });
717
+ const sharesWei = parseUnits(args.sharesHuman, Number(dec));
718
+ if (sharesWei === 0n) throw new Error("Amount is zero after converting with share decimals.");
719
+ const redeemData = encodeFunctionData({
720
+ abi: susdeErc4626Abi,
721
+ functionName: "redeem",
722
+ args: [sharesWei, receiver, executor]
723
+ });
724
+ const step = { to: susde, data: redeemData, value: 0n };
725
+ const purposeSuffix = "Ethena: 1-tx \u2014 redeem sUSDe shares to USDe (ERC-4626; no cooldown).";
726
+ return buildEthenaStakedUsdeOneTxMultisignBody({
727
+ keyGen: args.keyGen,
728
+ chainId: args.chainId,
729
+ rpcUrl: args.rpcUrl,
730
+ chainDetail: args.chainDetail,
731
+ useCustomGas: args.useCustomGas,
732
+ customGasChainDetails: args.customGasChainDetails,
733
+ susde,
734
+ executor,
735
+ step,
736
+ purposeText: args.purposeText,
737
+ purposeSuffix,
738
+ gasFallback: ETHENA_SUSDE_REDEEM_FALLBACK,
739
+ makeBatchMeta: ({ gasLimitI }) => ({
740
+ destinationAddress: susde,
741
+ signatureText: JSON.stringify({
742
+ kind: "Ethena",
743
+ name: "StakedUSDe (ERC-4626).redeem",
744
+ function: "redeem(uint256 shares, address receiver, address owner)",
745
+ shares: sharesWei.toString(),
746
+ receiver,
747
+ owner: executor,
748
+ amountHuman: args.sharesHuman
749
+ }),
750
+ evm: { type: "ethena_susde_redeem", version: 1, chainId: "1" },
751
+ ethena: { step: "redeem", vault: susde, gasBuildRedeem: { baseGasUnits: gasLimitI.toString() } }
752
+ })
753
+ });
754
+ }
755
+ async function buildEvmMultisignBodyEthenaSusdeCooldownShares(args) {
756
+ if (args.chainId !== 1) {
757
+ throw new Error("Ethena sUSDe cooldown is only supported on Ethereum mainnet (chain id 1).");
758
+ }
759
+ const susde = getAddress(args.susdeVault ?? SUSDE_ETHEREUM_MAINNET);
760
+ if (getAddress(SUSDE_ETHEREUM_MAINNET).toLowerCase() !== susde.toLowerCase()) {
761
+ throw new Error("sUSDe vault address mismatch.");
762
+ }
763
+ const executor = getAddress(args.executorAddress);
764
+ const ch = defineChain({
765
+ id: 1,
766
+ name: "Ethereum",
767
+ nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
768
+ rpcUrls: { default: { http: [args.rpcUrl.trim()] } }
769
+ });
770
+ const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl.trim()) });
771
+ const dec = await publicClient.readContract({
772
+ address: susde,
773
+ abi: susdeErc4626Abi,
774
+ functionName: "decimals"
775
+ });
776
+ const sharesWei = parseUnits(args.sharesHuman, Number(dec));
777
+ if (sharesWei === 0n) throw new Error("Amount is zero after converting with share decimals.");
778
+ const data = encodeFunctionData({
779
+ abi: stakedUsdeV2UnstakeAbi,
780
+ functionName: "cooldownShares",
781
+ args: [sharesWei, executor]
782
+ });
783
+ const step = { to: susde, data, value: 0n };
784
+ const purposeSuffix = "Ethena: 1-tx \u2014 cooldownShares (sUSDe \u2192 silo, claim USDe in Unlock after cooldown).";
785
+ return buildEthenaStakedUsdeOneTxMultisignBody({
786
+ keyGen: args.keyGen,
787
+ chainId: args.chainId,
788
+ rpcUrl: args.rpcUrl,
789
+ chainDetail: args.chainDetail,
790
+ useCustomGas: args.useCustomGas,
791
+ customGasChainDetails: args.customGasChainDetails,
792
+ susde,
793
+ executor,
794
+ step,
795
+ purposeText: args.purposeText,
796
+ purposeSuffix,
797
+ gasFallback: ETHENA_SUSDE_COOLDOWN_SHARES_FALLBACK,
798
+ makeBatchMeta: ({ gasLimitI }) => ({
799
+ destinationAddress: susde,
800
+ signatureText: JSON.stringify({
801
+ kind: "Ethena",
802
+ name: "StakedUSDeV2.cooldownShares",
803
+ function: "cooldownShares(uint256 shares, address owner)",
804
+ shares: sharesWei.toString(),
805
+ owner: executor,
806
+ amountHuman: args.sharesHuman
807
+ }),
808
+ evm: { type: "ethena_susde_cooldown_shares", version: 1, chainId: "1" },
809
+ ethena: { step: "cooldown_shares", vault: susde, gasBuildCooldown: { baseGasUnits: gasLimitI.toString() } }
810
+ })
811
+ });
812
+ }
813
+ async function buildEvmMultisignBodyEthenaUnstakeClaim(args) {
814
+ if (args.chainId !== 1) {
815
+ throw new Error("Ethena unstake claim is only supported on Ethereum mainnet (chain id 1).");
816
+ }
817
+ const susde = getAddress(args.susdeVault ?? SUSDE_ETHEREUM_MAINNET);
818
+ if (getAddress(SUSDE_ETHEREUM_MAINNET).toLowerCase() !== susde.toLowerCase()) {
819
+ throw new Error("sUSDe vault address mismatch.");
820
+ }
821
+ const executor = getAddress(args.executorAddress);
822
+ const receiver = getAddress(args.receiver);
823
+ const data = encodeFunctionData({
824
+ abi: stakedUsdeV2UnstakeAbi,
825
+ functionName: "unstake",
826
+ args: [receiver]
827
+ });
828
+ const step = { to: susde, data, value: 0n };
829
+ const purposeSuffix = "Ethena: 1-tx \u2014 unstake (claim USDe after cooldown).";
830
+ return buildEthenaStakedUsdeOneTxMultisignBody({
831
+ keyGen: args.keyGen,
832
+ chainId: args.chainId,
833
+ rpcUrl: args.rpcUrl,
834
+ chainDetail: args.chainDetail,
835
+ useCustomGas: args.useCustomGas,
836
+ customGasChainDetails: args.customGasChainDetails,
837
+ susde,
838
+ executor,
839
+ step,
840
+ purposeText: args.purposeText,
841
+ purposeSuffix,
842
+ gasFallback: ETHENA_SUSDE_UNSTAKE_CLAIM_FALLBACK,
843
+ makeBatchMeta: ({ gasLimitI }) => ({
844
+ destinationAddress: susde,
845
+ signatureText: JSON.stringify({
846
+ kind: "Ethena",
847
+ name: "StakedUSDeV2.unstake",
848
+ function: "unstake(address receiver)",
849
+ receiver
850
+ }),
851
+ evm: { type: "ethena_susde_unstake", version: 1, chainId: "1" },
852
+ ethena: { step: "unstake", vault: susde, gasBuildUnstake: { baseGasUnits: gasLimitI.toString() } }
853
+ })
854
+ });
855
+ }
856
+ function parseExtraJsonObject(detail) {
857
+ if (!detail) return null;
858
+ const raw = detail.ExtraJSON ?? detail.extraJSON;
859
+ if (raw == null) return null;
860
+ if (typeof raw === "object" && !Array.isArray(raw)) return raw;
861
+ if (typeof raw === "string" && raw.trim()) {
862
+ try {
863
+ const p = JSON.parse(raw);
864
+ if (p && typeof p === "object" && !Array.isArray(p)) return p;
865
+ } catch {
866
+ return null;
867
+ }
868
+ }
869
+ return null;
870
+ }
871
+ function parseOptionalGasLimitString(raw) {
872
+ if (raw == null) return null;
873
+ const s = String(raw).trim();
874
+ if (!s) return null;
875
+ try {
876
+ if (/^0x[0-9a-fA-F]+$/.test(s)) return BigInt(s);
877
+ return BigInt(s);
878
+ } catch {
879
+ return null;
880
+ }
881
+ }
882
+ function isEthenaSusdeDepositBatchStep(detail, batchIndex) {
883
+ if (batchIndex < 0) return false;
884
+ const ex = parseExtraJsonObject(detail);
885
+ const bm = ex?.batchMeta;
886
+ if (!Array.isArray(bm) || !bm[batchIndex] || typeof bm[batchIndex] !== "object" || Array.isArray(bm[batchIndex])) {
887
+ return false;
888
+ }
889
+ const evm = bm[batchIndex].evm;
890
+ return String(evm?.type ?? "") === "ethena_susde_deposit";
891
+ }
892
+ function resolveEthenaBatchStepGasFromSignRequest(detail, batchIndex) {
893
+ if (!detail || batchIndex < 0) return null;
894
+ const propBatch = detail.proposal_tx_params ?? detail.proposalTxParams ?? detail.ProposalTxParams;
895
+ if (Array.isArray(propBatch) && propBatch.length > batchIndex) {
896
+ const row = propBatch[batchIndex];
897
+ if (row && typeof row === "object" && !Array.isArray(row)) {
898
+ const gl = parseOptionalGasLimitString(row.gasLimit);
899
+ if (gl != null && gl > 0n) return gl;
900
+ }
901
+ }
902
+ const ex = parseExtraJsonObject(detail);
903
+ const bm = ex?.batchMeta;
904
+ if (!Array.isArray(bm) || !bm[batchIndex] || typeof bm[batchIndex] !== "object" || Array.isArray(bm[batchIndex])) {
905
+ return null;
906
+ }
907
+ const ethena = bm[batchIndex].ethena;
908
+ if (!ethena || typeof ethena !== "object") return null;
909
+ for (const key of ["gasBuildDeposit", "gasBuildCooldown", "gasBuildUnstake", "gasBuildRedeem"]) {
910
+ const g = ethena[key];
911
+ if (g && typeof g === "object") {
912
+ const p = parseOptionalGasLimitString(g.baseGasUnits);
913
+ if (p != null && p > 0n) return p;
914
+ }
915
+ }
916
+ return null;
917
+ }
918
+
919
+ // src/protocols/evm/ethena/index.ts
920
+ var ETHENA_PROTOCOL_ID = "ethena";
921
+ var ethenaProtocolModule = {
922
+ id: ETHENA_PROTOCOL_ID,
923
+ chainCategory: "evm",
924
+ isChainSupported(ctx) {
925
+ if (ctx.chainCategory !== "evm") return false;
926
+ const n = typeof ctx.chainId === "number" ? ctx.chainId : Number.parseInt(String(ctx.chainId), 10);
927
+ return isEvmChainInEthenaUsdeList(n);
928
+ },
929
+ isTokenSupported(token) {
930
+ return token.category === "evm" && token.kind === "erc20";
931
+ },
932
+ actions: [
933
+ { id: "ethena.stake-usde", protocolId: ETHENA_PROTOCOL_ID, chainCategory: "evm", description: "Stake USDe \u2192 sUSDe", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: { amountHuman: { type: "string", required: true, description: "USDe amount" } } },
934
+ { id: "ethena.redeem-susde", protocolId: ETHENA_PROTOCOL_ID, chainCategory: "evm", description: "Redeem sUSDe \u2192 USDe (no cooldown)", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
935
+ { id: "ethena.cooldown-shares", protocolId: ETHENA_PROTOCOL_ID, chainCategory: "evm", description: "Start sUSDe cooldown", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
936
+ { id: "ethena.claim-unstake", protocolId: ETHENA_PROTOCOL_ID, chainCategory: "evm", description: "Claim after cooldown", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} }
937
+ ]
938
+ };
939
+ registerProtocolModule(ethenaProtocolModule);
940
+
941
+ export { ETHENA_KEY_ADDRESSES_DOC, ETHENA_LABS_GITHUB, ETHENA_PROTOCOL_ID, ETHENA_SUSDE_DEPOSIT_GET_SIG_GAS_FALLBACK, SUSDE_ETHEREUM_MAINNET, USDE_ETHEREUM_MAINNET, USDE_MOST_L2S, USDE_ZKSYNC_ERA, buildEvmMultisignBodyEthenaSusdeCooldownShares, buildEvmMultisignBodyEthenaSusdeRedeemToUsde, buildEvmMultisignBodyEthenaUnstakeClaim, buildEvmMultisignBodyEthenaUsdeStakeToSusde, ethenaProtocolModule, isEthenaSusdeDepositBatchStep, isEthenaUsdeOnAssetsChain, isEvmChainInEthenaUsdeList, listEthenaUsdeEvmNetworkRows, readEthenaStakePreviewShares, readEthenaUnstakePreviewUsde, resolveEthenaBatchStepGasFromSignRequest, usdeTokenAddressOnEvmChain };
942
+ //# sourceMappingURL=index.js.map
943
+ //# sourceMappingURL=index.js.map