Solidity
Solidity中的載入模具算法?
我正在嘗試實現一個智能合約,它接受一個謹慎的分佈並返回一個載入的隨機數。數字越高,1-10000 的機會就越高。
**有人對此有任何經驗或想法嗎?
這是“使用乙太坊智能合約建構遊戲”的範例。
contract SatoshiDice { struct Bet { address user; uint block; uint cap; uint amount; } uint public constant FEE_NUMERATOR = 1; uint public constant FEE_DENOMINATOR = 100; uint public constant MAXIMUM_CAP = 100000; uint public constant MAXIMUM_BET_SIZE = 1e18; address owner; uint public counter = 0; mapping(uint => Bet) public bets; event BetPlaced(uint id, address user, uint cap, uint amount); event Roll(uint id, uint rolled); function SatoshiDice () public { owner = msg.sender; } function wager (uint cap) public payable { require(cap <= MAXIMUM_CAP); require(msg.value <= MAXIMUM_BET_SIZE); counter++; bets[counter] = Bet(msg.sender, block.number + 3, cap, msg.value); BetPlaced(counter, msg.sender, cap, msg.value); } function roll(uint id) public { Bet storage bet = bets[id]; require(msg.sender == bet.user); require(block.number >= bet.block); require(block.number <= bet.block + 255); bytes32 random = keccak256(block.blockhash(bet.block), id); uint rolled = uint(random) % MAXIMUM_CAP; if (rolled < bet.cap) { uint payout = bet.amount * MAXIMUM_CAP / bet.cap; uint fee = payout * FEE_NUMERATOR / FEE_DENOMINATOR; payout -= fee; msg.sender.transfer(payout); } Roll(id, rolled); delete bets[id]; } function fund () payable public {} function kill () public { require(msg.sender == owner); selfdestruct(owner); } }
由於對這種合約的解釋超出了 StackExchange 的答案,我建議閱讀這本書和/或更多關於鏈上隨機數生成的內容。