「The Ethernaut by Zeppelin Level 1. Fallback」を翻訳してみました(CryptoZombiesのように?)
原文はこちら
- Fallbackをクリック
1. フォールバック
難易度 1/6
下記のコントラクトコードをよく見るのだ。
このレベルのクリア条件は
- スマートコントラクトのオーナーを自分自身のアドレスに変更する
- コントラクトの残高を0にする
回答の助けになるヒント
- ABI(Application Binary Interface)とやり取りするときにether を送る方法
- ABIの外部にether を送る方法
- wei/etherとの間で変換する(help()コマンドで参照)
- フォールバックメソッドについて
ソースコード
pragma solidity ^0.4.18;
import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
contract Fallback is Ownable {
mapping(address => uint) public contributions;
function Fallback() public {
contributions[msg.sender] = 1000 * (1 ether);
}
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(this.balance);
}
function() payable public {
require(msg.value > 0 && contributions[msg.sender] > 0);
owner = msg.sender;
}
}