Developers use our platform to develop oracles to provide data for their applications. If your smart contract use case requires external data, you can use the TruSource platform to develop oracles for the and ThunderCore blockchains following the guide below.
Generation
Let’s start, go to the and provide an email, this will be used to authenticate you, no passwords needed!
Once logged in, you will see that no oracles have been created yet, let’s create one! Click Create Oracle.
Design
Here, Oracle Name is used to name the oracles you create and you can connect to a maximum of 5 APIs per oracle. For each request, provide a name that is used for the corresponding smart contract function, a HTTP method and URL. Ensure you name the requests uniquely and provide a valid URL.
Currently we support public APIs with support for authenticated APIs coming soon. The URLs provided can include a query string, we are adding support for building the query string dynamically using arguments.
Develop
Next select the platform, the network and generate! Currently we support integration with the and ThunderCore blockchains.
Your Oracle
Your oracle has been generated! We have generated a truffle project and monitoring server scripts. The truffle project includes three contracts.
Example.sol is an example contract that makes a query using your oracle. Making a query returns a queryId which you use to keep track of the query. A smart contract using your oracle must implement a trusource_callback function which is used to receive a response from your monitoring server. Within trusource_callback, you define how you handle the data you have requested using the queryId that is passed to identify the query.
OracleAPI.sol is an abstract contract that is inherited by Example.sol, it defines the interface to your oracle and abstracts the methods required from your smart contract. This limits the amount of oracle related logic required in your smart contract so you can focus on core functionality.
Oracle.sol is where your oracle is implemented. It maintains a mapping of contract addresses that can make requests to your oracle. Whenever a request is made by a smart contract, the LogQuery event is emitted.
Testing
We have setup the project and provided scripts to enable local testing. The instructions will set out running a local instance, deploying your contracts, running the monitoring server and demonstrating that it can listen and respond to queries. For local testing you will need to install the following dependencies:
- Node
- npm
- Ganache-cli
- Truffle v 5.0.0+
First unzip the downloaded folder, in code you will have two sub folders: server and truffle.
In a terminal run the command:
ganache-cli -b 1
This will run a local instance on port 8545 with a 1 second block-time.
Open a new terminal and navigate to the truffle directory and run:
npm install
to install the dependencies. Then run:
truffle migrate
to deploy your contracts to your local .
Next, open a new terminal and navigate to the server directory and run:
npm install
to install dependencies, then run:
npm run start
This will start the monitoring server which is listening for events emitted by your oracle and is ready to respond with the fetched result.
Finally switch back to your terminal in the truffle directory and run:
truffle exec ./server/call.js
This will execute a script that will make a query to your oracle by calling the query method in the Example.sol contract. Your server will catch the emitted event and respond with the data!
You’ve just created your first oracle and tested it using an example contract. We welcome any feedback, contact us
Published at Wed, 13 Mar 2019 01:09:54 +0000