The Oracle works on the basis of publisher-subscriber technique. Any dApp can generate request to get latest ETH price in USDT. Node (client) listens for generated request and reports price.
- EthPriceOracle.sol - Used by nodes(client).
- Caller.sol - Used by dApp.
- dApp calls
updateLatestPrice()ofCaller.solcontract which returns a randomly generated id. - Node listens for
GetLatestEthPriceevent ofEthPriceOracle.solcontract and starts reporting latest ETH price in USDT for request from Binance ticker. - As soon as responses for request id reaches to threshold value, mean of all the reported responses is considered as final price.
- dApp listens for
PriceUpdatedevent ofCaller.solto get the updated price. - dApp can also access the latest price reported by oracle by calling
getEthPrice().
To run node(client), you will need to add the following environment variables to your .env file:
ORACLE_PRIVATE_KEYPROVIDER_URLETH_ORACLE_ADDRESS
To deploy contracts, you will need to add the following environment variables to your .env file:
PRIVATE_KEYMUMBAI_URL
To deploy the contracts run
npm install
npx hardhat run scripts/deploy.ts --network [NETWORK_NAME]To start node on local run
npm startTo run tests, run the following command
npm run test- Deploy the contract to matic mumbai.(Add PRIVATE_KEY and MUMBAI_URL in
.env)
npx hardhat run scripts/deploy.ts --network mumbaiThe above command will deploy EthPriceOracl.sol and Caller.sol contract. It will also update threshold to 1.
- Add/Update
ETH_ORACLE_ADDRESSandCALLER_ADDRESSin.envfile which you get from above command. - Add/Update
ORACLE_PRIVATE_KEYin .env file. - Grant ORACLE_ROLE to ORACLE_PRIVATE_KEY address. Change ORACLE_ADDRESS in
scripts/addOracle.tsfile.
npx hardhat run --network mumbai scripts/addOracle.ts- Start running node.
npm start- Generate a requestId to be reported by oracle
npx hardhat run scripts/getEthPrice.ts --network mumbai
As soon as the above tx is confirmed the node will report eth price from API and report to oracle.
- Latest eth price reported by oracle can be retrieved with the following command.
npx hardhat run scripts/getLatestEthPrice.ts --network mumbai