文件 1 的 4:EarninigsPools.sol
pragma solidity =0.6.6;
import './libraries/TransferHelper.sol';
import './libraries/SafeMath.sol';
import './interfaces/IERC20.sol';
contract EarningsPools {
using SafeMath for uint;
mapping(address=>mapping(address=>uint)) public userEarningsValue;
mapping(address=>mapping(address=>uint)) public userTakeoutValue;
address public owner;
event e_dfbtc_earning_record(address indexed _user, address indexed tokenAddress, uint addValue, uint _createTime);
event e_dfbtc_takeout_record(address indexed _user, address indexed tokenAddress, uint outValue, uint _createTime);
uint private unlocked = 1;
modifier lock() {
require(unlocked == 1, 'dfbtc: LOCKED');
unlocked = 0;
_;
unlocked = 1;
}
modifier onlyOnwer {
require(owner == msg.sender, "dfbtc: The caller must be onwer!!!");
_;
}
constructor() public {
owner = msg.sender;
}
function setOwner(address newOwner) public onlyOnwer {
owner = newOwner;
}
function addTokenToPools(address[] memory _userList, uint[] memory _valueList, address tokenAddress) public onlyOnwer lock {
for(uint i=0; i<_userList.length; i++){
userEarningsValue[_userList[i]][tokenAddress] = userEarningsValue[_userList[i]][tokenAddress].add(_valueList[i]);
emit e_dfbtc_earning_record(_userList[i], tokenAddress, _valueList[i], now);
}
}
function getEarnings(address tokenAddress, uint transferValue) public lock {
require(userEarningsValue[msg.sender][tokenAddress] >= transferValue, "dfbtc: More than the number that can be collected!");
require(IERC20(tokenAddress).balanceOf(address(this)) >= transferValue, "dfbtc: The pool of funds is insufficient!");
userEarningsValue[msg.sender][tokenAddress] = userEarningsValue[msg.sender][tokenAddress].sub(transferValue);
userTakeoutValue[msg.sender][tokenAddress] = userTakeoutValue[msg.sender][tokenAddress].add(transferValue);
TransferHelper.safeTransfer(tokenAddress, msg.sender, transferValue);
emit e_dfbtc_takeout_record(msg.sender, tokenAddress, transferValue, now);
}
function getBlockTime() public view returns(uint){
return now;
}
}
文件 2 的 4:IERC20.sol
pragma solidity >=0.5.0;
interface IERC20 {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);
}
文件 3 的 4:SafeMath.sol
pragma solidity =0.6.6;
library SafeMath {
function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c) {
if (_a == 0) {
return 0;
}
c = _a * _b;
require(c / _a == _b, "mul error!!");
return c;
}
function div(uint256 _a, uint256 _b) internal pure returns (uint256) {
return _a / _b;
}
function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
require(_b <= _a, "sub error!!");
return _a - _b;
}
function add(uint256 _a, uint256 _b) internal pure returns (uint256 c) {
c = _a + _b;
require(c >= _a, "add error!!");
return c;
}
}
文件 4 的 4:TransferHelper.sol
pragma solidity >=0.6.6;
library TransferHelper {
function safeApprove(
address token,
address to,
uint256 value
) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::safeApprove: approve failed'
);
}
function safeTransfer(
address token,
address to,
uint256 value
) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::safeTransfer: transfer failed'
);
}
function safeTransferFrom(
address token,
address from,
address to,
uint256 value
) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::transferFrom: transferFrom failed'
);
}
}
{
"compilationTarget": {
"browser/EarninigsPools.sol": "EarningsPools"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_user","type":"address"},{"indexed":true,"internalType":"address","name":"tokenAddress","type":"address"},{"indexed":false,"internalType":"uint256","name":"addValue","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_createTime","type":"uint256"}],"name":"e_dfbtc_earning_record","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_user","type":"address"},{"indexed":true,"internalType":"address","name":"tokenAddress","type":"address"},{"indexed":false,"internalType":"uint256","name":"outValue","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_createTime","type":"uint256"}],"name":"e_dfbtc_takeout_record","type":"event"},{"inputs":[{"internalType":"address[]","name":"_userList","type":"address[]"},{"internalType":"uint256[]","name":"_valueList","type":"uint256[]"},{"internalType":"address","name":"tokenAddress","type":"address"}],"name":"addTokenToPools","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getBlockTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"uint256","name":"transferValue","type":"uint256"}],"name":"getEarnings","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"setOwner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"userEarningsValue","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"userTakeoutValue","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]