创建交易
创建原生代币交易
Section titled “创建原生代币交易”在本教程中,我们将介绍如何使用 ChainGateway V2 API 为 Ethereum(ETH)、Binance Smart Chain(BNB)、Polygon(MATIC)、Tron(TRX)和 Bitcoin(BTC)创建交易。我们将提供多种编程语言的示例,包括 Shell(cURL)、PHP、Python 和 JavaScript。
在本教程中,我们将为原生代币 ETH BNB TRX MATIC 创建交易。要发送 TRC20、ERC20 和 BEP20 交易,请访问创建代币交易
有关获取 API key 和了解授权的信息,请参阅 Chaingateway 文档的快速入门部分。
为 ETH、BNB 和 MATIC 创建交易
Section titled “为 ETH、BNB 和 MATIC 创建交易”在 Ethereum、Binance Smart Chain 或 Polygon 上进行交易时,你需要使用适当的参数向 ChainGateway API 发送请求。
- from:发送方地址。
- to:接收方地址。
- amount:要发送的金额。
- password:用于加密发送方地址 keystore 文件的密码。
Authorization Header 示例
Section titled “Authorization Header 示例”将 YOUR_API_TOKEN 替换为你的实际 API token。
curl --request POST\ --url https://api.chaingateway.io/v2/ethereum/transactions\ --header 'Accept: application/json'\ --header 'content-type: application/json'\ --header 'Authorization: YOUR_API_TOKEN'\ --data '{ "from": "0xYourSenderAddress", "to": "0xRecipientAddress", "amount": "0.1", "password": "YourPassword"}'import requests
url = "https://api.chaingateway.io/v2/ethereum/transactions"payload = { "from": "0xYourSenderAddress", "to": "0xRecipientAddress", "amount": "0.1", "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())const axios = require('axios');
const url = 'https://api.chaingateway.io/v2/ethereum/transactions';const payload = { from: '0xYourSenderAddress', to: '0xRecipientAddress', amount: '0.1', 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); });<?phprequire 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();$response = $client->post('https://api.chaingateway.io/v2/ethereum/transactions', [ 'headers' => [ 'Accept' => 'application/json', 'content-type' => 'application/json', 'Authorization' => 'YOUR_API_TOKEN', ], 'json' => [ 'from' => '0xYourSenderAddress', 'to' => '0xRecipientAddress', 'amount' => '0.1', 'password' => 'YourPassword' ]]);
echo $response->getBody();?>代码应返回以下响应:
{ "status": 201, "ok": true, "message": "Succesfully created transaction", "data": { "txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27" }}为 TRX 创建交易
Section titled “为 TRX 创建交易”- from:发送方地址。
- to:接收方地址。
- amount:要发送的金额。
- privateKey:发送方地址的私钥。
Authorization Header 示例
Section titled “Authorization Header 示例”将 YOUR_API_TOKEN 替换为你的实际 API token。
curl --request POST\ --url https://api.chaingateway.io/v2/tron/transactions\ --header 'Accept: application/json'\ --header 'content-type: application/json'\ --header 'Authorization: YOUR_API_TOKEN'\ --data '{ "from": "YourSenderAddress", "to": "RecipientAddress", "amount": "0.001", "privateKey": "YourPrivateKey"}'import requests
url = "https://api.chaingateway.io/v2/tron/transactions"payload = { "from": "YourSenderAddress", "to": "RecipientAddress", "amount": "0.001", "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';const payload = { from: 'YourSenderAddress', to: 'RecipientAddress', amount: '0.001', 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', [ 'headers' => [ 'Accept' => 'application/json', 'content-type' => 'application/json', 'Authorization' => 'YOUR_API_TOKEN', ], 'json' => [ 'from' => 'YourSenderAddress', 'to' => 'RecipientAddress', 'amount' => '0.001', 'privateKey' => 'YourPrivateKey' ]]);
echo $response->getBody(); ?>{ "status": 201, "ok": true, "message": "Succesfully created transaction", "data": { "txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27" }}要创建交易,用户需要提供地址的密码(适用于 Ethereum、Binance Smart Chain、Polygon 和 Bitcoin)或私钥(适用于 Tron)。ChainGateway 不会存储这些敏感信息。因此,用户必须将它们保存在安全的地方。如果丢失,ChainGateway 无法协助恢复,因为它无法访问这些信息。
有关获取 API key 和了解授权的信息,请参阅 ChainGateway 文档的”快速开始”部分。