首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >交互智能合约

交互智能合约
EN

Stack Overflow用户
提问于 2020-11-30 10:58:27
回答 2查看 221关注 0票数 1

我是第一次接触区块链和智能合约,我在理解一些概念时遇到了困难。我必须设计一个基于智能合约的决策架构。我必须根据天气情况来管理酒店预订。天气信息由先知提供。在先知收集了天气数据之后,会发生什么?是否有oracle智能合约可以与我的智能合约进行通信?

EN

回答 2

Stack Overflow用户

发布于 2020-11-30 15:27:22

如果你想使用以太坊,最简单的方法是从你的智能合约中调用oracle的一些视图方法,并获得所需的数据。下面显示了一个示例。

代码语言:javascript
运行
复制
 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) ;
       }
    
    }
票数 1
EN

Stack Overflow用户

发布于 2021-01-01 17:17:53

在这里,我将概略地描述一种将事件传输到oracle的变体。最后列出了相应的智能合约。操作顺序如下:

  1. 在网络上部署合同天气,其address.This必须从Oracle节点拥有的帐户获取。
  2. 将合同天气的地址从(1)输入Booking合同的原始文本到WeatherAddr变量中。编译Booking,并在调用contract

Booking

  1. BookRequest方法时将其部署到网络以下是在执行< code >E138E239合同的天气合同的天气合同的GetWeather<代码>E237方法时,通过传输请求ID和位置代码,以及从具有(3)的GetWeather<代码>方法中接收到的资金,编译GetWeather BookRequest中的请求上下文RequestWeather事件被发起,在该事件中发送请求数据,请求合同Booking
  2. The的地址Oracle节点捕获RequestWeather事件。为此,可以使用 RPC或eth_getFilterLogs eth_getLogs方法(或它们在web3接口中的类似方法)。为了避免分叉,我在最后一个块后面使用了eth_getLogs 5-6块。
  3. Oracle节点从事件数据中提取location数据,并确定所需的information.
  4. The Oracle节点是否通过调用CallBack方法并将请求ID和天气数据传递到事件数据中传递的Booking合同地址来形成事务。当执行<代码>E166合同的回调方法时,合同,以下是请求上下文的performed:
  • restoration,按其标识符从请求的mapping
  • processing请求中获得。根据接收到的天气data
  1. Periodically,,节点通过调用SendMoneyOwner方法将从Booking contracts收到的资金转移到其自己的帐户,从而形成天气 contract的事务处理。

代码语言:javascript
运行
复制
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 ;
       }
    
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65067583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档