8
n8n 中文网amn8n.com

Uniswap V3 区间交易者(第二版)

高级

这是一个Miscellaneous, Multimodal AI领域的自动化工作流,包含 41 个节点。主要使用 If, Code, Switch, OneShot, Telegram 等节点。 使用 Uniswap V3、Telegram 提醒和 MetaMask 委托的自动化区间交易

前置要求
  • Telegram Bot Token
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "jV4KrTuSUO48rJbF",
  "meta": {
    "instanceId": "62f017ec8f130d172e2e5f39bbf09515036bfd403dfa60fe06f5ab14b78705d0",
    "templateCredsSetupCompleted": true
  },
  "name": "Uniswap V3 区间交易者(第二版)",
  "tags": [],
  "nodes": [
    {
      "id": "bd963ac7-6b0f-4698-8b3b-d9fa205b5682",
      "name": "计算 TWAP",
      "type": "n8n-nodes-base.code",
      "position": [
        -2240,
        432
      ],
      "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 (tokenOut per tokenIn, 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\n\nconst sqrtTWAPricex96 = getSqrtRatioAtTick(averageTick);\nconst TWAP = getPriceFromSqrtPriceX96(sqrtTWAPricex96, $('Swap Configs').first().json.token0Decimals, $('Swap Configs').first().json.token1Decimals);\nconst TWAL = secondsBetweenX128 / BigInt(diffSecondsPerLIquidityX128);\n\n\n$input.first().json.twap = TWAP; \n$input.first().json.sqrtTWAPriceX96 = sqrtTWAPricex96; \n$input.first().json.twal = TWAL; \n\nreturn $input.all()\n"
      },
      "typeVersion": 2
    },
    {
      "id": "cfffec98-a206-4142-a15e-de7a37e12e4a",
      "name": "获取池 TWA 观察数据",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -2464,
        432
      ],
      "parameters": {
        "params": "={\n  \"secondsAgos\": [\"0\",\"{{ $json.secondsAgo }}\"]\n} ",
        "operation": "read",
        "contractMethodId": "98267bd2-13c9-4c27-bc86-ee2389da1025"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "4f6c68a7-5a83-4a5c-bf73-44a71ba4478a",
      "name": "计划触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -2912,
        432
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "8878368a-aac0-4714-a652-0b6544f192f2",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2864,
        -96
      ],
      "parameters": {
        "width": 800,
        "height": 464,
        "content": "## 交换配置"
      },
      "typeVersion": 1
    },
    {
      "id": "f6a1753f-8aca-4439-bc9d-c0f3a411e742",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1328,
        -352
      ],
      "parameters": {
        "width": 720,
        "height": 400,
        "content": "## 连接到您的 1Shot API 账户"
      },
      "typeVersion": 1
    },
    {
      "id": "c0fd0bc1-b1d2-4bed-8186-5afe8e0f667c",
      "name": "交换配置",
      "type": "n8n-nodes-base.code",
      "position": [
        -2688,
        432
      ],
      "parameters": {
        "jsCode": "const amountDCA = 200000; // amount to swap each time\nconst upperPrice = 4600; \nconst lowerPrice = 4500;\nconst delegator = \"0x9fead8b19c044c2f404dac38b925ea16adaa2954\"; // your delegated wallet address.\nconst telegramChatId = 5034284669; // the chat id between you and your Telegram bot\nconst router = \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\"; // the uniswap SwapRouterV2\nconst token0 = \"0x82aF49447D8a07e3bd95BD0d56f35241523fBab1\"; // The pool's token0\nconst token1 = \"0xaf88d065e77c8cC2239327C5EDb3A432268e5831\"; // The pool's token1\nconst token0Decimals = 18; // WETH has 18 decimals\nconst token1Decimals = 6; // USDC has 6 decimals\nconst fee = 500; // the fee of your target pool, most pools have a fee of 500\nconst slippage = 0.025 // the amount the price can move by from quote to swap\nconst secondsAgo = 120; // the size of your TWAP window\n\n$input.first().json.amountDCA = amountDCA;\n$input.first().json.upperPrice = upperPrice;\n$input.first().json.lowerPrice = lowerPrice;\n$input.first().json.telegramChatId = telegramChatId;\n$input.first().json.delegator = delegator;\n$input.first().json.router = router;\n$input.first().json.token0 = token0;\n$input.first().json.token0Decimals = token0Decimals;\n$input.first().json.token1 = token1;\n$input.first().json.token1Decimals = token1Decimals;\n$input.first().json.fee = fee;\n$input.first().json.slippage = slippage;\n$input.first().json.secondsAgo = secondsAgo;\n\nreturn $input.all();\n"
      },
      "typeVersion": 2
    },
    {
      "id": "249a6828-7ea8-4baf-9b7b-b19be57d1fe8",
      "name": "便签说明4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2080,
        1216
      ],
      "parameters": {
        "width": 800,
        "content": "## 设置 1Shot API 钱包并导入所需方法:"
      },
      "typeVersion": 1
    },
    {
      "id": "326fb0a3-a3f8-4b8d-ab25-76df36b7b05c",
      "name": "便签说明5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3104,
        784
      ],
      "parameters": {
        "width": 768,
        "height": 560,
        "content": "## YouTube 教程"
      },
      "typeVersion": 1
    },
    {
      "id": "a6ea7892-1392-42c1-83a6-b0946bc5e1d4",
      "name": "买入、卖出或持有",
      "type": "n8n-nodes-base.switch",
      "position": [
        -1568,
        432
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Sell ETH",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "loose"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "0080b3b6-9666-4cae-bb60-a157a3bc31c8",
                    "operator": {
                      "type": "number",
                      "operation": "gt"
                    },
                    "leftValue": "={{ $('Calculate TWAP').item.json.twap }}",
                    "rightValue": "={{ $('Swap Configs').item.json.upperPrice }}"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Buy ETH",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "loose"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "6bb3a399-cb6e-4d37-a402-867dfb857cf1",
                    "operator": {
                      "type": "number",
                      "operation": "lt"
                    },
                    "leftValue": "={{ $('Calculate TWAP').item.json.twap }}",
                    "rightValue": "={{ $('Swap Configs').item.json.lowerPrice }}"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {},
        "looseTypeValidation": true
      },
      "typeVersion": 3.2
    },
    {
      "id": "56b4b80a-1ea1-447f-904d-5b2a8de618b7",
      "name": "确认买入",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -896,
        656
      ],
      "webhookId": "24e7b950-a7a3-4c3b-b0a2-45af4bac4f2f",
      "parameters": {
        "chatId": "={{ $('Swap Configs').item.json.telegramChatId }}",
        "message": "=Purchase {{ parseFloat($json.result.decodedData[0]) / 1e18 }} ETH at ${{ $('Calculate TWAP').item.json.twap }}?",
        "options": {
          "limitWaitTime": {
            "values": {
              "resumeUnit": "minutes"
            }
          }
        },
        "operation": "sendAndWait",
        "formFields": {
          "values": [
            {
              "fieldType": "dropdown",
              "fieldLabel": "Confirm",
              "fieldOptions": {
                "values": [
                  {
                    "option": "yes"
                  },
                  {
                    "option": "no"
                  }
                ]
              },
              "requiredField": true
            }
          ]
        },
        "responseType": "customForm"
      },
      "credentials": {
        "telegramApi": {
          "id": "uN6xtW1sUnA0WiMc",
          "name": "@1shotdemobot"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "36198d4a-3ca6-4b77-ba78-d55e548531e2",
      "name": "购买已取消",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -448,
        752
      ],
      "webhookId": "c33c27ed-e732-4335-ac10-ebc28527e517",
      "parameters": {
        "text": "Purchase cancelled",
        "chatId": "={{ $('Swap Configs').item.json.telegramChatId }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "uN6xtW1sUnA0WiMc",
          "name": "@1shotdemobot"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "77f6f92f-878c-4409-b88c-85b28494b493",
      "name": "买入还是取消?",
      "type": "n8n-nodes-base.if",
      "position": [
        -672,
        656
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "f021075c-1415-4cf9-825f-eedc01e0a369",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.data.Confirm }}",
              "rightValue": "no"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "7e9043b3-d298-49e5-9dce-fccb6dd903c6",
      "name": "检查剩余资金",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        0,
        432
      ],
      "parameters": {
        "params": "={\n  \"account\": \"{{ $('Swap Configs').item.json.delegator }}\"\n}",
        "operation": "read",
        "contractMethodId": "bfc44279-e20b-4452-b809-d422134700b3"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7577e612-bcf6-4a4b-b178-125e58188d9d",
      "name": "确认卖出",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -896,
        224
      ],
      "webhookId": "24e7b950-a7a3-4c3b-b0a2-45af4bac4f2f",
      "parameters": {
        "chatId": "={{ $('Swap Configs').item.json.telegramChatId }}",
        "message": "=Purchase {{ parseFloat($json.result.decodedData[0]) / 1e18 }} ETH at ${{ $('Calculate TWAP').item.json.twap }}?",
        "options": {
          "limitWaitTime": {
            "values": {
              "resumeUnit": "minutes"
            }
          }
        },
        "operation": "sendAndWait",
        "formFields": {
          "values": [
            {
              "fieldType": "dropdown",
              "fieldLabel": "Confirm",
              "fieldOptions": {
                "values": [
                  {
                    "option": "yes"
                  },
                  {
                    "option": "no"
                  }
                ]
              },
              "requiredField": true
            }
          ]
        },
        "responseType": "customForm"
      },
      "credentials": {
        "telegramApi": {
          "id": "uN6xtW1sUnA0WiMc",
          "name": "@1shotdemobot"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "b0a823d0-d17c-4566-add2-d2d3602e359b",
      "name": "卖出还是取消?",
      "type": "n8n-nodes-base.if",
      "position": [
        -672,
        224
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "f021075c-1415-4cf9-825f-eedc01e0a369",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.data.Confirm }}",
              "rightValue": "no"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "f9ce9b66-c489-4071-9178-98d70234901d",
      "name": "卖出已取消",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -448,
        336
      ],
      "webhookId": "c33c27ed-e732-4335-ac10-ebc28527e517",
      "parameters": {
        "text": "Purchase cancelled",
        "chatId": "={{ $('Swap Configs').item.json.telegramChatId }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "uN6xtW1sUnA0WiMc",
          "name": "@1shotdemobot"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "c3031a2c-dd9b-4dcb-be1d-fe71cfe15745",
      "name": "授予路由器批准(卖出)",
      "type": "n8n-nodes-1shot.oneShotSynch",
      "onError": "continueRegularOutput",
      "position": [
        -448,
        128
      ],
      "parameters": {
        "params": "={\n  \"spender\": \"{{ $('Swap Configs').item.json.router }}\", \n  \"amount\": \"{{ $('Parse Last Trade').item.json.memo.amoutOut }}\"\n} ",
        "operation": "executeAsDelegator",
        "additionalFields": {
          "memo": "=Range Trade Approve for {{ $('Swap Configs').item.json.amountDCA }} {{ $('Swap Configs').item.json.token0 }}"
        },
        "contractMethodId": "fdd46f58-7871-42e8-870a-cd8736b0d8a3",
        "delegatorWalletAddress": "={{ $('Swap Configs').item.json.delegator }}"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7dbf11c3-106e-4592-bb5e-d2f2b4a9c658",
      "name": "授予路由器批准(买入)",
      "type": "n8n-nodes-1shot.oneShotSynch",
      "onError": "continueRegularOutput",
      "position": [
        -448,
        560
      ],
      "parameters": {
        "params": "={\n  \"spender\": \"{{ $('Swap Configs').item.json.router }}\", \n  \"value\": \"{{ $('Swap Configs').item.json.amountDCA }}\"\n} ",
        "operation": "executeAsDelegator",
        "additionalFields": {
          "memo": "=Range Trade Approve for {{ $('Swap Configs').item.json.amountDCA }} {{ $('Swap Configs').item.json.token1 }}"
        },
        "contractMethodId": "e35f72f2-6b5b-43af-9382-60458448f32c",
        "delegatorWalletAddress": "={{ $('Swap Configs').item.json.delegator }}"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "df982398-d661-4352-b944-2f8bcb748108",
      "name": "买入 ETH",
      "type": "n8n-nodes-1shot.oneShotSynch",
      "onError": "continueRegularOutput",
      "position": [
        -224,
        480
      ],
      "parameters": {
        "params": "={\n\"params\": {\n\"tokenIn\": \"{{ $('Swap Configs').item.json.token1 }}\",\n\"tokenOut\": \"{{ $('Swap Configs').item.json.token0 }}\",\n\"fee\": \"{{ $('Swap Configs').item.json.fee }}\",\n\"recipient\": \"{{ $('Swap Configs').item.json.delegator }}\",\n\"amountIn\": \"{{ $('Swap Configs').item.json.amountDCA }}\",\n\"amountOutMinimum\": \"{{ Math.ceil(parseFloat($('Get Buy Qoute').item.json.result.decodedData[0]) * ( 1 - parseFloat($('Swap Configs').item.json.slippage))) }}\",\n\"sqrtPriceLimitX96\": \"0\"\n}\n}",
        "operation": "executeAsDelegator",
        "additionalFields": {
          "memo": "={\"rangeTrade\": \"buy\", \"amountIn\": {{ $('Swap Configs').item.json.amountDCA }}, \"amoutOut\": {{ $('Get Buy Qoute').item.json.result.decodedData[0] }}, \"TWAP\": {{ $('Calculate TWAP').item.json.twap }}}"
        },
        "contractMethodId": "ba3cf19f-52cf-4104-88e8-d9be40e23fe2",
        "delegatorWalletAddress": "={{ $('Swap Configs').item.json.delegator }}"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "fc00c558-1fda-425a-b2d6-011c5e847170",
      "name": "卖出 ETH",
      "type": "n8n-nodes-1shot.oneShotSynch",
      "onError": "continueRegularOutput",
      "position": [
        -224,
        48
      ],
      "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\": \"{{ $('Parse Last Trade').item.json.memo.amoutOut }}\",\n\"amountOutMinimum\": \"{{ Math.ceil(parseFloat($('Get Sell Qoute').item.json.result.decodedData[0]) * ( 1 - parseFloat($('Swap Configs').item.json.slippage))) }}\",\n\"sqrtPriceLimitX96\": \"0\"\n}\n}",
        "operation": "executeAsDelegator",
        "additionalFields": {
          "memo": "={\"rangeTrade\": \"sell\", \"amountIn\": {{ $('Parse Last Trade').item.json.memo.amoutOut }}, \"amoutOut\": {{ $('Get Sell Qoute').item.json.result.decodedData[0] }}, \"TWAP\": {{ $('Calculate TWAP').item.json.twap }}}"
        },
        "contractMethodId": "ba3cf19f-52cf-4104-88e8-d9be40e23fe2",
        "delegatorWalletAddress": "={{ $('Swap Configs').item.json.delegator }}"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "be662cb9-f5f2-4242-8139-3a81a32bdd80",
      "name": "成功详情(卖出)",
      "type": "n8n-nodes-base.telegram",
      "position": [
        224,
        0
      ],
      "webhookId": "4bc3c459-fb40-44e5-a4b6-8565f2e92b62",
      "parameters": {
        "text": "=✅ Swapped `{{ $('Parse Last Trade').item.json.memo.amoutOut }}` `{{ $('Swap Configs').item.json.token0 }}` for `{{ $('Get Sell 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 `{{ $('Sell ETH').item.json.transactionHash }}`.\n\nYou have `{{ $json.response }}` of token `{{ $('Swap Configs').item.json.token0 }}` left. ",
        "chatId": "={{ $('Swap Configs').item.json.telegramChatId }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "uN6xtW1sUnA0WiMc",
          "name": "@1shotdemobot"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "a46a0307-e7e3-4d64-9425-cfced93bd3ec",
      "name": "成功详情(买入)",
      "type": "n8n-nodes-base.telegram",
      "position": [
        224,
        432
      ],
      "webhookId": "4bc3c459-fb40-44e5-a4b6-8565f2e92b62",
      "parameters": {
        "text": "=✅ Swapped `{{ $('Swap Configs').item.json.amountDCA }}` `{{ $('Swap Configs').item.json.token0 }}` for `{{ $('Get Buy 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 `{{ $('Buy ETH').item.json.transactionHash }}`.\n\nYou have `{{ $json.response }}` of token `{{ $('Swap Configs').item.json.token0 }}` left. ",
        "chatId": "={{ $('Swap Configs').item.json.telegramChatId }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "uN6xtW1sUnA0WiMc",
          "name": "@1shotdemobot"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "39c1bc27-e563-4d9c-839a-cc740d72dc6e",
      "name": "买入失败通知",
      "type": "n8n-nodes-base.telegram",
      "position": [
        0,
        656
      ],
      "webhookId": "4bc3c459-fb40-44e5-a4b6-8565f2e92b62",
      "parameters": {
        "text": "=❌ Swap Failed",
        "chatId": "={{ $('Swap Configs').item.json.telegramChatId }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "uN6xtW1sUnA0WiMc",
          "name": "@1shotdemobot"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "3510a402-98e3-439f-b388-83ffd08c89b7",
      "name": "卖出失败通知",
      "type": "n8n-nodes-base.telegram",
      "position": [
        0,
        224
      ],
      "webhookId": "4bc3c459-fb40-44e5-a4b6-8565f2e92b62",
      "parameters": {
        "text": "=❌ Swap Failed",
        "chatId": "={{ $('Swap Configs').item.json.telegramChatId }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "uN6xtW1sUnA0WiMc",
          "name": "@1shotdemobot"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "167776fb-38c3-43d5-8faf-4cdec052eccd",
      "name": "检查新资金",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        0,
        0
      ],
      "parameters": {
        "params": "={\n  \"account\": \"{{ $('Swap Configs').item.json.delegator }}\"\n}",
        "operation": "read",
        "contractMethodId": "bfc44279-e20b-4452-b809-d422134700b3"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "229c3ea3-0719-439e-bd1c-18efa6709a92",
      "name": "获取买入报价",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -1120,
        656
      ],
      "parameters": {
        "params": "={\n  \"params\": \n    {\n  \"tokenIn\": \"{{ $('Swap Configs').item.json.token1 }}\",\n  \"tokenOut\": \"{{ $('Swap Configs').item.json.token0 }}\",\n  \"amountIn\": \"{{ $('Swap Configs').item.json.amountDCA }}\",\n  \"fee\": \"{{ $('Swap Configs').item.json.fee }}\",\n  \"sqrtPriceLimitX96\": \"0\"\n    }\n} ",
        "operation": "simulate",
        "contractMethodId": "48b96850-aea3-4faf-87d5-4e67f2baee89"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "d42e68cf-6315-4ba1-9ac9-9b208c2aef05",
      "name": "获取卖出报价",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -1120,
        224
      ],
      "parameters": {
        "params": "={\n  \"params\": \n    {\n  \"tokenIn\": \"{{ $('Swap Configs').item.json.token0 }}\",\n  \"tokenOut\": \"{{ $('Swap Configs').item.json.token1 }}\",\n  \"amountIn\": \"{{ $('Parse Last Trade').item.json.memo.amoutOut }}\",\n  \"fee\": \"{{ $('Swap Configs').item.json.fee }}\",\n  \"sqrtPriceLimitX96\": \"0\"\n    }\n} ",
        "operation": "simulate",
        "contractMethodId": "48b96850-aea3-4faf-87d5-4e67f2baee89"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "5000be28-4040-4c7e-b65b-7daeaec2da19",
      "name": "获取最后交易类型",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -2016,
        432
      ],
      "parameters": {
        "memo": "rangeTrade",
        "status": "Completed",
        "chainId": 42161,
        "pageSize": 1,
        "resource": "transactions",
        "createdAfter": 1757701327,
        "createdBefore": 0
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "76fcda42-9808-4332-8092-28b14de4d0c3",
      "name": "解析最后交易",
      "type": "n8n-nodes-base.code",
      "position": [
        -1792,
        432
      ],
      "parameters": {
        "jsCode": "try {\n  const memo = JSON.parse($input.first().json.memo)\n  console.log(\"memo:\", memo)\n  $input.first().json.memo = memo; \n} catch {\n  console.log(\"no memo\")\n}\n\nreturn $input.all();"
      },
      "typeVersion": 2
    },
    {
      "id": "1395fd6a-ae0c-481f-bf29-449a5ac3bb79",
      "name": "不重复买入",
      "type": "n8n-nodes-base.if",
      "position": [
        -1344,
        656
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "id": "8f1a4e9c-21d1-499b-8221-318875775842",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.memo.rangeTrade }}",
              "rightValue": "sell"
            },
            {
              "id": "7bee6833-7f7e-442e-aef2-99ccb83e34bb",
              "operator": {
                "type": "object",
                "operation": "notExists",
                "singleValue": true
              },
              "leftValue": "={{ $json.memo }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2,
      "alwaysOutputData": true
    },
    {
      "id": "a5b4d19e-973b-474b-8e61-bb7147e7fe59",
      "name": "不重复卖出",
      "type": "n8n-nodes-base.if",
      "position": [
        -1344,
        224
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "8f1a4e9c-21d1-499b-8221-318875775842",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.memo.rangeTrade }}",
              "rightValue": "buy"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "055a9f8b-8d73-49fe-bd12-8292c306cd6b",
      "name": "列出钱包",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -2000,
        1440
      ],
      "parameters": {
        "chainId": 42161,
        "resource": "wallets"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "98655cd2-a117-4157-aa39-0eb3e4fdbf38",
      "name": "当点击\"执行工作流\"时",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -2224,
        1440
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "fd15c4bd-83e7-451c-9571-b76e29341bc0",
      "name": "确保 WETH 方法",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -1104,
        1056
      ],
      "parameters": {
        "chainId": 42161,
        "promptId": "a9bac97d-cf4d-4de0-8523-bad09b6b507d",
        "walletId": "={{ $('Get Wallet ID').item.json.walletId }}",
        "operation": "assureContractMethodsFromPrompt",
        "contractAddress": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "237672b9-330d-4040-bff7-ec4d65923654",
      "name": "确保 USDC 方法",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -1104,
        1440
      ],
      "parameters": {
        "chainId": 42161,
        "promptId": "7d92d5db-99e3-4b1e-9cfe-74e80eeb842b",
        "walletId": "={{ $('Get Wallet ID').item.json.walletId }}",
        "operation": "assureContractMethodsFromPrompt",
        "contractAddress": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a93fcdc6-8c97-4ffb-b2c2-34941a630703",
      "name": "确保 QuoterV2 方法",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -1104,
        1824
      ],
      "parameters": {
        "chainId": 42161,
        "promptId": "e439252a-a9e9-43d8-bec8-f4ce3391c49d",
        "walletId": "={{ $('Get Wallet ID').item.json.walletId }}",
        "operation": "assureContractMethodsFromPrompt",
        "contractAddress": "0x61fFE014bA17989E743c5F6cB21bF9697530B21e"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "d8c27145-4ef5-4841-b836-d299054ea3b0",
      "name": "确保 SwapRouter02 方法",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -1104,
        1248
      ],
      "parameters": {
        "chainId": 42161,
        "promptId": "4fa14406-c913-4642-991d-e310ff138a8a",
        "walletId": "={{ $('Get Wallet ID').item.json.walletId }}",
        "operation": "assureContractMethodsFromPrompt",
        "contractAddress": "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "acc5304d-a360-4cd7-b0a0-78b94ccb8b8a",
      "name": "确保 ETH/USDC 池方法",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -1104,
        1632
      ],
      "parameters": {
        "chainId": 42161,
        "promptId": "9a97c00a-797a-4dc9-9a3f-e32596cf9301",
        "walletId": "={{ $('Get Wallet ID').item.json.walletId }}",
        "operation": "assureContractMethodsFromPrompt",
        "contractAddress": "0xC6962004f452bE9203591991D15f6b388e09E8D0"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "964a4547-2b84-45df-9e55-74b2e7da262b",
      "name": "条件判断",
      "type": "n8n-nodes-base.if",
      "position": [
        -1776,
        1440
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "b2f25fac-5a5d-4963-b1f6-05c8dfce25a6",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.id }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "2b75e779-ea57-487b-a2da-4313a1a290a2",
      "name": "创建钱包",
      "type": "n8n-nodes-1shot.oneShot",
      "position": [
        -1552,
        1520
      ],
      "parameters": {
        "name": "=\"{{ $today }}\"",
        "chainId": 42161,
        "resource": "wallets",
        "operation": "create",
        "description": "区间交易钱包"
      },
      "credentials": {
        "oneShotOAuth2Api": {
          "id": "nkfF9AitCKUCrErK",
          "name": "1Shot account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2490cf18-023a-4453-b5af-b6c525e940f9",
      "name": "获取钱包 ID",
      "type": "n8n-nodes-base.code",
      "position": [
        -1328,
        1440
      ],
      "parameters": {
        "jsCode": "let walletId; \ntry {\n  walletId = $input.first().json.id;\n} catch {\n  walletId = $input.first().json.id;\n}\n\n$input.first().json.walletId = walletId; \n\nreturn $input.all();"
      },
      "typeVersion": 2
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "3fa4c611-face-44b3-bfe2-1572d517e771",
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "Get Wallet ID",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create wallet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buy ETH": {
      "main": [
        [
          {
            "node": "Check Remaining Funds",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Buy Failure Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sell ETH": {
      "main": [
        [
          {
            "node": "Check New Funds",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Sell Failure Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Confirm Buy": {
      "main": [
        [
          {
            "node": "Buy or Cancel?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Confirm Sell": {
      "main": [
        [
          {
            "node": "Sell or Cancel?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List wallets": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Swap Configs": {
      "main": [
        [
          {
            "node": "Fetch Pool TWA Observations",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create wallet": {
      "main": [
        [
          {
            "node": "Get Wallet ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Buy Qoute": {
      "main": [
        [
          {
            "node": "Confirm Buy",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Wallet ID": {
      "main": [
        [
          {
            "node": "Assure WETH Methods",
            "type": "main",
            "index": 0
          },
          {
            "node": "Assure SwapRouter02 Methods",
            "type": "main",
            "index": 0
          },
          {
            "node": "Assure USDC Methods",
            "type": "main",
            "index": 0
          },
          {
            "node": "Assure ETH/USDC Pool Methods",
            "type": "main",
            "index": 0
          },
          {
            "node": "Assure QuoterV2 Methods",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buy or Cancel?": {
      "main": [
        [
          {
            "node": "Give Approval to Router (Buy)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Purchase Cancelled",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate TWAP": {
      "main": [
        [
          {
            "node": "Get Last Trade Type",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Sell Qoute": {
      "main": [
        [
          {
            "node": "Confirm Sell",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check New Funds": {
      "main": [
        [
          {
            "node": "Success Details (Sell)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sell or Cancel?": {
      "main": [
        [
          {
            "node": "Give Approval to Router (Sell)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Sell Cancelled",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Last Trade": {
      "main": [
        [
          {
            "node": "Buy, Sell or Hold",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Swap Configs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buy, Sell or Hold": {
      "main": [
        [
          {
            "node": "Don't Repeat Sells",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Don't Repeat Buys",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Don't Repeat Buys": {
      "main": [
        [
          {
            "node": "Get Buy Qoute",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Don't Repeat Sells": {
      "main": [
        [
          {
            "node": "Get Sell Qoute",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assure USDC Methods": {
      "main": [
        []
      ]
    },
    "Assure WETH Methods": {
      "main": [
        []
      ]
    },
    "Get Last Trade Type": {
      "main": [
        [
          {
            "node": "Parse Last Trade",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Remaining Funds": {
      "main": [
        [
          {
            "node": "Success Details (Buy)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assure QuoterV2 Methods": {
      "main": [
        []
      ]
    },
    "Fetch Pool TWA Observations": {
      "main": [
        [
          {
            "node": "Calculate TWAP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assure ETH/USDC Pool Methods": {
      "main": [
        []
      ]
    },
    "Give Approval to Router (Buy)": {
      "main": [
        [
          {
            "node": "Buy ETH",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Buy Failure Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Give Approval to Router (Sell)": {
      "main": [
        [
          {
            "node": "Sell ETH",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Sell Failure Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Execute workflow’": {
      "main": [
        [
          {
            "node": "List wallets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 杂项, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量41
分类2
节点类型9
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
1Shot API

1Shot API

@oneshotapi

1Shot API lets you bring onchain actions into your n8n workflows.

外部链接
在 n8n.io 查看

分享此工作流