我正试图完成一个在线Solidity训练营,但我很难弄清楚这个特定的问题到底在问我什么。问题如下:
";在Bet Contract上写一个外部的、可支付的函数placeBet。此功能应采用游戏。团队选择参数,并返回代表投注支出的uint256。
使用calculatePayout函数来确定赌注的支出。该函数的输入将是发送到placeBet函数的以太币数量和由getScoreDifference函数确定的分数差">
以下是正在使用的有问题的合同:
contract Game {
int public team1Score;
int public team2Score;
enum Teams { Team1, Team2 }
function addScore(Teams teamNumber) external {
// TODO: add score to the specified team
if (teamNumber == Teams.Team1) {
team1Score += 1;
} else if (teamNumber == Teams.Team2) {
team2Score += 1;
}
}
}
和主要问题合同:
import "./Game.sol";
contract Bet {
Game public game;
constructor(address _gameAddress) {
game = Game(_gameAddress);
}
// calculates the payout of a bet based on the score difference between the two teams
function calculatePayout(uint amount, int scoreDifference) private pure returns(uint) {
uint abs = uint(scoreDifference > 0 ? scoreDifference : scoreDifference * -1);
uint odds = 2 ** abs;
if(scoreDifference < 0) {
return amount + amount / odds;
}
return amount + amount * odds;
}
function getScoreDifference(Game.Teams x) public view returns (int256) {
if (x == Game.Teams.Team1) {
return game.team1Score() - game.team2Score();
} else if (x == Game.Teams.Team2) {
return game.team2Score() - game.team1Score();
} return game.team1Score();
}
function placeBet(Game.Teams x) external payable returns (uint256) {
}
}
因此,问题是要求我"使用calculatePayout函数来确定赌注的支出。该函数的输入将是发送到placeBet函数的以太币数量和由getScoreDifference函数确定的分数差">
我把函数名、参数等写对了(我想(,但我不知道该在正文中包含什么,因为这个问题的措辞不太好。任何帮助都将是伟大的!谢谢
这就是我根据问题编写它的方式:
function placeBet(Game.Teams x) external payable returns (uint256) {
int256 _diff = getScoreDifference(x);
return calculatePayout(msg.value, _diff);
}
然而,在生产代码中,这是没有意义的,因为用户正在为赌注付费,但这并没有在任何地方注册。但这个问题没有提到写一个完整的博彩系统,所以我想这没关系