# Retirement Fund
This retirement fund is what economists call a commitment device (opens new window). I’m trying to make sure I hold on to 1 ether for retirement.
I’ve committed 1 ether to the contract below, and I won’t withdraw it until 10 years have passed. If I do withdraw early, 10% of my ether goes to the beneficiary (you!).
I really don’t want you to have 0.1 of my ether, so I’m resolved to leave those funds alone until 10 years from now. Good luck!
// SPDX-License-Identifier: MIT
// ref. https://capturetheether.com/challenges/math/retirement-fund/
pragma solidity ^0.8.11;
contract RetirementFund {
uint256 startBalance;
address owner = msg.sender;
address beneficiary;
uint256 expiration;
constructor(address _player) payable {
require(msg.value == 1 gwei);
expiration = block.timestamp + 3650 days;
beneficiary = _player;
startBalance = msg.value;
}
function isComplete() public view returns (bool) {
return address(this).balance == 0;
}
function withdraw() public {
require(msg.sender == owner);
if (block.timestamp < expiration) {
// early withdrawal incurs a 10% penalty
payable(msg.sender).transfer(address(this).balance * 9 / 10);
} else {
payable(msg.sender).transfer(address(this).balance);
}
}
function collectPenalty() public {
require(msg.sender == beneficiary);
// an early withdrawal occurred
require(startBalance != address(this).balance);
// penalty is what's left
payable(msg.sender).transfer(address(this).balance);
}
}
← Force Re-entrancy →