Creare transazioni
Creare transazioni di token nativi
Sezione intitolata “Creare transazioni di token nativi”Introduzione
Sezione intitolata “Introduzione”In questo tutorial vedremo come effettuare transazioni per Ethereum (ETH), Binance Smart Chain (BNB), Polygon (MATIC), Tron (TRX) e Bitcoin (BTC) usando l’API ChainGateway V2. Forniremo esempi in vari linguaggi di programmazione, tra cui Shell (cURL), PHP, Python e JavaScript.
In questo tutorial creeremo transazioni per i token nativi ETH BNB TRX MATIC. Per inviare transazioni TRC20, ERC20 e BEP20, visita Creare transazioni di token
Per ottenere una API key e informazioni sull’autorizzazione, consulta la sezione Guida rapida della documentazione Chaingateway.
Creare transazioni per ETH, BNB e MATIC
Sezione intitolata “Creare transazioni per ETH, BNB e MATIC”Quando effettui una transazione su Ethereum, Binance Smart Chain o Polygon, 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.
- 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\ --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();?>Il codice dovrebbe restituire questa risposta:
{ "status": 201, "ok": true, "message": "Succesfully created transaction", "data": { "txid": "0x73344d178812d4919e9002c560781f288030edf72ece88823ef1c377dfb71f27" }}Creare transazioni per TRX
Sezione intitolata “Creare transazioni per TRX”Parametri
Sezione intitolata “Parametri”- from: L’indirizzo del mittente.
- to: L’indirizzo del destinatario.
- amount: L’importo da inviare.
- 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\ --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" }}Informazioni importanti
Sezione intitolata “Informazioni importanti”Per creare transazioni, gli utenti devono fornire la password dell’indirizzo (per Ethereum, Binance Smart Chain, Polygon e Bitcoin) o la private key (per Tron). ChainGateway non memorizza queste informazioni sensibili. Gli utenti devono quindi conservarle in un luogo sicuro. Se vengono perse, ChainGateway non può aiutare al recupero poiché non vi ha accesso.
Per ottenere una API key e informazioni sull’autorizzazione, consulta la sezione Per iniziare della documentazione ChainGateway.