Blockchain 对于智能合约来说,什么是不断听取价格反馈并在价格达到时立即执行的最佳方式?
我已经编写了一个智能合约,该合约应该从两个地址下注,一个是投注创建者,一个是投注者。赌注是ETH/美元(通过链接) 智能合约的最佳方式是不断听取ETH/USD的价格,以便无论何时价格达到赌注的一方或另一方,合约都会自动Blockchain 对于智能合约来说,什么是不断听取价格反馈并在价格达到时立即执行的最佳方式?,blockchain,ethereum,solidity,smartcontracts,decentralized-applications,Blockchain,Ethereum,Solidity,Smartcontracts,Decentralized Applications,我已经编写了一个智能合约,该合约应该从两个地址下注,一个是投注创建者,一个是投注者。赌注是ETH/美元(通过链接) 智能合约的最佳方式是不断听取ETH/USD的价格,以便无论何时价格达到赌注的一方或另一方,合约都会自动generatebetootcome() pragma solidity ^0.8.4; import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/i
generatebetootcome()
pragma solidity ^0.8.4;
import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract Bet {
//bet status
uint constant STATUS_WIN = 1;
uint constant STATUS_LOSE = 2;
uint constant STATUS_TIE = 3;
uint constant STATUS_PENDING = 4;
//game status
uint constant STATUS_NOT_STARTED = 1;
uint constant STATUS_STARTED = 2;
uint constant STATUS_COMPLETE = 3;
//general status
uint constant STATUS_ERROR = 4;
//the betting structure
struct DoubleBet {
uint guess;
address addr;
uint status;
}
//the 'game' structure
struct Game {
uint256 betAmount;
uint outcome;
uint status;
DoubleBet creator;
DoubleBet taker;
}
Game game;
receive() external payable {
}
address payable owner;
AggregatorV3Interface internal priceFeed;
/**
* Network: Kovan
* Aggregator: ETH/USD
* Address: 0x9326BFA02ADD2366b30bacB125260Af641031331
*/
constructor() public {
priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
}
function createBet(uint _guess) public payable {
game = Game(msg.value, 0, STATUS_STARTED, DoubleBet(_guess, msg.sender, STATUS_PENDING), DoubleBet(0, msg.sender, STATUS_NOT_STARTED));
game.creator = DoubleBet(_guess, msg.sender, STATUS_PENDING);
}
function takeBet(uint _guess) public payable {
//requires the taker to make the same bet amount
require(msg.value == game.betAmount);
game.taker = DoubleBet(_guess, msg.sender, STATUS_PENDING);
generateBetOutcome();
}
function generateBetOutcome() private {
game.outcome = uint(getThePrice());
game.status = STATUS_COMPLETE;
if (game.creator.guess == game.taker.guess) {
game.creator.status = STATUS_TIE;
game.taker.status = STATUS_TIE;
} else if (game.creator.guess > game.outcome && game.taker.guess > game.outcome) {
game.creator.status = STATUS_TIE;
game.taker.status = STATUS_TIE;
} else {
if ((game.outcome - game.creator.guess) < (game.outcome - game.taker.guess)) {
game.creator.status = STATUS_WIN;
game.taker.status = STATUS_LOSE;
} else if ((game.outcome - game.taker.guess) < (game.outcome - game.creator.guess)) {
game.creator.status = STATUS_LOSE;
game.taker.status = STATUS_WIN;
} else {
game.creator.status = STATUS_ERROR;
game.taker.status = STATUS_ERROR;
game.status = STATUS_ERROR;
}
}
}
//returns - [<description>, 'originator', <originator status>, 'taker', <taker status>]
function getBetOutcome() public view returns
(string memory description, string memory originatorKey, uint originatorStatus, string memory takerKey, uint takerStatus)
{
if (game.creator.status == STATUS_TIE || game.taker.status == STATUS_TIE) {
description = "Both bets were the same or were over the number, the pot will be split";
} else {
if (game.creator.status == STATUS_WIN) {
description = "Bet originator guess was closer to the number and will receive the pot";
} else if (game.taker.status == STATUS_WIN) {
description = "Bet taker guess was closer to the number and will receive the pot";
} else {
description = "Unknown Bet Outcome";
}
}
originatorKey = "creator";
originatorStatus = game.creator.status;
takerKey = "taker";
takerStatus = game.taker.status;
}
/**
* Returns the latest price
*/
function getThePrice() public view returns (int) {
(
uint80 roundID,
int price,
uint startedAt,
uint timeStamp,
uint80 answeredInRound
) = priceFeed.latestRoundData();
return price;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function getBalance() public view returns (uint balance) {
return address(this).balance;
}
}
pragma-solidity^0.8.4;
进口”https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
合同赌注{
//下注状态
uint常量状态_WIN=1;
uint常量状态_LOSE=2;
uint恒定状态=3;
uint常量状态_PENDING=4;
//游戏状态
uint常量状态未启动=1;
uint常量状态_STARTED=2;
uint常量状态_COMPLETE=3;
//一般状况
uint常量状态_错误=4;
//投注结构
结构双重赌注{
不要猜测;
地址地址;
uint状态;
}
//“游戏”结构
结构游戏{
uint256 betAmount;
结果;
uint状态;
双重赌注创造者;
双重赌注接受者;
}
游戏;
收到()外部应付款项{
}
应付业主地址;
AggregatorV3接口内部价格源;
/**
*网络:科万
*聚合器:ETH/美元
*地址:0x9326BFA02AD2366B30BACB15260AF641031331
*/
构造函数()公共{
priceFeed=AggregatorV3接口(0x9326BFA02AD2366B30BACB15260AF641031331);
}
函数createBet(uint _guess)公共支付{
游戏=游戏(msg.value,0,状态为已启动,双注(\u猜测,msg.sender,状态为待定),双注(0,msg.sender,状态为未启动));
game.creator=DoubleBet(_guess,msg.sender,STATUS_PENDING);
}
函数takeBet(uint_guess)公共应付款{
//要求接受者进行相同的下注金额
要求(msg.value==game.betAmount);
game.taker=DoubleBet(_guess,msg.sender,STATUS_PENDING);
generateBetOutcome();
}
函数GenerateBeToutCode()private{
game.outcome=uint(getThePrice());
game.status=状态\完成;
if(game.creator.guess==game.taker.guess){
game.creator.status=status\u TIE;
game.taker.status=状态;
}else if(game.creator.guess>game.output&&game.taker.guess>game.output){
game.creator.status=status\u TIE;
game.taker.status=状态;
}否则{
if((game.output-game.creator.guess)<(game.output-game.taker.guess)){
game.creator.status=状态\胜利;
game.taker.status=状态\输;
}else if((game.output-game.taker.guess)<(game.output-game.creator.guess)){
game.creator.status=状态\失败;
game.taker.status=状态\胜利;
}否则{
game.creator.status=状态\错误;
game.taker.status=状态\错误;
game.status=状态\错误;
}
}
}
//返回-[,'发起人','接受者',]
函数getBetOutput()返回公共视图
(字符串内存描述、字符串内存起始工作、uint起始状态、字符串内存起始状态、uint起始状态)
{
if(game.creator.status==状态| | game.taker.status==状态|{
description=“两次下注相同或超过了号码,将拆分赌注”;
}否则{
如果(game.creator.status==status\u WIN){
description=“投注发起人猜测更接近数字,将收到赌注”;
}else if(game.taker.status==status\u WIN){
description=“投注者猜到的数字更接近,将收到赌注”;
}否则{
description=“未知下注结果”;
}
}
Originateworkey=“创建者”;
originatorStatus=game.creator.status;
takerKey=“taker”;
takerStatus=game.taker.status;
}
/**
*返回最新价格
*/
函数getThePrice()公共视图返回(int){
(
uint80圆形ID,
国际价格,
开始时,
uint时间戳,
uint80回答
)=priceFeed.latestRoundData();
退货价格;
}
仅修饰符所有者(){
要求(msg.sender==所有者);
_;
}
函数getBalance()公共视图返回(uint balance){
返回地址(此)。余额;
}
}
智能合约无法访问区块链本身之外的任何内容。唯一的方法是使用
<>甲骨文只是一个普通软件(可以用C++或PHP或者java编写,或者任何你喜欢的东西),它可以访问链接链接上的ETH/USD价格等外部资源,然后基于你写的逻辑,当条件满足时,调用智能协议的方法。
为了确保只有您的oracle可以调用该方法(例如调用
generatebetootcome
),并避免第三方过早调用该方法进行欺诈,您可以编写代码来验证调用方是您的oracle。智能合约无法访问区块链本身之外的任何内容。唯一的方法是使用
<>甲骨文只是一个普通软件(可以用C++或PHP或者java编写,或者任何你喜欢的东西),它可以访问链接链接上的ETH/USD价格等外部资源,然后基于你写的逻辑,当条件满足时,调用智能协议的方法。
确保只有oracle可以调用该方法(例如callin