Interaction smart contract

Viewed 315

I am approaching blockchain and smart contracts for the first time and I have trouble understanding some concepts. I have to design a decision making architecture based on smart contracts. I have to manage hotel reservations based on the weather. The weather information is provided by the oracle. After the oracle has collected the weather data what happens? Is there an oracle smart contract that communicates with my smart contract?

2 Answers

If you want to use Ethereum, the easiest way is to call the oracle some view-method from your smart contract and get the required data. An example is shown below.


 pragma solidity >=0.5.8 <0.6.0;
    
    contract Booking
    {
    
        Weather  WeatherAddr ;


       constructor() public
       {
       }
    
       function AnyFunction(bytes32  place_) public
       {
          int256  Conditions ;
          int256  Temperature ;

    
            (Conditions, Temperature)=WeatherAddr.GetWeather(place_) ;

//     ...
    
       }
    
    }
    
    contract Weather
    {

      struct PlaceWeather
      {
          int256  Temperature ;
          int256  Conditions ;
      }

    mapping (bytes32 => PlaceWeather) Places ;

    
       constructor() public
       {
       }


       function GetWeather(bytes32 place_) public view returns (int256, int256  retVal)
       {
          return(Places[place_].Conditions, Places[place_].Temperature) ;
       }
    
    }

Here I will schematically describe a variant with the transmission of an event to a oracle. The corresponding smart contracts are listed at the end. The sequence of actions is as follows:

  1. Deploy the contract Weather on the network and get its address.This must be done from the account owned by the Oracle node.
  2. Enter the address of the contract Weather from (1) into the original text of the Booking contract into the WeatherAddr variable, compile Booking and deploy it to the network
  3. When calling the method BookRequest of the contract Booking the following is executed:
  • saving the context of the request in the Requests mapping
  • calling the method GetWeather of the contract Weather with the transfer of the request ID and the location code, as well as the funds received from the transaction
  1. Simultaneously with (3), when the GetWeather method of the Weather contract is executed, the RequestWeather event is initiated, in which send the request data and the address of the requesting contract Booking
  2. The Oracle node captures the RequestWeather event. For this, the eth_getLogs or eth_getFilterLogs RPC API methods (or their analogs from the web3 interface) can be used. To avoid forks, I use eth_getLogs 5-6 blocks behind the last block.
  3. The Oracle node extracts location data from the event data and determines the required weather information.
  4. The Oracle node forms a transaction to the Booking contract address passed in the event data by calling the CallBack method and passing the request ID and weather data there.
  5. When executing the CallBack method of the Booking contract, the following is performed:
  • restoration of the request context by its identifier from the Requests mapping
  • processing of the request in accordance with the received weather data
  1. Periodically, the Oracle node forms a transaction for the Weather contract by calling the SendMoneyOwner method to transfer funds received from Booking contracts to its own account.
pragma solidity >=0.5.8 <0.6.0;
    
    contract Booking
    {

             Weather  WeatherAddr=0x00 ;  // Address of contract Weather

      struct Request
      {
          bytes32  Location ;
          bytes32  Attr1 ;
           int256  Attr2 ;
      }

    mapping (bytes32 => Request) Requests ;

       constructor() public
       {
       }
    
       function BookRequest(bytes32  id_, bytes32  location_, bytes32  attr1, int256  attr2) public payable
       {
           bool  result ;

            Requests[id_]=Request({ Location: location_, 
                                       Attr1: attr1,
                                       Attr2: attr2     }) ;

            (result,)=address(WeatherAddr).call.gas(0x300000).value(msg.value)(abi.encodeWithSignature("GetWetaher(bytes32,bytes32)", id_, location_)) ;
          if(result==false)  require(false) ;
       }

       function CallBack(bytes32  id_, int256  tempirature_) public
       {
//          1. Restore request context from Requests[id_]
//          2. Process request for booking
       }
    
    }
    
    contract Weather
    {

        address  Owner ; 
        uint256  Value ;

         event RequestWeather(address booking, bytes32 id, bytes32 location) ;

       constructor() public
       {
           Owner=tx.origin ;
       }


       function GetWeather(bytes32  id_, bytes32  location_) public payable 
       {
           Value+=msg.value ;

          emit RequestWeather(msg.sender, id_, location_) ;
       }
    
       function SendMoneyOwner() public
       {
           bool  result ;
               
            (result,)=Owner.call.gas(0x30000).value(Value)("") ;
          if(result==false)  require(false) ;

           Value=0 ;
       }
    
    }
    
Related