Logo

Quick Start

Integrating TRON Smart Contracts with Bridge Oracle System

In order to integrate a TRON smart contract with Bridge oracle system, previous knowledge of Solidity language and TRON ecosystem is required. Interaction between TRON smart contracts and Bridge oracle system is an asynchronous procedure. Any kind of request on behalf of user which is sent by their smart contract is subjected in two steps:

  1. In first step, user sends a request by executing a function and performing a transaction and broadcasting a query into TRON blockchain. Executed function has specific properties that is able to exactly manifest the user's request to Bridge oracle system which is constantly monitoring TRON blockchain for such requests.
  2. In second step, based on broadcasted parameters, Bridge recognizes what data is needed. Then Bridge fetches or computes corresponding result and after that it signs and broadcasts a transaction that injects data into the user's contract. In fact Bridge triggers __callback function (which should be put by users into their smart contracts) and injects data into this method. Of course note that only Bridge's oracle data carriers can trigger this function. Calling __callback method by other invalid sources is impossible.

Public Oracle

In this section, several examples of public oracle are presented in order to show how to use various data sources.

Cautious: It is better to use Nile testnet as a testing environment in comparison with Shasta testnet. Because there is no possibility of establishing a dedicated full node in Shasta testnet due to TRON foundation limitations. Therefore Bridge oracle is using trongrid public node for catching events and replying to requests in Shasta testnet; so, it is not completely efficient and it is possible that users' queries to be failed in it. However, Bridge oracle has a dedicated full node on Nile testnet which is completely reliable. Thus using Nile testnet is recommended for smart contract developers.

Connecting to Public Oracle Using BridgePublicAPI.sol

Before starting to develop your own contract, you need to take special actions to connect your main smart contract to public oracle. You need to import BridgePublicAPI.sol into your smart contract. There are two methods to integrate your contract with public oracle as follows:

  1. using TRON IDE upload tool
  2. copying the whole content into a *.sol file in local directory of main contract and import the file into the main smart contract

In first method you need to refer to www.tronide.io link and push GitHub button.

IDE

​ Then copy BridgePublicAPI.sol directory from Bridge Oracle System GitHub repository in the blank field which is something like https://github.com/cryptoland-blockchain-laboratory/Bridge-oracle-system/blob/master/BridgePublicAPI.sol and then click OK.

IDE2

​ In second method you can refer to BridgePublicAPI.sol file in GitHub and copy the full content into a *.sol file in local directory of your main smart contract as follows:

Content

​ Then you should import the BridgePublicAPI.sol into your main smart contract as follows:

Content2

​ Using one of the above methods, you can integrate your smart contract with public oracle of Bridge ecosystem. After integration you can develop your thoughts into your main smart contract.

URL Data Source

JSON Queries

Following example can fetch real-time exchange rate of BTC/USD from Coinbase website. Every time send_query() function is called, latest price of BTC/USD is injected into the smart contract.

pragma solidity ^0.5.9;
//Import "BridgePublicAPI.sol" from https://github.com/cryptoland-blockchain-laboratory/Bridge-oracle-system/ using TRON IDE upload tool or copy the whole content into a *.sol file in your smart contract's local directory and import the file into your main smart contract.
import "./BridgePublicAPI.sol";
contract priceFeed is BridgePublicAPI {
event price(uint256 _amount);
string public BTCUSD;
function send_query() public {
bridge_query("URL", "json(https://api.pro.coinbase.com/products/BTC-USD/ticker).price");
}
function __callback(bytes32 _myid, string memory _result) public {
require(msg.sender == oracle_cbAddress());
BTCUSD = _result;
uint256 res = parseInt(_result, 0);
emit price(res);
}
}

​ Note that this is a sample from user's smart contract. Above example includes several important sections:

  • It shows that user's smart contract needs to inherit from Bridge's Public oracle API (BridgePublicAPI.sol).
  • Any further actions and computations should be written in __callback function in order to be calculated based on fetched result. In this example BTCUSD state variable is filled by _result that is injected by Bridge oracle system.

XML Queries

In this example fetching country of a region is presented:

pragma solidity ^0.5.9;
import "./BridgePublicAPI.sol";
contract country is BridgePublicAPI {
event countryLog(string _amount);
string public countryName;
constructor() public {
countryName = "US";
}
function send_query() public {
bridge_query("URL", "xml(https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=439d4b804bc8187953eb36d2a8c26a02).current.city.country");
}
function __callback(bytes32 _myid, string memory _result) public {
require(msg.sender == oracle_cbAddress());
countryName = _result;
emit countryLog(_result);
}
}

As you can see, we initially filled countryName with US string but after executing send_query() function, the countryName was changed into GB .

HTML Queries

In this example fetching name of Wikipedia website is expected:

pragma solidity ^0.5.9;
import "./BridgePublicAPI.sol";
contract wikiName is BridgePublicAPI {
event LogWikiName(string _name);
string public name;
constructor() public {
send_query();
}
function send_query() public {
bridge_query("URL", 'html(https://wikipedia.org/).//*[contains(@class, 'central-textlogo__image')]/text()');
}
function __callback(bytes32 _myid, string memory _result) public {
require(msg.sender == oracle_cbAddress());
name = _result;
emit LogWikiName(_result);
}
}

Random Data Source

Using this data source, you can infuse a random digit between two numbers into your smart contract. For now and for test use cases in testnet, Bridge Oracle use random.org API, however we will implement a method to create random number from within a TEE (trusted execution environment like Ledger or Intel SGX) in order to be highly reliable for severe cases in main net in the future. This version is for test and temporary. You can infuse a random number like following:

pragma solidity ^0.5.9;
import "./BridgePublicAPI.sol";
contract randomNumber is BridgePublicAPI {
event myRandomNum(string randNum);
string public randomNum;
function send_query() public {
bridge_query("RANDOM", '1-10'); //The result of this format will be a random number between 1 and 10. Template is like following: bridge_query("RANDOM", 'minimum number-maximum number') This format will infuse a random number between min and max numbers in which min and max numbers are both included.
}
function __callback(bytes32 _myid, string memory _result) public {
require(msg.sender == oracle_cbAddress());
randomNum = _result;
emit myRandomNum(randomNum);
}
}

WolframAlpha Data Source

Using this data source, users are able to ask any knowledge-based questions in their smart contracts and Bridge Oracle will reply the questions based on information of WolframAlpha knowledge-based engine. One can infuse data from wolframalpha knowledge engine using "WOLFRAMALPHA" data source through following instructions:

pragma solidity ^0.5.9;
import "./BridgePublicAPI.sol";
contract president is BridgePublicAPI {
event presidentName(string name);
string public name;
function send_query() public {
bridge_query("WOLFRAMALPHA", 'who is 44th US president?'); //The result will be 44th president of US, 'Barack Obama'.
}
function __callback(bytes32 _myid, string memory _result) public {
require(msg.sender == oracle_cbAddress());
name = _result;
emit presidentName(name);
}
}