'Does this Solidity code actually create a random number each time or is the end result always the same?
This is code from a defi game. The point of this code is to generate a "coin flip". I feel like it is creating more winners than losers. Additionally, would this code not produce the same result each time considering it uses a USERS public wallet address (msg.sender) "constant number" as its base for the function?
For msg.sender variable you can use and ERC-20 wallet address ie
0xA2C8EB70b58D57Db442a2395e8aB80b640C655a7
If the entire contract is needed, it can be found here.
https://bscscan.com/address/0xc4661ce2ef5dd31a1e020985e7364708c1af03d5#code
//address(this).balance is increased by msg.value even before code is executed. Thus "address(this).balance-msg.value"
//Create a random number. Use the mining difficulty & the player's address, hash it, convert this hex to int, divide by modulo 2 which results in either 0 or 1 and return as uint8
uint8 result = uint8(uint256(keccak256(abi.encodePacked(block.difficulty, msg.sender, block.timestamp)))%2);
bool won = false;
if (guess == result) {
won = true;
uint256 win = (amount* 2/100*(100-houseedge));
tokenpiggies.transfer(msg.sender, win-amount);
}else{
tokenpiggies.transferFrom(msg.sender, address(this), amount);
}
emit GameResult(result);
lastPlayedGames.push(Game(msg.sender, amount, guess,3, won, block.timestamp));
return won; //Return value can only be used by other functions, but not within web3.js (as of 2019)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
