forked from ciaranmcveigh5/ethernaut-x-foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fallback.sol
46 lines (36 loc) · 1.4 KB
/
Fallback.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10; // Latest solidity version
import 'openzeppelin-contracts/contracts/utils/math/SafeMath.sol'; // Path change of openzeppelin contract
contract Fallback {
using SafeMath for uint256;
mapping(address => uint) public contributions;
address payable public owner;
constructor() public {
owner = payable(msg.sender); // Type issues must be payable address
contributions[msg.sender] = 1000 * (1 ether);
}
modifier onlyOwner {
require(
msg.sender == owner,
"caller is not the owner"
);
_;
}
function contribute() public payable {
require(msg.value < 0.001 ether, "msg.value must be < 0.001"); // Add message with require
contributions[msg.sender] += msg.value;
if(contributions[msg.sender] > contributions[owner]) {
owner = payable(msg.sender); // Type issues must be payable address
}
}
function getContribution() public view returns (uint) {
return contributions[msg.sender];
}
function withdraw() public onlyOwner {
owner.transfer(address(this).balance);
}
fallback() external payable { // naming has switched to fallback
require(msg.value > 0 && contributions[msg.sender] > 0, "tx must have value and msg.send must have made a contribution"); // Add message with require
owner = payable(msg.sender); // Type issues must be payable address
}
}