智能合约不断收听价格反馈并在达到价格后立即执行的最佳方式是什么?
What is the best way for a smart contract to constantly listen to a price feed and execute immediately once a price is reached?
我编写了一个智能合约,它应该从 2 个地址下注,一个下注者和一个下注者。赌注是 ETH/USD 的价格(通过 ChainLink)。
让智能合约不断监听 ETH/USD 价格的最佳方式是什么,这样每当价格到达赌注的一方或另一方时,合约就会 generateBetOutcome()
自动?
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;
}
}
智能合约无法访问区块链本身之外的任何内容。 仅 方法是使用 oracle.
Oracle 只是一个普通软件(您可以用 C++ 或 PHP 或 Java 或任何您喜欢的语言编写),它可以访问外部资源,例如 ETH/USD ChainLink 然后根据您编写的逻辑在满足条件时调用您的智能合约上的方法。
为了确保只有您的 oracle 可以调用该方法(例如调用 generateBetOutcome
)并避免第 3 方通过过早调用该方法来作弊,您可以编写代码来验证调用者是您的 oracle。
我编写了一个智能合约,它应该从 2 个地址下注,一个下注者和一个下注者。赌注是 ETH/USD 的价格(通过 ChainLink)。
让智能合约不断监听 ETH/USD 价格的最佳方式是什么,这样每当价格到达赌注的一方或另一方时,合约就会 generateBetOutcome()
自动?
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;
}
}
智能合约无法访问区块链本身之外的任何内容。 仅 方法是使用 oracle.
Oracle 只是一个普通软件(您可以用 C++ 或 PHP 或 Java 或任何您喜欢的语言编写),它可以访问外部资源,例如 ETH/USD ChainLink 然后根据您编写的逻辑在满足条件时调用您的智能合约上的方法。
为了确保只有您的 oracle 可以调用该方法(例如调用 generateBetOutcome
)并避免第 3 方通过过早调用该方法来作弊,您可以编写代码来验证调用者是您的 oracle。