|
| 1 | +/* |
| 2 | +ERC721StakingModule |
| 3 | +
|
| 4 | +https://github.com/gysr-io/core |
| 5 | +
|
| 6 | +SPDX-License-Identifier: MIT |
| 7 | +*/ |
| 8 | + |
| 9 | +pragma solidity 0.8.4; |
| 10 | + |
| 11 | +import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; |
| 12 | + |
| 13 | +import "./interfaces/IStakingModule.sol"; |
| 14 | + |
| 15 | +/** |
| 16 | + * @title ERC721 staking module |
| 17 | + * |
| 18 | + * @notice this staking module allows users to deposit one or more ERC721 |
| 19 | + * tokens in exchange for shares credited to their address. When the user |
| 20 | + * unstakes, these shares will be burned and a reward will be distributed. |
| 21 | + */ |
| 22 | +contract ERC721StakingModule is IStakingModule { |
| 23 | + // constant |
| 24 | + uint256 public constant SHARES_PER_TOKEN = 10**18; |
| 25 | + |
| 26 | + // members |
| 27 | + IERC721 private immutable _token; |
| 28 | + address private immutable _factory; |
| 29 | + |
| 30 | + mapping(address => uint256) public counts; |
| 31 | + mapping(uint256 => address) public owners; |
| 32 | + mapping(address => mapping(uint256 => uint256)) public tokenByOwner; |
| 33 | + mapping(uint256 => uint256) public tokenIndex; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param token_ the token that will be rewarded |
| 37 | + */ |
| 38 | + constructor(address token_, address factory_) { |
| 39 | + require(IERC165(token_).supportsInterface(0x80ac58cd), "smn1"); |
| 40 | + _token = IERC721(token_); |
| 41 | + _factory = factory_; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @inheritdoc IStakingModule |
| 46 | + */ |
| 47 | + function tokens() |
| 48 | + external |
| 49 | + view |
| 50 | + override |
| 51 | + returns (address[] memory tokens_) |
| 52 | + { |
| 53 | + tokens_ = new address[](1); |
| 54 | + tokens_[0] = address(_token); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @inheritdoc IStakingModule |
| 59 | + */ |
| 60 | + function balances(address user) |
| 61 | + external |
| 62 | + view |
| 63 | + override |
| 64 | + returns (uint256[] memory balances_) |
| 65 | + { |
| 66 | + balances_ = new uint256[](1); |
| 67 | + balances_[0] = counts[user]; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @inheritdoc IStakingModule |
| 72 | + */ |
| 73 | + function factory() external view override returns (address) { |
| 74 | + return _factory; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @inheritdoc IStakingModule |
| 79 | + */ |
| 80 | + function totals() |
| 81 | + external |
| 82 | + view |
| 83 | + override |
| 84 | + returns (uint256[] memory totals_) |
| 85 | + { |
| 86 | + totals_ = new uint256[](1); |
| 87 | + totals_[0] = _token.balanceOf(address(this)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @inheritdoc IStakingModule |
| 92 | + */ |
| 93 | + function stake( |
| 94 | + address user, |
| 95 | + uint256 amount, |
| 96 | + bytes calldata data |
| 97 | + ) external override onlyOwner returns (address, uint256) { |
| 98 | + // validate |
| 99 | + require(amount > 0, "smn2"); |
| 100 | + require(amount <= _token.balanceOf(user), "smn3"); |
| 101 | + require(data.length == 32 * amount, "smn4"); |
| 102 | + |
| 103 | + uint256 sz = counts[user]; |
| 104 | + |
| 105 | + // stake |
| 106 | + for (uint256 i = 0; i < amount; i++) { |
| 107 | + // get token id |
| 108 | + uint256 id; |
| 109 | + uint256 pos = 132 + 32 * i; |
| 110 | + assembly { |
| 111 | + id := calldataload(pos) |
| 112 | + } |
| 113 | + |
| 114 | + // ownership mappings |
| 115 | + owners[id] = user; |
| 116 | + uint256 len = sz + i; |
| 117 | + tokenByOwner[user][len] = id; |
| 118 | + tokenIndex[id] = len; |
| 119 | + |
| 120 | + // transfer to module |
| 121 | + _token.transferFrom(user, address(this), id); |
| 122 | + } |
| 123 | + |
| 124 | + // update position |
| 125 | + counts[user] = sz + amount; |
| 126 | + |
| 127 | + // emit |
| 128 | + uint256 s = amount * SHARES_PER_TOKEN; |
| 129 | + emit Staked(user, address(_token), amount, s); |
| 130 | + |
| 131 | + return (user, s); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @inheritdoc IStakingModule |
| 136 | + */ |
| 137 | + function unstake( |
| 138 | + address user, |
| 139 | + uint256 amount, |
| 140 | + bytes calldata data |
| 141 | + ) external override onlyOwner returns (address, uint256) { |
| 142 | + // validate |
| 143 | + require(amount > 0, "smn5"); |
| 144 | + uint256 sz = counts[user]; |
| 145 | + require(amount <= sz, "smn6"); |
| 146 | + require(data.length == 32 * amount, "smn7"); |
| 147 | + |
| 148 | + // unstake |
| 149 | + for (uint256 i = 0; i < amount; i++) { |
| 150 | + // get token id |
| 151 | + uint256 id; |
| 152 | + uint256 pos = 132 + 32 * i; |
| 153 | + assembly { |
| 154 | + id := calldataload(pos) |
| 155 | + } |
| 156 | + |
| 157 | + // ownership |
| 158 | + require(owners[id] == user, "smn8"); |
| 159 | + delete owners[id]; |
| 160 | + |
| 161 | + // clean up ownership mappings |
| 162 | + uint256 lastIndex = sz - 1 - i; |
| 163 | + if (amount != sz) { |
| 164 | + // reindex on partial unstake |
| 165 | + uint256 index = tokenIndex[id]; |
| 166 | + if (index != lastIndex) { |
| 167 | + uint256 lastId = tokenByOwner[user][lastIndex]; |
| 168 | + tokenByOwner[user][index] = lastId; |
| 169 | + tokenIndex[lastId] = index; |
| 170 | + } |
| 171 | + } |
| 172 | + delete tokenByOwner[user][lastIndex]; |
| 173 | + delete tokenIndex[id]; |
| 174 | + |
| 175 | + // transfer to user |
| 176 | + _token.safeTransferFrom(address(this), user, id); |
| 177 | + } |
| 178 | + |
| 179 | + // update position |
| 180 | + counts[user] = sz - amount; |
| 181 | + |
| 182 | + // emit |
| 183 | + uint256 s = amount * SHARES_PER_TOKEN; |
| 184 | + emit Unstaked(user, address(_token), amount, s); |
| 185 | + |
| 186 | + return (user, s); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * @inheritdoc IStakingModule |
| 191 | + */ |
| 192 | + function claim( |
| 193 | + address user, |
| 194 | + uint256 amount, |
| 195 | + bytes calldata |
| 196 | + ) external override onlyOwner returns (address, uint256) { |
| 197 | + // validate |
| 198 | + require(amount > 0, "smn9"); |
| 199 | + require(amount <= counts[user], "smn10"); |
| 200 | + |
| 201 | + uint256 s = amount * SHARES_PER_TOKEN; |
| 202 | + emit Claimed(user, address(_token), amount, s); |
| 203 | + return (user, s); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * @inheritdoc IStakingModule |
| 208 | + */ |
| 209 | + function update(address) external override {} |
| 210 | + |
| 211 | + /** |
| 212 | + * @inheritdoc IStakingModule |
| 213 | + */ |
| 214 | + function clean() external override {} |
| 215 | +} |
0 commit comments