编译器
0.6.12+commit.27d51765
文件 1 的 5:Farm.sol
pragma solidity 0.6.12;
import "../libraries/math/SafeMath.sol";
import "../libraries/token/IERC20.sol";
import "../libraries/utils/ReentrancyGuard.sol";
import "../interfaces/IX2Fund.sol";
contract Farm is ReentrancyGuard, IERC20 {
using SafeMath for uint256;
string public constant name = "XVIX UNI Farm";
string public constant symbol = "UNI:FARM";
uint8 public constant decimals = 18;
uint256 constant PRECISION = 1e30;
address public token;
address public gov;
address public distributor;
uint256 public override totalSupply;
mapping (address => uint256) public balances;
uint256 public cumulativeRewardPerToken;
mapping (address => uint256) public claimableReward;
mapping (address => uint256) public previousCumulatedRewardPerToken;
event Deposit(address account, uint256 amount);
event Withdraw(address account, uint256 amount);
event Transfer(address indexed from, address indexed to, uint256 value);
event GovChange(address gov);
event Claim(address receiver, uint256 amount);
modifier onlyGov() {
require(msg.sender == gov, "Farm: forbidden");
_;
}
constructor(address _token) public {
token = _token;
gov = msg.sender;
}
receive() external payable {}
function setGov(address _gov) external onlyGov {
gov = _gov;
emit GovChange(_gov);
}
function setDistributor(address _distributor) external onlyGov {
distributor = _distributor;
}
function deposit(uint256 _amount, address _receiver) external nonReentrant {
require(_amount > 0, "Farm: insufficient amount");
_updateRewards(_receiver, true);
IERC20(token).transferFrom(msg.sender, address(this), _amount);
balances[_receiver] = balances[_receiver].add(_amount);
totalSupply = totalSupply.add(_amount);
emit Deposit(_receiver, _amount);
emit Transfer(address(0), _receiver, _amount);
}
function withdraw(address _receiver, uint256 _amount) external nonReentrant {
require(_amount > 0, "Farm: insufficient amount");
address account = msg.sender;
_updateRewards(account, true);
_withdraw(account, _receiver, _amount);
}
function withdrawWithoutDistribution(address _receiver, uint256 _amount) external nonReentrant {
require(_amount > 0, "Farm: insufficient amount");
address account = msg.sender;
_updateRewards(account, false);
_withdraw(account, _receiver, _amount);
}
function claim(address _receiver) external nonReentrant {
address _account = msg.sender;
_updateRewards(_account, true);
uint256 rewardToClaim = claimableReward[_account];
claimableReward[_account] = 0;
(bool success,) = _receiver.call{value: rewardToClaim}("");
require(success, "Farm: transfer failed");
emit Claim(_receiver, rewardToClaim);
}
function balanceOf(address account) public override view returns (uint256) {
return balances[account];
}
function transfer(address , uint256 ) public override returns (bool) {
revert("Farm: non-transferrable");
}
function allowance(address , address ) public view virtual override returns (uint256) {
return 0;
}
function approve(address , uint256 ) public virtual override returns (bool) {
revert("Farm: non-transferrable");
}
function transferFrom(address , address , uint256 ) public virtual override returns (bool) {
revert("Farm: non-transferrable");
}
function _withdraw(address _account, address _receiver, uint256 _amount) private {
require(balances[_account] >= _amount, "Farm: insufficient balance");
balances[_account] = balances[_account].sub(_amount);
totalSupply = totalSupply.sub(_amount);
IERC20(token).transfer(_receiver, _amount);
emit Withdraw(_account, _amount);
emit Transfer(_account, address(0), _amount);
}
function _updateRewards(address _account, bool _distribute) private {
uint256 blockReward;
if (_distribute && distributor != address(0)) {
blockReward = IX2Fund(distributor).distribute();
}
uint256 _cumulativeRewardPerToken = cumulativeRewardPerToken;
if (totalSupply > 0 && blockReward > 0) {
_cumulativeRewardPerToken = _cumulativeRewardPerToken.add(blockReward.mul(PRECISION).div(totalSupply));
cumulativeRewardPerToken = _cumulativeRewardPerToken;
}
if (_cumulativeRewardPerToken == 0) {
return;
}
uint256 _previousCumulatedReward = previousCumulatedRewardPerToken[_account];
uint256 _claimableReward = claimableReward[_account].add(
uint256(balances[_account]).mul(_cumulativeRewardPerToken.sub(_previousCumulatedReward)).div(PRECISION)
);
claimableReward[_account] = _claimableReward;
previousCumulatedRewardPerToken[_account] = _cumulativeRewardPerToken;
}
}
文件 2 的 5:IERC20.sol
pragma solidity 0.6.12;
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);
}
文件 3 的 5:IX2Fund.sol
pragma solidity 0.6.12;
interface IX2Fund {
function distribute() external returns (uint256);
}
文件 4 的 5:ReentrancyGuard.sol
pragma solidity 0.6.12;
contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor () internal {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
}
文件 5 的 5:SafeMath.sol
pragma solidity 0.6.12;
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) {
return sub(a, b, "SafeMath: subtraction overflow");
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
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) {
return div(a, b, "SafeMath: division by zero");
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
{
"compilationTarget": {
"contracts/xvix/Farm.sol": "Farm"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_token","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"receiver","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Claim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"gov","type":"address"}],"name":"GovChange","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Withdraw","type":"event"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_receiver","type":"address"}],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"claimableReward","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"cumulativeRewardPerToken","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"},{"internalType":"address","name":"_receiver","type":"address"}],"name":"deposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"distributor","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"gov","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"previousCumulatedRewardPerToken","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_distributor","type":"address"}],"name":"setDistributor","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_gov","type":"address"}],"name":"setGov","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_receiver","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_receiver","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"withdrawWithoutDistribution","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]