@continuumdao/ctm-mpc-defi 0.2.12 → 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 (41) hide show
  1. package/dist/agent/catalog.cjs +868 -283
  2. package/dist/agent/catalog.cjs.map +1 -1
  3. package/dist/agent/catalog.d.ts +1657 -304
  4. package/dist/agent/catalog.js +861 -284
  5. package/dist/agent/catalog.js.map +1 -1
  6. package/dist/agent/skills/morpho/SKILL.md +8 -22
  7. package/dist/chains/evm/index.cjs +75 -0
  8. package/dist/chains/evm/index.cjs.map +1 -1
  9. package/dist/chains/evm/index.d.ts +31 -1
  10. package/dist/chains/evm/index.js +73 -2
  11. package/dist/chains/evm/index.js.map +1 -1
  12. package/dist/index.cjs +140 -32
  13. package/dist/index.cjs.map +1 -1
  14. package/dist/index.d.ts +1 -1
  15. package/dist/index.js +138 -34
  16. package/dist/index.js.map +1 -1
  17. package/dist/protocols/evm/euler-v2/index.cjs +29 -2
  18. package/dist/protocols/evm/euler-v2/index.cjs.map +1 -1
  19. package/dist/protocols/evm/euler-v2/index.d.ts +23 -1
  20. package/dist/protocols/evm/euler-v2/index.js +29 -3
  21. package/dist/protocols/evm/euler-v2/index.js.map +1 -1
  22. package/dist/protocols/evm/gmx/index.cjs +42 -8
  23. package/dist/protocols/evm/gmx/index.cjs.map +1 -1
  24. package/dist/protocols/evm/gmx/index.d.ts +20 -4
  25. package/dist/protocols/evm/gmx/index.js +42 -9
  26. package/dist/protocols/evm/gmx/index.js.map +1 -1
  27. package/dist/protocols/evm/maple/index.cjs +9 -1
  28. package/dist/protocols/evm/maple/index.cjs.map +1 -1
  29. package/dist/protocols/evm/maple/index.d.ts +2 -1
  30. package/dist/protocols/evm/maple/index.js +9 -1
  31. package/dist/protocols/evm/maple/index.js.map +1 -1
  32. package/dist/protocols/evm/morpho/index.cjs +56 -0
  33. package/dist/protocols/evm/morpho/index.cjs.map +1 -1
  34. package/dist/protocols/evm/morpho/index.d.ts +28 -1
  35. package/dist/protocols/evm/morpho/index.js +56 -1
  36. package/dist/protocols/evm/morpho/index.js.map +1 -1
  37. package/dist/protocols/evm/uniswap-v4/index.cjs +98 -26
  38. package/dist/protocols/evm/uniswap-v4/index.cjs.map +1 -1
  39. package/dist/protocols/evm/uniswap-v4/index.js +99 -27
  40. package/dist/protocols/evm/uniswap-v4/index.js.map +1 -1
  41. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export { getAaveGraphqlProxyUrl, getCoingeckoProxyUrl, getEulerGraphqlProxyUrl,
4
4
  export { C as ChainCategoryBuildInput, a as ChainCategoryModule, M as MultisignLeg, c as coreChainCategoryModule, f as finalizeMultisign } from './envelope-CpBUh9eP.js';
5
5
  export { g as getActionsByChainCategory, a as getProtocolModule, b as getProtocolModules, r as registerProtocolModule } from './registry-Bv5o37_w.js';
6
6
  export { E as EvmChainDetail, a as EvmProtocolContext, b as EvmTxStep, i as isEvmNativeToken, m as matchEvmTokenKind } from './types-DUeNJLr9.js';
7
- export { COINGECKO_PLATFORM_BY_CHAIN_ID, EvmBatchMetaBuilder, EvmBuildBatchArgs, buildEvmMultisignBatch, coingeckoPlatformForChainId, evmChainCategoryModule, normalizeCurveRouterAmountString, normalizeHumanDecimalAmount, parseEvmChainIdToNumber, routerSwapGasLimitFromEstimate } from './chains/evm/index.js';
7
+ export { COINGECKO_PLATFORM_BY_CHAIN_ID, ConditionalApproveStep, EvmBatchMetaBuilder, EvmBuildBatchArgs, approveStepsToEvmTxSteps, buildConditionalErc20ApproveSteps, buildEvmMultisignBatch, coingeckoPlatformForChainId, evmChainCategoryModule, normalizeCurveRouterAmountString, normalizeHumanDecimalAmount, parseEvmChainIdToNumber, publicClientForRpc, resolveErc20Decimals, routerSwapGasLimitFromEstimate } from './chains/evm/index.js';
8
8
  export { SOLANA_CHAIN_CATEGORY, solanaChainCategoryModule } from './chains/solana/index.js';
9
9
  export { NEAR_CHAIN_CATEGORY, nearChainCategoryModule } from './chains/near/index.js';
10
10
  export { uniswapV4, uniswapV4ProtocolModule } from './protocols/evm/uniswap-v4/index.js';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { getClientIdFromKeyGenResult, gasLimitFromEstimateAndChainConfig, fetchChainFeeParams, gweiToDecimalString, proposalTxParamsToFeeSnapshot, alignEip1559FeesWithLatestBase, nodeFetchWithReadAuth } from '@continuumdao/continuum-node-sdk';
2
2
  export { getClientIdFromKeyGenResult as firstClientIdFromKeyGen } from '@continuumdao/continuum-node-sdk';
3
- import { parseAbi, parseAbiItem, getAddress, zeroAddress, defineChain, createPublicClient, http, parseGwei, serializeTransaction, keccak256, parseUnits, formatUnits, encodeFunctionData, erc20Abi, decodeFunctionData, isAddress } from 'viem';
3
+ import { parseAbi, parseAbiItem, getAddress, zeroAddress, defineChain, createPublicClient, http, parseGwei, serializeTransaction, keccak256, parseUnits, formatUnits, encodeFunctionData, decodeFunctionData, erc20Abi, isAddress } from 'viem';
4
4
 
5
5
  // src/core/index.ts
6
6
 
@@ -370,6 +370,77 @@ function normalizeHumanDecimalAmount(raw, tokenDecimals) {
370
370
  return formatUnits(wei, tokenDecimals);
371
371
  }
372
372
  var normalizeCurveRouterAmountString = normalizeHumanDecimalAmount;
373
+ var erc20AllowanceAbi = parseAbi([
374
+ "function allowance(address owner, address spender) view returns (uint256)",
375
+ "function decimals() view returns (uint8)"
376
+ ]);
377
+ var erc20ApproveAbi = parseAbi(["function approve(address spender, uint256 amount) returns (bool)"]);
378
+ function publicClientForRpc(chainId, rpcUrl) {
379
+ const ch = defineChain({
380
+ id: chainId,
381
+ name: `chain-${chainId}`,
382
+ nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
383
+ rpcUrls: { default: { http: [rpcUrl.trim()] } }
384
+ });
385
+ return createPublicClient({ chain: ch, transport: http(rpcUrl.trim()) });
386
+ }
387
+ async function resolveErc20Decimals(args) {
388
+ const token = getAddress(args.token);
389
+ const onChain = await args.publicClient.readContract({
390
+ address: token,
391
+ abi: erc20AllowanceAbi,
392
+ functionName: "decimals"
393
+ });
394
+ const n = typeof onChain === "bigint" ? Number(onChain) : Number(onChain);
395
+ if (!Number.isFinite(n) || n < 0 || n > 36) {
396
+ throw new Error(`Invalid on-chain decimals for ${token}.`);
397
+ }
398
+ for (const hint of [args.registryDecimals, args.apiDecimals]) {
399
+ }
400
+ return n;
401
+ }
402
+ async function buildConditionalErc20ApproveSteps(args) {
403
+ if (args.amountWei <= 0n) return [];
404
+ const token = getAddress(args.token);
405
+ const spender = getAddress(args.spender);
406
+ const owner = getAddress(args.owner);
407
+ const fallback = args.fallbackGas ?? 100000n;
408
+ const allowance = await args.publicClient.readContract({
409
+ address: token,
410
+ abi: erc20AllowanceAbi,
411
+ functionName: "allowance",
412
+ args: [owner, spender]
413
+ });
414
+ if (allowance >= args.amountWei) return [];
415
+ const steps = [];
416
+ if (allowance > 0n) {
417
+ steps.push({
418
+ to: token,
419
+ data: encodeFunctionData({ abi: erc20ApproveAbi, functionName: "approve", args: [spender, 0n] }),
420
+ value: 0n,
421
+ fallbackGas: fallback
422
+ });
423
+ }
424
+ steps.push({
425
+ to: token,
426
+ data: encodeFunctionData({
427
+ abi: erc20ApproveAbi,
428
+ functionName: "approve",
429
+ args: [spender, args.amountWei]
430
+ }),
431
+ value: 0n,
432
+ fallbackGas: fallback
433
+ });
434
+ return steps;
435
+ }
436
+ function approveStepsToEvmTxSteps(steps) {
437
+ return steps.map((s) => ({
438
+ to: s.to,
439
+ data: s.data,
440
+ value: s.value,
441
+ fallbackGas: s.fallbackGas
442
+ }));
443
+ }
373
444
 
374
445
  // src/chains/evm/coingecko.ts
375
446
  var COINGECKO_PLATFORM_BY_CHAIN_ID = {
@@ -1151,10 +1222,23 @@ async function buildEvmMultisignBodyUniswapV4SkipPermit2Batch(args) {
1151
1222
  if (expiration48 > MAX_UINT48) expiration48 = MAX_UINT48;
1152
1223
  }
1153
1224
  const erc20ApproveSpender = usePermit2Triple ? PERMIT2_ADDRESS : toRouter;
1154
- const approveData = encodeFunctionData({
1155
- abi: erc20Abi,
1156
- functionName: "approve",
1157
- args: [erc20ApproveSpender, approveAmountWei]
1225
+ const executor = getAddress(args.executorAddress);
1226
+ const publicClient = createPublicClient({
1227
+ chain: defineChain({
1228
+ id: args.chainId,
1229
+ name: `uniswap-swap-${args.chainId}`,
1230
+ nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" },
1231
+ rpcUrls: { default: { http: [args.rpcUrl] } }
1232
+ }),
1233
+ transport: http(args.rpcUrl)
1234
+ });
1235
+ const erc20ApproveSteps = await buildConditionalErc20ApproveSteps({
1236
+ publicClient,
1237
+ token: tokenIn,
1238
+ spender: erc20ApproveSpender,
1239
+ owner: executor,
1240
+ amountWei: approveAmountWei,
1241
+ fallbackGas: 100000n
1158
1242
  });
1159
1243
  const valueWei = (() => {
1160
1244
  const v = args.swap.value;
@@ -1166,13 +1250,16 @@ async function buildEvmMultisignBodyUniswapV4SkipPermit2Batch(args) {
1166
1250
  }
1167
1251
  })();
1168
1252
  const swapRecord = args.swap;
1169
- const swapMsgIndex = usePermit2Triple ? 2 : 1;
1253
+ const swapMsgIndex = usePermit2Triple ? erc20ApproveSteps.length + 1 : erc20ApproveSteps.length;
1170
1254
  let gasBuildSource = "rpcEstimate";
1171
1255
  let estimateGasError;
1172
1256
  let swapBaseGasUnits = DEFAULT_UNIVERSAL_ROUTER_GAS_FALLBACK;
1173
- const steps = [
1174
- { to: tokenIn, data: approveData, value: 0n, fallbackGas: 100000n }
1175
- ];
1257
+ const steps = erc20ApproveSteps.map((s) => ({
1258
+ to: s.to,
1259
+ data: s.data,
1260
+ value: s.value,
1261
+ fallbackGas: s.fallbackGas
1262
+ }));
1176
1263
  if (usePermit2Triple) {
1177
1264
  const permit2ApproveData = encodeFunctionData({
1178
1265
  abi: permit2ApproveRouterAbi,
@@ -1189,8 +1276,9 @@ async function buildEvmMultisignBodyUniswapV4SkipPermit2Batch(args) {
1189
1276
  fallbackGas: DEFAULT_UNIVERSAL_ROUTER_GAS_FALLBACK
1190
1277
  });
1191
1278
  const swapStepIndex = steps.length - 1;
1192
- const approveMsgRawNo0x = approveData.startsWith("0x") ? approveData.slice(2) : approveData;
1193
- 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).";
1279
+ 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;
1280
+ const approveCount = erc20ApproveSteps.length;
1281
+ 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).";
1194
1282
  const buildSwapAudit = () => ({
1195
1283
  skipPermit2Batch: true,
1196
1284
  approvalPath: usePermit2Triple ? "permit2_triple" : "dispatcher",
@@ -1267,18 +1355,18 @@ async function buildEvmMultisignBodyUniswapV4SkipPermit2Batch(args) {
1267
1355
  firstMsgRawNo0x: approveMsgRawNo0x,
1268
1356
  destinationAddress: tokenIn,
1269
1357
  payableValueWei: valueWei > 0n ? valueWei : void 0,
1270
- estimateGasForStep: async ({ step, index, publicClient, executor }) => {
1358
+ estimateGasForStep: async ({ step, index, publicClient: publicClient2, executor: executor2 }) => {
1271
1359
  if (index !== swapStepIndex) {
1272
- return publicClient.estimateGas({
1360
+ return publicClient2.estimateGas({
1273
1361
  to: step.to,
1274
1362
  data: step.data,
1275
1363
  value: step.value,
1276
- account: executor
1364
+ account: executor2
1277
1365
  });
1278
1366
  }
1279
1367
  const r = await estimateUniswapRouterSwapGas({
1280
- publicClient,
1281
- executor,
1368
+ publicClient: publicClient2,
1369
+ executor: executor2,
1282
1370
  to: toRouter,
1283
1371
  data: dataHex,
1284
1372
  value: valueWei,
@@ -1781,7 +1869,7 @@ function isNativeUniswapLpTokenAddress(token) {
1781
1869
 
1782
1870
  // src/protocols/evm/uniswap-v4/liquidityMultisign.ts
1783
1871
  var wethDepositAbi = parseAbi(["function deposit() payable"]);
1784
- var erc20AllowanceAbi = parseAbi([
1872
+ var erc20AllowanceAbi2 = parseAbi([
1785
1873
  "function allowance(address owner, address spender) view returns (uint256)",
1786
1874
  "function decimals() view returns (uint8)"
1787
1875
  ]);
@@ -1855,7 +1943,7 @@ async function resolveApproveTargets(args) {
1855
1943
  }
1856
1944
  const allowance = await args.publicClient.readContract({
1857
1945
  address: row.token,
1858
- abi: erc20AllowanceAbi,
1946
+ abi: erc20AllowanceAbi2,
1859
1947
  functionName: "allowance",
1860
1948
  args: [args.executor, args.spender]
1861
1949
  });
@@ -1897,16 +1985,32 @@ async function buildEvmMultisignBodyUniswapV4LiquidityBatchInternal(args) {
1897
1985
  value: target.amount,
1898
1986
  fallbackGas: UNISWAP_V4_LP_WETH_DEPOSIT_FALLBACK
1899
1987
  });
1900
- steps.push({
1901
- to: target.token,
1902
- data: encodeFunctionData({
1903
- abi: erc20Abi,
1904
- functionName: "approve",
1905
- args: [spender, target.amount]
1906
- }),
1907
- value: 0n,
1908
- fallbackGas: UNISWAP_V4_LP_ERC20_APPROVE_FALLBACK
1988
+ const wethAllowance = await publicClient.readContract({
1989
+ address: target.token,
1990
+ abi: erc20AllowanceAbi2,
1991
+ functionName: "allowance",
1992
+ args: [executor, spender]
1909
1993
  });
1994
+ if (wethAllowance < target.amount) {
1995
+ if (wethAllowance > 0n) {
1996
+ steps.push({
1997
+ to: target.token,
1998
+ data: encodeFunctionData({ abi: erc20Abi, functionName: "approve", args: [spender, 0n] }),
1999
+ value: 0n,
2000
+ fallbackGas: UNISWAP_V4_LP_ERC20_APPROVE_FALLBACK
2001
+ });
2002
+ }
2003
+ steps.push({
2004
+ to: target.token,
2005
+ data: encodeFunctionData({
2006
+ abi: erc20Abi,
2007
+ functionName: "approve",
2008
+ args: [spender, target.amount]
2009
+ }),
2010
+ value: 0n,
2011
+ fallbackGas: UNISWAP_V4_LP_ERC20_APPROVE_FALLBACK
2012
+ });
2013
+ }
1910
2014
  } else {
1911
2015
  steps.push({
1912
2016
  to: target.token,
@@ -2346,36 +2450,36 @@ async function buildEvmMultisignBodyCurveDaoBatch(args) {
2346
2450
  rpcUrls: { default: { http: [args.rpcUrl] } }
2347
2451
  });
2348
2452
  const publicClient = createPublicClient({ chain: ch, transport: http(args.rpcUrl) });
2349
- const erc20AllowanceAbi2 = parseAbi([
2453
+ const erc20AllowanceAbi3 = parseAbi([
2350
2454
  "function allowance(address owner, address spender) view returns (uint256)",
2351
2455
  "function decimals() view returns (uint8)"
2352
2456
  ]);
2353
- const erc20ApproveAbi = parseAbi(["function approve(address spender, uint256 amount) returns (bool)"]);
2457
+ const erc20ApproveAbi2 = parseAbi(["function approve(address spender, uint256 amount) returns (bool)"]);
2354
2458
  const steps = [];
2355
2459
  if (!isNativeIn) {
2356
2460
  const decimalsN = await publicClient.readContract({
2357
2461
  address: tokenIn,
2358
- abi: erc20AllowanceAbi2,
2462
+ abi: erc20AllowanceAbi3,
2359
2463
  functionName: "decimals"
2360
2464
  });
2361
2465
  const amountWei = parseUnits(args.amountHuman, Number(decimalsN));
2362
2466
  const currentAllowance = await publicClient.readContract({
2363
2467
  address: tokenIn,
2364
- abi: erc20AllowanceAbi2,
2468
+ abi: erc20AllowanceAbi3,
2365
2469
  functionName: "allowance",
2366
2470
  args: [executor, routerAddr]
2367
2471
  });
2368
2472
  if (currentAllowance < amountWei) {
2369
2473
  if (currentAllowance > 0n) {
2370
2474
  const dataReset = encodeFunctionData({
2371
- abi: erc20ApproveAbi,
2475
+ abi: erc20ApproveAbi2,
2372
2476
  functionName: "approve",
2373
2477
  args: [routerAddr, 0n]
2374
2478
  });
2375
2479
  steps.push({ to: tokenIn, data: dataReset, value: 0n, kind: "approve", fallbackGas: 80000n });
2376
2480
  }
2377
2481
  const dataApprove = encodeFunctionData({
2378
- abi: erc20ApproveAbi,
2482
+ abi: erc20ApproveAbi2,
2379
2483
  functionName: "approve",
2380
2484
  args: [routerAddr, amountWei]
2381
2485
  });
@@ -2512,6 +2616,6 @@ var curveDao = {
2512
2616
  registerProtocolModule(uniswapV4ProtocolModule);
2513
2617
  registerProtocolModule(curveDaoProtocolModule);
2514
2618
 
2515
- export { COINGECKO_PLATFORM_BY_CHAIN_ID, NEAR_CHAIN_CATEGORY, SOLANA_CHAIN_CATEGORY, buildEvmMultisignBatch, coingeckoPlatformForChainId, coreChainCategoryModule, curveDao, curveDaoProtocolModule, evmChainCategoryModule, finalizeMultisign, getAaveGraphqlProxyUrl, getActionsByChainCategory, getCoingeckoProxyUrl, getEulerGraphqlProxyUrl, getMapleGraphqlProxyUrl, getMorphoGraphqlProxyUrl, getProtocolModule, getProtocolModules, isEvmNativeToken, matchEvmTokenKind, mergePurposeText, nearChainCategoryModule, normalizeCurveRouterAmountString, normalizeHumanDecimalAmount, parseEvmChainIdToNumber, postJsonViaOptionalProxy, registerProtocolModule, routerSwapGasLimitFromEstimate, setAaveGraphqlProxyUrl, setCoingeckoProxyUrl, setEulerGraphqlProxyUrl, setMapleGraphqlProxyUrl, setMorphoGraphqlProxyUrl, solanaChainCategoryModule, uniswapV4, uniswapV4ProtocolModule };
2619
+ export { COINGECKO_PLATFORM_BY_CHAIN_ID, NEAR_CHAIN_CATEGORY, SOLANA_CHAIN_CATEGORY, approveStepsToEvmTxSteps, buildConditionalErc20ApproveSteps, buildEvmMultisignBatch, coingeckoPlatformForChainId, coreChainCategoryModule, curveDao, curveDaoProtocolModule, evmChainCategoryModule, finalizeMultisign, getAaveGraphqlProxyUrl, getActionsByChainCategory, getCoingeckoProxyUrl, getEulerGraphqlProxyUrl, getMapleGraphqlProxyUrl, getMorphoGraphqlProxyUrl, getProtocolModule, getProtocolModules, isEvmNativeToken, matchEvmTokenKind, mergePurposeText, nearChainCategoryModule, normalizeCurveRouterAmountString, normalizeHumanDecimalAmount, parseEvmChainIdToNumber, postJsonViaOptionalProxy, publicClientForRpc, registerProtocolModule, resolveErc20Decimals, routerSwapGasLimitFromEstimate, setAaveGraphqlProxyUrl, setCoingeckoProxyUrl, setEulerGraphqlProxyUrl, setMapleGraphqlProxyUrl, setMorphoGraphqlProxyUrl, solanaChainCategoryModule, uniswapV4, uniswapV4ProtocolModule };
2516
2620
  //# sourceMappingURL=index.js.map
2517
2621
  //# sourceMappingURL=index.js.map