Tron Lib
Installation
Node.js:
// npm
$ npm install @brickglobal/coin_tron
// yarn
$ yarn add @brickglobal/coin_tron
Creating an Instance
First off, in your javascript file, define TronLib
:
const { TronLib } = require('@brickglobal/coin_tron');
ES module
import { TronLib } from "@brickglobal/coin_tron";
When you instantiate TronLib
you must provide
- fullNodeUri
- privateKey: optional
const tronLib = new TronLib("https://api.nileex.io/")
// or
const tronLib = new TronLib("https://api.nileex.io/", "your private key")
Methods
There are total 4 methods:
1. createAccount
Using to create account
Usage
tronLib.createAccount();
Params
Null
Return
Promise { <pending> }
> {
privateKey: string,
publicKey: string,
address {
base58: string,
hex: string,
}
}
Example
tronLib.createAccount();
// return
Promise { <pending> }
> {
address: {
base58: "TPbBpRXnt6ztse8XkCL******",
hex: "4195679F3AAF5211991781D4****",
}
privateKey: "08089C24EC3BAEB34254DDF5297CF8F******"
publicKey: "04EE63599802B5D31A29C95CC7DF04F427E8F0A124BED9333F3A80404ACFC31276594962656EF******"
}
2. sendTrx
Sends TRX from one address to another.
WARNING: Do not use this in any web / user-facing applications. This will expose the private key. |
---|
Usage
tronLib.sendTrx();
Params
Parameter | Description | Data Type |
---|---|---|
receiverAddress | Address to send TRX to, converted to a hex string. | Hex String |
amount | Amount of TRX to send (units in SUN) | Integer (units in SUN) |
privateKey | Optionally provide a private key to sign the transaction. If left blank, will use the address associated with the private key. | Hex string |
Return
Promise { <pending> }
> txid: string
Example
const sendtrx = await tronLib.sendTrx("TPbBpRXnt6ztse8XkCL******",1000000)
// or
const sendtrx = await tronLib.sendTrx("TPbBpRXnt6ztse8XkCL******",1000000, 'your private key')
// return
Promise { <pending> }
> 456f9e1090dc8ad6baf9237db99334d66e7d640b5********
3. sendToken10
Sends TRC10 token from one address to another.
WARNING: Do not use this in any web / user-facing applications. This will expose the private key. |
---|
Usage
tronLib.sendToken10();
Params
Parameter | Description | Data Type |
---|---|---|
receiverAddress | Address to send TRC10 to. | Hex string |
amount | Amount of TRC10 to send. | positive integers |
tokenId | Name of the token, matching the exact capitalization. | string |
privateKey | Optionally provide a private key to sign the transaction. If left blank, will use the address associated with the private key. | Hex string |
Return
Promise { <pending> }
> txid: string
Example
const send_token_10 = await tronLib.sendTrc10("TPbBpRXnt6ztse8XkCL******",1000000, '100010')
// or
const send_token_10 = await tronLib.sendTrc10("TPbBpRXnt6ztse8XkCL******",1000000, '100010', 'your private key')
// return
Promise { <pending> }
> 456f9e1090dc8ad6baf9237db99334d66e7d640b5********
4. sendToken20
Sends TRC20 token from one address to another.
WARNING: Do not use this in any web / user-facing applications. This will expose the private key. |
---|
Usage
tronLib.sendToken20();
Params
Parameter | Description | Data Type |
---|---|---|
receiverAddress | Address to send TRC20 to. | |
amount | Amount of TRC20 to send. | positive integers |
tokenAddress | Contract address. | Hex string |
privateKey | Optionally provide a private key to sign the transaction. If left blank, will use the address associated with the private key. | Hex string |
Return
Promise { <pending> }
> txid: string
Example
const send_token_20 = await tronLib.sendTrc20("TPbBpRXnt6ztse8XkCL******",1000000, 'TUa1qN4ahSW7xzi5MRgA1YT******')
// or
const send_token_20 = await tronLib.sendTrc20("TPbBpRXnt6ztse8XkCL******",1000000, 'TUa1qN4ahSW7xzi5MRgA1YT******', 'your private key')
// return
Promise { <pending> }
> 456f9e1090dc8ad6baf9237db99334d66e7d640b5********