n8n用客製節點,自動執行區塊鏈轉賬

n8n用客製節點,自動執行區塊鏈轉賬

你有沒有想過

存放在區塊鏈上的錢,能夠自動轉出!?

現在用 n8n 就能做到了

那要怎麼做呢?

首先假設這筆錢,不存放在交易所,不然直接用交易所的 API 就好了~
而是持有私鑰的地址,有著一筆錢。

現在私鑰有了。什麼情況要轉帳呢?

最有可能是某個新聞消息。誰又中彈、誰被抓去關等等......

我們可以假設是從 RSS 來的,因為新聞不只一筆,所以要逐筆處理。
然後我們可以透過 AI 去判斷是不是我們指定的相關新聞,例如:哪個政治人物怎樣。
透過 Switch 把相關事件,交給我們今天的主角「客製化的節點」。

製作客製化節點(Node)

要製作客製化節點,首先我們可以去 GitHub 把官方範例下載下來 https://github.com/n8n-io/n8n-nodes-starter
然後放到 n8n 的使用者目錄中。預設是在 ~/n8n,我們在裡面建立 custom/node_modules/

你可以透過下面這指令,完成上述步驟。

cd ~/.n8n
mkdir -p custom/node_modules
git clone https://github.com/n8n-io/n8n-nodes-starter
cd n8n-nodes-starter
npm install

這樣我們就可以開始開發了

我們可以看到,目錄裡有兩個子目錄 credentials, nodes
credentials 是用來存放憑證相關資料的,我們就拿來放私鑰
nodes 就是我們所設計的節點囉

我們先來看 credentials

import {
ICredentialType,
INodeProperties,
} from 'n8n-workflow';

export class WalletPrivateKeyApi implements ICredentialType {
name = 'walletPrivateKeyApi';
displayName = 'Wallet Private Key API';
properties: INodeProperties[] = [
{
displayName: 'Private Key',
name: 'privateKey',
type: 'string',
typeOptions: { password: true },
default: '',
},
];
}

官方範例裡原本就提供 ExampleCredentialsApi.credentials.ts ,我們把它改成我們的 WalletPrivateKeyApi.credentials.ts
內容非常簡單,只要注意 class 和 name 以及檔名和目錄都要相同(只差在 name 開頭要小寫)

下面 properties 填寫你想存放的資料,這裡就是私鑰。

再來看 Node 怎麼做吧

在這裡我們會用到 ethers 這以太坊的互動的工具。

npm install ethers

同樣官方有提供兩個範例,我們把它改成我們要的樣子。

nodes/SendCryptoCoin/SendCryptoCoin.node.ts

import type {
IExecuteFunctions,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { ethers } from "ethers";


export class SendCryptoCoin implements INodeType {
description: INodeTypeDescription = {
displayName: 'Send Crypto Coin',
name: 'sendCryptoCoin',
group: ['transform'],
version: 1,
description: 'Send Crypto Coin',
defaults: {
name: 'Send Crypto Coin',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'walletPrivateKeyApi',
required: true,
},
],
properties: [
{
displayName: 'RPC URL',
name: 'rpcUrl',
type: 'string',
default: '',
placeholder: 'http://localhost:8545',
description: 'RPC URL to connect to',
},
{
displayName: 'Chain ID',
name: 'chainId',
type: 'number',
default: 1337,
description: 'Chain ID is the unique identifier for the blockchain network'
},
{
displayName: 'To Address',
name: 'toAddress',
type: 'string',
default: '',
placeholder: '0x',
description: 'To address is the address of the recipient'
},
{
displayName: 'Transfer Amount',
name: 'amount',
type: 'string',
default: '',
placeholder: 'Transfer Amount'
},
],
};

async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
// 來自前一個節點的資料,我們不會用到
// const items = this.getInputData();

const rpcUrl = this.getNodeParameter('rpcUrl', 0) as string
const chainId = this.getNodeParameter('chainId', 0) as number
const toAddress = this.getNodeParameter('toAddress', 0) as string
const amount = this.getNodeParameter('amount', 0) as number
// 從 credentials 拿取資料
const privateKey = (await this.getCredentials('walletPrivateKeyApi')).privateKey as string

// 連接到以太坊節點
const provider = new ethers.JsonRpcProvider(rpcUrl)
// 用私鑰建立錢包
const wallet = await new ethers.Wallet(privateKey, provider)
// 取得手續費資料
const feeData = await provider.getFeeData()
// 取得 nonce
const nonce = await provider.getTransactionCount(wallet.address, 'latest')
// 建立交易
let tx: ethers.TransactionRequest = {
from: wallet.address,
to: toAddress,
value: amount,
chainId: chainId,
gasLimit: 21000,
maxFeePerGas: feeData.maxFeePerGas,
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
nonce: nonce,
}
// 簽署交易
const sendedTx = await wallet.sendTransaction(tx)
// 等待交易被打包,並取得交易完成的資料
const blockOutTxHash = await provider.waitForTransaction(sendedTx.hash)

// 回傳的資料
let item: INodeExecutionData = {
json: {
address: wallet.address,
txReciept: blockOutTxHash,
},
};

// 回傳資料
return [[item]];
}
}

打包和修改 package.josn

現在做好了 credentials 和 nodes 但是還不能用,最後我們要做打包的動作,提供給 n8n 使用。

回到 n8n-nodes-starter 這層目錄,執行 npm run build,就會出現 dist 的目錄。

npm run build

然後修改 package.json 告訴 n8n 要去哪個目錄找到 credentials 和 nodes。

你會在 package.json 裡面看到描述 credentials 和 nodes 路徑的文字,把它修改成 dist 裡面的檔案目錄。

這樣就大功告成了。

回到 n8n 執行節點吧~

別忘了要 Create New Credential 來放入私鑰

最後你可以把這客製化節點,上傳到 NPM 與所有人分享

請觀看:

上傳套件到 NPM 倉庫
上傳到 NPM 需要先註冊 NPM 帳號 然後建立 Nodejs 的專案 mkdir mason-demo cd demo npm init 確認 package.json { “name”: “mason-demo”, # 你自己的專案名稱,不可以和現有NPM專案名稱相同 “version”: “0.0.1″, # 版本。每次修改都要往上增加,沒辦法覆蓋或降級。 “main”: “index.js”, # 進入點 “scripts”: { “test”: “echo \“Error: no test specified\” && exit 1″ }, “author”: “Mason”, # 作者 “license”: “ISC”, # License “description”: ”″ # 說明 } 並且從

Read more

n8n怎麼做防抖debounce?

n8n怎麼做防抖debounce?

防抖 debounce, 是程式設計重要的概念之一。 用意是短時間有多個訊息進來,只處理一次。 處理哪一次呢?只處理最後一次。 就比如,有人點擊習慣什麼都按兩下, 你就要每次都處理兩次嗎? 又比如,你做 Line 機器人。講一句回一句。 那如果使用者一次傳好幾句呢? 像是打錯字,習慣的修正,再送一次。或是使用者分段講完。 你要跑一次一起處理,還是跑多次? 現在你知道使用情境了,那在 n8n 怎麼做呢? 就比如 Line 訊息好了,你其實可以把Webhook 來的訊息存到 db 裡。 另外做一個 短時間(ex:10s) 就跑一次的 schedule, 去檢查新訊息,並休息一下(ex:5s)。再檢查新訊息。 如果兩次新訊息,筆數都一樣,就表示沒有新訊息進來了。就可以開始處理。 最後再把新訊息標記成舊訊息。 讓

By Mason Tang
n8n 主從架構,解放n8n效能,進行更多任務

n8n 主從架構,解放n8n效能,進行更多任務

n8n 有提供主從架構,讓多個 n8n 程式一起為你工作。 他們會懂得調派任務。 比如你把任務給主管,主管收到任務,就會把任務分配給底下的員工。而你身為老闆的你,只要面對主管。 這主管就是 Master, 員工就是 Slave (奴隸,真貼切) 透過多個程式,讓效率大幅提高。同時間能進行的任務更多。 那實際怎麼做呢? 設定 N8N_ENCRYPTION_KEY 這是用來加密資料庫資料的密鑰,在原本單一 n8n ,不是那麼需要,啟動時就會幫你建立。 會存在 .n8n/config 中,長得就像這樣 { "encryptionKey": "cjw5GKuWL6eoqaC0MOnHdBNWOfxAzXsn" } 今天你要跑多個 n8n ,每個 n8n 都要讀資料庫,那些加密的資料就需要同樣的 encryptionKey 才能讀取。 所以需要直接在環境變數中直接設定

By Mason Tang
n8n 做個計數器,保存變數到下一次執行

n8n 做個計數器,保存變數到下一次執行

n8n 可能有人會好奇,怎麼做計數器,例如一天只能使用 200次。 但是 Node 裡,似乎沒看到這功能。 但其實這功能就藏在 Code 裡 獲取靜態資料 其實這功能就藏在 $getWorkflowStaticData 使用這個函數,拿到的物件,其實是能持久化的,即使 n8n 關掉再開,資料也還在。 整個 n8n 共用這變數 使用 $getWorkflowStaticData('global') 這邊可以看到,這個 count 已經被使用 9 次了,並且在其他 Code 也可以獲得這個 count 單一 Node 使用,不能跨 Node 使用 使用 $getWorkflowStaticData('node&

By Mason Tang
n8n 教學,匯出/匯入所有憑證,輕鬆搞定 n8n 搬家

n8n 教學,匯出/匯入所有憑證,輕鬆搞定 n8n 搬家

上次介紹了如何在 Zeabur, Docker, Node.js 部署自動化工具 n8n。 n8n教學-搭建自動化工具 n8n 的三個方案近幾年隨著 AI 發展,AI Agent 和自動化也成為企業轉型和提升效率的重要工具。這些技術不僅能夠處理大量的數據分析,還能自動完成重複性高、耗時的任務,讓員工能夠專注於更具創造性和戰略性的工作。 今天就教你,搭建自動化工具 n8n 的三個方案。 三種搭建n8n的方案 1. 使用 Zeabur 的 Saas 服務 2. 使用 Docker 部署在自家機器上 3. 使用 Node.js 直接啟動在自家機器上 使用 Zeabur 的 Saas 服務 這我也是看雷蒙才知道有 Zeabur 這平台,的確是新手很適合的入門。也推薦給大家使用 優點:

By Mason Tang