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