Skip to content

Latest commit

 

History

History
116 lines (93 loc) · 5.39 KB

06.md

File metadata and controls

116 lines (93 loc) · 5.39 KB
title actions requireLogin material
The Callback Function
checkAnswer
hints
true
editor
language startingCode answer
sol
caller/CallerContract.sol caller/EthPriceOracleInterface.sol
pragma solidity 0.5.0; import "./EthPriceOracleInterface.sol"; import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; contract CallerContract is Ownable { // 1. Declare ethPrice EthPriceOracleInterface private oracleInstance; address private oracleAddress; mapping(uint256=>bool) myRequests; event newOracleAddressEvent(address oracleAddress); event ReceivedNewRequestIdEvent(uint256 id); // 2. Declare PriceUpdatedEvent function setOracleInstanceAddress (address _oracleInstanceAddress) public onlyOwner { oracleAddress = _oracleInstanceAddress; oracleInstance = EthPriceOracleInterface(oracleAddress); emit newOracleAddressEvent(oracleAddress); } function updateEthPrice() public { uint256 id = oracleInstance.getLatestEthPrice(); myRequests[id] = true; emit ReceivedNewRequestIdEvent(id); } function callback(uint256 _ethPrice, uint256 _id) public { // 3. Continue here } }
pragma solidity 0.5.0; contract EthPriceOracleInterface { function getLatestEthPrice() public returns (uint256); }
pragma solidity 0.5.0; import "./EthPriceOracleInterface.sol"; import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; contract CallerContract is Ownable { uint256 private ethPrice; EthPriceOracleInterface private oracleInstance; address private oracleAddress; mapping(uint256=>bool) myRequests; event newOracleAddressEvent(address oracleAddress); event ReceivedNewRequestIdEvent(uint256 id); event PriceUpdatedEvent(uint256 ethPrice, uint256 id); function setOracleInstanceAddress (address _oracleInstanceAddress) public onlyOwner { oracleAddress = _oracleInstanceAddress; oracleInstance = EthPriceOracleInterface(oracleAddress); emit newOracleAddressEvent(oracleAddress); } function updateEthPrice() public { uint256 id = oracleInstance.getLatestEthPrice(); myRequests[id] = true; emit ReceivedNewRequestIdEvent(id); } function callback(uint256 _ethPrice, uint256 _id) public { require(myRequests[_id], "This request is not in my pending list."); ethPrice = _ethPrice; delete myRequests[_id]; emit PriceUpdatedEvent(_ethPrice, _id); } }

The caller contract logic is almost complete, but there's one more thing you should take care of.

As mentioned in the previous chapter, calling the Binance public API is an asynchronous operation. Thus, the caller smart contract must provide a callback function which the oracle should call at a later time, namely when the ETH price is fetched.

Here's how the callback function works:

  • First, you would want to make sure that the function can only be called for a valid id. For that, you'll use a require statement.

    Simply put, a require statement throws an error and stops the execution of the function if a condition is false.

    Let's look at an example from the Solidity official documentation:

    require(msg.sender == chairperson, "Only chairperson can give right to vote.");

    The first parameter evaluates to true or false. If it's false, the function execution will stop and the smart contract will throw an error- "Only chairperson can give right to vote."

  • Once you know that the id is valid, you can go ahead and remove it from the myRequests mapping.

    Note: To remove an element from a mapping, you can use something like the following: delete myMapping[key];

  • Lastly, your function should fire an event to let the front-end know the price was successfully updated.

Put It to the Test

We've gone ahead and declared a public function called callback. It takes two arguments of type uint256: _ethPrice and _id.

  1. After the line that declares your smart contract, create a uint256 variable called ethPrice. Let's make it private and don't assign it to anything yet.

  2. Create an event called PriceUpdatedEvent. It takes two parameters: ethPrice and id. Both parameters are of type uint256.

Next, let's fill in the body of the callback function.

  1. The function should first check to make sure myRequests[id] is true. Use a require statement to do this. The first parameter should be myRequests[id] and the second parameter should be "This request is not in my pending list."

  2. Next, save the new ETH price (the one that comes from the function parameters) into the ethPrice variable.

  3. The function should then call delete to remove the current id from the myRequests mapping.

  4. Lastly, let's fire the PriceUpdatedEvent. The arguments should come from the function arguments.

This was a lot of logic to implement, but there's still something you've left out. Can you guess what?

Give it a thought before moving to the next chapter.