文件 1 的 8:Address.sol
pragma solidity ^0.5.0;
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly { size := extcodesize(account) }
return size > 0;
}
}
文件 2 的 8:ApmLockup.sol
pragma solidity ^0.5.6;
import "./rapm-contracts/token/ERC20/IERC20.sol";
import "./rapm-contracts/token/ERC20/SafeERC20.sol";
import "./rapm-contracts/utils/ReentrancyGuard.sol";
import "./ManagerRole.sol";
contract ApmLockup is ManagerRole, ReentrancyGuard {
using SafeMath for uint256;
using SafeERC20 for IERC20;
IERC20 private _lockToken;
IERC20 private _rewardToken;
uint256 private _startTimestamp;
uint256 private _endTimestamp;
uint256 private _duration;
uint256 private _totalRewardAmount;
uint256 private _totalLockedBalance;
mapping(address => uint256) private _lockedBalances;
uint256 private _denominator;
mapping(address => uint256) private _numerators;
mapping(address => bool) private _claimed;
address[] private _lockerAddressList;
constructor(
address lockToken,
address rewardToken,
uint256 startTimestamp,
uint256 duration
) public {
require(startTimestamp >= now, "startTimestamp cannot be smaller than now");
require(duration > 0, "duration must be larger than 0");
_lockToken = IERC20(lockToken);
_rewardToken = IERC20(rewardToken);
_startTimestamp = startTimestamp;
_endTimestamp = startTimestamp.add(duration);
_duration = duration;
}
function lockToken() external view returns(address) {
return address(_lockToken);
}
function rewardToken() external view returns(address) {
return address(_rewardToken);
}
function startTimestamp() external view returns(uint256) {
return _startTimestamp;
}
function endTimestamp() external view returns(uint256) {
return _endTimestamp;
}
function totalRewardAmount() external view returns(uint256) {
return _totalRewardAmount;
}
function totalLockedBalance() external view returns(uint256) {
return _totalLockedBalance;
}
function lockedBalance(address user) external view returns(uint256) {
return _lockedBalances[user];
}
function totalWeightedBalance() external view returns(uint256) {
return _denominator;
}
function weightedBalance(address user) external view returns(uint256) {
return _numerators[user];
}
function lockers() external view returns(address[] memory) {
return _lockerAddressList;
}
function claimed(address user) external view returns(bool) {
return _claimed[user];
}
function lockedBalances(address[] calldata users) external view returns(uint256[] memory) {
uint256[] memory result = new uint256[](users.length);
for(uint256 i = 0; i < users.length; i++) {
result[i] = _lockedBalances[users[i]];
}
return result;
}
function points(address[] calldata users) external view returns(uint256[] memory) {
uint256[] memory result = new uint256[](users.length);
for(uint256 i = 0; i < users.length; i++) {
result[i] = _numerators[users[i]];
}
return result;
}
function calculateReward(address user) public view returns(uint256) {
return _totalRewardAmount.mul(_numerators[user]).div(_denominator);
}
function addRewardAmount(uint256 rewardAmount) external onlyManager {
require(_rewardToken.allowance(msg.sender, address(this)) >= rewardAmount, "Not enough allowance");
_totalRewardAmount = _totalRewardAmount.add(rewardAmount);
_rewardToken.safeTransferFrom(msg.sender, address(this), rewardAmount);
emit RewardAdded(rewardAmount);
}
function removeRewardAmount() external onlyManager expired {
require(_denominator == 0, "Locker exists");
_rewardToken.transfer(msg.sender, _totalRewardAmount);
_totalRewardAmount = 0;
emit RewardRemoved(_totalRewardAmount);
}
function lock(uint256 amount) external nonReentrant notExpired {
require(amount > 0, "Cannot lock 0");
require(_lockToken.allowance(msg.sender, address(this)) >= amount, "Not enough allowance");
_totalLockedBalance = _totalLockedBalance.add(amount);
_lockedBalances[msg.sender] = _lockedBalances[msg.sender].add(amount);
_lockToken.safeTransferFrom(msg.sender, address(this), amount);
if(_numerators[msg.sender] == 0) {
_lockerAddressList.push(msg.sender);
}
uint256 numerator = amount.mul(_endTimestamp.sub(block.timestamp)).div(_duration);
_numerators[msg.sender] = _numerators[msg.sender].add(numerator);
_denominator = _denominator.add(numerator);
emit Locked(msg.sender, amount);
}
function unlock() external {
if (block.timestamp < _endTimestamp) {
_leave(msg.sender);
uint256 _lockedBalance = _lockedBalances[msg.sender];
_totalLockedBalance = _totalLockedBalance.sub(_lockedBalance);
_lockedBalances[msg.sender] = _lockedBalances[msg.sender].sub(_lockedBalance);
_lockToken.safeTransfer(msg.sender, _lockedBalance);
emit Withdrawn(msg.sender, _lockedBalance);
} else {
require(!_claimed[msg.sender], "already claimed");
_getReward(msg.sender);
_lockToken.safeTransfer(msg.sender, _lockedBalances[msg.sender]);
_claimed[msg.sender] = true;
}
}
function _leave(address user) private nonReentrant notExpired {
uint256 numerator = _numerators[user];
require(numerator > 0, "Locked token's amount is 0");
_denominator = _denominator.sub(numerator);
_numerators[user] = 0;
emit Leaved(user);
}
function _getReward(address user) private nonReentrant expired {
require(_numerators[user] > 0, "reward zero");
uint256 reward = calculateReward(user);
_rewardToken.safeTransfer(user, reward);
emit RewardPaid(user, reward);
}
modifier notExpired {
require(block.timestamp >= _startTimestamp, 'The lock period has not yet come');
require(block.timestamp < _endTimestamp, 'The lock period expired');
_;
}
modifier expired {
require(block.timestamp >= _endTimestamp, 'The lock period has not expired');
_;
}
event RewardAdded(uint256 rewardAmount);
event RewardRemoved(uint256 rewardAmount);
event Locked(address indexed user, uint256 amount);
event Leaved(address indexed user);
event Withdrawn(address indexed user, uint256 amount);
event RewardPaid(address indexed user, uint256 reward);
}
文件 3 的 8:IERC20.sol
pragma solidity ^0.5.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 4 的 8:ManagerRole.sol
pragma solidity 0.5.6;
import "./rapm-contracts/access/Roles.sol";
contract ManagerRole {
using Roles for Roles.Role;
Roles.Role private managers;
event ManagerAdded(address indexed account);
event ManagerRemoved(address indexed account);
modifier onlyManager() {
require(managers.has(msg.sender), "ManagerRole: caller does not have the Manager role");
_;
}
constructor() public {
managers.add(msg.sender);
}
function addManager(address account) public onlyManager {
managers.add(account);
emit ManagerAdded(account);
}
function removeManager(address account) public onlyManager {
managers.remove(account);
emit ManagerRemoved(account);
}
function isManager(address account) public view returns (bool) {
return managers.has(account);
}
}
文件 5 的 8:ReentrancyGuard.sol
pragma solidity ^0.5.0;
contract ReentrancyGuard {
uint256 private _guardCounter;
constructor () internal {
_guardCounter = 1;
}
modifier nonReentrant() {
_guardCounter += 1;
uint256 localCounter = _guardCounter;
_;
require(localCounter == _guardCounter, "ReentrancyGuard: reentrant call");
}
}
文件 6 的 8:Roles.sol
pragma solidity ^0.5.0;
library Roles {
struct Role {
mapping (address => bool) bearer;
}
function add(Role storage role, address account) internal {
require(!has(role, account), "Roles: account already has role");
role.bearer[account] = true;
}
function remove(Role storage role, address account) internal {
require(has(role, account), "Roles: account does not have role");
role.bearer[account] = false;
}
function has(Role storage role, address account) internal view returns (bool) {
require(account != address(0), "Roles: account is the zero address");
return role.bearer[account];
}
}
文件 7 的 8:SafeERC20.sol
pragma solidity ^0.5.0;
import "./IERC20.sol";
import "../../math/SafeMath.sol";
import "../../utils/Address.sol";
library SafeERC20 {
using SafeMath for uint256;
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(IERC20 token, address spender, uint256 value) internal {
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).add(value);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).sub(value);
callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function callOptionalReturn(IERC20 token, bytes memory data) private {
require(address(token).isContract(), "SafeERC20: call to non-contract");
(bool success, bytes memory returndata) = address(token).call(data);
require(success, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 8 的 8:SafeMath.sol
pragma solidity ^0.5.0;
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
uint256 c = a - b;
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
uint256 c = a / b;
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, "SafeMath: modulo by zero");
return a % b;
}
}
{
"compilationTarget": {
"contracts/ApmLockup.sol": "ApmLockup"
},
"evmVersion": "petersburg",
"libraries": {},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"constant":true,"inputs":[],"name":"lockers","outputs":[{"name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"account","type":"address"}],"name":"addManager","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalLockedBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"users","type":"address[]"}],"name":"lockedBalances","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"user","type":"address"}],"name":"weightedBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalWeightedBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalRewardAmount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"removeRewardAmount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"user","type":"address"}],"name":"lockedBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"rewardAmount","type":"uint256"}],"name":"addRewardAmount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"unlock","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"endTimestamp","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"account","type":"address"}],"name":"removeManager","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"lockToken","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"user","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"users","type":"address[]"}],"name":"points","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"user","type":"address"}],"name":"calculateReward","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"amount","type":"uint256"}],"name":"lock","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"startTimestamp","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"account","type":"address"}],"name":"isManager","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"rewardToken","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"lockToken","type":"address"},{"name":"rewardToken","type":"address"},{"name":"startTimestamp","type":"uint256"},{"name":"duration","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"rewardAmount","type":"uint256"}],"name":"RewardAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"rewardAmount","type":"uint256"}],"name":"RewardRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Locked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"user","type":"address"}],"name":"Leaved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Withdrawn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"user","type":"address"},{"indexed":false,"name":"reward","type":"uint256"}],"name":"RewardPaid","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"account","type":"address"}],"name":"ManagerAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"account","type":"address"}],"name":"ManagerRemoved","type":"event"}]