What is TronWeb?
Tron Web - Developer Document
TronWeb aims to deliver a unified, seamless development experience influenced by Ethereum's Web3 implementation. We have taken the core ideas and expanded upon it to unlock the functionality of TRON's unique feature set along with offering new tools for integrating DApps in the browser, Node.js and IoT devices.
Compatibility
- Version built for Node.js v6 and above
- Version built for browsers with more than 0.25% market share
You can access either version specifically from the dist/
folder.
TronWeb is also compatible with frontend frameworks such as Angular, React and Vue.
You can also ship TronWeb in a Chrome extension.
Installation
npm install tronweb
Example
To look at the examples, first clone this repo, install the dependencies and run the example:
git clone https://github.com/tronprotocol/tron-web.git
cd tron-web
yarn
yarn build -d
yarn example
The example is at examples/server/index.js
.
TRON provides a private network to test with
var express = require('express');
var proxy = require('http-proxy-middleware');
function onProxyRes(proxyRes, req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,Accept')
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS')
console.log(req.originalUrl)
}
var fullnode = express();
fullnode.use('/', proxy({
target: 'http://127.0.0.1:16667',
changeOrigin: true,
onProxyRes
}));
fullnode.listen(8090);
var soliditynode = express();
soliditynode.use('/', proxy({
target: 'http://127.0.0.1:16668',
changeOrigin: true,
onProxyRes,
onError
}));
soliditynode.listen(8091);
Creating an Instance
First off, in your javascript file, define TronWeb:
const TronWeb = require('TronWeb')
Specify the API endpoints:
const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider('https://api.trongrid.io');
const solidityNode = new HttpProvider('https://api.trongrid.io:');
const eventServer = 'https://api.trongrid.io';
The provider above is optional, you can just use a url for the nodes instead, like here:
const fullNode = 'https://api.trongrid.io';
const solidityNode = 'https://api.trongrid.io';
const eventServer = 'https://api.trongrid.io/';
Now, instance a tronWeb object:
const privateKey = 'da146374a75310b9666e834ee4ad0866d6f4035967bfc76217c5a495fff9f0d0';
const tronWeb = new TronWeb(
fullNode,
solidityNode,
eventServer,
privateKey
);
A full example:
const TronWeb = require('TronWeb')
const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider('https://api.trongrid.io');
const solidityNode = new HttpProvider('https://api.trongrid.io');
const eventServer = 'https://api.trongrid.io/';
const privateKey = 'da146374a75310b9666e834ee4ad0866d6f4035967bfc76217c5a495fff9f0d0';
const tronWeb = new TronWeb(
fullNode,
solidityNode,
eventServer,
privateKey
);
async function getBalance() {
const address = 'TPL66VK2gCXNCD7EJg9pgJRfqcRazjhUZY';
const balance = await tronWeb.trx.getBalance(address);
console.log({balance});
tronWeb.trx.getBalance(address).then(balance => {
console.log({balance});
}).catch(err => console.error(err));
tronWeb.trx.getBalance(address, (err, balance) => {
if (err)
return console.error(err);
console.log({balance});
});
}
getBalance();
Documentation for TronWeb 1.0
Are you still using TronWeb 1 ? You can find documentation here: