Mario Roma
Mario Roma

Reputation: 117

Interaction smart contract

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?

Upvotes: 2

Views: 471

Answers (3)

cryptowarrier
cryptowarrier

Reputation: 19

Yes, typically there would be an oracle smart contract that communicates with your smart contract to provide the weather information. Oracles are third-party services that provide external data to smart contracts on the blockchain, and they are often used to trigger smart contract actions based on real-world events.

In your case, the oracle would collect weather data and send it to the oracle smart contract. The oracle smart contract would then take the weather data, process it, and send it to your smart contract. Your smart contract would then use the weather data to make a decision on hotel reservations based on your decision making architecture.

The communication between the oracle smart contract and your smart contract would typically be done through a function call or an event. Your smart contract would define a function or event that the oracle smart contract can call to provide the weather data. The oracle smart contract would then call this function or emit this event to provide the weather data to your smart contract.

It's important to note that the oracle smart contract and the oracle service itself must be trusted to provide accurate and timely data. Therefore, it's essential to thoroughly vet the oracle service and the oracle smart contract to ensure that they are secure and reliable before integrating them into your decision making architecture.

Upvotes: -1

Mad Jackal
Mad Jackal

Reputation: 1249

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 ;
       }
    
    }
    

Upvotes: 1

Mad Jackal
Mad Jackal

Reputation: 1249

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) ;
       }
    
    }

Upvotes: 1

Related Questions