summaryrefslogtreecommitdiff
path: root/Year_3
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-06-16 16:14:46 +0200
committerSanto Cariotti <santo@dcariotti.me>2022-06-16 16:14:46 +0200
commita5e27da469c6224179b6a27ad89d0c6b534b5ae1 (patch)
tree135eed39c736545c611aecacc14755f7dc48afcc /Year_3
parent04c61dba3db9a885a6955ec5433d9ac77f2345b6 (diff)
Add exercise
Diffstat (limited to 'Year_3')
-rw-r--r--Year_3/Blockchain/RandomMoney.sol43
1 files changed, 43 insertions, 0 deletions
diff --git a/Year_3/Blockchain/RandomMoney.sol b/Year_3/Blockchain/RandomMoney.sol
new file mode 100644
index 0000000..2a5def4
--- /dev/null
+++ b/Year_3/Blockchain/RandomMoney.sol
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: MIT
+
+pragma solidity >0.7 <0.9;
+
+contract RandomMoney {
+ uint public total;
+ uint public constant minFee = 10;
+ mapping(address => uint) public amounts;
+ address[] private members;
+
+
+ function play() public payable returns (uint) {
+ require(msg.value >= minFee, "Value is less than the required minimum");
+
+
+ amounts[msg.sender] += msg.value;
+
+ if (amounts[msg.sender] == msg.value) {
+ members.push(msg.sender);
+ }
+
+ total += msg.value;
+
+ return total;
+ }
+
+ function pay() public payable {
+ address payable account = payable(randomAddress());
+ account.transfer(total);
+ total = 0;
+ }
+
+ function randomAddress() internal view returns (address) {
+ uint8 num = uint8(
+ uint256(
+ keccak256(abi.encode(block.timestamp, block.difficulty))
+ ) % members.length
+ );
+
+ return members[num];
+ }
+}
+