发送 ERC20 和 TRC20 代币转账
用 Chaingateway 发送 ERC20、BEP20 或 TRC20 代币时,你要把发送方、接收方、金额和代币的合约地址,连同钱包密码或私钥,提交到该链的代币端点。代币转账是一次智能合约调用,所以发送方还需要持有该链的原生币来支付手续费:ETH、BNB、MATIC 或 TRX。响应会返回交易哈希。
API token 的获取方式见快速入门。在 Ethereum、BNB Smart Chain 和 Polygon 上,发送地址必须是通过 Chaingateway API 创建的,或已导入其中,否则签名步骤会失败。创建地址的方法见创建地址。
哪条链用哪个端点发送代币?
Section titled “哪条链用哪个端点发送代币?”每条链都有以代币标准命名的独立代币端点,所有 EVM 链的请求体相同。
| 区块链 | API 路由 | 标准 |
|---|---|---|
| Ethereum | /api/v2/ethereum/transactions/erc20 | ERC-20 |
| Binance Smart Chain | /api/v2/bsc/transactions/bep20 | BEP20 |
| Polygon | /api/v2/polygon/transactions/erc20 | ERC-20 |
| Arbitrum | /api/v2/arbitrum/transactions/erc20 | ERC-20 |
| Tron | /api/v2/tron/transactions/trc20 | TRC20 |
| Solana | /api/v2/solana/transactions/SPL | SPL |
要在测试网上运行请求,请添加请求头 X-Network: testnet。测试网列表见支持的网络。Bitcoin 没有代币端点。
如何发送 ERC20 或 BEP20 代币?
Section titled “如何发送 ERC20 或 BEP20 代币?”把下列字段提交到你所用链的路由。示例在 Ethereum 上发送 ERC20 代币。
from:发送方地址。to:接收方地址。amount:要发送的代币数量。contractaddress:代币的合约地址。password:钱包密码,没有privatekey时必填。privatekey:发送方的私钥,用于不带密码创建的地址。
gas、gasprice、maxPriorityFeePerGas、maxFeePerGas 和 nonce 是可选的。
curl --request POST \ --url https://app.chaingateway.io/api/v2/ethereum/transactions/erc20 \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer YOUR_API_TOKEN' \ --data '{ "from": "0xYourSenderAddress", "to": "0xRecipientAddress", "amount": 100, "contractaddress": "0xTokenContractAddress", "password": "YourPassword" }'const axios = require('axios');
const url = 'https://app.chaingateway.io/api/v2/ethereum/transactions/erc20';const payload = { from: '0xYourSenderAddress', to: '0xRecipientAddress', amount: 100, contractaddress: '0xTokenContractAddress', 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); });import requests
url = "https://app.chaingateway.io/api/v2/ethereum/transactions/erc20"payload = { "from": "0xYourSenderAddress", "to": "0xRecipientAddress", "amount": 100, "contractaddress": "0xTokenContractAddress", "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())<?phprequire 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();$response = $client->post('https://app.chaingateway.io/api/v2/ethereum/transactions/erc20', [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer YOUR_API_TOKEN', ], 'json' => [ 'from' => '0xYourSenderAddress', 'to' => '0xRecipientAddress', 'amount' => 100, 'contractaddress' => '0xTokenContractAddress', 'password' => 'YourPassword' ]]);
echo $response->getBody();代码会返回交易哈希:
{ "status": 201, "ok": true, "message": "Succesfully created", "data": { "txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27" }}如何发送 TRC20 代币?
Section titled “如何发送 TRC20 代币?”TRC20 请求与 ERC20 请求几乎相同。路由是 /api/v2/tron/transactions/trc20,地址是 Tron 地址,签名使用 password 或 privatekey。OpenAPI 文件把 from、to、contractaddress 和 amount 列为必填。示例使用的是不带密码创建的地址的私钥。
curl --request POST \ --url https://app.chaingateway.io/api/v2/tron/transactions/trc20 \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer YOUR_API_TOKEN' \ --data '{ "from": "TYourSenderAddress", "to": "TRecipientAddress", "amount": 100, "contractaddress": "TTokenContractAddress", "privatekey": "YourPrivateKey"}'创建 TRC20 代币交易完整讲解了 TRC20 的整个流程,包括读取合约和自行签名的端点。
发送前如何检查代币余额?
Section titled “发送前如何检查代币余额?”请先读取余额,以免发出一笔注定失败的交易。在 EVM 链上,GET /api/v2/ethereum/balances/{address}/erc20/{contract_address} 返回某个代币的余额,GET /api/v2/ethereum/balances/{address}/erc20 列出一个钱包的代币余额。在 BNB Smart Chain 上,路径中的片段是 bep20;在 Tron 上是 GET /api/v2/tron/balances/{address}/trc20/{contract_address}。GET /api/v2/ethereum/erc20/{contract_address} 返回代币的合约信息。
代币转账为什么会失败?
Section titled “代币转账为什么会失败?”大多数失败属于下面三种原因之一,每种对应的消息不同。
- 密码错误会返回状态码 401 和
Wrong password provided。 - 缺少字段或字段格式错误会返回状态码 422 和
Data validation failed,字段名列在errors中。 - 发送方的原生币不足会返回状态码 422 和节点给出的消息,例如
insufficient funds for gas * price + value。在 Tron 上,对应的消息是balance is not sufficient。
最后一种原因常让只持有代币的人意外:手续费用 ETH、BNB、MATIC 或 TRX 支付,从来不用代币支付。EVM 链的手续费模型请阅读 Ethereum gas 文档,energy 和 bandwidth 请阅读 TRON 的资源模型。如果你的 TRON 地址持有代币却没有 TRX,请参阅代付 TRC20 手续费中列出的方案。
如何发送 NFT?
Section titled “如何发送 NFT?”NFT 转账使用 ERC721 端点,例如 POST /api/v2/ethereum/transactions/erc721。请求体的签名字段与上文相同,但用 NFT 的 id tokenId 取代 amount。from、contractaddress、to 和 tokenId 是必填项。GET /api/v2/ethereum/nfts/{contract_address}/owner/{token_id} 返回当前所有者,.../uri/{token_id} 返回代币 URI。该标准定义在 EIP-721 中。
- EIP-20: Token Standard 定义了 ERC-20 代币的
transfer和balanceOf。 - TRON 开发者文档中的 TRC20 protocol interface。
- Ethereum 开发者文档中的 Gas and fees。