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.sol
contract which returns a randomly generated id. - Node listens for
GetLatestEthPrice
event ofEthPriceOracle.sol
contract 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
PriceUpdated
event ofCaller.sol
to 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_KEY
PROVIDER_URL
ETH_ORACLE_ADDRESS
To deploy contracts, you will need to add the following environment variables to your .env file:
PRIVATE_KEY
MUMBAI_URL
To deploy the contracts run
npm install
npx hardhat run scripts/deploy.ts --network [NETWORK_NAME]
To start node on local run
npm start
To 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 mumbai
The above command will deploy EthPriceOracl.sol
and Caller.sol
contract. It will also update threshold to 1.
- Add/Update
ETH_ORACLE_ADDRESS
andCALLER_ADDRESS
in.env
file which you get from above command. - Add/Update
ORACLE_PRIVATE_KEY
in .env file. - Grant ORACLE_ROLE to ORACLE_PRIVATE_KEY address. Change ORACLE_ADDRESS in
scripts/addOracle.ts
file.
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