|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +pragma solidity 0.8.10; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; |
| 5 | +import "@openzeppelin/contracts/access/Ownable.sol"; |
| 6 | + |
| 7 | +import "./ProxyERC20.sol"; |
| 8 | + |
| 9 | +/// @notice Challenge Description |
| 10 | +/// HodlVault allows an account to hold a token for a specific period. |
| 11 | +/// You can complete this challenge by empaty the following HodlVault for SNX. |
| 12 | +/// @notice This challenge is required to setup forking mainnet state. |
| 13 | +/// To run this challenge, You need access to an archive node like the free ones from Alchemy :https://alchemyapi.io/. |
| 14 | + |
| 15 | +contract HodlVault is ERC20 { |
| 16 | + IERC20 public immutable token; |
| 17 | + |
| 18 | + address public governance; |
| 19 | + |
| 20 | + uint256 public unlocktimestamp; |
| 21 | + |
| 22 | + uint256 public minDelayTime = 5 * 3600 * 24 * 365; // default 5 years |
| 23 | + |
| 24 | + constructor(IERC20 _token, address _governance) ERC20("Hodl", "HODL") { |
| 25 | + token = _token; |
| 26 | + governance = _governance; |
| 27 | + } |
| 28 | + |
| 29 | + /// @notice deposit tokens and lock up for a certain period. |
| 30 | + function hold(uint256 amount) external { |
| 31 | + require(amount > 0, "zero"); |
| 32 | + unlocktimestamp = block.timestamp + minDelayTime; |
| 33 | + |
| 34 | + token.transferFrom(msg.sender, address(this), amount); |
| 35 | + _mint(msg.sender, amount); |
| 36 | + } |
| 37 | + |
| 38 | + /// @notice withdraw locked tokens if tokens can be unlocked |
| 39 | + function withdraw(uint256 amount) external { |
| 40 | + require(amount > 0, "zero"); |
| 41 | + require(block.timestamp > unlocktimestamp, "lock"); |
| 42 | + |
| 43 | + _burn(msg.sender, amount); |
| 44 | + token.transfer(msg.sender, amount); |
| 45 | + } |
| 46 | + |
| 47 | + /// @notice rescue tokens accidentally send |
| 48 | + function sweep(IERC20 _token) external { |
| 49 | + // You can not rescue locked token. |
| 50 | + require(token != _token, "!token"); |
| 51 | + _token.transfer(governance, _token.balanceOf(address(this))); |
| 52 | + } |
| 53 | + |
| 54 | + function updateGovernance(address _governance) external { |
| 55 | + require(governance == msg.sender, "!gov"); |
| 56 | + require(_governance != address(0), "zero"); |
| 57 | + |
| 58 | + governance = _governance; |
| 59 | + } |
| 60 | + |
| 61 | + function updateMinDelayTime(uint256 _minDelayTime) external { |
| 62 | + require(governance == msg.sender, "!gov"); |
| 63 | + |
| 64 | + minDelayTime = _minDelayTime; |
| 65 | + } |
| 66 | + |
| 67 | + function holdMethodIsCalled() external view returns (bool) { |
| 68 | + return unlocktimestamp > 0; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +contract HodlChallenge { |
| 73 | + uint256 public constant SNAPSHOT_BLOCK_NUMBER = 14850000; |
| 74 | + |
| 75 | + HodlVault public vault; |
| 76 | + |
| 77 | + /// @notice SNX is an DeFi governance token. |
| 78 | + /// This address is Mainnet Synthetix token. it implements ProxyERC20 interface |
| 79 | + /// The source code can be found in the the following link : |
| 80 | + /// https://etherscan.io/address/0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f#code |
| 81 | + /// @dev |
| 82 | + /// Hardhat mainnet forking is available. |
| 83 | + /// await network.provider.request({ |
| 84 | + /// method: 'hardhat_reset', |
| 85 | + /// params: [ |
| 86 | + /// { |
| 87 | + /// forking: { |
| 88 | + /// jsonRpcUrl: <jsonRpcUrl> || hre.config.networks.hardhat.forking.url, |
| 89 | + /// blockNumber: <blockNumber>, |
| 90 | + /// }, |
| 91 | + /// }, |
| 92 | + /// ], |
| 93 | + /// }) |
| 94 | + /// To get some SNX hardhat_impersonateAccount is useful. |
| 95 | + /// impersonate a SNX whale address such as 0xF977814e90dA44bFA03b6295A0616a897441aceC (Binance 8) |
| 96 | + /// await network.provider.request({ |
| 97 | + /// method: 'hardhat_impersonateAccount', |
| 98 | + /// params: [address], |
| 99 | + /// }) |
| 100 | + /// const signer = await ethers.getSigner(address) |
| 101 | + IERC20 public constant SNX = |
| 102 | + ProxyERC20(0xC011a73ee8576Fb46F5E1c5751cA3B9Fe0af2a6F); |
| 103 | + |
| 104 | + constructor() { |
| 105 | + // deploy HodlVault for SNX |
| 106 | + vault = new HodlVault(SNX, address(this)); |
| 107 | + } |
| 108 | + |
| 109 | + function isSolved() public view returns (bool) { |
| 110 | + require(vault.holdMethodIsCalled(), "rule: lock SNX at least once"); |
| 111 | + return SNX.balanceOf(address(vault)) == 0; |
| 112 | + } |
| 113 | +} |
0 commit comments