我是第一次接触区块链和智能合约,我在理解一些概念时遇到了困难。我必须设计一个基于智能合约的决策架构。我必须根据天气情况来管理酒店预订。天气信息由先知提供。在先知收集了天气数据之后,会发生什么?是否有oracle智能合约可以与我的智能合约进行通信?
发布于 2020-11-30 15:27:22
如果你想使用以太坊,最简单的方法是从你的智能合约中调用oracle的一些视图方法,并获得所需的数据。下面显示了一个示例。
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) ;
}
}
发布于 2021-01-01 17:17:53
在这里,我将概略地描述一种将事件传输到oracle的变体。最后列出了相应的智能合约。操作顺序如下:
Booking
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 ;
}
}
https://stackoverflow.com/questions/65067583
复制相似问题