使用Uniswap V3的DCA
中级
这是一个Crypto Trading, Multimodal AI领域的自动化工作流,包含 14 个节点。主要使用 Code, OneShot, Telegram, OneShotSynch, ScheduleTrigger 等节点。 在Uniswap V3和1Shot API上使用美元成本平均法自动化代币购买
前置要求
- •Telegram Bot Token
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
"id": "maYy0i7nYbJwepaQ",
"meta": {
"instanceId": "62f017ec8f130d172e2e5f39bbf09515036bfd403dfa60fe06f5ab14b78705d0",
"templateCredsSetupCompleted": true
},
"name": "使用 Uniswap V3 的 DCA",
"tags": [],
"nodes": [
{
"id": "f43b592f-1203-47b8-9dac-61e8a9d3c9d3",
"name": "计算 TWAP",
"type": "n8n-nodes-base.code",
"position": [
432,
160
],
"parameters": {
"jsCode": "// Constants\nconst MaxUint256 = (1n << 256n) - 1n;\nconst Q32 = 1n << 32n;\nconst ZERO = 0n;\nconst ONE = 1n;\n\nfunction mulShift(val, mulBy) {\n return (val * BigInt(mulBy)) >> 128n;\n}\n\n/**\n * Returns the sqrt ratio as a Q64.96 for the given tick.\n * The sqrt ratio is computed as sqrt(1.0001)^tick\n * @param {number} tick the tick for which to compute the sqrt ratio\n */\nfunction getSqrtRatioAtTick(tick) {\n if (!Number.isInteger(tick)) throw new Error(\"Tick must be integer\");\n const MIN_TICK = -887272;\n const MAX_TICK = 887272;\n\n if (tick < MIN_TICK || tick > MAX_TICK) throw new Error(\"Tick out of bounds\");\n\n const absTick = tick < 0 ? -tick : tick;\n\n let ratio =\n (absTick & 0x1) != 0\n ? 0xfffcb933bd6fad37aa2d162d1a594001n\n : 0x100000000000000000000000000000000n;\n if ((absTick & 0x2) != 0) ratio = mulShift(ratio, 0xfff97272373d413259a46990580e213an);\n if ((absTick & 0x4) != 0) ratio = mulShift(ratio, 0xfff2e50f5f656932ef12357cf3c7fdccn);\n if ((absTick & 0x8) != 0) ratio = mulShift(ratio, 0xffe5caca7e10e4e61c3624eaa0941cd0n);\n if ((absTick & 0x10) != 0) ratio = mulShift(ratio, 0xffcb9843d60f6159c9db58835c926644n);\n if ((absTick & 0x20) != 0) ratio = mulShift(ratio, 0xff973b41fa98c081472e6896dfb254c0n);\n if ((absTick & 0x40) != 0) ratio = mulShift(ratio, 0xff2ea16466c96a3843ec78b326b52861n);\n if ((absTick & 0x80) != 0) ratio = mulShift(ratio, 0xfe5dee046a99a2a811c461f1969c3053n);\n if ((absTick & 0x100) != 0) ratio = mulShift(ratio, 0xfcbe86c7900a88aedcffc83b479aa3a4n);\n if ((absTick & 0x200) != 0) ratio = mulShift(ratio, 0xf987a7253ac413176f2b074cf7815e54n);\n if ((absTick & 0x400) != 0) ratio = mulShift(ratio, 0xf3392b0822b70005940c7a398e4b70f3n);\n if ((absTick & 0x800) != 0) ratio = mulShift(ratio, 0xe7159475a2c29b7443b29c7fa6e889d9n);\n if ((absTick & 0x1000) != 0) ratio = mulShift(ratio, 0xd097f3bdfd2022b8845ad8f792aa5825n);\n if ((absTick & 0x2000) != 0) ratio = mulShift(ratio, 0xa9f746462d870fdf8a65dc1f90e061e5n);\n if ((absTick & 0x4000) != 0) ratio = mulShift(ratio, 0x70d869a156d2a1b890bb3df62baf32f7n);\n if ((absTick & 0x8000) != 0) ratio = mulShift(ratio, 0x31be135f97d08fd981231505542fcfa6n);\n if ((absTick & 0x10000) != 0) ratio = mulShift(ratio, 0x9aa508b5b7a84e1c677de54f3e99bc9n);\n if ((absTick & 0x20000) != 0) ratio = mulShift(ratio, 0x5d6af8dedb81196699c329225ee604n);\n if ((absTick & 0x40000) != 0) ratio = mulShift(ratio, 0x2216e584f5fa1ea926041bedfe98n);\n if ((absTick & 0x80000) != 0) ratio = mulShift(ratio, 0x48a170391f7dc42444e8fa2n);\n\n if (tick > 0) {\n ratio = MaxUint256 / ratio;\n }\n\n // back to Q96\n return ratio % Q32 > 0n ? ratio / Q32 + ONE : ratio / Q32;\n}\n\nfunction getPriceFromSqrtPriceX96(sqrtPriceX96, decimals0, decimals1) {\n // Ensure input is BigInt\n const sqrtPrice = BigInt(sqrtPriceX96.toString());\n\n // (sqrtPriceX96 ^ 2)\n const numerator = sqrtPrice * sqrtPrice;\n\n // Denominator = 2^192\n const denominator = 1n << 192n;\n\n // Raw price ratio (token1 per token0, no decimals adjusted)\n let ratio = Number(numerator * 10n**18n / denominator) / 1e18; \n // (we scale by 1e18 to stay precise when converting to Number)\n\n // Adjust for token decimals\n const decimalFactor = 10 ** (decimals0 - decimals1);\n const price = ratio * decimalFactor;\n\n return price;\n}\n\nconst diffTickCumulative = parseInt($('Fetch Pool TWA Observations').first().json.response[0][0]) - parseInt($('Fetch Pool TWA Observations').first().json.response[0][1]);\nconst diffSecondsPerLIquidityX128 = parseInt($('Fetch Pool TWA Observations').first().json.response[1][0]) - parseInt($('Fetch Pool TWA Observations').first().json.response[1][1]);\nconst secondsBetween = parseInt($('Swap Configs').first().json.secondsAgo);\nconst secondsBetweenX128 = BigInt(secondsBetween) << BigInt(128);\nconst averageTick = parseInt(diffTickCumulative/secondsBetween);\n\nconst sqrtPriceX96After = $input.first().json.result.decodedData[1];\nconst priceAfter = getPriceFromSqrtPriceX96(sqrtPriceX96After, 6, 8);\n\nconst sqrtTWAPricex96 = getSqrtRatioAtTick(averageTick);\nconst TWAP = getPriceFromSqrtPriceX96(sqrtTWAPricex96, 6,8);\nconst TWAL = secondsBetweenX128 / BigInt(diffSecondsPerLIquidityX128);\n\nconsole.log(\"TWAP\", 1/TWAP);\nconsole.log(\"Price After: \", 1/priceAfter);\nconsole.log(\"TWAL\", TWAL);\n\n$input.first().json.twap = TWAP; \n$input.first().json.sqrtTWAPriceX96 = sqrtTWAPricex96; \n$input.first().json.twal = TWAL; \n$input.first().json.quotePriceAfter = priceAfter; \n\nreturn $input.all()\n"
},
"typeVersion": 2
},
{
"id": "bd180960-416b-48f8-b524-9841428a34e9",
"name": "获取池 TWA 观察数据",
"type": "n8n-nodes-1shot.oneShot",
"position": [
-16,
160
],
"parameters": {
"params": "={\n \"secondsAgos\": [\"0\",\"{{ $json.secondsAgo }}\"]\n} ",
"operation": "read",
"contractMethodId": "19aa2a88-2121-4727-b1af-e4922259b645"
},
"credentials": {
"oneShotOAuth2Api": {
"id": "nkfF9AitCKUCrErK",
"name": "1Shot account"
}
},
"typeVersion": 1
},
{
"id": "46184b6b-263e-4e14-9518-2515e2d311a3",
"name": "获取交换报价",
"type": "n8n-nodes-1shot.oneShot",
"position": [
208,
160
],
"parameters": {
"params": "={\n \"params\": \n {\n \"tokenIn\": \"{{ $('Swap Configs').item.json.token0 }}\",\n \"tokenOut\": \"{{ $('Swap Configs').item.json.token1 }}\",\n \"amountIn\": \"{{ $('Swap Configs').item.json.secondsAgo }}\",\n \"fee\": \"500\",\n \"sqrtPriceLimitX96\": \"0\"\n }\n} ",
"operation": "simulate",
"contractMethodId": "8de7d518-a62a-4a05-8f79-68808b6fd609"
},
"credentials": {
"oneShotOAuth2Api": {
"id": "nkfF9AitCKUCrErK",
"name": "1Shot account"
}
},
"typeVersion": 1
},
{
"id": "6b4d7900-7fae-4c1d-80cf-2900dfb4e299",
"name": "交换代币",
"type": "n8n-nodes-1shot.oneShotSynch",
"onError": "continueRegularOutput",
"position": [
880,
80
],
"parameters": {
"params": "={\n\"params\": {\n\"tokenIn\": \"{{ $('Swap Configs').item.json.token0 }}\",\n\"tokenOut\": \"{{ $('Swap Configs').item.json.token1 }}\",\n\"fee\": \"{{ $('Swap Configs').item.json.fee }}\",\n\"recipient\": \"{{ $('Swap Configs').item.json.delegator }}\",\n\"amountIn\": \"{{ $('Swap Configs').item.json.amountDCA }}\",\n\"amountOutMinimum\": \"0\",\n\"sqrtPriceLimitX96\": \"{{ $('Calculate TWAP').item.json.sqrtTWAPriceX96 }}\"\n}\n}",
"operation": "executeAsDelegator",
"additionalFields": {
"memo": "=DCA Swap for {{ $('Swap Configs').item.json.amountDCA }}, TWAP: {{ $('Calculate TWAP').item.json.twap }}"
},
"contractMethodId": "86616048-5330-452f-8637-58859dc872c6",
"delegatorWalletAddress": "={{ $('Swap Configs').item.json.delegator }}"
},
"credentials": {
"oneShotOAuth2Api": {
"id": "nkfF9AitCKUCrErK",
"name": "1Shot account"
}
},
"typeVersion": 1
},
{
"id": "a11c7481-97bf-4cee-8a3f-e604cd09236d",
"name": "计划触发器",
"type": "n8n-nodes-base.scheduleTrigger",
"position": [
-464,
160
],
"parameters": {
"rule": {
"interval": [
{
"field": "minutes",
"minutesInterval": 10
}
]
}
},
"typeVersion": 1.2
},
{
"id": "8c435f3f-72dc-4102-99bd-13cda1ea27c8",
"name": "失败详情",
"type": "n8n-nodes-base.telegram",
"position": [
1104,
256
],
"webhookId": "4bc3c459-fb40-44e5-a4b6-8565f2e92b62",
"parameters": {
"text": "=❌ Swap Failed",
"chatId": "5034284669",
"additionalFields": {}
},
"credentials": {
"telegramApi": {
"id": "uN6xtW1sUnA0WiMc",
"name": "@1shotdemobot"
}
},
"typeVersion": 1.2
},
{
"id": "d1512fe2-112f-48c0-8a98-cbbb08ab8f65",
"name": "便签",
"type": "n8n-nodes-base.stickyNote",
"position": [
-864,
224
],
"parameters": {
"width": 320,
"height": 224,
"content": "## 设置您的 DCA 计划"
},
"typeVersion": 1
},
{
"id": "4f6661d0-978c-4554-97eb-f91519dd8c85",
"name": "便签1",
"type": "n8n-nodes-base.stickyNote",
"position": [
-528,
-160
],
"parameters": {
"width": 720,
"height": 256,
"content": "## DCA 配置"
},
"typeVersion": 1
},
{
"id": "4b46e786-07d6-4184-8a90-ff264686c075",
"name": "便签2",
"type": "n8n-nodes-base.stickyNote",
"position": [
-128,
352
],
"parameters": {
"width": 672,
"height": 288,
"content": "## 连接到您的 1Shot API 账户"
},
"typeVersion": 1
},
{
"id": "bb585dd2-901b-4bb3-b822-5ddf3ad80be7",
"name": "授予路由器批准",
"type": "n8n-nodes-1shot.oneShotSynch",
"onError": "continueRegularOutput",
"position": [
656,
160
],
"parameters": {
"params": "={\n \"spender\": \"{{ $('Swap Configs').item.json.router }}\", \n \"value\": \"{{ $('Swap Configs').item.json.amountDCA }}\"\n} ",
"operation": "executeAsDelegator",
"additionalFields": {
"memo": "=DCA Approve for {{ $('Swap Configs').item.json.amountDCA }}"
},
"contractMethodId": "b2c3382b-37d0-4d49-b5d2-d1c6e8770658",
"delegatorWalletAddress": "={{ $('Swap Configs').item.json.delegator }}"
},
"credentials": {
"oneShotOAuth2Api": {
"id": "nkfF9AitCKUCrErK",
"name": "1Shot account"
}
},
"typeVersion": 1
},
{
"id": "792928fb-59e5-44ba-a6b9-9f388c74f02a",
"name": "便签3",
"type": "n8n-nodes-base.stickyNote",
"position": [
1344,
224
],
"parameters": {
"width": 544,
"height": 176,
"content": "## Telegram 通知"
},
"typeVersion": 1
},
{
"id": "2edc9432-92f2-4feb-a44a-f6d6f5608db1",
"name": "成功详情",
"type": "n8n-nodes-base.telegram",
"position": [
1328,
32
],
"webhookId": "4bc3c459-fb40-44e5-a4b6-8565f2e92b62",
"parameters": {
"text": "=✅ Swapped `{{ $('Swap Configs').item.json.amountDCA }}` `{{ $('Swap Configs').item.json.token0 }}` for {{ $('Get Swap Qoute').item.json.result.decodedData[0] }} {{ $('Swap Configs').item.json.token1 }}. \n\nThe `{{ $('Swap Configs').item.json.secondsAgo }}` second TWAP was `{{ $('Calculate TWAP').item.json.twap }}`. Your tx hash is `{{ $('Swap Tokens').item.json.transactionHash }}`.\n\nYou have {{ $json.response }} of token `{{ $('Swap Configs').item.json.token0 }}` left. ",
"chatId": "5034284669",
"additionalFields": {}
},
"credentials": {
"telegramApi": {
"id": "uN6xtW1sUnA0WiMc",
"name": "@1shotdemobot"
}
},
"typeVersion": 1.2
},
{
"id": "3147ded9-fc81-4d00-b230-c313c0c3501a",
"name": "获取剩余 DCA 资金余额",
"type": "n8n-nodes-1shot.oneShot",
"position": [
1104,
32
],
"parameters": {
"params": "={\n \"account\": \"{{ $('Swap Configs').item.json.delegator }}\"\n}",
"operation": "read",
"contractMethodId": "5fa806d3-e891-43de-b834-38f82dae653e"
},
"credentials": {
"oneShotOAuth2Api": {
"id": "nkfF9AitCKUCrErK",
"name": "1Shot account"
}
},
"typeVersion": 1
},
{
"id": "4d7da856-78f7-4553-a9f7-2a372688712b",
"name": "交换配置",
"type": "n8n-nodes-base.code",
"position": [
-240,
160
],
"parameters": {
"jsCode": "const secondsAgo = 120; // the size of your TWAP window\nconst amountDCA = 100000; // amount to swap each time\nconst delegator = \"0x9fead8b19c044c2f404dac38b925ea16adaa2954\"; // your delegated wallet address.\nconst router = \"0x3bFA4769FB09eefC5a80d6E87c3B9C650f7Ae48E\"; // the uniswap SwapRouterV2\nconst token0 = \"0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238\"; // token0 of you r target pool\nconst token1 = \"0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14\"; // token1 of your target pool\nconst fee = 500; // the fee of your target pool\n\n$input.first().json.secondsAgo = secondsAgo;\n$input.first().json.amountDCA = amountDCA;\n$input.first().json.delegator = delegator;\n$input.first().json.router = router;\n$input.first().json.token0 = token0;\n$input.first().json.token1 = token1;\n$input.first().json.fee = fee;\n\nreturn $input.all();\n"
},
"typeVersion": 2
}
],
"active": false,
"pinData": {},
"settings": {
"executionOrder": "v1"
},
"versionId": "8c4f8d71-a91f-4481-a83b-17404b73e600",
"connections": {
"Swap Tokens": {
"main": [
[
{
"node": "Get Remaining DCA Funds Balance",
"type": "main",
"index": 0
}
],
[
{
"node": "Failure Details",
"type": "main",
"index": 0
}
]
]
},
"Swap Configs": {
"main": [
[
{
"node": "Fetch Pool TWA Observations",
"type": "main",
"index": 0
}
]
]
},
"Calculate TWAP": {
"main": [
[
{
"node": "Give Approval to Router",
"type": "main",
"index": 0
}
]
]
},
"Get Swap Qoute": {
"main": [
[
{
"node": "Calculate TWAP",
"type": "main",
"index": 0
}
]
]
},
"Schedule Trigger": {
"main": [
[
{
"node": "Swap Configs",
"type": "main",
"index": 0
}
]
]
},
"Give Approval to Router": {
"main": [
[
{
"node": "Swap Tokens",
"type": "main",
"index": 0
}
],
[
{
"node": "Failure Details",
"type": "main",
"index": 0
}
]
]
},
"Fetch Pool TWA Observations": {
"main": [
[
{
"node": "Get Swap Qoute",
"type": "main",
"index": 0
}
]
]
},
"Get Remaining DCA Funds Balance": {
"main": [
[
{
"node": "Success Details",
"type": "main",
"index": 0
}
]
]
}
}
}常见问题
如何使用这个工作流?
复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。
这个工作流适合什么场景?
中级 - 加密货币交易, 多模态 AI
需要付费吗?
本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。
相关工作流推荐
Aave收益优化(无设置子流程)
通过1Shot API和Telegram自动化钱包与Aave之间的USDC余额管理
If
Code
Switch
+5
24 节点1Shot API
内容创作
Uniswap V3 区间交易者(第二版)
使用 Uniswap V3、Telegram 提醒和 MetaMask 委托的自动化区间交易
If
Code
Switch
+6
41 节点1Shot API
杂项
n8n支付协调器与1Shot API
使用x402和1Shot API创建自托管区块链支付处理器
If
Code
Webhook
+4
27 节点1Shot API
加密货币交易
多链1Shot加油站
基于Li.Fi的多链代币交换中继器
If
Code
Webhook
+5
22 节点1Shot API
加密货币交易
x402
使用x402支付协议和1Shot API实现工作流货币化
If
Code
Webhook
+4
15 节点1Shot API
加密货币交易
比特币和以太坊加密货币下跌警报(Telegram、Slack 和 SMS)
通过 Telegram、Slack 和 SMS 发送比特币和以太坊加密货币下跌警报
If
Code
Slack
+5
8 节点David Olusola
加密货币交易