跳转到内容

创建代币交易

在本教程中,我们将介绍如何使用 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 创建。否则授权流程将无法正常工作。

要创建地址,请参阅教程部分

在 Ethereum、Polygon(ERC20)或 Binance Smart Chain(BEP20)上进行代币交易时,你需要使用适当的参数向 Chaingateway API 发送请求。

  • from:发送方地址。
  • to:接收方地址。
  • amount:要发送的金额。
  • contractaddress:代币的合约地址。
  • password:用于加密发送方地址 keystore 文件的密码。

YOUR_API_TOKEN 替换为你的实际 API token。

Terminal window
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"
}'

代码应返回以下响应:

{
"status": 201,
"ok": true,
"message": "Succesfully created transaction",
"data": {
"txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27"
}
}

为 Tron 代币创建交易的方式几乎相同。唯一的区别是你需要提供 privateKey 而不是 password 参数。

  • from:发送方地址。
  • to:接收方地址。
  • amount:要发送的金额。
  • contractaddress:代币的合约地址。
  • privateKey:发送方地址的私钥。

YOUR_API_TOKEN 替换为你的实际 API token。

Terminal window
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"
}'

代码应返回以下响应:

{
"status": 201,
"ok": true,
"message": "Succesfully created transaction",
"data": {
"txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27"
}
}