创建代币交易
在本教程中,我们将介绍如何使用 Chaingateway V2 API 创建 ERC20(Ethereum)、BEP20(Binance Smart Chain)和 TRC20(Tron)代币交易。我们将提供多种编程语言的示例,包括 Shell(cURL)、PHP、Python 和 JavaScript。
有关获取 API key 和了解授权的信息,请参阅 ChainGateway 文档的快速开始部分。
要从 Ethereum、BSC 或 Polygon 发起交易,你的地址需要已导入 Chaingateway,或已通过 Chaingateway API 创建。否则授权流程将无法正常工作。
要创建地址,请参阅教程部分
创建 ERC20 和 BEP20 交易
Section titled “创建 ERC20 和 BEP20 交易”在 Ethereum、Polygon(ERC20)或 Binance Smart Chain(BEP20)上进行代币交易时,你需要使用适当的参数向 Chaingateway API 发送请求。
- from:发送方地址。
- to:接收方地址。
- amount:要发送的金额。
- contractaddress:代币的合约地址。
- password:用于加密发送方地址 keystore 文件的密码。
Authorization Header 示例
Section titled “Authorization Header 示例”将 YOUR_API_TOKEN 替换为你的实际 API token。
curl --request POST \ --url https://api.chaingateway.io/v2/ethereum/transactions/erc20 \ --header 'Accept: application/json' \ --header 'content-type: application/json' \ --header 'Authorization: YOUR_API_TOKEN' \ --data '{ "from": "0xYourSenderAddress", "to": "0xRecipientAddress", "amount": "100", "contractaddress": "0xTokenContractAddress", "password": "YourPassword" }'const axios = require('axios');
const url = 'https://api.chaingateway.io/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': 'YOUR_API_TOKEN'};
axios.post(url, payload, { headers }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });import requests
url = "https://api.chaingateway.io/v2/ethereum/transactions/erc20"payload = { "from": "0xYourSenderAddress", "to": "0xRecipientAddress", "amount": "100", "contractaddress": "0xTokenContractAddress", "password": "YourPassword"}headers = { 'Accept': 'application/json', 'content-type': 'application/json', 'Authorization': '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://api.chaingateway.io/v2/ethereum/transactions/erc20', [ 'headers' => [ 'Accept' => 'application/json', 'content-type' => 'application/json', 'Authorization' => 'YOUR_API_TOKEN', ], 'json' => [ 'from' => '0xYourSenderAddress', 'to' => '0xRecipientAddress', 'amount' => '100', 'contractaddress' => '0xTokenContractAddress', 'password' => 'YourPassword' ]]);
echo $response->getBody();?>代码应返回以下响应:
{ "status": 201, "ok": true, "message": "Succesfully created transaction", "data": { "txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27" }}创建 TRC20 交易
Section titled “创建 TRC20 交易”为 Tron 代币创建交易的方式几乎相同。唯一的区别是你需要提供 privateKey 而不是 password 参数。
- from:发送方地址。
- to:接收方地址。
- amount:要发送的金额。
- contractaddress:代币的合约地址。
- privateKey:发送方地址的私钥。
Authorization Header 示例
Section titled “Authorization Header 示例”将 YOUR_API_TOKEN 替换为你的实际 API token。
curl --request POST\ --url https://api.chaingateway.io/v2/tron/transactions/trc20\ --header 'Accept: application/json'\ --header 'content-type: application/json'\ --header 'Authorization: YOUR_API_TOKEN'\ --data '{ "from": "TYourSenderAddress", "to": "TRecipientAddress", "amount": "100", "contractaddress": "TTokenContractAddress", "privateKey": "YourPrivateKey"}'import requests
url = "https://api.chaingateway.io/v2/tron/transactions/trc20"payload = { "from": "TYourSenderAddress", "to": "TRecipientAddress", "amount": "100", "contractaddress": "TTokenContractAddress", "privateKey": "YourPrivateKey"}headers = { 'Accept': 'application/json', 'content-type': 'application/json', 'Authorization': 'YOUR_API_TOKEN'}
response = requests.post(url, json=payload, headers=headers)print(response.json())const axios = require('axios');
const url = 'https://api.chaingateway.io/v2/tron/transactions/trc20';const payload = { from: 'TYourSenderAddress', to: 'TRecipientAddress', amount: '100', contractaddress: 'TTokenContractAddress', privateKey: 'YourPrivateKey'};const headers = { 'Accept': 'application/json', 'content-type': 'application/json', 'Authorization': 'YOUR_API_TOKEN'};
axios.post(url, payload, { headers }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });<?phprequire 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();$response = $client->post('https://api.chaingateway.io/v2/tron/transactions/trc20', [ 'headers' => [ 'Accept' => 'application/json', 'content-type' => 'application/json', 'Authorization' => 'YOUR_API_TOKEN', ], 'json' => [ 'from' => 'TYourSenderAddress', 'to' => 'TRecipientAddress', 'amount' => '100', 'contractaddress' => 'TTokenContractAddress', 'privateKey' => 'YourPrivateKey' ]]);
echo $response->getBody();?>代码应返回以下响应:
{ "status": 201, "ok": true, "message": "Succesfully created transaction", "data": { "txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27" }}