Loading...
Tron is a blockchain-based decentralized digital platform with its own cryptocurrency called Tronix or TRX. Founded in 2017 by a Singapore non-profit organization, the Tron Foundation, Tron aims to host a global entertainment system for the cost-effective sharing of digital content. As a smart contract-capable blockchain, TRON allows developers to build and deploy highly capable DApps that can be designed for practically any purpose including online games, decentralized exchanges, yield farms, open lending platforms and much more.
Tron's Smart Contract uses solidity langue. Communication between tron blockchain and dapps based on tron can be done with tronweb library. This smartbook will be an introduction to the TronWeb library.
You can visit chrome store and install TronLink extension. Create two accounts for this toutorial.
Shasta is testnetwork for tron blockchain. You can get test trx on shasta network from here , Enter your account address and submit to get the test trx.
To initialise tronweb object it requires an API key of the provider. TronGrid lets us create an RPC provider. Visit trongrid , same from the previous step and signup/login. It's dashboard will give an interface to create a project and api key.
We first need to initialise node to create our node project.
> npm init
> npm install tronweb
There are two ways you can inititalise tronweb object in a node project. As per their documentation, The latest version 3.2.6 of Tronweb can set API Key parameters through the setHeader method.
const TronWeb = require('tronweb')
const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://api.shasta.trongrid.io"); // For Shasta
//const fullNode = new HttpProvider("https://api.trongrid.io"); // For Mainnet
const solidityNode = new HttpProvider("https://api.trongrid.io");
const eventServer = new HttpProvider("https://api.trongrid.io");
const privateKey = "your private key";
const tronWeb = new TronWeb(fullNode,solidityNode,eventServer,privateKey);
tronWeb.setHeader({"TRON-PRO-API-KEY": 'your api key'});
const TronWeb = require('tronweb')
const tronWeb = new TronWeb({
//fullHost: 'https://api.trongrid.io', // For Mainnet
fullHost : 'https://api.shasta.trongrid.io', // For Shasta testnet
headers : { "TRON-PRO-API-KEY": 'your api key' },
privateKey : 'your private key'
})
Sending TRX from account A to account B takes place in three steps.
Method : tronWeb.transactionBuilder.sendTrx( to_address, amount, from_address );
Parameters :
Returns : Send Transaction Object
Method : tronWeb.trx.sign(transaction_object, private_key).
Parameters :
Returns : Signed transaction object by sender with its private key.
Method : tronWeb.trx.sendRawTransaction( signedtxn )
Parameters :
Returns : Reciept object of the transaction.
async function send_trx() {
var from_add = "< Account 1 >"; // Account whose private key is used to initialise Tronweb
var to_add = "< Account 2 >";
var amount = 1000000; // Value is in Sun : 1 TRX = 10 ** 6 Sun
var transaction_object = await tronWeb.transactionBuilder.sendTrx(
to_add,
amount,
from_add
).catch((err) => { console.log(err) });
const signedtxn = await tronWeb.trx.sign(
transaction_object,
private_key
).catch((err) => {console.log(err)});
const receipt = await tronWeb.trx.sendRawTransaction(
signedtxn
).catch((err) => { console.log(err);});
console.log('- Output:', receipt, '\n');
}
send_trx();
We put method B for tronweb initialisation here
const TronWeb = require('tronweb')
const tronWeb = new TronWeb({
//fullHost: 'https://api.trongrid.io', // For Mainnet
fullHost : 'https://api.shasta.trongrid.io', // For Shasta testnet
headers : { "TRON-PRO-API-KEY": 'your api key' },
privateKey : 'your private key'
});
async function send_trx() {
var from_add = "< Account 1 >"; // Account whose private key is used to initialise Tronweb
var to_add = "< Account 2 >";
var amount = 1000000; // Value is in Sun : 1 TRX = 10 ** 6 Sun
var transaction_object = await tronWeb.transactionBuilder.sendTrx(
to_add,
amount,
from_add
).catch((err) => { console.log(err) });
const signedtxn = await tronWeb.trx.sign(
transaction_object,
private_key
).catch((err) => {console.log(err)});
const receipt = await tronWeb.trx.sendRawTransaction(
signedtxn
).catch((err) => { console.log(err);});
console.log('- Output:', receipt, '\n');
}
send_trx();