Integrating Blockchain APIs
Query via Transaction ID
curl https://api.trongrid.io/event/transaction/5c3747ffa94fc87a2188708a9e0758cbd01f000d3d01f6589651921930183f6a
Querying by Contract Events
You can query a contract for events and sort the results based on parameters passed in.
Case 1 |
---|
Pass since or fromTimestamp Example: https://api.trongrid.io/event/contract/TEEXEWrkMFKapSMJ6mErg39ELFKDqEs6w3?since=1551779730000 https://api.trongrid.io/event/contract/TEEXEWrkMFKapSMJ6mErg39ELFKDqEs6w3?fromTimestamp=1551779730000 Result: Returns all events that occurred before the specified timestamp, starting from the specified timestamp, in order of most recent on top (descending). |
Case 2 |
---|
Pass since or fromTimestamp & sort=-block_timestamp Example: https://api.trongrid.io/event/contract/TEEXEWrkMFKapSMJ6mErg39ELFKDqEs6w3?since=1551779730000&sort=-block_timestamp https://api.trongrid.io/event/contract/TEEXEWrkMFKapSMJ6mErg39ELFKDqEs6w3?fromTimestamp=1551779730000&sort=-block_timestamp Result: Returns all events that occurred before the specified timestamp, starting from the specified timestamp, in order of most recent on top (descending). |
Case 3 |
---|
Pass since or fromTimestamp & sort=block_timestamp Example: https://api.trongrid.io/event/contract/TEEXEWrkMFKapSMJ6mErg39ELFKDqEs6w3?since=1551779730000&sort=block_timestamp https://api.trongrid.io/event/contract/TEEXEWrkMFKapSMJ6mErg39ELFKDqEs6w3?fromTimestamp=1551779730000&sort=block_timestamp Result: Returns all events that occurred after the specified timestamp, starting from the specified timestamp, in order of most recent on the bottom of the page (ascending). |
Case 4 |
---|
No Value Passed Example: https://api.trongrid.io/event/contract/TEEXEWrkMFKapSMJ6mErg39ELFKDqEs6w3 Result: Returns all events starting with most recent on top of the page (descending order). |
Example Contract
pragma solidity ^0.4.23;
contract Fibonacci {
event Notify(uint input, uint result);
function fibonacci(uint number) public constant returns(uint result) {
if (number == 0) return 0;
else if (number == 1) return 1;
else return Fibonacci.fibonacci(number - 1) + Fibonacci.fibonacci(number - 2);
}
function fibonacciNotify(uint number) public returns(uint result) {
result = fibonacci(number);
emit Notify(number, result);
}
}
Deploy contract using TronWeb, TronBox, or directly using RPC or HTTP call:
deploycontract DataStore [{"constant":false,"inputs":[{"name":"number","type":"uint256"}],"name":"fibonacciNotify","outputs":[{"name":"result","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"number","type":"uint256"}],"name":"fibonacci","outputs":[{"name":"result","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"input","type":"uint256"},{"indexed":false,"name":"result","type":"uint256"}],"name":"Notify","type":"event"}] 608060405234801561001057600080fd5b50610196806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633c7fdc701461005157806361047ff414610092575b600080fd5b34801561005d57600080fd5b5061007c600480360381019080803590602001909291905050506100d3565b6040518082815260200191505060405180910390f35b34801561009e57600080fd5b506100bd60048036038101908080359060200190929190505050610124565b6040518082815260200191505060405180910390f35b60006100de82610124565b90507f71e71a8458267085d5ab16980fd5f114d2d37f232479c245d523ce8d23ca40ed8282604051808381526020018281526020019250505060405180910390a1919050565b6000808214156101375760009050610165565b60018214156101495760019050610165565b61015560028303610124565b61016160018403610124565b0190505b9190505600a165627a7a723058201540ed8f82b334522f0e3a11793ba18c1d184536d7b797b30adbba3ca9b7f52c0029 1000000 30 0
After triggering fibonacciNotify
and event notify:
triggersmartcontract 417C5D1624B699C425355B99D1C8D188D367FA232D fibonacciNotify(uint256) 7 false 1000000 0000000000000000000000000000000000000000000000000000000000000000
Return Value:
[
{
"block_number": 88,
"block_timestamp": 1534767012000,
"contract_address": "TMJnJcHfdP5rhmXVkwRYb1a9A6gS46PUm6",
"event_name": "Notify",
"result": [
"7",
"13"
],
"transaction_id": "5c3747ffa94fc87a2188708a9e0758cbd01f000d3d01f6589651921930183f6a"
}
]
Return Format:
block_number: block height
block_timestamp: event time stamp
contract_address: contract address
event_name: event name
result: event parameter
transaction_id: transcation id
Blockchain Interaction
Use Tron-Web & Tron-Grid to interact with the Blockchain:
import TronWeb from 'TronWeb'
const HttpProvider = TronWeb.providers.HttpProvider; // Optional provider, can just use a url for the nodes instead
const fullNode = new HttpProvider('https://api.trongrid.io:8090'); // Full node http endpoint
const solidityNode = new HttpProvider('https://api.trongrid.io:8091'); // Solidity node http endpoint
const eventServer = 'https://api.trongrid.io/'; // Contract events http endpoint
const privateKey = 'da146374a75310b9666e834ee4a...c76217c5a495fff9f0d0';
const tronWeb = new TronWeb(
fullNode,
solidityNode,
eventServer,
privateKey
);
// The majority of the function calls are asynchronus,
// meaning that they cannot return the result instantly.
// These methods therefore return a promise, which you can await.
const balance = await tronWeb.trx.getBalance(address);
console.log({ balance });
// You can also bind a `then` and `catch` method.
tronWeb.trx.getBalance(address).then(balance => {
console.log({ balance });
}).catch(err => console.error(err));
// If you'd like to use a similar API to Web3, provide a callback function.
tronWeb.trx.getBalance(address, (err, balance) => {
if(err)
return console.error(err);
console.log({ balance });
});
Updated almost 6 years ago
What’s Next