As I understood in the blockchain, it is "not" possible to interact with webservices outside the blockchain. This must be done over oracles in order to verfiy that the request is secure, and the state change can be done in the blockchain. Please correct me, if I am wrong.
I am currently working with Testrpc, Truffle, and React in order to test my smart contract. Now I want to integrate oraclize.it into my environment. What is the best way to do this?
I tried to copy the testexamples into my Contract folder, but in the contract there is this reference:
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
How can I integrate this reference to my folder structure?
I tried to copy the following files to my contract folder:
https://github.com/oraclize/ethereum-api/
But I got a couple of errors.
What's the best way to implement Oraclize with Truffle and Testrpc/Ganache?
Best Answer
It is very possible to use the Oraclize service with testrpc alongside truffle.
Firstly, you need to follow the naming convention of contract filenames in truffle. As per their docs:
Therefore you should download the Oraclize API from the Oraclize github repo, here's a specific link to the file oraclizeAPI_0.4.sol. Download this to the truffle contracts folder of your working directory, and rename the file to
usingOraclize.sol
. This is required so at compilation time, truffle knows which specific contract to compile from within the file. The other alternative is to directly paste all of the API into the required contract, but adding and renaming the API itself is likely the most elegant solution I'm aware of at the moment.Now you will be able to import the API on your other contracts within that folder by including
import "usingOraclize.sol"
at the beginning of them, and then specifying its inheritance on the proper contract within, assuming the name of the example contract you need the API in is namedmyContract
, the inheritance method should looks likeThe next step is making it so that your private testrpc chain is able to communicate with the Oraclize service, giving it the ability to send requests and receive the results back. For this, you will need the ethereum-bridge. Git clone that repository into a separate folder somewhere. What the bridge will do is, as its name suggest, setup a bridge for you between the Oraclize service and your blockchain while it runs.
Once ethereum-bridge is properly set-up, testrpc should be configured to use a static set of addresses, as the bridge will be dependent upon a specified address for deploying its required contracts on your private testrpc chain, therefore if you want those addresses to stay constant, between testrpc runs, you should be running testrpc with some re-usable addresses. You can do this by adding the
--mnemonic
flag when starting it, and using the same mnemonic phrase over when restarting testrpc, to ensure it generates the same Oraclize OAR (OraclizeAddressResolver), which you will need to use in the next step.Now, we will need to find out the OAR that gets generated by the bridge.
testrpc --mnemonic "my test example" --accounts 50
Since 50 accounts are generated, the last address' index is 49.
node bridge -a 49
The following part of step 2 only needs to be done on your first-run
Now wait, until you see the line:
Your address should of course be different, as you'd be using a different mnemonic phrase. Add that specific line, right at the beginning of your
myContract
constructor so that it looks like this: