@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,1560 @@
|
|
|
1
|
+
import { parseAbi, getAddress, defineChain, createPublicClient, http, parseUnits, encodeFunctionData, zeroAddress, maxUint256, 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
|
+
var EULER_SAME_ASSET_BORROW_PROTOCOL_HEADROOM_BPS = 50n;
|
|
15
|
+
var EULER_SAME_ASSET_BORROW_APPROVAL_BUFFER_BPS = 100n;
|
|
16
|
+
var EULER_SAME_ASSET_BORROW_MAX_ROUNDS = 48;
|
|
17
|
+
var EULER_SAME_ASSET_BORROW_RATIO_STOP_EPS_BPS = 15n;
|
|
18
|
+
function eulerBorrowAndCollateralSameAsset(row) {
|
|
19
|
+
const c = (row.collateralAssetAddressLower ?? "").trim().toLowerCase();
|
|
20
|
+
const b = (row.borrowAssetAddressLower ?? "").trim().toLowerCase();
|
|
21
|
+
if (!c.startsWith("0x") || !b.startsWith("0x")) return false;
|
|
22
|
+
try {
|
|
23
|
+
return getAddress(c).toLowerCase() === getAddress(b).toLowerCase();
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function planSameAssetLeveragedBorrows(args) {
|
|
29
|
+
const { initialCollateralWei: C0wei, oneBorrowUnitUoAWei, borrowDecimals, maxNewBorrowWei } = args;
|
|
30
|
+
const targetRaw = Number(args.targetLtvBps);
|
|
31
|
+
const maxLtvBps = Number(args.maxLtvBps);
|
|
32
|
+
if (C0wei <= 0n || oneBorrowUnitUoAWei <= 0n || maxNewBorrowWei <= 0n) return null;
|
|
33
|
+
if (!Number.isFinite(targetRaw) || !(targetRaw > 0) || targetRaw >= 1e4) return null;
|
|
34
|
+
if (!Number.isFinite(maxLtvBps) || !(maxLtvBps > 0) || maxLtvBps > 1e4) return null;
|
|
35
|
+
const unit = 10n ** BigInt(borrowDecimals);
|
|
36
|
+
const effectiveMax = maxLtvBps > Number(EULER_SAME_ASSET_BORROW_PROTOCOL_HEADROOM_BPS) ? maxLtvBps - Number(EULER_SAME_ASSET_BORROW_PROTOCOL_HEADROOM_BPS) : maxLtvBps;
|
|
37
|
+
let targetBps = Math.min(targetRaw, effectiveMax);
|
|
38
|
+
if (targetBps <= 0) return null;
|
|
39
|
+
const workingCeiling = Math.max(1, effectiveMax - Number(EULER_SAME_ASSET_BORROW_RATIO_STOP_EPS_BPS));
|
|
40
|
+
targetBps = Math.min(targetBps, workingCeiling);
|
|
41
|
+
const uoaFromBorrowWei = (w) => {
|
|
42
|
+
if (w <= 0n) return 0n;
|
|
43
|
+
return w * oneBorrowUnitUoAWei / unit;
|
|
44
|
+
};
|
|
45
|
+
let C = uoaFromBorrowWei(C0wei);
|
|
46
|
+
let D = 0n;
|
|
47
|
+
if (C <= 0n) return null;
|
|
48
|
+
const borrowWeis = [];
|
|
49
|
+
let liquidityLeft = maxNewBorrowWei;
|
|
50
|
+
const targetBn = BigInt(targetBps);
|
|
51
|
+
const stopBn = targetBn - BigInt(Number(EULER_SAME_ASSET_BORROW_RATIO_STOP_EPS_BPS));
|
|
52
|
+
const capBn = BigInt(effectiveMax);
|
|
53
|
+
for (let i = 0; i < EULER_SAME_ASSET_BORROW_MAX_ROUNDS; i++) {
|
|
54
|
+
if (C <= 0n) break;
|
|
55
|
+
const ratioBps = D * 10000n / C;
|
|
56
|
+
if (ratioBps >= stopBn) break;
|
|
57
|
+
const headroomUoa = capBn * C / 10000n - D;
|
|
58
|
+
if (headroomUoa <= 0n) break;
|
|
59
|
+
const den = 10000n - targetBn;
|
|
60
|
+
if (den <= 0n) break;
|
|
61
|
+
let bUoa = (targetBn * C - 10000n * D) / den;
|
|
62
|
+
if (bUoa <= 0n) break;
|
|
63
|
+
if (bUoa > headroomUoa) bUoa = headroomUoa;
|
|
64
|
+
let borrowWei = bUoa * unit / oneBorrowUnitUoAWei;
|
|
65
|
+
if (borrowWei <= 0n) break;
|
|
66
|
+
if (borrowWei > liquidityLeft) borrowWei = liquidityLeft;
|
|
67
|
+
if (borrowWei <= 0n) break;
|
|
68
|
+
const bUoaActual = uoaFromBorrowWei(borrowWei);
|
|
69
|
+
borrowWeis.push(borrowWei);
|
|
70
|
+
D += bUoaActual;
|
|
71
|
+
C += bUoaActual;
|
|
72
|
+
liquidityLeft -= borrowWei;
|
|
73
|
+
}
|
|
74
|
+
if (borrowWeis.length === 0) return null;
|
|
75
|
+
const totalBorrowWei = borrowWeis.reduce((a, w) => a + w, 0n);
|
|
76
|
+
const projectedFinalLtvBps = C > 0n ? D * 10000n / C : 0n;
|
|
77
|
+
return { borrowWeis, totalBorrowWei, projectedFinalLtvBps };
|
|
78
|
+
}
|
|
79
|
+
function eulerSameAssetTotalCollateralPullWei(args) {
|
|
80
|
+
let s = args.initialCollateralWei;
|
|
81
|
+
for (const w of args.loopBorrowWeis) s += w;
|
|
82
|
+
return s;
|
|
83
|
+
}
|
|
84
|
+
function eulerSameAssetApproveAmountWithBuffer(args) {
|
|
85
|
+
const extra = args.totalPullWei * EULER_SAME_ASSET_BORROW_APPROVAL_BUFFER_BPS / 10000n;
|
|
86
|
+
return args.totalPullWei + extra + 1n;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/core/purpose.ts
|
|
90
|
+
function mergePurposeText(purposeText, purposeSuffix) {
|
|
91
|
+
const t = purposeText.trim();
|
|
92
|
+
const suffix = (purposeSuffix ?? "").trim();
|
|
93
|
+
if (!suffix) return t;
|
|
94
|
+
return t ? `${t}
|
|
95
|
+
|
|
96
|
+
${suffix}` : suffix;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// src/core/envelope.ts
|
|
100
|
+
function finalizeMultisign(input) {
|
|
101
|
+
const { keyGen, destinationChainID, legs } = input;
|
|
102
|
+
if (legs.length === 0) {
|
|
103
|
+
throw new Error("finalizeMultisign requires at least one leg");
|
|
104
|
+
}
|
|
105
|
+
const ph = (keyGen.pubkeyhex ?? "").trim();
|
|
106
|
+
if (!ph) throw new Error("keyGen pubKey (pubkeyhex) is required");
|
|
107
|
+
const keyList = keyGen.keylist ?? [];
|
|
108
|
+
const clientId = getClientIdFromKeyGenResult(keyGen);
|
|
109
|
+
const first = legs[0];
|
|
110
|
+
const messageHashes = legs.map((l) => l.msgHash);
|
|
111
|
+
const messageRawBatch = legs.map((l) => l.msgRaw);
|
|
112
|
+
const batchMeta = legs.map((l) => ({
|
|
113
|
+
destinationAddress: l.destinationAddress,
|
|
114
|
+
signatureText: l.signatureText,
|
|
115
|
+
...l.audit
|
|
116
|
+
}));
|
|
117
|
+
const proposalTxParams = legs.map((l) => l.proposalTxParams).filter((p) => p != null && typeof p === "object");
|
|
118
|
+
const extraPayload = {
|
|
119
|
+
batchMeta,
|
|
120
|
+
...input.extraJSON ?? {}
|
|
121
|
+
};
|
|
122
|
+
const extraJSON = JSON.stringify(extraPayload);
|
|
123
|
+
const bodyForSign = {
|
|
124
|
+
keyList,
|
|
125
|
+
pubKey: ph,
|
|
126
|
+
msgHash: messageHashes[0],
|
|
127
|
+
msgRaw: first.msgRaw,
|
|
128
|
+
destinationChainID,
|
|
129
|
+
destinationAddress: input.destinationAddress ?? first.destinationAddress,
|
|
130
|
+
extraJSON,
|
|
131
|
+
signatureText: first.signatureText,
|
|
132
|
+
purpose: mergePurposeText(input.purposeText, input.purposeSuffix),
|
|
133
|
+
...first.feeSnapshot
|
|
134
|
+
};
|
|
135
|
+
if (legs.length > 1) {
|
|
136
|
+
bodyForSign.messageHashes = messageHashes;
|
|
137
|
+
bodyForSign.messageRawBatch = messageRawBatch;
|
|
138
|
+
}
|
|
139
|
+
if (proposalTxParams.length > 0) {
|
|
140
|
+
bodyForSign.proposalTxParams = proposalTxParams;
|
|
141
|
+
}
|
|
142
|
+
const valueWei = first.valueWei;
|
|
143
|
+
if (valueWei != null && valueWei > 0n) {
|
|
144
|
+
bodyForSign.value = valueWei.toString();
|
|
145
|
+
}
|
|
146
|
+
if (clientId) bodyForSign.clientId = clientId;
|
|
147
|
+
return { bodyForSign, messageToSign: JSON.stringify(bodyForSign) };
|
|
148
|
+
}
|
|
149
|
+
function routerSwapGasLimitFromEstimate(estimatedGas, chainGasLimit) {
|
|
150
|
+
if (chainGasLimit != null && Number.isFinite(chainGasLimit) && chainGasLimit > 0) {
|
|
151
|
+
return gasLimitFromEstimateAndChainConfig(estimatedGas, chainGasLimit);
|
|
152
|
+
}
|
|
153
|
+
return (estimatedGas * 12n + 9n) / 10n;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/chains/evm/buildBatch.ts
|
|
157
|
+
async function buildEvmMultisignBatch(args) {
|
|
158
|
+
const { context, steps } = args;
|
|
159
|
+
const {
|
|
160
|
+
chainId,
|
|
161
|
+
rpcUrl,
|
|
162
|
+
executorAddress,
|
|
163
|
+
chainDetail,
|
|
164
|
+
useCustomGas,
|
|
165
|
+
customGasChainDetails,
|
|
166
|
+
keyGen,
|
|
167
|
+
purposeText
|
|
168
|
+
} = context;
|
|
169
|
+
if (steps.length === 0) throw new Error("buildEvmMultisignBatch requires at least one step");
|
|
170
|
+
const ch = defineChain({
|
|
171
|
+
id: chainId,
|
|
172
|
+
name: "Destination",
|
|
173
|
+
nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
|
|
174
|
+
rpcUrls: { default: { http: [rpcUrl] } }
|
|
175
|
+
});
|
|
176
|
+
const publicClient = createPublicClient({ chain: ch, transport: http(rpcUrl) });
|
|
177
|
+
const feeParams = await fetchChainFeeParams(rpcUrl, chainId);
|
|
178
|
+
const legacy = Boolean(chainDetail?.legacy) || !feeParams.isEip1559;
|
|
179
|
+
const latestBaseFeeWei = !legacy ? (await publicClient.getBlock({ blockTag: "latest" })).baseFeePerGas ?? 0n : 0n;
|
|
180
|
+
const gasLimitConfig = useCustomGas && chainDetail?.gasLimit != null ? Number(chainDetail.gasLimit) : void 0;
|
|
181
|
+
const chainGasLimitRouter = chainDetail?.gasLimit != null && Number.isFinite(Number(chainDetail.gasLimit)) && Number(chainDetail.gasLimit) > 0 ? Number(chainDetail.gasLimit) : void 0;
|
|
182
|
+
const gasFeeMultiplier = useCustomGas && chainDetail?.gasMultiplier != null ? Number(chainDetail.gasMultiplier) : void 0;
|
|
183
|
+
const executor = getAddress(executorAddress);
|
|
184
|
+
const baseNonce = await publicClient.getTransactionCount({ address: executor, blockTag: "pending" });
|
|
185
|
+
const legs = [];
|
|
186
|
+
for (let i = 0; i < steps.length; i++) {
|
|
187
|
+
const step = steps[i];
|
|
188
|
+
const currentNonce = baseNonce + i;
|
|
189
|
+
let estimatedGas;
|
|
190
|
+
if (args.estimateGasForStep) {
|
|
191
|
+
estimatedGas = await args.estimateGasForStep({ step, index: i, publicClient, executor });
|
|
192
|
+
} else {
|
|
193
|
+
try {
|
|
194
|
+
estimatedGas = await publicClient.estimateGas({
|
|
195
|
+
to: step.to,
|
|
196
|
+
data: step.data,
|
|
197
|
+
value: step.value,
|
|
198
|
+
account: executor
|
|
199
|
+
});
|
|
200
|
+
} catch {
|
|
201
|
+
estimatedGas = step.fallbackGas ?? 100000n;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
let gasLimitI;
|
|
205
|
+
if (args.resolveGasLimit) {
|
|
206
|
+
gasLimitI = await args.resolveGasLimit({ step, index: i, estimatedGas, publicClient });
|
|
207
|
+
} else if (step.routerSwap) {
|
|
208
|
+
gasLimitI = routerSwapGasLimitFromEstimate(estimatedGas, chainGasLimitRouter);
|
|
209
|
+
} else {
|
|
210
|
+
gasLimitI = useCustomGas ? gasLimitFromEstimateAndChainConfig(estimatedGas, gasLimitConfig) : estimatedGas;
|
|
211
|
+
}
|
|
212
|
+
let proposalTxParams;
|
|
213
|
+
let feeSnapshot;
|
|
214
|
+
let serialized;
|
|
215
|
+
if (legacy) {
|
|
216
|
+
let gasPriceWei = await publicClient.getGasPrice();
|
|
217
|
+
if (useCustomGas && gasFeeMultiplier != null && gasFeeMultiplier > 0) {
|
|
218
|
+
gasPriceWei = gasPriceWei * BigInt(100 + gasFeeMultiplier) / 100n;
|
|
219
|
+
}
|
|
220
|
+
if (useCustomGas && chainDetail?.gasPrice != null && chainDetail.gasPrice > 0) {
|
|
221
|
+
const configured = parseGwei(gweiToDecimalString(Number(chainDetail.gasPrice)));
|
|
222
|
+
if (configured > gasPriceWei) gasPriceWei = configured;
|
|
223
|
+
}
|
|
224
|
+
serialized = serializeTransaction({
|
|
225
|
+
type: "legacy",
|
|
226
|
+
to: step.to,
|
|
227
|
+
data: step.data,
|
|
228
|
+
value: step.value,
|
|
229
|
+
gas: gasLimitI,
|
|
230
|
+
gasPrice: gasPriceWei,
|
|
231
|
+
nonce: currentNonce,
|
|
232
|
+
chainId
|
|
233
|
+
});
|
|
234
|
+
proposalTxParams = {
|
|
235
|
+
nonce: currentNonce,
|
|
236
|
+
gasLimit: gasLimitI.toString(),
|
|
237
|
+
txType: "legacy",
|
|
238
|
+
gasPrice: gasPriceWei.toString()
|
|
239
|
+
};
|
|
240
|
+
feeSnapshot = proposalTxParamsToFeeSnapshot(proposalTxParams);
|
|
241
|
+
} else {
|
|
242
|
+
const fetchedBase = feeParams.baseFeeGwei ?? 0;
|
|
243
|
+
const fetchedPriority = feeParams.priorityFeeGwei ?? 0;
|
|
244
|
+
const configuredBase = useCustomGas && chainDetail?.baseFee != null ? Number(chainDetail.baseFee) : 0;
|
|
245
|
+
const configuredPriority = useCustomGas && chainDetail?.priorityFee != null ? Number(chainDetail.priorityFee) : 0;
|
|
246
|
+
const effectiveBaseFeeGwei = Math.max(fetchedBase, configuredBase);
|
|
247
|
+
const effectivePriorityFeeGwei = Math.max(fetchedPriority, configuredPriority);
|
|
248
|
+
const baseFeeMultiplierPct = useCustomGas && chainDetail?.baseFeeMultiplier != null ? Math.max(100, Number(chainDetail.baseFeeMultiplier)) : 100;
|
|
249
|
+
const baseComponentGwei = effectiveBaseFeeGwei * baseFeeMultiplierPct / 100;
|
|
250
|
+
const maxFeePerGasGwei = baseComponentGwei + effectivePriorityFeeGwei;
|
|
251
|
+
let maxPriorityFeePerGas = effectivePriorityFeeGwei > 0 ? parseGwei(gweiToDecimalString(effectivePriorityFeeGwei)) : parseGwei("1");
|
|
252
|
+
let maxFeePerGas = parseGwei(gweiToDecimalString(maxFeePerGasGwei));
|
|
253
|
+
if (useCustomGas && gasFeeMultiplier != null && gasFeeMultiplier > 0) {
|
|
254
|
+
maxPriorityFeePerGas = maxPriorityFeePerGas * BigInt(100 + gasFeeMultiplier) / 100n;
|
|
255
|
+
maxFeePerGas = maxFeePerGas * BigInt(100 + gasFeeMultiplier) / 100n;
|
|
256
|
+
}
|
|
257
|
+
({ maxFeePerGas, maxPriorityFeePerGas } = alignEip1559FeesWithLatestBase(
|
|
258
|
+
maxFeePerGas,
|
|
259
|
+
maxPriorityFeePerGas,
|
|
260
|
+
latestBaseFeeWei
|
|
261
|
+
));
|
|
262
|
+
serialized = serializeTransaction({
|
|
263
|
+
type: "eip1559",
|
|
264
|
+
to: step.to,
|
|
265
|
+
data: step.data,
|
|
266
|
+
value: step.value,
|
|
267
|
+
gas: gasLimitI,
|
|
268
|
+
maxFeePerGas,
|
|
269
|
+
maxPriorityFeePerGas,
|
|
270
|
+
nonce: currentNonce,
|
|
271
|
+
chainId
|
|
272
|
+
});
|
|
273
|
+
proposalTxParams = {
|
|
274
|
+
nonce: currentNonce,
|
|
275
|
+
gasLimit: gasLimitI.toString(),
|
|
276
|
+
txType: "eip1559",
|
|
277
|
+
maxFeePerGas: maxFeePerGas.toString(),
|
|
278
|
+
maxPriorityFeePerGas: maxPriorityFeePerGas.toString()
|
|
279
|
+
};
|
|
280
|
+
feeSnapshot = i === 0 ? proposalTxParamsToFeeSnapshot(proposalTxParams) : {};
|
|
281
|
+
}
|
|
282
|
+
const h = keccak256(serialized);
|
|
283
|
+
const msgHash = h.startsWith("0x") ? h.slice(2) : h;
|
|
284
|
+
const batchMetaExtra = args.buildBatchMeta({ step, index: i, gasLimit: gasLimitI });
|
|
285
|
+
legs.push({
|
|
286
|
+
msgHash,
|
|
287
|
+
msgRaw: i === 0 && args.firstMsgRawNo0x != null ? args.firstMsgRawNo0x : serialized,
|
|
288
|
+
destinationAddress: step.to,
|
|
289
|
+
signatureText: typeof batchMetaExtra.signatureText === "string" ? batchMetaExtra.signatureText : JSON.stringify(batchMetaExtra.signatureText ?? {}),
|
|
290
|
+
audit: batchMetaExtra,
|
|
291
|
+
feeSnapshot: i === 0 ? feeSnapshot : {},
|
|
292
|
+
proposalTxParams,
|
|
293
|
+
valueWei: i === 0 ? step.value : void 0
|
|
294
|
+
});
|
|
295
|
+
if (i === 0 && args.firstMsgRawNo0x != null) {
|
|
296
|
+
legs[0].msgRaw = args.firstMsgRawNo0x;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const extraJSON = {};
|
|
300
|
+
if (useCustomGas && customGasChainDetails && Object.keys(customGasChainDetails).length > 0) {
|
|
301
|
+
extraJSON.customGasChainDetails = customGasChainDetails;
|
|
302
|
+
}
|
|
303
|
+
const result = finalizeMultisign({
|
|
304
|
+
keyGen,
|
|
305
|
+
purposeText,
|
|
306
|
+
purposeSuffix: args.purposeSuffix,
|
|
307
|
+
destinationChainID: String(chainId),
|
|
308
|
+
destinationAddress: args.destinationAddress ?? steps[0].to,
|
|
309
|
+
legs,
|
|
310
|
+
extraJSON: Object.keys(extraJSON).length > 0 ? extraJSON : void 0
|
|
311
|
+
});
|
|
312
|
+
const pv = args.payableValueWei;
|
|
313
|
+
if (pv != null && pv > 0n) {
|
|
314
|
+
result.bodyForSign.value = pv.toString();
|
|
315
|
+
}
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// src/protocols/evm/euler-v2/vaultWithdrawMultisign.ts
|
|
320
|
+
var EULER_V2_VAULT_WITHDRAW_FALLBACK_GAS = 900000n;
|
|
321
|
+
var erc20DecimalsAbi = parseAbi(["function decimals() view returns (uint8)"]);
|
|
322
|
+
var erc4626AssetAbi = parseAbi(["function asset() view returns (address)"]);
|
|
323
|
+
var erc4626MaxWithdrawAbi = parseAbi(["function maxWithdraw(address owner) view returns (uint256)"]);
|
|
324
|
+
var erc4626WithdrawAbi = parseAbi([
|
|
325
|
+
"function withdraw(uint256 assets, address receiver, address owner) returns (uint256 shares)"
|
|
326
|
+
]);
|
|
327
|
+
async function fetchEulerVaultUnderlyingMeta(args) {
|
|
328
|
+
const ch = defineChain({
|
|
329
|
+
id: args.chainId,
|
|
330
|
+
name: "Destination",
|
|
331
|
+
nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
|
|
332
|
+
rpcUrls: { default: { http: [args.rpcUrl] } }
|
|
333
|
+
});
|
|
334
|
+
const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl) });
|
|
335
|
+
const assetAddrRaw = await publicClient.readContract({
|
|
336
|
+
address: args.evault,
|
|
337
|
+
abi: erc4626AssetAbi,
|
|
338
|
+
functionName: "asset"
|
|
339
|
+
});
|
|
340
|
+
const assetAddr = getAddress(assetAddrRaw);
|
|
341
|
+
const d = await publicClient.readContract({
|
|
342
|
+
address: assetAddr,
|
|
343
|
+
abi: erc20DecimalsAbi,
|
|
344
|
+
functionName: "decimals"
|
|
345
|
+
});
|
|
346
|
+
const n = typeof d === "bigint" ? Number(d) : Number(d);
|
|
347
|
+
const decimals = !Number.isFinite(n) || n < 0 || n > 36 ? 18 : n;
|
|
348
|
+
return { asset: assetAddr, decimals };
|
|
349
|
+
}
|
|
350
|
+
function clampEulerUnderlyingDecimalsForEulerUi(args) {
|
|
351
|
+
const a = args.underlyingAssetLower.trim().toLowerCase();
|
|
352
|
+
let d = args.fetchedDecimals;
|
|
353
|
+
if (args.wrappedGasAliasesLower.has(a)) {
|
|
354
|
+
return Math.max(d, 18);
|
|
355
|
+
}
|
|
356
|
+
if (d >= 18) return d;
|
|
357
|
+
const label = `${args.underlyingSymbol} ${args.marketName}`;
|
|
358
|
+
const ethish = /\b(WETH|wstETH|stETH|rETH|weETH|eETH)\b/i.test(label) || /(^|[^A-Z0-9])ETH([^A-Z0-9]|$)/i.test(label);
|
|
359
|
+
if (ethish) return 18;
|
|
360
|
+
return d;
|
|
361
|
+
}
|
|
362
|
+
async function fetchEulerVaultAssetDecimals(args) {
|
|
363
|
+
const m = await fetchEulerVaultUnderlyingMeta(args);
|
|
364
|
+
return m.decimals;
|
|
365
|
+
}
|
|
366
|
+
async function fetchEulerVaultMaxWithdrawWei(args) {
|
|
367
|
+
const ch = defineChain({
|
|
368
|
+
id: args.chainId,
|
|
369
|
+
name: "Destination",
|
|
370
|
+
nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
|
|
371
|
+
rpcUrls: { default: { http: [args.rpcUrl] } }
|
|
372
|
+
});
|
|
373
|
+
const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl) });
|
|
374
|
+
return publicClient.readContract({
|
|
375
|
+
address: args.evault,
|
|
376
|
+
abi: erc4626MaxWithdrawAbi,
|
|
377
|
+
functionName: "maxWithdraw",
|
|
378
|
+
args: [args.owner]
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
var eulerVaultSharesCashAbi = parseAbi([
|
|
382
|
+
"function balanceOf(address account) view returns (uint256)",
|
|
383
|
+
"function convertToAssets(uint256 shares) view returns (uint256)",
|
|
384
|
+
"function cash() view returns (uint256)"
|
|
385
|
+
]);
|
|
386
|
+
async function eulerDirectWithdrawSimulatesOk(args) {
|
|
387
|
+
const withdrawData = encodeFunctionData({
|
|
388
|
+
abi: erc4626WithdrawAbi,
|
|
389
|
+
functionName: "withdraw",
|
|
390
|
+
args: [args.assetsWei, args.receiver, args.vaultShareOwner]
|
|
391
|
+
});
|
|
392
|
+
try {
|
|
393
|
+
await args.publicClient.call({
|
|
394
|
+
account: args.txSender,
|
|
395
|
+
to: args.evault,
|
|
396
|
+
data: withdrawData,
|
|
397
|
+
gas: 8000000n
|
|
398
|
+
});
|
|
399
|
+
return true;
|
|
400
|
+
} catch {
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
async function eulerLendEarnEffectiveMaxWeiBySimulation(args) {
|
|
405
|
+
const ch = defineChain({
|
|
406
|
+
id: args.chainId,
|
|
407
|
+
name: "EulerLendRedeem",
|
|
408
|
+
nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
|
|
409
|
+
rpcUrls: { default: { http: [args.rpcUrl] } }
|
|
410
|
+
});
|
|
411
|
+
const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl) });
|
|
412
|
+
let shareBal;
|
|
413
|
+
let vaultCash;
|
|
414
|
+
try {
|
|
415
|
+
;
|
|
416
|
+
[shareBal, vaultCash] = await Promise.all([
|
|
417
|
+
publicClient.readContract({
|
|
418
|
+
address: args.evault,
|
|
419
|
+
abi: eulerVaultSharesCashAbi,
|
|
420
|
+
functionName: "balanceOf",
|
|
421
|
+
args: [args.vaultShareOwner]
|
|
422
|
+
}),
|
|
423
|
+
publicClient.readContract({
|
|
424
|
+
address: args.evault,
|
|
425
|
+
abi: eulerVaultSharesCashAbi,
|
|
426
|
+
functionName: "cash"
|
|
427
|
+
})
|
|
428
|
+
]);
|
|
429
|
+
} catch {
|
|
430
|
+
return 0n;
|
|
431
|
+
}
|
|
432
|
+
if (shareBal === 0n) return 0n;
|
|
433
|
+
let assetsOwned;
|
|
434
|
+
try {
|
|
435
|
+
assetsOwned = await publicClient.readContract({
|
|
436
|
+
address: args.evault,
|
|
437
|
+
abi: eulerVaultSharesCashAbi,
|
|
438
|
+
functionName: "convertToAssets",
|
|
439
|
+
args: [shareBal]
|
|
440
|
+
});
|
|
441
|
+
} catch {
|
|
442
|
+
return 0n;
|
|
443
|
+
}
|
|
444
|
+
const hi = vaultCash <= 0n ? 0n : vaultCash <= assetsOwned ? vaultCash : assetsOwned;
|
|
445
|
+
if (hi === 0n) return 0n;
|
|
446
|
+
let lo = 0n;
|
|
447
|
+
let hiProbe = hi;
|
|
448
|
+
let best = 0n;
|
|
449
|
+
while (lo <= hiProbe) {
|
|
450
|
+
const mid = lo + (hiProbe - lo) / 2n;
|
|
451
|
+
const ok = await eulerDirectWithdrawSimulatesOk({
|
|
452
|
+
publicClient,
|
|
453
|
+
evault: args.evault,
|
|
454
|
+
vaultShareOwner: args.vaultShareOwner,
|
|
455
|
+
receiver: args.receiver,
|
|
456
|
+
txSender: args.txSender,
|
|
457
|
+
assetsWei: mid
|
|
458
|
+
});
|
|
459
|
+
if (ok) {
|
|
460
|
+
best = mid;
|
|
461
|
+
lo = mid + 1n;
|
|
462
|
+
} else {
|
|
463
|
+
hiProbe = mid - 1n;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return best;
|
|
467
|
+
}
|
|
468
|
+
async function fetchEulerLendEarnVaultEffectiveMaxWithdrawWei(args) {
|
|
469
|
+
const rpc = args.rpcUrl.trim();
|
|
470
|
+
if (!rpc) return 0n;
|
|
471
|
+
const evault = getAddress(args.evault);
|
|
472
|
+
const vaultShareOwner = getAddress(args.vaultShareOwner);
|
|
473
|
+
const txSender = getAddress(args.txSender);
|
|
474
|
+
const receiver = txSender;
|
|
475
|
+
const std = await fetchEulerVaultMaxWithdrawWei({
|
|
476
|
+
rpcUrl: rpc,
|
|
477
|
+
chainId: args.chainId,
|
|
478
|
+
evault,
|
|
479
|
+
owner: vaultShareOwner
|
|
480
|
+
});
|
|
481
|
+
if (std > 0n) return std;
|
|
482
|
+
return eulerLendEarnEffectiveMaxWeiBySimulation({
|
|
483
|
+
rpcUrl: rpc,
|
|
484
|
+
chainId: args.chainId,
|
|
485
|
+
evault,
|
|
486
|
+
vaultShareOwner,
|
|
487
|
+
receiver,
|
|
488
|
+
txSender
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
async function buildEvmMultisignBodyEulerV2VaultWithdraw(args) {
|
|
492
|
+
const evault = getAddress(args.evault);
|
|
493
|
+
const receiver = getAddress(args.owner);
|
|
494
|
+
const shareOwner = getAddress(args.vaultShareOwner ?? args.owner);
|
|
495
|
+
const executor = getAddress(args.executorAddress);
|
|
496
|
+
const dec = await fetchEulerVaultAssetDecimals({ rpcUrl: args.rpcUrl, chainId: args.chainId, evault });
|
|
497
|
+
const amountWei = parseUnits(args.amountHuman, dec);
|
|
498
|
+
if (amountWei === 0n) throw new Error("Withdraw amount is zero after converting with token decimals.");
|
|
499
|
+
const maxW = await fetchEulerLendEarnVaultEffectiveMaxWithdrawWei({
|
|
500
|
+
rpcUrl: args.rpcUrl,
|
|
501
|
+
chainId: args.chainId,
|
|
502
|
+
evault,
|
|
503
|
+
vaultShareOwner: shareOwner,
|
|
504
|
+
txSender: executor
|
|
505
|
+
});
|
|
506
|
+
if (amountWei > maxW) {
|
|
507
|
+
throw new Error("Withdraw amount exceeds simulated max redeem for this vault (LTV / vault cash). Try a smaller amount.");
|
|
508
|
+
}
|
|
509
|
+
const withdrawData = encodeFunctionData({
|
|
510
|
+
abi: erc4626WithdrawAbi,
|
|
511
|
+
functionName: "withdraw",
|
|
512
|
+
args: [amountWei, receiver, shareOwner]
|
|
513
|
+
});
|
|
514
|
+
const vaultLabel = (args.vaultMarketLabel ?? "").trim() || "Euler vault";
|
|
515
|
+
const purposeSuffix = `Euler v2: 1-tx \u2014 eVault.withdraw (${args.amountHuman} underlying) from isolated vault "${vaultLabel}" (ERC-4626).`;
|
|
516
|
+
const evmSteps = [
|
|
517
|
+
{
|
|
518
|
+
to: evault,
|
|
519
|
+
data: withdrawData,
|
|
520
|
+
value: 0n,
|
|
521
|
+
fallbackGas: EULER_V2_VAULT_WITHDRAW_FALLBACK_GAS
|
|
522
|
+
}
|
|
523
|
+
];
|
|
524
|
+
const firstDataNo0x = withdrawData.startsWith("0x") ? withdrawData.slice(2) : withdrawData;
|
|
525
|
+
return buildEvmMultisignBatch({
|
|
526
|
+
context: {
|
|
527
|
+
chainCategory: "evm",
|
|
528
|
+
keyGen: args.keyGen,
|
|
529
|
+
purposeText: args.purposeText,
|
|
530
|
+
chainId: args.chainId,
|
|
531
|
+
rpcUrl: args.rpcUrl,
|
|
532
|
+
executorAddress: executor,
|
|
533
|
+
chainDetail: args.chainDetail,
|
|
534
|
+
useCustomGas: args.useCustomGas,
|
|
535
|
+
customGasChainDetails: args.customGasChainDetails
|
|
536
|
+
},
|
|
537
|
+
steps: evmSteps,
|
|
538
|
+
purposeSuffix,
|
|
539
|
+
firstMsgRawNo0x: firstDataNo0x,
|
|
540
|
+
destinationAddress: evault,
|
|
541
|
+
buildBatchMeta: ({ gasLimit }) => ({
|
|
542
|
+
signatureText: JSON.stringify({
|
|
543
|
+
kind: "EulerV2",
|
|
544
|
+
name: "EVault.withdraw",
|
|
545
|
+
function: "withdraw(uint256 assets, address receiver, address owner)",
|
|
546
|
+
evault,
|
|
547
|
+
receiver,
|
|
548
|
+
owner: shareOwner,
|
|
549
|
+
vaultMarket: vaultLabel,
|
|
550
|
+
amountHuman: args.amountHuman
|
|
551
|
+
}),
|
|
552
|
+
evm: { type: "euler_v2_vault_withdraw", version: 1, chainId: String(args.chainId) },
|
|
553
|
+
eulerV2: {
|
|
554
|
+
vaultMarket: vaultLabel,
|
|
555
|
+
amountHuman: args.amountHuman,
|
|
556
|
+
evault,
|
|
557
|
+
owner: shareOwner,
|
|
558
|
+
gasBuildWithdraw: { baseGasUnits: gasLimit.toString() }
|
|
559
|
+
}
|
|
560
|
+
})
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// src/protocols/evm/euler-v2/borrowCollateralMaxWithdrawWei.ts
|
|
565
|
+
var erc4626WithdrawAbi2 = parseAbi([
|
|
566
|
+
"function withdraw(uint256 assets, address receiver, address owner) returns (uint256 shares)"
|
|
567
|
+
]);
|
|
568
|
+
var evcBatchAbi = parseAbi([
|
|
569
|
+
"function batch((address targetContract, address onBehalfOfAccount, uint256 value, bytes data)[])"
|
|
570
|
+
]);
|
|
571
|
+
var eulerCollateralVaultReadAbi = parseAbi([
|
|
572
|
+
"function balanceOf(address account) view returns (uint256)",
|
|
573
|
+
"function convertToAssets(uint256 shares) view returns (uint256)",
|
|
574
|
+
"function cash() view returns (uint256)"
|
|
575
|
+
]);
|
|
576
|
+
async function evcWithdrawSimulatesOk(args) {
|
|
577
|
+
const withdrawData = encodeFunctionData({
|
|
578
|
+
abi: erc4626WithdrawAbi2,
|
|
579
|
+
functionName: "withdraw",
|
|
580
|
+
args: [args.assetsWei, args.caller, args.subAccount]
|
|
581
|
+
});
|
|
582
|
+
const batchData = encodeFunctionData({
|
|
583
|
+
abi: evcBatchAbi,
|
|
584
|
+
functionName: "batch",
|
|
585
|
+
args: [
|
|
586
|
+
[
|
|
587
|
+
{
|
|
588
|
+
targetContract: args.collateralVault,
|
|
589
|
+
onBehalfOfAccount: args.subAccount,
|
|
590
|
+
value: 0n,
|
|
591
|
+
data: withdrawData
|
|
592
|
+
}
|
|
593
|
+
]
|
|
594
|
+
]
|
|
595
|
+
});
|
|
596
|
+
try {
|
|
597
|
+
await args.publicClient.call({
|
|
598
|
+
account: args.caller,
|
|
599
|
+
to: args.evc,
|
|
600
|
+
data: batchData,
|
|
601
|
+
gas: 8000000n
|
|
602
|
+
});
|
|
603
|
+
return true;
|
|
604
|
+
} catch {
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
async function collateralMaxWithdrawBySimulation(args) {
|
|
609
|
+
const ch = defineChain({
|
|
610
|
+
id: args.chainId,
|
|
611
|
+
name: "EulerColMaxW",
|
|
612
|
+
nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
|
|
613
|
+
rpcUrls: { default: { http: [args.rpcUrl] } }
|
|
614
|
+
});
|
|
615
|
+
const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl) });
|
|
616
|
+
let shareBal;
|
|
617
|
+
let assetsOwned;
|
|
618
|
+
let vaultCash;
|
|
619
|
+
try {
|
|
620
|
+
;
|
|
621
|
+
[shareBal, vaultCash] = await Promise.all([
|
|
622
|
+
publicClient.readContract({
|
|
623
|
+
address: args.collateralVault,
|
|
624
|
+
abi: eulerCollateralVaultReadAbi,
|
|
625
|
+
functionName: "balanceOf",
|
|
626
|
+
args: [args.subAccount]
|
|
627
|
+
}),
|
|
628
|
+
publicClient.readContract({
|
|
629
|
+
address: args.collateralVault,
|
|
630
|
+
abi: eulerCollateralVaultReadAbi,
|
|
631
|
+
functionName: "cash"
|
|
632
|
+
})
|
|
633
|
+
]);
|
|
634
|
+
} catch {
|
|
635
|
+
return 0n;
|
|
636
|
+
}
|
|
637
|
+
if (shareBal === 0n) return 0n;
|
|
638
|
+
try {
|
|
639
|
+
assetsOwned = await publicClient.readContract({
|
|
640
|
+
address: args.collateralVault,
|
|
641
|
+
abi: eulerCollateralVaultReadAbi,
|
|
642
|
+
functionName: "convertToAssets",
|
|
643
|
+
args: [shareBal]
|
|
644
|
+
});
|
|
645
|
+
} catch {
|
|
646
|
+
return 0n;
|
|
647
|
+
}
|
|
648
|
+
const hi = vaultCash <= 0n ? 0n : vaultCash <= assetsOwned ? vaultCash : assetsOwned;
|
|
649
|
+
if (hi === 0n) return 0n;
|
|
650
|
+
let lo = 0n;
|
|
651
|
+
let hiProbe = hi;
|
|
652
|
+
let best = 0n;
|
|
653
|
+
while (lo <= hiProbe) {
|
|
654
|
+
const mid = lo + (hiProbe - lo) / 2n;
|
|
655
|
+
const ok = await evcWithdrawSimulatesOk({
|
|
656
|
+
publicClient,
|
|
657
|
+
evc: args.evc,
|
|
658
|
+
collateralVault: args.collateralVault,
|
|
659
|
+
subAccount: args.subAccount,
|
|
660
|
+
caller: args.caller,
|
|
661
|
+
assetsWei: mid
|
|
662
|
+
});
|
|
663
|
+
if (ok) {
|
|
664
|
+
best = mid;
|
|
665
|
+
lo = mid + 1n;
|
|
666
|
+
} else {
|
|
667
|
+
hiProbe = mid - 1n;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return best;
|
|
671
|
+
}
|
|
672
|
+
async function fetchEulerBorrowCollateralMaxWithdrawAssetsWei(args) {
|
|
673
|
+
const rpcUrl = args.rpcUrl.trim();
|
|
674
|
+
if (!rpcUrl) return 0n;
|
|
675
|
+
const evc = getAddress(args.evc);
|
|
676
|
+
const collateralVault = getAddress(args.collateralVault);
|
|
677
|
+
const subAccount = getAddress(args.subAccount);
|
|
678
|
+
const caller = getAddress(args.caller);
|
|
679
|
+
const stdMax = await fetchEulerVaultMaxWithdrawWei({
|
|
680
|
+
rpcUrl,
|
|
681
|
+
chainId: args.chainId,
|
|
682
|
+
evault: collateralVault,
|
|
683
|
+
owner: subAccount
|
|
684
|
+
});
|
|
685
|
+
if (stdMax > 0n) return stdMax;
|
|
686
|
+
return collateralMaxWithdrawBySimulation({
|
|
687
|
+
rpcUrl,
|
|
688
|
+
chainId: args.chainId,
|
|
689
|
+
evc,
|
|
690
|
+
collateralVault,
|
|
691
|
+
subAccount,
|
|
692
|
+
caller
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
var EULER_V2_ISOLATED_VAULT_DEPOSIT_FALLBACK_GAS = 950000n;
|
|
696
|
+
var EULER_VAULT_DEPOSIT_ESTIMATE_FALLBACK = EULER_V2_ISOLATED_VAULT_DEPOSIT_FALLBACK_GAS;
|
|
697
|
+
var EULER_ERC20_APPROVE_FALLBACK = 100000n;
|
|
698
|
+
var EULER_WETH_DEPOSIT_FALLBACK = 120000n;
|
|
699
|
+
var wethDepositAbi = parseAbi(["function deposit() payable"]);
|
|
700
|
+
var erc20AllowanceAbi = parseAbi([
|
|
701
|
+
"function allowance(address owner, address spender) view returns (uint256)",
|
|
702
|
+
"function decimals() view returns (uint8)"
|
|
703
|
+
]);
|
|
704
|
+
var erc20ApproveAbi = parseAbi(["function approve(address spender, uint256 amount) returns (bool)"]);
|
|
705
|
+
var erc4626DepositAbi = parseAbi([
|
|
706
|
+
"function deposit(uint256 assets, address receiver) returns (uint256 shares)"
|
|
707
|
+
]);
|
|
708
|
+
async function buildEvmMultisignBodyEulerV2IsolatedLendDepositBatch(args) {
|
|
709
|
+
const asset = getAddress(args.underlying);
|
|
710
|
+
const evault = getAddress(args.evault);
|
|
711
|
+
const weth = getAddress(args.nativeWrapped);
|
|
712
|
+
const executor = getAddress(args.executorAddress);
|
|
713
|
+
const receiver = getAddress(args.receiver);
|
|
714
|
+
if (args.isNativeIn && asset.toLowerCase() !== weth.toLowerCase()) {
|
|
715
|
+
throw new Error("Native lend path: underlying asset must match the chain wrapped native token.");
|
|
716
|
+
}
|
|
717
|
+
const ch = defineChain({
|
|
718
|
+
id: args.chainId,
|
|
719
|
+
name: "Destination",
|
|
720
|
+
nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
|
|
721
|
+
rpcUrls: { default: { http: [args.rpcUrl] } }
|
|
722
|
+
});
|
|
723
|
+
const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl) });
|
|
724
|
+
const dec = await publicClient.readContract({
|
|
725
|
+
address: asset,
|
|
726
|
+
abi: erc20AllowanceAbi,
|
|
727
|
+
functionName: "decimals"
|
|
728
|
+
});
|
|
729
|
+
const amountWei = parseUnits(args.amountHuman, Number(dec));
|
|
730
|
+
if (amountWei === 0n) throw new Error("Amount is zero after converting with token decimals.");
|
|
731
|
+
const steps = [];
|
|
732
|
+
if (args.isNativeIn) {
|
|
733
|
+
const dataDeposit = encodeFunctionData({ abi: wethDepositAbi, functionName: "deposit", args: [] });
|
|
734
|
+
steps.push({ kind: "weth_deposit", to: weth, data: dataDeposit, value: amountWei });
|
|
735
|
+
const wethAllowance = await publicClient.readContract({
|
|
736
|
+
address: weth,
|
|
737
|
+
abi: erc20AllowanceAbi,
|
|
738
|
+
functionName: "allowance",
|
|
739
|
+
args: [executor, evault]
|
|
740
|
+
});
|
|
741
|
+
if (wethAllowance < amountWei) {
|
|
742
|
+
if (wethAllowance > 0n) {
|
|
743
|
+
const dataReset = encodeFunctionData({
|
|
744
|
+
abi: erc20ApproveAbi,
|
|
745
|
+
functionName: "approve",
|
|
746
|
+
args: [evault, 0n]
|
|
747
|
+
});
|
|
748
|
+
steps.push({ kind: "approve", to: weth, data: dataReset, value: 0n });
|
|
749
|
+
}
|
|
750
|
+
const dataApprove = encodeFunctionData({
|
|
751
|
+
abi: erc20ApproveAbi,
|
|
752
|
+
functionName: "approve",
|
|
753
|
+
args: [evault, amountWei]
|
|
754
|
+
});
|
|
755
|
+
steps.push({ kind: "approve", to: weth, data: dataApprove, value: 0n });
|
|
756
|
+
}
|
|
757
|
+
} else {
|
|
758
|
+
const currentAllowance = await publicClient.readContract({
|
|
759
|
+
address: asset,
|
|
760
|
+
abi: erc20AllowanceAbi,
|
|
761
|
+
functionName: "allowance",
|
|
762
|
+
args: [executor, evault]
|
|
763
|
+
});
|
|
764
|
+
if (currentAllowance < amountWei) {
|
|
765
|
+
if (currentAllowance > 0n) {
|
|
766
|
+
const dataReset = encodeFunctionData({
|
|
767
|
+
abi: erc20ApproveAbi,
|
|
768
|
+
functionName: "approve",
|
|
769
|
+
args: [evault, 0n]
|
|
770
|
+
});
|
|
771
|
+
steps.push({ kind: "approve", to: asset, data: dataReset, value: 0n });
|
|
772
|
+
}
|
|
773
|
+
const dataApprove = encodeFunctionData({
|
|
774
|
+
abi: erc20ApproveAbi,
|
|
775
|
+
functionName: "approve",
|
|
776
|
+
args: [evault, amountWei]
|
|
777
|
+
});
|
|
778
|
+
steps.push({ kind: "approve", to: asset, data: dataApprove, value: 0n });
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
const depositData = encodeFunctionData({
|
|
782
|
+
abi: erc4626DepositAbi,
|
|
783
|
+
functionName: "deposit",
|
|
784
|
+
args: [amountWei, receiver]
|
|
785
|
+
});
|
|
786
|
+
steps.push({ kind: "vault_deposit", to: evault, data: depositData, value: 0n });
|
|
787
|
+
const vaultLabel = (args.vaultMarketLabel ?? "").trim() || "Euler vault";
|
|
788
|
+
const evmSteps = steps.map((s) => ({
|
|
789
|
+
to: s.to,
|
|
790
|
+
data: s.data,
|
|
791
|
+
value: s.value,
|
|
792
|
+
fallbackGas: s.kind === "weth_deposit" ? EULER_WETH_DEPOSIT_FALLBACK : s.kind === "approve" ? EULER_ERC20_APPROVE_FALLBACK : EULER_VAULT_DEPOSIT_ESTIMATE_FALLBACK
|
|
793
|
+
}));
|
|
794
|
+
const n = steps.length;
|
|
795
|
+
const hasWrap = args.isNativeIn;
|
|
796
|
+
const purposeSuffix = (() => {
|
|
797
|
+
if (hasWrap) {
|
|
798
|
+
return `Euler v2: ${n}-tx batch \u2014 wrap native to WETH (if needed), approve eVault for the exact amount, then ERC-4626 deposit into isolated vault "${vaultLabel}".`;
|
|
799
|
+
}
|
|
800
|
+
if (n === 1) {
|
|
801
|
+
return `Euler v2: 1-tx \u2014 eVault.deposit (allowance already sufficient) into "${vaultLabel}".`;
|
|
802
|
+
}
|
|
803
|
+
return `Euler v2: ${n}-tx batch \u2014 approve eVault for the exact amount, then ERC-4626 deposit into "${vaultLabel}".`;
|
|
804
|
+
})();
|
|
805
|
+
const firstDataNo0x = evmSteps[0].data.startsWith("0x") ? evmSteps[0].data.slice(2) : evmSteps[0].data;
|
|
806
|
+
return buildEvmMultisignBatch({
|
|
807
|
+
context: {
|
|
808
|
+
chainCategory: "evm",
|
|
809
|
+
keyGen: args.keyGen,
|
|
810
|
+
purposeText: args.purposeText,
|
|
811
|
+
chainId: args.chainId,
|
|
812
|
+
rpcUrl: args.rpcUrl,
|
|
813
|
+
executorAddress: executor,
|
|
814
|
+
chainDetail: args.chainDetail,
|
|
815
|
+
useCustomGas: args.useCustomGas,
|
|
816
|
+
customGasChainDetails: args.customGasChainDetails
|
|
817
|
+
},
|
|
818
|
+
steps: evmSteps,
|
|
819
|
+
purposeSuffix,
|
|
820
|
+
firstMsgRawNo0x: firstDataNo0x,
|
|
821
|
+
destinationAddress: steps[0].to,
|
|
822
|
+
buildBatchMeta: ({ index, gasLimit }) => {
|
|
823
|
+
const s = steps[index];
|
|
824
|
+
if (s.kind === "weth_deposit") {
|
|
825
|
+
return {
|
|
826
|
+
signatureText: JSON.stringify({
|
|
827
|
+
kind: "EulerV2",
|
|
828
|
+
name: "WETH.deposit",
|
|
829
|
+
function: "deposit()",
|
|
830
|
+
valueWei: amountWei.toString(),
|
|
831
|
+
vaultMarket: vaultLabel,
|
|
832
|
+
note: "Wrap native for Euler v2 isolated vault deposit (same batch)."
|
|
833
|
+
}),
|
|
834
|
+
evm: { type: "euler_v2_weth_deposit", version: 1, chainId: String(args.chainId) },
|
|
835
|
+
eulerV2: {
|
|
836
|
+
step: "weth_deposit",
|
|
837
|
+
vaultMarket: vaultLabel,
|
|
838
|
+
amountHuman: args.amountHuman,
|
|
839
|
+
evault,
|
|
840
|
+
underlying: asset
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
if (s.kind === "approve") {
|
|
845
|
+
return {
|
|
846
|
+
signatureText: JSON.stringify({
|
|
847
|
+
kind: "EulerV2",
|
|
848
|
+
name: "ERC20.approve",
|
|
849
|
+
to: "Euler eVault",
|
|
850
|
+
function: "approve(address spender, uint256 amount)",
|
|
851
|
+
evault,
|
|
852
|
+
amountHuman: args.amountHuman,
|
|
853
|
+
note: "Allowance for this deposit amount only (not unlimited)."
|
|
854
|
+
}),
|
|
855
|
+
evm: { type: "euler_v2_erc20_approve", version: 1, chainId: String(args.chainId) },
|
|
856
|
+
eulerV2: { vaultMarket: vaultLabel, amountHuman: args.amountHuman, evault, underlying: asset }
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
return {
|
|
860
|
+
signatureText: JSON.stringify({
|
|
861
|
+
kind: "EulerV2",
|
|
862
|
+
name: "EVault.deposit",
|
|
863
|
+
function: "deposit(uint256 assets, address receiver)",
|
|
864
|
+
evault,
|
|
865
|
+
underlying: asset,
|
|
866
|
+
receiver,
|
|
867
|
+
vaultMarket: vaultLabel,
|
|
868
|
+
amountHuman: args.amountHuman
|
|
869
|
+
}),
|
|
870
|
+
evm: { type: "euler_v2_vault_deposit", version: 1, chainId: String(args.chainId) },
|
|
871
|
+
eulerV2: {
|
|
872
|
+
vaultMarket: vaultLabel,
|
|
873
|
+
amountHuman: args.amountHuman,
|
|
874
|
+
evault,
|
|
875
|
+
underlying: asset,
|
|
876
|
+
receiver,
|
|
877
|
+
gasBuildDeposit: { baseGasUnits: gasLimit.toString() }
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
var EULER_BORROW_BATCH_FALLBACK_GAS = 2500000n;
|
|
884
|
+
var EULER_BORROW_BATCH_FALLBACK_GAS_PER_ROUND = 350000n;
|
|
885
|
+
var EULER_ERC20_APPROVE_FALLBACK2 = 100000n;
|
|
886
|
+
var EULER_WETH_DEPOSIT_FALLBACK2 = 120000n;
|
|
887
|
+
var wethDepositAbi2 = parseAbi(["function deposit() payable"]);
|
|
888
|
+
var erc20AllowanceAbi2 = parseAbi([
|
|
889
|
+
"function allowance(address owner, address spender) view returns (uint256)",
|
|
890
|
+
"function decimals() view returns (uint8)"
|
|
891
|
+
]);
|
|
892
|
+
var erc20ApproveAbi2 = parseAbi(["function approve(address spender, uint256 amount) returns (bool)"]);
|
|
893
|
+
var erc4626DepositAbi2 = parseAbi([
|
|
894
|
+
"function deposit(uint256 assets, address receiver) returns (uint256 shares)"
|
|
895
|
+
]);
|
|
896
|
+
var evaultBorrowAbi = parseAbi(["function borrow(uint256 amount, address receiver) returns (uint256)"]);
|
|
897
|
+
var evcAbi = parseAbi([
|
|
898
|
+
"function batch((address targetContract, address onBehalfOfAccount, uint256 value, bytes data)[])"
|
|
899
|
+
]);
|
|
900
|
+
async function buildEvmMultisignBodyEulerV2IsolatedBorrowBatch(args) {
|
|
901
|
+
const evc = getAddress(args.evc);
|
|
902
|
+
const borrowVault = getAddress(args.borrowVault);
|
|
903
|
+
const collateralVault = getAddress(args.collateralVault);
|
|
904
|
+
const collateralAsset = getAddress(args.collateralUnderlying);
|
|
905
|
+
const borrowAsset = getAddress(args.borrowUnderlying);
|
|
906
|
+
const weth = getAddress(args.nativeWrapped);
|
|
907
|
+
const executor = getAddress(args.executorAddress);
|
|
908
|
+
const receiver = getAddress(args.receiver);
|
|
909
|
+
if (args.isNativeCollateralIn && collateralAsset.toLowerCase() !== weth.toLowerCase()) {
|
|
910
|
+
throw new Error("Native collateral path: collateral underlying must be the chain wrapped native token.");
|
|
911
|
+
}
|
|
912
|
+
const ch = defineChain({
|
|
913
|
+
id: args.chainId,
|
|
914
|
+
name: "Destination",
|
|
915
|
+
nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
|
|
916
|
+
rpcUrls: { default: { http: [args.rpcUrl] } }
|
|
917
|
+
});
|
|
918
|
+
const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl) });
|
|
919
|
+
const cDecRaw = await publicClient.readContract({
|
|
920
|
+
address: collateralAsset,
|
|
921
|
+
abi: erc20AllowanceAbi2,
|
|
922
|
+
functionName: "decimals"
|
|
923
|
+
});
|
|
924
|
+
const collateralDecimals = Number(cDecRaw);
|
|
925
|
+
const collateralWei = parseUnits(args.collateralAmountHuman, collateralDecimals);
|
|
926
|
+
if (collateralWei === 0n) throw new Error("Collateral amount is zero after converting with token decimals.");
|
|
927
|
+
await publicClient.readContract({
|
|
928
|
+
address: borrowAsset,
|
|
929
|
+
abi: erc20AllowanceAbi2,
|
|
930
|
+
functionName: "decimals"
|
|
931
|
+
});
|
|
932
|
+
const loops = [...args.loopBorrowWeis];
|
|
933
|
+
if (loops.length === 0) throw new Error("At least one borrow round is required.");
|
|
934
|
+
for (const w of loops) {
|
|
935
|
+
if (w <= 0n) throw new Error("Each borrow round must be positive.");
|
|
936
|
+
}
|
|
937
|
+
if (!args.redepositBorrowedToCollateral && loops.length !== 1) {
|
|
938
|
+
throw new Error("Multiple borrow rounds require redepositBorrowedToCollateral (same borrow and collateral asset).");
|
|
939
|
+
}
|
|
940
|
+
const borrowWeiTotal = loops.reduce((a, w) => a + w, 0n);
|
|
941
|
+
const totalPullWei = eulerSameAssetTotalCollateralPullWei({
|
|
942
|
+
initialCollateralWei: collateralWei,
|
|
943
|
+
loopBorrowWeis: args.redepositBorrowedToCollateral ? loops : []
|
|
944
|
+
});
|
|
945
|
+
const approveTargetWei = eulerSameAssetApproveAmountWithBuffer({ totalPullWei });
|
|
946
|
+
const steps = [];
|
|
947
|
+
if (args.isNativeCollateralIn) {
|
|
948
|
+
const dataDeposit = encodeFunctionData({ abi: wethDepositAbi2, functionName: "deposit", args: [] });
|
|
949
|
+
steps.push({ kind: "weth_deposit", to: weth, data: dataDeposit, value: collateralWei });
|
|
950
|
+
const wethAllowance = await publicClient.readContract({
|
|
951
|
+
address: weth,
|
|
952
|
+
abi: erc20AllowanceAbi2,
|
|
953
|
+
functionName: "allowance",
|
|
954
|
+
args: [executor, collateralVault]
|
|
955
|
+
});
|
|
956
|
+
if (wethAllowance < approveTargetWei) {
|
|
957
|
+
if (wethAllowance > 0n) {
|
|
958
|
+
const dataReset = encodeFunctionData({
|
|
959
|
+
abi: erc20ApproveAbi2,
|
|
960
|
+
functionName: "approve",
|
|
961
|
+
args: [collateralVault, 0n]
|
|
962
|
+
});
|
|
963
|
+
steps.push({ kind: "approve", to: weth, data: dataReset, value: 0n });
|
|
964
|
+
}
|
|
965
|
+
const dataApprove = encodeFunctionData({
|
|
966
|
+
abi: erc20ApproveAbi2,
|
|
967
|
+
functionName: "approve",
|
|
968
|
+
args: [collateralVault, approveTargetWei]
|
|
969
|
+
});
|
|
970
|
+
steps.push({ kind: "approve", to: weth, data: dataApprove, value: 0n });
|
|
971
|
+
}
|
|
972
|
+
} else {
|
|
973
|
+
const currentAllowance = await publicClient.readContract({
|
|
974
|
+
address: collateralAsset,
|
|
975
|
+
abi: erc20AllowanceAbi2,
|
|
976
|
+
functionName: "allowance",
|
|
977
|
+
args: [executor, collateralVault]
|
|
978
|
+
});
|
|
979
|
+
if (currentAllowance < approveTargetWei) {
|
|
980
|
+
if (currentAllowance > 0n) {
|
|
981
|
+
const dataReset = encodeFunctionData({
|
|
982
|
+
abi: erc20ApproveAbi2,
|
|
983
|
+
functionName: "approve",
|
|
984
|
+
args: [collateralVault, 0n]
|
|
985
|
+
});
|
|
986
|
+
steps.push({ kind: "approve", to: collateralAsset, data: dataReset, value: 0n });
|
|
987
|
+
}
|
|
988
|
+
const dataApprove = encodeFunctionData({
|
|
989
|
+
abi: erc20ApproveAbi2,
|
|
990
|
+
functionName: "approve",
|
|
991
|
+
args: [collateralVault, approveTargetWei]
|
|
992
|
+
});
|
|
993
|
+
steps.push({ kind: "approve", to: collateralAsset, data: dataApprove, value: 0n });
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
const depositData = encodeFunctionData({
|
|
997
|
+
abi: erc4626DepositAbi2,
|
|
998
|
+
functionName: "deposit",
|
|
999
|
+
args: [collateralWei, receiver]
|
|
1000
|
+
});
|
|
1001
|
+
const enableCollData = encodeFunctionData({
|
|
1002
|
+
abi: parseAbi(["function enableCollateral(address account, address vault)"]),
|
|
1003
|
+
functionName: "enableCollateral",
|
|
1004
|
+
args: [receiver, collateralVault]
|
|
1005
|
+
});
|
|
1006
|
+
const enableCtrlData = encodeFunctionData({
|
|
1007
|
+
abi: parseAbi(["function enableController(address account, address vault)"]),
|
|
1008
|
+
functionName: "enableController",
|
|
1009
|
+
args: [receiver, borrowVault]
|
|
1010
|
+
});
|
|
1011
|
+
const batchItems = [
|
|
1012
|
+
{ targetContract: collateralVault, onBehalfOfAccount: receiver, value: 0n, data: depositData },
|
|
1013
|
+
{ targetContract: evc, onBehalfOfAccount: zeroAddress, value: 0n, data: enableCollData },
|
|
1014
|
+
{ targetContract: evc, onBehalfOfAccount: zeroAddress, value: 0n, data: enableCtrlData }
|
|
1015
|
+
];
|
|
1016
|
+
for (const bw of loops) {
|
|
1017
|
+
const borrowData = encodeFunctionData({
|
|
1018
|
+
abi: evaultBorrowAbi,
|
|
1019
|
+
functionName: "borrow",
|
|
1020
|
+
args: [bw, receiver]
|
|
1021
|
+
});
|
|
1022
|
+
batchItems.push({
|
|
1023
|
+
targetContract: borrowVault,
|
|
1024
|
+
onBehalfOfAccount: receiver,
|
|
1025
|
+
value: 0n,
|
|
1026
|
+
data: borrowData
|
|
1027
|
+
});
|
|
1028
|
+
if (args.redepositBorrowedToCollateral) {
|
|
1029
|
+
const redepositData = encodeFunctionData({
|
|
1030
|
+
abi: erc4626DepositAbi2,
|
|
1031
|
+
functionName: "deposit",
|
|
1032
|
+
args: [bw, receiver]
|
|
1033
|
+
});
|
|
1034
|
+
batchItems.push({
|
|
1035
|
+
targetContract: collateralVault,
|
|
1036
|
+
onBehalfOfAccount: receiver,
|
|
1037
|
+
value: 0n,
|
|
1038
|
+
data: redepositData
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
const batchData = encodeFunctionData({
|
|
1043
|
+
abi: evcAbi,
|
|
1044
|
+
functionName: "batch",
|
|
1045
|
+
args: [batchItems]
|
|
1046
|
+
});
|
|
1047
|
+
steps.push({ kind: "evc_batch", to: evc, data: batchData, value: 0n });
|
|
1048
|
+
const borrowRoundsExtraGas = EULER_BORROW_BATCH_FALLBACK_GAS_PER_ROUND * BigInt(Math.max(0, loops.length - 1 + (args.redepositBorrowedToCollateral ? loops.length : 0)));
|
|
1049
|
+
const vaultLabel = (args.vaultMarketLabel ?? "").trim() || "Euler vault";
|
|
1050
|
+
const evmSteps = steps.map((s) => ({
|
|
1051
|
+
to: s.to,
|
|
1052
|
+
data: s.data,
|
|
1053
|
+
value: s.value,
|
|
1054
|
+
fallbackGas: s.kind === "weth_deposit" ? EULER_WETH_DEPOSIT_FALLBACK2 : s.kind === "approve" ? EULER_ERC20_APPROVE_FALLBACK2 : EULER_BORROW_BATCH_FALLBACK_GAS + borrowRoundsExtraGas
|
|
1055
|
+
}));
|
|
1056
|
+
const n = steps.length;
|
|
1057
|
+
const hasWrap = args.isNativeCollateralIn;
|
|
1058
|
+
const purposeSuffix = (() => {
|
|
1059
|
+
const tail = args.redepositBorrowedToCollateral ? `deposit collateral, enableCollateral, enableController, then ${loops.length}\xD7 borrow+redeposit on "${vaultLabel}" (same-asset target LTV loop).` : `deposit collateral, enableCollateral, enableController, borrow from "${vaultLabel}".`;
|
|
1060
|
+
if (hasWrap) {
|
|
1061
|
+
return `Euler v2: ${n}-tx batch \u2014 wrap native collateral (if needed), approve collateral eVault (buffered for all deposits), then EVC batch: ${tail}`;
|
|
1062
|
+
}
|
|
1063
|
+
return `Euler v2: ${n}-tx batch \u2014 approve collateral (if needed) with buffer for all deposits, then EVC batch: ${tail}`;
|
|
1064
|
+
})();
|
|
1065
|
+
const firstDataNo0x = evmSteps[0].data.startsWith("0x") ? evmSteps[0].data.slice(2) : evmSteps[0].data;
|
|
1066
|
+
return buildEvmMultisignBatch({
|
|
1067
|
+
context: {
|
|
1068
|
+
chainCategory: "evm",
|
|
1069
|
+
keyGen: args.keyGen,
|
|
1070
|
+
purposeText: args.purposeText,
|
|
1071
|
+
chainId: args.chainId,
|
|
1072
|
+
rpcUrl: args.rpcUrl,
|
|
1073
|
+
executorAddress: executor,
|
|
1074
|
+
chainDetail: args.chainDetail,
|
|
1075
|
+
useCustomGas: args.useCustomGas,
|
|
1076
|
+
customGasChainDetails: args.customGasChainDetails
|
|
1077
|
+
},
|
|
1078
|
+
steps: evmSteps,
|
|
1079
|
+
purposeSuffix,
|
|
1080
|
+
firstMsgRawNo0x: firstDataNo0x,
|
|
1081
|
+
destinationAddress: steps[0].to,
|
|
1082
|
+
buildBatchMeta: ({ index }) => {
|
|
1083
|
+
const s = steps[index];
|
|
1084
|
+
if (s.kind === "weth_deposit") {
|
|
1085
|
+
return {
|
|
1086
|
+
signatureText: JSON.stringify({
|
|
1087
|
+
kind: "EulerV2",
|
|
1088
|
+
name: "WETH.deposit",
|
|
1089
|
+
function: "deposit()",
|
|
1090
|
+
valueWei: collateralWei.toString(),
|
|
1091
|
+
vaultMarket: vaultLabel,
|
|
1092
|
+
note: "Wrap native for Euler v2 isolated borrow collateral (same batch as borrow flow)."
|
|
1093
|
+
}),
|
|
1094
|
+
evm: { type: "euler_v2_weth_deposit", version: 1, chainId: String(args.chainId) },
|
|
1095
|
+
eulerV2: { step: "weth_deposit", vaultMarket: vaultLabel, flow: "borrow" }
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
if (s.kind === "approve") {
|
|
1099
|
+
return {
|
|
1100
|
+
signatureText: JSON.stringify({
|
|
1101
|
+
kind: "EulerV2",
|
|
1102
|
+
name: "ERC20.approve",
|
|
1103
|
+
to: "Euler collateral eVault",
|
|
1104
|
+
function: "approve(address spender, uint256 amount)",
|
|
1105
|
+
collateralVault,
|
|
1106
|
+
amountHuman: args.collateralAmountHuman,
|
|
1107
|
+
note: "Allowance for initial and follow-on collateral deposits (buffered).",
|
|
1108
|
+
approveTotalWei: approveTargetWei.toString()
|
|
1109
|
+
}),
|
|
1110
|
+
evm: { type: "euler_v2_erc20_approve", version: 1, chainId: String(args.chainId) },
|
|
1111
|
+
eulerV2: { vaultMarket: vaultLabel, flow: "borrow_collateral_approve" }
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
const borrowNote = args.redepositBorrowedToCollateral ? `Same-asset leverage: ${loops.length} borrow\u2192deposit round(s); total borrow wei ${borrowWeiTotal.toString()}.` : "Deposit collateral, enableCollateral, enableController, borrow in one EVC batch.";
|
|
1115
|
+
return {
|
|
1116
|
+
signatureText: JSON.stringify({
|
|
1117
|
+
kind: "EulerV2",
|
|
1118
|
+
name: "EVC.batch",
|
|
1119
|
+
function: "batch(BatchItem[])",
|
|
1120
|
+
evc,
|
|
1121
|
+
borrowVault,
|
|
1122
|
+
collateralVault,
|
|
1123
|
+
collateralAmountHuman: args.collateralAmountHuman,
|
|
1124
|
+
borrowAmountHuman: args.borrowAmountHuman,
|
|
1125
|
+
vaultMarket: vaultLabel,
|
|
1126
|
+
loopBorrowRounds: loops.length,
|
|
1127
|
+
redepositBorrowedToCollateral: args.redepositBorrowedToCollateral,
|
|
1128
|
+
borrowWeiTotal: borrowWeiTotal.toString(),
|
|
1129
|
+
note: borrowNote
|
|
1130
|
+
}),
|
|
1131
|
+
evm: { type: "euler_v2_evc_borrow_batch", version: 1, chainId: String(args.chainId) },
|
|
1132
|
+
eulerV2: {
|
|
1133
|
+
flow: "borrow_batch",
|
|
1134
|
+
vaultMarket: vaultLabel,
|
|
1135
|
+
borrowVault,
|
|
1136
|
+
collateralVault,
|
|
1137
|
+
collateralAmountHuman: args.collateralAmountHuman,
|
|
1138
|
+
borrowAmountHuman: args.borrowAmountHuman,
|
|
1139
|
+
loopBorrowRounds: loops.length,
|
|
1140
|
+
redepositBorrowedToCollateral: args.redepositBorrowedToCollateral,
|
|
1141
|
+
borrowWeiTotal: borrowWeiTotal.toString()
|
|
1142
|
+
}
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
var EULER_REPAY_BATCH_FALLBACK = 1200000n;
|
|
1148
|
+
var EULER_ERC20_APPROVE_FALLBACK3 = 100000n;
|
|
1149
|
+
var erc20AllowanceAbi3 = parseAbi([
|
|
1150
|
+
"function allowance(address owner, address spender) view returns (uint256)",
|
|
1151
|
+
"function decimals() view returns (uint8)"
|
|
1152
|
+
]);
|
|
1153
|
+
var erc20ApproveAbi3 = parseAbi(["function approve(address spender, uint256 amount) returns (bool)"]);
|
|
1154
|
+
var evaultRepayAbi = parseAbi(["function repay(uint256 amount, address receiver) returns (uint256)"]);
|
|
1155
|
+
var evcAbi2 = parseAbi([
|
|
1156
|
+
"function batch((address targetContract, address onBehalfOfAccount, uint256 value, bytes data)[])"
|
|
1157
|
+
]);
|
|
1158
|
+
async function buildEvmMultisignBodyEulerV2BorrowRepayBatch(args) {
|
|
1159
|
+
const evc = getAddress(args.evc);
|
|
1160
|
+
const borrowVault = getAddress(args.borrowVault);
|
|
1161
|
+
const borrowAsset = getAddress(args.borrowUnderlying);
|
|
1162
|
+
const subAccount = getAddress(args.subAccount);
|
|
1163
|
+
const executor = getAddress(args.executorAddress);
|
|
1164
|
+
const ch = defineChain({
|
|
1165
|
+
id: args.chainId,
|
|
1166
|
+
name: "EulerRepay",
|
|
1167
|
+
nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
|
|
1168
|
+
rpcUrls: { default: { http: [args.rpcUrl] } }
|
|
1169
|
+
});
|
|
1170
|
+
const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl) });
|
|
1171
|
+
const bDecRaw = await publicClient.readContract({
|
|
1172
|
+
address: borrowAsset,
|
|
1173
|
+
abi: erc20AllowanceAbi3,
|
|
1174
|
+
functionName: "decimals"
|
|
1175
|
+
});
|
|
1176
|
+
const borrowDecimals = Number(bDecRaw);
|
|
1177
|
+
const debtAbi = parseAbi(["function debtOf(address account) view returns (uint256)"]);
|
|
1178
|
+
const owed = await publicClient.readContract({
|
|
1179
|
+
address: borrowVault,
|
|
1180
|
+
abi: debtAbi,
|
|
1181
|
+
functionName: "debtOf",
|
|
1182
|
+
args: [subAccount]
|
|
1183
|
+
});
|
|
1184
|
+
let repayWei;
|
|
1185
|
+
if (args.repayAll) {
|
|
1186
|
+
repayWei = maxUint256;
|
|
1187
|
+
} else {
|
|
1188
|
+
repayWei = parseUnits(args.amountHuman, borrowDecimals);
|
|
1189
|
+
if (repayWei === 0n) throw new Error("Repay amount is zero after converting with token decimals.");
|
|
1190
|
+
if (repayWei > owed) {
|
|
1191
|
+
repayWei = owed;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
const steps = [];
|
|
1195
|
+
const allowanceTarget = repayWei === maxUint256 ? owed : repayWei;
|
|
1196
|
+
const currentAllowance = await publicClient.readContract({
|
|
1197
|
+
address: borrowAsset,
|
|
1198
|
+
abi: erc20AllowanceAbi3,
|
|
1199
|
+
functionName: "allowance",
|
|
1200
|
+
args: [executor, borrowVault]
|
|
1201
|
+
});
|
|
1202
|
+
if (currentAllowance < allowanceTarget) {
|
|
1203
|
+
if (currentAllowance > 0n) {
|
|
1204
|
+
const dataReset = encodeFunctionData({
|
|
1205
|
+
abi: erc20ApproveAbi3,
|
|
1206
|
+
functionName: "approve",
|
|
1207
|
+
args: [borrowVault, 0n]
|
|
1208
|
+
});
|
|
1209
|
+
steps.push({ kind: "approve", to: borrowAsset, data: dataReset, value: 0n });
|
|
1210
|
+
}
|
|
1211
|
+
const approveAmt = repayWei === maxUint256 ? maxUint256 : repayWei;
|
|
1212
|
+
const dataApprove = encodeFunctionData({
|
|
1213
|
+
abi: erc20ApproveAbi3,
|
|
1214
|
+
functionName: "approve",
|
|
1215
|
+
args: [borrowVault, approveAmt]
|
|
1216
|
+
});
|
|
1217
|
+
steps.push({ kind: "approve", to: borrowAsset, data: dataApprove, value: 0n });
|
|
1218
|
+
}
|
|
1219
|
+
const repayData = encodeFunctionData({
|
|
1220
|
+
abi: evaultRepayAbi,
|
|
1221
|
+
functionName: "repay",
|
|
1222
|
+
args: [repayWei, subAccount]
|
|
1223
|
+
});
|
|
1224
|
+
const batchItems = [
|
|
1225
|
+
{ targetContract: borrowVault, onBehalfOfAccount: subAccount, value: 0n, data: repayData }
|
|
1226
|
+
];
|
|
1227
|
+
const batchData = encodeFunctionData({
|
|
1228
|
+
abi: evcAbi2,
|
|
1229
|
+
functionName: "batch",
|
|
1230
|
+
args: [batchItems]
|
|
1231
|
+
});
|
|
1232
|
+
steps.push({ kind: "evc_batch", to: evc, data: batchData, value: 0n });
|
|
1233
|
+
const vaultLabel = (args.vaultMarketLabel ?? "").trim() || "Euler vault";
|
|
1234
|
+
const evmSteps = steps.map((s) => ({
|
|
1235
|
+
to: s.to,
|
|
1236
|
+
data: s.data,
|
|
1237
|
+
value: s.value,
|
|
1238
|
+
fallbackGas: s.kind === "approve" ? EULER_ERC20_APPROVE_FALLBACK3 : EULER_REPAY_BATCH_FALLBACK
|
|
1239
|
+
}));
|
|
1240
|
+
const n = steps.length;
|
|
1241
|
+
const purposeSuffix = `Euler v2: ${n}-tx batch \u2014 repay borrow on "${vaultLabel}" (approve if needed, then EVC repay).`;
|
|
1242
|
+
const firstDataNo0x = evmSteps[0].data.startsWith("0x") ? evmSteps[0].data.slice(2) : evmSteps[0].data;
|
|
1243
|
+
return buildEvmMultisignBatch({
|
|
1244
|
+
context: {
|
|
1245
|
+
chainCategory: "evm",
|
|
1246
|
+
keyGen: args.keyGen,
|
|
1247
|
+
purposeText: args.purposeText,
|
|
1248
|
+
chainId: args.chainId,
|
|
1249
|
+
rpcUrl: args.rpcUrl,
|
|
1250
|
+
executorAddress: executor,
|
|
1251
|
+
chainDetail: args.chainDetail,
|
|
1252
|
+
useCustomGas: args.useCustomGas,
|
|
1253
|
+
customGasChainDetails: args.customGasChainDetails
|
|
1254
|
+
},
|
|
1255
|
+
steps: evmSteps,
|
|
1256
|
+
purposeSuffix,
|
|
1257
|
+
firstMsgRawNo0x: firstDataNo0x,
|
|
1258
|
+
destinationAddress: steps[0].to,
|
|
1259
|
+
buildBatchMeta: ({ index }) => {
|
|
1260
|
+
const s = steps[index];
|
|
1261
|
+
if (s.kind === "approve") {
|
|
1262
|
+
return {
|
|
1263
|
+
signatureText: JSON.stringify({
|
|
1264
|
+
kind: "EulerV2",
|
|
1265
|
+
name: "ERC20.approve",
|
|
1266
|
+
function: "approve(address spender, uint256 amount)",
|
|
1267
|
+
spender: borrowVault,
|
|
1268
|
+
borrowUnderlying: borrowAsset,
|
|
1269
|
+
note: "Allow Euler liability vault to pull assets for repay."
|
|
1270
|
+
}),
|
|
1271
|
+
evm: { type: "euler_v2_erc20_approve", version: 1, chainId: String(args.chainId) },
|
|
1272
|
+
eulerV2: { vaultMarket: vaultLabel, flow: "borrow_repay_approve" }
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1275
|
+
return {
|
|
1276
|
+
signatureText: JSON.stringify({
|
|
1277
|
+
kind: "EulerV2",
|
|
1278
|
+
name: "EVC.batch",
|
|
1279
|
+
function: "batch(BatchItem[]) \u2014 repay",
|
|
1280
|
+
evc,
|
|
1281
|
+
borrowVault,
|
|
1282
|
+
subAccount,
|
|
1283
|
+
amountHuman: args.repayAll ? "max" : args.amountHuman,
|
|
1284
|
+
vaultMarket: vaultLabel,
|
|
1285
|
+
note: "Repay borrow on Euler v2 liability vault via EVC."
|
|
1286
|
+
}),
|
|
1287
|
+
evm: { type: "euler_v2_borrow_repay_batch", version: 1, chainId: String(args.chainId) },
|
|
1288
|
+
eulerV2: {
|
|
1289
|
+
flow: "borrow_repay",
|
|
1290
|
+
vaultMarket: vaultLabel,
|
|
1291
|
+
borrowVault,
|
|
1292
|
+
subAccount,
|
|
1293
|
+
repayAll: args.repayAll,
|
|
1294
|
+
amountHuman: args.amountHuman
|
|
1295
|
+
}
|
|
1296
|
+
};
|
|
1297
|
+
}
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
var EULER_COLLATERAL_DEPOSIT_BATCH_FALLBACK = 1600000n;
|
|
1301
|
+
var EULER_ERC20_APPROVE_FALLBACK4 = 100000n;
|
|
1302
|
+
var erc20AllowanceAbi4 = parseAbi([
|
|
1303
|
+
"function allowance(address owner, address spender) view returns (uint256)"
|
|
1304
|
+
]);
|
|
1305
|
+
var erc20ApproveAbi4 = parseAbi(["function approve(address spender, uint256 amount) returns (bool)"]);
|
|
1306
|
+
var erc4626DepositAbi3 = parseAbi([
|
|
1307
|
+
"function deposit(uint256 assets, address receiver) returns (uint256 shares)"
|
|
1308
|
+
]);
|
|
1309
|
+
var evcAbi3 = parseAbi([
|
|
1310
|
+
"function batch((address targetContract, address onBehalfOfAccount, uint256 value, bytes data)[])"
|
|
1311
|
+
]);
|
|
1312
|
+
async function buildEvmMultisignBodyEulerV2BorrowCollateralDepositBatch(args) {
|
|
1313
|
+
const evc = getAddress(args.evc);
|
|
1314
|
+
const collateralVault = getAddress(args.collateralVault);
|
|
1315
|
+
const collateralAsset = getAddress(args.collateralUnderlying);
|
|
1316
|
+
const subAccount = getAddress(args.subAccount);
|
|
1317
|
+
const executor = getAddress(args.executorAddress);
|
|
1318
|
+
const ch = defineChain({
|
|
1319
|
+
id: args.chainId,
|
|
1320
|
+
name: "EulerColDeposit",
|
|
1321
|
+
nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
|
|
1322
|
+
rpcUrls: { default: { http: [args.rpcUrl] } }
|
|
1323
|
+
});
|
|
1324
|
+
const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl) });
|
|
1325
|
+
const dec = await fetchEulerVaultAssetDecimals({
|
|
1326
|
+
rpcUrl: args.rpcUrl,
|
|
1327
|
+
chainId: args.chainId,
|
|
1328
|
+
evault: collateralVault
|
|
1329
|
+
});
|
|
1330
|
+
const amountWei = parseUnits(args.amountHuman, dec);
|
|
1331
|
+
if (amountWei === 0n) throw new Error("Deposit amount is zero after converting with token decimals.");
|
|
1332
|
+
const steps = [];
|
|
1333
|
+
const currentAllowance = await publicClient.readContract({
|
|
1334
|
+
address: collateralAsset,
|
|
1335
|
+
abi: erc20AllowanceAbi4,
|
|
1336
|
+
functionName: "allowance",
|
|
1337
|
+
args: [executor, collateralVault]
|
|
1338
|
+
});
|
|
1339
|
+
if (currentAllowance < amountWei) {
|
|
1340
|
+
if (currentAllowance > 0n) {
|
|
1341
|
+
const dataReset = encodeFunctionData({
|
|
1342
|
+
abi: erc20ApproveAbi4,
|
|
1343
|
+
functionName: "approve",
|
|
1344
|
+
args: [collateralVault, 0n]
|
|
1345
|
+
});
|
|
1346
|
+
steps.push({ kind: "approve", to: collateralAsset, data: dataReset, value: 0n });
|
|
1347
|
+
}
|
|
1348
|
+
const dataApprove = encodeFunctionData({
|
|
1349
|
+
abi: erc20ApproveAbi4,
|
|
1350
|
+
functionName: "approve",
|
|
1351
|
+
args: [collateralVault, amountWei]
|
|
1352
|
+
});
|
|
1353
|
+
steps.push({ kind: "approve", to: collateralAsset, data: dataApprove, value: 0n });
|
|
1354
|
+
}
|
|
1355
|
+
const depositData = encodeFunctionData({
|
|
1356
|
+
abi: erc4626DepositAbi3,
|
|
1357
|
+
functionName: "deposit",
|
|
1358
|
+
args: [amountWei, subAccount]
|
|
1359
|
+
});
|
|
1360
|
+
const batchItems = [
|
|
1361
|
+
{
|
|
1362
|
+
targetContract: collateralVault,
|
|
1363
|
+
onBehalfOfAccount: subAccount,
|
|
1364
|
+
value: 0n,
|
|
1365
|
+
data: depositData
|
|
1366
|
+
}
|
|
1367
|
+
];
|
|
1368
|
+
const batchData = encodeFunctionData({
|
|
1369
|
+
abi: evcAbi3,
|
|
1370
|
+
functionName: "batch",
|
|
1371
|
+
args: [batchItems]
|
|
1372
|
+
});
|
|
1373
|
+
steps.push({ kind: "evc_batch", to: evc, data: batchData, value: 0n });
|
|
1374
|
+
const vaultLabel = (args.vaultMarketLabel ?? "").trim() || "Euler vault";
|
|
1375
|
+
const evmSteps = steps.map((s) => ({
|
|
1376
|
+
to: s.to,
|
|
1377
|
+
data: s.data,
|
|
1378
|
+
value: s.value,
|
|
1379
|
+
fallbackGas: s.kind === "approve" ? EULER_ERC20_APPROVE_FALLBACK4 : EULER_COLLATERAL_DEPOSIT_BATCH_FALLBACK
|
|
1380
|
+
}));
|
|
1381
|
+
const n = steps.length;
|
|
1382
|
+
const purposeSuffix = `Euler v2: ${n}-tx batch \u2014 deposit ${args.amountHuman} collateral into "${vaultLabel}" (approve if needed, then EVC deposit).`;
|
|
1383
|
+
const firstDataNo0x = evmSteps[0].data.startsWith("0x") ? evmSteps[0].data.slice(2) : evmSteps[0].data;
|
|
1384
|
+
return buildEvmMultisignBatch({
|
|
1385
|
+
context: {
|
|
1386
|
+
chainCategory: "evm",
|
|
1387
|
+
keyGen: args.keyGen,
|
|
1388
|
+
purposeText: args.purposeText,
|
|
1389
|
+
chainId: args.chainId,
|
|
1390
|
+
rpcUrl: args.rpcUrl,
|
|
1391
|
+
executorAddress: executor,
|
|
1392
|
+
chainDetail: args.chainDetail,
|
|
1393
|
+
useCustomGas: args.useCustomGas,
|
|
1394
|
+
customGasChainDetails: args.customGasChainDetails
|
|
1395
|
+
},
|
|
1396
|
+
steps: evmSteps,
|
|
1397
|
+
purposeSuffix,
|
|
1398
|
+
firstMsgRawNo0x: firstDataNo0x,
|
|
1399
|
+
destinationAddress: steps[0].to,
|
|
1400
|
+
buildBatchMeta: ({ index }) => {
|
|
1401
|
+
const s = steps[index];
|
|
1402
|
+
if (s.kind === "approve") {
|
|
1403
|
+
return {
|
|
1404
|
+
signatureText: JSON.stringify({
|
|
1405
|
+
kind: "EulerV2",
|
|
1406
|
+
name: "ERC20.approve",
|
|
1407
|
+
function: "approve(address spender, uint256 amount)",
|
|
1408
|
+
spender: collateralVault,
|
|
1409
|
+
collateralUnderlying: collateralAsset,
|
|
1410
|
+
note: "Allow Euler collateral eVault to pull assets for collateral deposit."
|
|
1411
|
+
}),
|
|
1412
|
+
evm: { type: "euler_v2_erc20_approve", version: 1, chainId: String(args.chainId) },
|
|
1413
|
+
eulerV2: { vaultMarket: vaultLabel, flow: "borrow_collateral_deposit_approve" }
|
|
1414
|
+
};
|
|
1415
|
+
}
|
|
1416
|
+
return {
|
|
1417
|
+
signatureText: JSON.stringify({
|
|
1418
|
+
kind: "EulerV2",
|
|
1419
|
+
name: "EVC.batch",
|
|
1420
|
+
function: "batch(BatchItem[]) \u2014 collateral deposit",
|
|
1421
|
+
evc,
|
|
1422
|
+
collateralVault,
|
|
1423
|
+
subAccount,
|
|
1424
|
+
amountHuman: args.amountHuman,
|
|
1425
|
+
vaultMarket: vaultLabel,
|
|
1426
|
+
note: "Deposit collateral into Euler v2 collateral eVault via EVC (reduces LTV)."
|
|
1427
|
+
}),
|
|
1428
|
+
evm: { type: "euler_v2_borrow_collateral_deposit_batch", version: 1, chainId: String(args.chainId) },
|
|
1429
|
+
eulerV2: {
|
|
1430
|
+
flow: "borrow_collateral_deposit",
|
|
1431
|
+
vaultMarket: vaultLabel,
|
|
1432
|
+
collateralVault,
|
|
1433
|
+
subAccount,
|
|
1434
|
+
amountHuman: args.amountHuman
|
|
1435
|
+
}
|
|
1436
|
+
};
|
|
1437
|
+
}
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
var EULER_COLLATERAL_WITHDRAW_BATCH_FALLBACK = 1400000n;
|
|
1441
|
+
var erc4626WithdrawAbi3 = parseAbi([
|
|
1442
|
+
"function withdraw(uint256 assets, address receiver, address owner) returns (uint256 shares)"
|
|
1443
|
+
]);
|
|
1444
|
+
var evcAbi4 = parseAbi([
|
|
1445
|
+
"function batch((address targetContract, address onBehalfOfAccount, uint256 value, bytes data)[])"
|
|
1446
|
+
]);
|
|
1447
|
+
async function buildEvmMultisignBodyEulerV2BorrowCollateralWithdrawBatch(args) {
|
|
1448
|
+
const evc = getAddress(args.evc);
|
|
1449
|
+
const collateralVault = getAddress(args.collateralVault);
|
|
1450
|
+
const subAccount = getAddress(args.subAccount);
|
|
1451
|
+
const receiver = getAddress(args.receiver);
|
|
1452
|
+
const executor = getAddress(args.executorAddress);
|
|
1453
|
+
const dec = await fetchEulerVaultAssetDecimals({ rpcUrl: args.rpcUrl, chainId: args.chainId, evault: collateralVault });
|
|
1454
|
+
const amountWei = parseUnits(args.amountHuman, dec);
|
|
1455
|
+
if (amountWei === 0n) throw new Error("Withdraw amount is zero after converting with token decimals.");
|
|
1456
|
+
const maxW = await fetchEulerBorrowCollateralMaxWithdrawAssetsWei({
|
|
1457
|
+
rpcUrl: args.rpcUrl,
|
|
1458
|
+
chainId: args.chainId,
|
|
1459
|
+
evc,
|
|
1460
|
+
collateralVault,
|
|
1461
|
+
subAccount,
|
|
1462
|
+
caller: executor
|
|
1463
|
+
});
|
|
1464
|
+
if (amountWei > maxW) {
|
|
1465
|
+
throw new Error(
|
|
1466
|
+
"Withdraw amount exceeds simulated max for this collateral vault (LTV cap, vault cash, or health check). Try a smaller amount."
|
|
1467
|
+
);
|
|
1468
|
+
}
|
|
1469
|
+
const withdrawData = encodeFunctionData({
|
|
1470
|
+
abi: erc4626WithdrawAbi3,
|
|
1471
|
+
functionName: "withdraw",
|
|
1472
|
+
args: [amountWei, receiver, subAccount]
|
|
1473
|
+
});
|
|
1474
|
+
const batchItems = [
|
|
1475
|
+
{
|
|
1476
|
+
targetContract: collateralVault,
|
|
1477
|
+
onBehalfOfAccount: subAccount,
|
|
1478
|
+
value: 0n,
|
|
1479
|
+
data: withdrawData
|
|
1480
|
+
}
|
|
1481
|
+
];
|
|
1482
|
+
const batchData = encodeFunctionData({
|
|
1483
|
+
abi: evcAbi4,
|
|
1484
|
+
functionName: "batch",
|
|
1485
|
+
args: [batchItems]
|
|
1486
|
+
});
|
|
1487
|
+
const steps = [{ kind: "evc_batch", to: evc, data: batchData, value: 0n }];
|
|
1488
|
+
const vaultLabel = (args.vaultMarketLabel ?? "").trim() || "Euler vault";
|
|
1489
|
+
const purposeSuffix = `Euler v2: 1-tx \u2014 withdraw ${args.amountHuman} collateral from "${vaultLabel}" via EVC (sub-account).`;
|
|
1490
|
+
const firstDataNo0x = batchData.startsWith("0x") ? batchData.slice(2) : batchData;
|
|
1491
|
+
const evmSteps = [
|
|
1492
|
+
{ to: evc, data: batchData, value: 0n, fallbackGas: EULER_COLLATERAL_WITHDRAW_BATCH_FALLBACK }
|
|
1493
|
+
];
|
|
1494
|
+
return buildEvmMultisignBatch({
|
|
1495
|
+
context: {
|
|
1496
|
+
chainCategory: "evm",
|
|
1497
|
+
keyGen: args.keyGen,
|
|
1498
|
+
purposeText: args.purposeText,
|
|
1499
|
+
chainId: args.chainId,
|
|
1500
|
+
rpcUrl: args.rpcUrl,
|
|
1501
|
+
executorAddress: executor,
|
|
1502
|
+
chainDetail: args.chainDetail,
|
|
1503
|
+
useCustomGas: args.useCustomGas,
|
|
1504
|
+
customGasChainDetails: args.customGasChainDetails
|
|
1505
|
+
},
|
|
1506
|
+
steps: evmSteps,
|
|
1507
|
+
purposeSuffix,
|
|
1508
|
+
firstMsgRawNo0x: firstDataNo0x,
|
|
1509
|
+
destinationAddress: steps[0].to,
|
|
1510
|
+
buildBatchMeta: () => ({
|
|
1511
|
+
signatureText: JSON.stringify({
|
|
1512
|
+
kind: "EulerV2",
|
|
1513
|
+
name: "EVC.batch",
|
|
1514
|
+
function: "batch \u2014 collateral withdraw",
|
|
1515
|
+
evc,
|
|
1516
|
+
collateralVault,
|
|
1517
|
+
subAccount,
|
|
1518
|
+
receiver,
|
|
1519
|
+
amountHuman: args.amountHuman,
|
|
1520
|
+
vaultMarket: vaultLabel,
|
|
1521
|
+
note: "Withdraw collateral from Euler v2 eVault via EVC (max from RPC simulate + LTV / vault cash)."
|
|
1522
|
+
}),
|
|
1523
|
+
evm: { type: "euler_v2_borrow_collateral_withdraw_batch", version: 1, chainId: String(args.chainId) },
|
|
1524
|
+
eulerV2: {
|
|
1525
|
+
flow: "borrow_collateral_withdraw",
|
|
1526
|
+
vaultMarket: vaultLabel,
|
|
1527
|
+
collateralVault,
|
|
1528
|
+
subAccount,
|
|
1529
|
+
receiver,
|
|
1530
|
+
amountHuman: args.amountHuman
|
|
1531
|
+
}
|
|
1532
|
+
})
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
// src/protocols/evm/euler-v2/index.ts
|
|
1537
|
+
var EULER_V2_PROTOCOL_ID = "euler-v2";
|
|
1538
|
+
var eulerV2ProtocolModule = {
|
|
1539
|
+
id: EULER_V2_PROTOCOL_ID,
|
|
1540
|
+
chainCategory: "evm",
|
|
1541
|
+
isChainSupported(ctx) {
|
|
1542
|
+
return ctx.chainCategory === "evm";
|
|
1543
|
+
},
|
|
1544
|
+
isTokenSupported(token) {
|
|
1545
|
+
return token.category === "evm" && (token.kind === "native" || token.kind === "erc20");
|
|
1546
|
+
},
|
|
1547
|
+
actions: [
|
|
1548
|
+
{ id: "euler-v2.isolated-lend", protocolId: EULER_V2_PROTOCOL_ID, chainCategory: "evm", description: "Deposit into Euler vault", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
|
|
1549
|
+
{ id: "euler-v2.isolated-borrow", protocolId: EULER_V2_PROTOCOL_ID, chainCategory: "evm", description: "Borrow from Euler vault", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
|
|
1550
|
+
{ id: "euler-v2.vault-withdraw", protocolId: EULER_V2_PROTOCOL_ID, chainCategory: "evm", description: "Withdraw from Euler vault", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
|
|
1551
|
+
{ id: "euler-v2.borrow-repay", protocolId: EULER_V2_PROTOCOL_ID, chainCategory: "evm", description: "Repay Euler borrow", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
|
|
1552
|
+
{ id: "euler-v2.collateral-deposit", protocolId: EULER_V2_PROTOCOL_ID, chainCategory: "evm", description: "Deposit borrow collateral", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} },
|
|
1553
|
+
{ id: "euler-v2.collateral-withdraw", protocolId: EULER_V2_PROTOCOL_ID, chainCategory: "evm", description: "Withdraw borrow collateral", commonParams: ["keyGen", "purposeText", "useCustomGas"], params: {} }
|
|
1554
|
+
]
|
|
1555
|
+
};
|
|
1556
|
+
registerProtocolModule(eulerV2ProtocolModule);
|
|
1557
|
+
|
|
1558
|
+
export { EULER_SAME_ASSET_BORROW_APPROVAL_BUFFER_BPS, EULER_SAME_ASSET_BORROW_MAX_ROUNDS, EULER_SAME_ASSET_BORROW_PROTOCOL_HEADROOM_BPS, EULER_SAME_ASSET_BORROW_RATIO_STOP_EPS_BPS, EULER_V2_ISOLATED_VAULT_DEPOSIT_FALLBACK_GAS, EULER_V2_PROTOCOL_ID, EULER_V2_VAULT_WITHDRAW_FALLBACK_GAS, buildEvmMultisignBodyEulerV2BorrowCollateralDepositBatch, buildEvmMultisignBodyEulerV2BorrowCollateralWithdrawBatch, buildEvmMultisignBodyEulerV2BorrowRepayBatch, buildEvmMultisignBodyEulerV2IsolatedBorrowBatch, buildEvmMultisignBodyEulerV2IsolatedLendDepositBatch, buildEvmMultisignBodyEulerV2VaultWithdraw, clampEulerUnderlyingDecimalsForEulerUi, eulerBorrowAndCollateralSameAsset, eulerSameAssetApproveAmountWithBuffer, eulerSameAssetTotalCollateralPullWei, eulerV2ProtocolModule, fetchEulerBorrowCollateralMaxWithdrawAssetsWei, fetchEulerLendEarnVaultEffectiveMaxWithdrawWei, fetchEulerVaultAssetDecimals, fetchEulerVaultMaxWithdrawWei, fetchEulerVaultUnderlyingMeta, planSameAssetLeveragedBorrows };
|
|
1559
|
+
//# sourceMappingURL=index.js.map
|
|
1560
|
+
//# sourceMappingURL=index.js.map
|