Creare transazioni di token
Introduzione
Sezione intitolata “Introduzione”In questo tutorial vedremo come creare transazioni di token ERC20 (Ethereum), BEP20 (Binance Smart Chain) e TRC20 (Tron) usando l’API Chaingateway V2. Forniremo esempi in vari linguaggi di programmazione, tra cui Shell (cURL), PHP, Python e JavaScript.
Per ottenere una API key e informazioni sull’autorizzazione, consulta la sezione Per iniziare della documentazione ChainGateway.
Per effettuare transazioni da Ethereum, BSC o Polygon, il tuo indirizzo deve essere importato in Chaingateway oppure essere stato creato con l’API Chaingateway. In caso contrario, la procedura di autorizzazione non funzionerà.
Per creare un indirizzo, consulta la sezione tutorial
Creare transazioni ERC20 e BEP20
Sezione intitolata “Creare transazioni ERC20 e BEP20”Quando effettui una transazione di token su Ethereum, Polygon (ERC20) o Binance Smart Chain (BEP20), devi inviare una richiesta all’API Chaingateway con i parametri appropriati.
Parametri
Sezione intitolata “Parametri”- from: L’indirizzo del mittente.
- to: L’indirizzo del destinatario.
- amount: L’importo da inviare.
- contractaddress: L’indirizzo del contratto del token.
- password: La password usata per crittografare il file keystore dell’indirizzo del mittente.
Esempio di header Authorization
Sezione intitolata “Esempio di header Authorization”Sostituisci YOUR_API_TOKEN con il tuo token API effettivo.
Esempio di codice
Sezione intitolata “Esempio di codice”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();?>Il codice dovrebbe restituire questa risposta:
{ "status": 201, "ok": true, "message": "Succesfully created transaction", "data": { "txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27" }}Creare transazioni TRC20
Sezione intitolata “Creare transazioni TRC20”Creare transazioni per i token Tron funziona quasi allo stesso modo. L’unica differenza è che dovrai fornire privateKey invece del parametro password.
Parametri
Sezione intitolata “Parametri”- from: L’indirizzo del mittente.
- to: L’indirizzo del destinatario.
- amount: L’importo da inviare.
- contractaddress: L’indirizzo del contratto del token.
- privateKey: La private key dell’indirizzo del mittente.
Esempio di header Authorization
Sezione intitolata “Esempio di header Authorization”Sostituisci YOUR_API_TOKEN con il tuo token API effettivo.
Esempio di codice
Sezione intitolata “Esempio di codice”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();?>Il codice dovrebbe restituire questa risposta:
{ "status": 201, "ok": true, "message": "Succesfully created transaction", "data": { "txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27" }}