@continuumdao/ctm-mpc-defi 0.2.13 → 0.2.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/agent/catalog.cjs +828 -280
  2. package/dist/agent/catalog.cjs.map +1 -1
  3. package/dist/agent/catalog.d.ts +1645 -300
  4. package/dist/agent/catalog.js +823 -281
  5. package/dist/agent/catalog.js.map +1 -1
  6. package/dist/chains/evm/index.cjs +75 -0
  7. package/dist/chains/evm/index.cjs.map +1 -1
  8. package/dist/chains/evm/index.d.ts +31 -1
  9. package/dist/chains/evm/index.js +73 -2
  10. package/dist/chains/evm/index.js.map +1 -1
  11. package/dist/index.cjs +140 -32
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +138 -34
  15. package/dist/index.js.map +1 -1
  16. package/dist/protocols/evm/euler-v2/index.cjs +29 -2
  17. package/dist/protocols/evm/euler-v2/index.cjs.map +1 -1
  18. package/dist/protocols/evm/euler-v2/index.d.ts +23 -1
  19. package/dist/protocols/evm/euler-v2/index.js +29 -3
  20. package/dist/protocols/evm/euler-v2/index.js.map +1 -1
  21. package/dist/protocols/evm/gmx/index.cjs +42 -8
  22. package/dist/protocols/evm/gmx/index.cjs.map +1 -1
  23. package/dist/protocols/evm/gmx/index.d.ts +20 -4
  24. package/dist/protocols/evm/gmx/index.js +42 -9
  25. package/dist/protocols/evm/gmx/index.js.map +1 -1
  26. package/dist/protocols/evm/maple/index.cjs +9 -1
  27. package/dist/protocols/evm/maple/index.cjs.map +1 -1
  28. package/dist/protocols/evm/maple/index.d.ts +2 -1
  29. package/dist/protocols/evm/maple/index.js +9 -1
  30. package/dist/protocols/evm/maple/index.js.map +1 -1
  31. package/dist/protocols/evm/morpho/index.cjs +56 -0
  32. package/dist/protocols/evm/morpho/index.cjs.map +1 -1
  33. package/dist/protocols/evm/morpho/index.d.ts +28 -1
  34. package/dist/protocols/evm/morpho/index.js +56 -1
  35. package/dist/protocols/evm/morpho/index.js.map +1 -1
  36. package/dist/protocols/evm/uniswap-v4/index.cjs +98 -26
  37. package/dist/protocols/evm/uniswap-v4/index.cjs.map +1 -1
  38. package/dist/protocols/evm/uniswap-v4/index.js +99 -27
  39. package/dist/protocols/evm/uniswap-v4/index.js.map +1 -1
  40. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import { parseAbiParameters, parseAbi, parseAbiItem, getAddress, zeroAddress, formatUnits, parseUnits, encodeFunctionData, erc20Abi, keccak256, encodeAbiParameters, decodeEventLog, defineChain, createPublicClient, http, decodeFunctionData, parseGwei, serializeTransaction } from 'viem';
1
+ import { parseAbi, parseAbiParameters, parseAbiItem, getAddress, zeroAddress, formatUnits, parseUnits, createPublicClient, http, defineChain, encodeFunctionData, keccak256, encodeAbiParameters, decodeEventLog, decodeFunctionData, parseGwei, serializeTransaction, erc20Abi } from 'viem';
2
2
  import { nodeFetchWithReadAuth, fetchChainFeeParams, gasLimitFromEstimateAndChainConfig, gweiToDecimalString, proposalTxParamsToFeeSnapshot, alignEip1559FeesWithLatestBase, getClientIdFromKeyGenResult } from '@continuumdao/continuum-node-sdk';
3
3
 
4
4
  // src/core/registry.ts
@@ -498,6 +498,45 @@ function formatUniswapQuoteForDisplay(res, args) {
498
498
  if (requestId) lines.push(`Request: ${requestId}`);
499
499
  return { title: "Quote received", lines, rawJson };
500
500
  }
501
+ var erc20AllowanceAbi = parseAbi([
502
+ "function allowance(address owner, address spender) view returns (uint256)",
503
+ "function decimals() view returns (uint8)"
504
+ ]);
505
+ var erc20ApproveAbi = parseAbi(["function approve(address spender, uint256 amount) returns (bool)"]);
506
+ async function buildConditionalErc20ApproveSteps(args) {
507
+ if (args.amountWei <= 0n) return [];
508
+ const token = getAddress(args.token);
509
+ const spender = getAddress(args.spender);
510
+ const owner = getAddress(args.owner);
511
+ const fallback = args.fallbackGas ?? 100000n;
512
+ const allowance = await args.publicClient.readContract({
513
+ address: token,
514
+ abi: erc20AllowanceAbi,
515
+ functionName: "allowance",
516
+ args: [owner, spender]
517
+ });
518
+ if (allowance >= args.amountWei) return [];
519
+ const steps = [];
520
+ if (allowance > 0n) {
521
+ steps.push({
522
+ to: token,
523
+ data: encodeFunctionData({ abi: erc20ApproveAbi, functionName: "approve", args: [spender, 0n] }),
524
+ value: 0n,
525
+ fallbackGas: fallback
526
+ });
527
+ }
528
+ steps.push({
529
+ to: token,
530
+ data: encodeFunctionData({
531
+ abi: erc20ApproveAbi,
532
+ functionName: "approve",
533
+ args: [spender, args.amountWei]
534
+ }),
535
+ value: 0n,
536
+ fallbackGas: fallback
537
+ });
538
+ return steps;
539
+ }
501
540
 
502
541
  // src/core/purpose.ts
503
542
  function mergePurposeText(purposeText, purposeSuffix) {
@@ -1185,10 +1224,23 @@ async function buildEvmMultisignBodyUniswapV4SkipPermit2Batch(args) {
1185
1224
  if (expiration48 > MAX_UINT48) expiration48 = MAX_UINT48;
1186
1225
  }
1187
1226
  const erc20ApproveSpender = usePermit2Triple ? PERMIT2_ADDRESS : toRouter;
1188
- const approveData = encodeFunctionData({
1189
- abi: erc20Abi,
1190
- functionName: "approve",
1191
- args: [erc20ApproveSpender, approveAmountWei]
1227
+ const executor = getAddress(args.executorAddress);
1228
+ const publicClient = createPublicClient({
1229
+ chain: defineChain({
1230
+ id: args.chainId,
1231
+ name: `uniswap-swap-${args.chainId}`,
1232
+ nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
1233
+ rpcUrls: { default: { http: [args.rpcUrl] } }
1234
+ }),
1235
+ transport: http(args.rpcUrl)
1236
+ });
1237
+ const erc20ApproveSteps = await buildConditionalErc20ApproveSteps({
1238
+ publicClient,
1239
+ token: tokenIn,
1240
+ spender: erc20ApproveSpender,
1241
+ owner: executor,
1242
+ amountWei: approveAmountWei,
1243
+ fallbackGas: 100000n
1192
1244
  });
1193
1245
  const valueWei = (() => {
1194
1246
  const v = args.swap.value;
@@ -1200,13 +1252,16 @@ async function buildEvmMultisignBodyUniswapV4SkipPermit2Batch(args) {
1200
1252
  }
1201
1253
  })();
1202
1254
  const swapRecord = args.swap;
1203
- const swapMsgIndex = usePermit2Triple ? 2 : 1;
1255
+ const swapMsgIndex = usePermit2Triple ? erc20ApproveSteps.length + 1 : erc20ApproveSteps.length;
1204
1256
  let gasBuildSource = "rpcEstimate";
1205
1257
  let estimateGasError;
1206
1258
  let swapBaseGasUnits = DEFAULT_UNIVERSAL_ROUTER_GAS_FALLBACK;
1207
- const steps = [
1208
- { to: tokenIn, data: approveData, value: 0n, fallbackGas: 100000n }
1209
- ];
1259
+ const steps = erc20ApproveSteps.map((s) => ({
1260
+ to: s.to,
1261
+ data: s.data,
1262
+ value: s.value,
1263
+ fallbackGas: s.fallbackGas
1264
+ }));
1210
1265
  if (usePermit2Triple) {
1211
1266
  const permit2ApproveData = encodeFunctionData({
1212
1267
  abi: permit2ApproveRouterAbi,
@@ -1223,8 +1278,9 @@ async function buildEvmMultisignBodyUniswapV4SkipPermit2Batch(args) {
1223
1278
  fallbackGas: DEFAULT_UNIVERSAL_ROUTER_GAS_FALLBACK
1224
1279
  });
1225
1280
  const swapStepIndex = steps.length - 1;
1226
- const approveMsgRawNo0x = approveData.startsWith("0x") ? approveData.slice(2) : approveData;
1227
- const purposeSuffix = usePermit2Triple ? "Uniswap V4: 3-tx batch (classic allowance) \u2014 (1) ERC-20 approve allowance hub, (2) hub approve(Universal Router), (3) swap (Trade /swap)." : "Uniswap V4: 2-tx batch (classic allowance, dispatcher) \u2014 (1) ERC-20 approve swap.to (dispatcher pulls tokens), (2) swap (Trade /swap).";
1281
+ const approveMsgRawNo0x = erc20ApproveSteps.length > 0 && erc20ApproveSteps[erc20ApproveSteps.length - 1].data.startsWith("0x") ? erc20ApproveSteps[erc20ApproveSteps.length - 1].data.slice(2) : dataHex.startsWith("0x") ? dataHex.slice(2) : dataHex;
1282
+ const approveCount = erc20ApproveSteps.length;
1283
+ const purposeSuffix = usePermit2Triple ? `Uniswap V4: ${approveCount > 0 ? "3" : "2"}-tx batch (classic allowance) \u2014 ERC-20 approve allowance hub when needed, hub approve(Universal Router), swap.` : approveCount > 0 ? "Uniswap V4: 2-tx batch (classic allowance, dispatcher) \u2014 ERC-20 approve swap.to when needed, then swap." : "Uniswap V4: 1-tx batch \u2014 swap only (ERC-20 allowance already sufficient).";
1228
1284
  const buildSwapAudit = () => ({
1229
1285
  skipPermit2Batch: true,
1230
1286
  approvalPath: usePermit2Triple ? "permit2_triple" : "dispatcher",
@@ -1301,18 +1357,18 @@ async function buildEvmMultisignBodyUniswapV4SkipPermit2Batch(args) {
1301
1357
  firstMsgRawNo0x: approveMsgRawNo0x,
1302
1358
  destinationAddress: tokenIn,
1303
1359
  payableValueWei: valueWei > 0n ? valueWei : void 0,
1304
- estimateGasForStep: async ({ step, index, publicClient, executor }) => {
1360
+ estimateGasForStep: async ({ step, index, publicClient: publicClient2, executor: executor2 }) => {
1305
1361
  if (index !== swapStepIndex) {
1306
- return publicClient.estimateGas({
1362
+ return publicClient2.estimateGas({
1307
1363
  to: step.to,
1308
1364
  data: step.data,
1309
1365
  value: step.value,
1310
- account: executor
1366
+ account: executor2
1311
1367
  });
1312
1368
  }
1313
1369
  const r = await estimateUniswapRouterSwapGas({
1314
- publicClient,
1315
- executor,
1370
+ publicClient: publicClient2,
1371
+ executor: executor2,
1316
1372
  to: toRouter,
1317
1373
  data: dataHex,
1318
1374
  value: valueWei,
@@ -2197,7 +2253,7 @@ function isNativeUniswapLpTokenAddress(token) {
2197
2253
 
2198
2254
  // src/protocols/evm/uniswap-v4/liquidityMultisign.ts
2199
2255
  var wethDepositAbi = parseAbi(["function deposit() payable"]);
2200
- var erc20AllowanceAbi = parseAbi([
2256
+ var erc20AllowanceAbi2 = parseAbi([
2201
2257
  "function allowance(address owner, address spender) view returns (uint256)",
2202
2258
  "function decimals() view returns (uint8)"
2203
2259
  ]);
@@ -2307,7 +2363,7 @@ async function resolveApproveTargets(args) {
2307
2363
  }
2308
2364
  const allowance = await args.publicClient.readContract({
2309
2365
  address: row.token,
2310
- abi: erc20AllowanceAbi,
2366
+ abi: erc20AllowanceAbi2,
2311
2367
  functionName: "allowance",
2312
2368
  args: [args.executor, args.spender]
2313
2369
  });
@@ -2349,16 +2405,32 @@ async function buildEvmMultisignBodyUniswapV4LiquidityBatchInternal(args) {
2349
2405
  value: target.amount,
2350
2406
  fallbackGas: UNISWAP_V4_LP_WETH_DEPOSIT_FALLBACK
2351
2407
  });
2352
- steps.push({
2353
- to: target.token,
2354
- data: encodeFunctionData({
2355
- abi: erc20Abi,
2356
- functionName: "approve",
2357
- args: [spender, target.amount]
2358
- }),
2359
- value: 0n,
2360
- fallbackGas: UNISWAP_V4_LP_ERC20_APPROVE_FALLBACK
2408
+ const wethAllowance = await publicClient.readContract({
2409
+ address: target.token,
2410
+ abi: erc20AllowanceAbi2,
2411
+ functionName: "allowance",
2412
+ args: [executor, spender]
2361
2413
  });
2414
+ if (wethAllowance < target.amount) {
2415
+ if (wethAllowance > 0n) {
2416
+ steps.push({
2417
+ to: target.token,
2418
+ data: encodeFunctionData({ abi: erc20Abi, functionName: "approve", args: [spender, 0n] }),
2419
+ value: 0n,
2420
+ fallbackGas: UNISWAP_V4_LP_ERC20_APPROVE_FALLBACK
2421
+ });
2422
+ }
2423
+ steps.push({
2424
+ to: target.token,
2425
+ data: encodeFunctionData({
2426
+ abi: erc20Abi,
2427
+ functionName: "approve",
2428
+ args: [spender, target.amount]
2429
+ }),
2430
+ value: 0n,
2431
+ fallbackGas: UNISWAP_V4_LP_ERC20_APPROVE_FALLBACK
2432
+ });
2433
+ }
2362
2434
  } else {
2363
2435
  steps.push({
2364
2436
  to: target.token,