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.

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 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(string memory arg) 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);
}
}