发送 ETH、BNB、TRX 和 BTC 交易
用 Chaingateway 发送原生币时,你要把发送方、接收方、金额和签名密钥提交到 POST /api/v2/{chain}/transactions。Chaingateway 会为交易签名并广播,然后返回交易 id(txid)。签名密钥对受密码保护的地址是钱包 password,对不带密码创建的地址则是 privatekey。本教程涵盖 ETH、BNB、MATIC、TRX、BTC 和 SOL。
USDT 等代币使用不同的端点,见创建代币交易。你需要 API token 和一个已充值的地址,参阅快速入门和创建地址。
如何发送 ETH、BNB 或 MATIC?
Section titled “如何发送 ETH、BNB 或 MATIC?”使用对应链的端点:/ethereum、/bsc、/polygon 或 /arbitrum。EVM 链的请求体有三个必填字段和一个签名密钥。
| 字段 | 是否必填 | 含义 |
|---|---|---|
from | 是 | 发送方地址,校验为 40 个十六进制字符,0x 前缀可选。 |
to | 是 | 接收方地址,格式相同。 |
amount | 是 | 要发送的金额。 |
password | 二选一 | 钱包密码。没有 privatekey 时必填。 |
privatekey | 二选一 | 发送方的私钥,用于不带密码创建的地址。 |
gas、gasprice、maxPriorityFeePerGas、maxFeePerGas、nonce、data | 否 | 可选的交易设置。 |
请先带上请求头 X-Network: testnet 在测试网发送一小笔金额,对比余额变化后再转移真实资金。amount 的单位以该端点的 API 参考为准,请在那里确认。授权请求头携带你的 token:Authorization: Bearer YOUR_API_TOKEN。
curl --request POST \ --url https://app.chaingateway.io/api/v2/ethereum/transactions \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer YOUR_API_TOKEN' \ --data '{ "from": "0xYourSenderAddress", "to": "0xRecipientAddress", "amount": "0.1", "password": "YourPassword"}'import requests
url = "https://app.chaingateway.io/api/v2/ethereum/transactions"payload = { "from": "0xYourSenderAddress", "to": "0xRecipientAddress", "amount": "0.1", "password": "YourPassword"}headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_TOKEN'}
response = requests.post(url, json=payload, headers=headers)print(response.json())const axios = require('axios');
const url = 'https://app.chaingateway.io/api/v2/ethereum/transactions';const payload = { from: '0xYourSenderAddress', to: '0xRecipientAddress', amount: '0.1', password: 'YourPassword'};const headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_TOKEN'};
axios.post(url, payload, { headers }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error.response?.data ?? error); });<?phprequire 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();$response = $client->post('https://app.chaingateway.io/api/v2/ethereum/transactions', [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer YOUR_API_TOKEN', ], 'json' => [ 'from' => '0xYourSenderAddress', 'to' => '0xRecipientAddress', 'amount' => '0.1', 'password' => 'YourPassword' ]]);
echo $response->getBody();调用成功时返回状态码 201 和交易哈希:
{ "status": 201, "ok": true, "message": "Succesfully created transaction", "data": { "txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27" }}如何发送 TRX?
Section titled “如何发送 TRX?”POST /api/v2/tron/transactions 接收 from、to 和 amount,再加上 password 或 privatekey。Tron 地址必须符合模式:字母 T 后跟 33 个字母数字字符。发送方既要有足够的 TRX 支付金额,也要有足够的 TRX 支付转账消耗的网络资源,详见 TRON 的资源模型。
curl --request POST \ --url https://app.chaingateway.io/api/v2/tron/transactions \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer YOUR_API_TOKEN' \ --data '{ "from": "YourSenderAddress", "to": "RecipientAddress", "amount": 1, "privatekey": "YourPrivateKey"}'响应的结构与上面的 EVM 响应相同,交易哈希在 data.txid 中。如果你在自己的代码中签名交易,POST /api/v2/tron/transactions/build 会返回未签名的交易,POST /api/v2/tron/transactions/broadcast 则在 signed_transaction 字段中接收已签名的交易。
如何发送 BTC?
Section titled “如何发送 BTC?”Bitcoin 从钱包发送,而不是从单个地址发送。POST /api/v2/bitcoin/transactions 需要 to、amount、walletname、password 和 speed。speed 取 fast、medium 或 slow,用来设定手续费级别。可选字段 subtractfee 会从金额中扣除手续费,而不是在金额之外另加。
curl --request POST \ --url https://app.chaingateway.io/api/v2/bitcoin/transactions \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer YOUR_API_TOKEN' \ --data '{ "to": "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq", "amount": 0.0001, "walletname": "mywallet", "password": "YourWalletPassword", "speed": "medium", "subtractfee": false}'Bitcoin 地址按模式 (bc1|tb1|[13]) 后跟 25 到 39 个字符来校验,主网和测试网的格式都能通过。Bitcoin 开发者指南中关于交易的章节讲解了输入、输出和手续费。
如何发送 SOL?
Section titled “如何发送 SOL?”POST /api/v2/solana/transactions 接收 from、to、amount 和发送方的 privatekey。OpenAPI 文件把这四个字段都列为必填,Solana 没有密码模式。SPL 代币使用 POST /api/v2/solana/transactions/SPL,并额外传入字段 contractaddress,其值为该代币的 mint 地址。
如何在 EVM 链上设置 gas 和手续费?
Section titled “如何在 EVM 链上设置 gas 和手续费?”你不必设置,因为 gas、gasprice、maxPriorityFeePerGas 和 maxFeePerGas 都是可选的。要自己设置,请先用 GET /api/v2/ethereum/gasprice 读取当前价格,它会在 data 中返回类似 {"gasPrice":"12.28"} 的值。两个 max 字段对应 EIP-1559 的手续费参数。Ethereum 文档中关于交易的页面列出了所有交易字段,包括 nonce。
如何确认交易已经完成?
Section titled “如何确认交易已经完成?”使用响应中的 txid。GET /api/v2/ethereum/transactions/{txid} 返回交易本身,GET /api/v2/ethereum/transactions/{txid}/decoded 返回带解码后输入数据的交易。节点不认识的交易哈希会返回状态码 422 和消息 transaction not found。如果想在资金到账时收到通知,请创建 webhook,而不是轮询。
可能遇到哪些错误?
Section titled “可能遇到哪些错误?”错误使用与成功调用相同的响应结构,其中 ok 为 false。
| 状态码 | 消息 | 原因 |
|---|---|---|
| 401 | Wrong password provided | password 与该地址不匹配。 |
| 422 | insufficient funds for gas * price + value: ... | 余额不足以支付金额加手续费。 |
| 422 | Data validation failed | 缺少字段或字段格式错误,errors 对象会指出是哪个字段。 |
| 422 | Error: The wallet passphrase entered was incorrect. | Bitcoin:钱包 password 错误。 |
Chaingateway 不会存储你的密码,也不会存储不带密码的地址的私钥。一旦丢失,任何人都无法找回该地址上的资金。请把两者保存在密钥存储中,切勿放进客户端代码。