文件 1 的 14:ClaimType.sol
pragma solidity 0.7.6;
enum ClaimType {
None,
CLAIMREWARD,
CLAIMDEPOSIT,
CLAIMTOTAL
}
文件 2 的 14:IStafiDistributor.sol
pragma solidity 0.7.6;
interface IStafiDistributor {
function distributeWithdrawals() external payable;
}
文件 3 的 14:IStafiEther.sol
pragma solidity 0.7.6;
interface IStafiEther {
function balanceOf(address _contractAddress) external view returns (uint256);
function depositEther() external payable;
function withdrawEther(uint256 _amount) external;
}
文件 4 的 14:IStafiEtherWithdrawer.sol
pragma solidity 0.7.6;
interface IStafiEtherWithdrawer {
function receiveEtherWithdrawal() external payable;
}
文件 5 的 14:IStafiFeePool.sol
pragma solidity 0.7.6;
interface IStafiFeePool {
function withdrawEther(address _to, uint256 _amount) external;
}
文件 6 的 14:IStafiNetworkSettings.sol
pragma solidity 0.7.6;
interface IStafiNetworkSettings {
function getNodeConsensusThreshold() external view returns (uint256);
function getSubmitBalancesEnabled() external view returns (bool);
function getProcessWithdrawalsEnabled() external view returns (bool);
function getNodeFee() external view returns (uint256);
function getPlatformFee() external view returns (uint256);
function getNodeRefundRatio() external view returns (uint256);
function getNodeTrustedRefundRatio() external view returns (uint256);
function getWithdrawalCredentials() external view returns (bytes memory);
function getSuperNodePubkeyLimit() external view returns (uint256);
}
文件 7 的 14:IStafiNodeManager.sol
pragma solidity 0.7.6;
interface IStafiNodeManager {
function getNodeCount() external view returns (uint256);
function getNodeAt(uint256 _index) external view returns (address);
function getTrustedNodeCount() external view returns (uint256);
function getTrustedNodeAt(uint256 _index) external view returns (address);
function getSuperNodeCount() external view returns (uint256);
function getSuperNodeAt(uint256 _index) external view returns (address);
function getNodeExists(address _nodeAddress) external view returns (bool);
function getNodeTrusted(address _nodeAddress) external view returns (bool);
function getSuperNodeExists(address _nodeAddress) external view returns (bool);
function registerNode(address _nodeAddress) external;
function setNodeTrusted(address _nodeAddress, bool _trusted) external;
function setNodeSuper(address _nodeAddress, bool _super) external;
}
文件 8 的 14:IStafiStorage.sol
pragma solidity 0.7.6;
interface IStafiStorage {
function getAddress(bytes32 _key) external view returns (address);
function getUint(bytes32 _key) external view returns (uint);
function getString(bytes32 _key) external view returns (string memory);
function getBytes(bytes32 _key) external view returns (bytes memory);
function getBool(bytes32 _key) external view returns (bool);
function getInt(bytes32 _key) external view returns (int);
function getBytes32(bytes32 _key) external view returns (bytes32);
function setAddress(bytes32 _key, address _value) external;
function setUint(bytes32 _key, uint _value) external;
function setString(bytes32 _key, string calldata _value) external;
function setBytes(bytes32 _key, bytes calldata _value) external;
function setBool(bytes32 _key, bool _value) external;
function setInt(bytes32 _key, int _value) external;
function setBytes32(bytes32 _key, bytes32 _value) external;
function deleteAddress(bytes32 _key) external;
function deleteUint(bytes32 _key) external;
function deleteString(bytes32 _key) external;
function deleteBytes(bytes32 _key) external;
function deleteBool(bytes32 _key) external;
function deleteInt(bytes32 _key) external;
function deleteBytes32(bytes32 _key) external;
}
文件 9 的 14:IStafiSuperNodeFeePool.sol
pragma solidity 0.7.6;
interface IStafiSuperNodeFeePool {
function withdrawEther(address _to, uint256 _amount) external;
}
文件 10 的 14:IStafiUserDeposit.sol
pragma solidity 0.7.6;
interface IStafiUserDeposit {
function getBalance() external view returns (uint256);
function getExcessBalance() external view returns (uint256);
function deposit() external payable;
function recycleDissolvedDeposit() external payable;
function recycleWithdrawDeposit() external payable;
function recycleDistributorDeposit() external payable;
function assignDeposits() external;
function withdrawExcessBalance(uint256 _amount) external;
function withdrawExcessBalanceForSuperNode(uint256 _amount) external;
function withdrawExcessBalanceForLightNode(uint256 _amount) external;
function withdrawExcessBalanceForWithdraw(uint256 _amount) external;
}
文件 11 的 14:MerkleProof.sol
pragma solidity ^0.7.0;
library MerkleProof {
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
bytes32 proofElement = proof[i];
if (computedHash <= proofElement) {
computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
} else {
computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
}
}
return computedHash == root;
}
}
文件 12 的 14:SafeMath.sol
pragma solidity ^0.7.0;
library SafeMath {
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a / b);
}
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a % b);
}
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");
return a - b;
}
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");
return a / b;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: modulo by zero");
return a % b;
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a / b;
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a % b;
}
}
文件 13 的 14:StafiBase.sol
pragma solidity 0.7.6;
import "./interfaces/storage/IStafiStorage.sol";
abstract contract StafiBase {
uint8 public version;
IStafiStorage stafiStorage = IStafiStorage(0);
modifier onlyLatestNetworkContract() {
require(getBool(keccak256(abi.encodePacked("contract.exists", msg.sender))), "Invalid or outdated network contract");
_;
}
modifier onlyLatestContract(string memory _contractName, address _contractAddress) {
require(_contractAddress == getAddress(keccak256(abi.encodePacked("contract.address", _contractName))), "Invalid or outdated contract");
_;
}
modifier onlyTrustedNode(address _nodeAddress) {
require(getBool(keccak256(abi.encodePacked("node.trusted", _nodeAddress))), "Invalid trusted node");
_;
}
modifier onlySuperNode(address _nodeAddress) {
require(getBool(keccak256(abi.encodePacked("node.super", _nodeAddress))), "Invalid super node");
_;
}
modifier onlyRegisteredStakingPool(address _stakingPoolAddress) {
require(getBool(keccak256(abi.encodePacked("stakingpool.exists", _stakingPoolAddress))), "Invalid staking pool");
_;
}
modifier onlyOwner() {
require(roleHas("owner", msg.sender), "Account is not the owner");
_;
}
modifier onlyAdmin() {
require(roleHas("admin", msg.sender), "Account is not an admin");
_;
}
modifier onlySuperUser() {
require(roleHas("owner", msg.sender) || roleHas("admin", msg.sender), "Account is not a super user");
_;
}
modifier onlyRole(string memory _role) {
require(roleHas(_role, msg.sender), "Account does not match the specified role");
_;
}
constructor(address _stafiStorageAddress) {
stafiStorage = IStafiStorage(_stafiStorageAddress);
}
function getContractAddress(string memory _contractName) internal view returns (address) {
address contractAddress = getAddress(keccak256(abi.encodePacked("contract.address", _contractName)));
require(contractAddress != address(0x0), "Contract not found");
return contractAddress;
}
function getContractName(address _contractAddress) internal view returns (string memory) {
string memory contractName = getString(keccak256(abi.encodePacked("contract.name", _contractAddress)));
require(keccak256(abi.encodePacked(contractName)) != keccak256(abi.encodePacked("")), "Contract not found");
return contractName;
}
function getAddress(bytes32 _key) internal view returns (address) { return stafiStorage.getAddress(_key); }
function getUint(bytes32 _key) internal view returns (uint256) { return stafiStorage.getUint(_key); }
function getString(bytes32 _key) internal view returns (string memory) { return stafiStorage.getString(_key); }
function getBytes(bytes32 _key) internal view returns (bytes memory) { return stafiStorage.getBytes(_key); }
function getBool(bytes32 _key) internal view returns (bool) { return stafiStorage.getBool(_key); }
function getInt(bytes32 _key) internal view returns (int256) { return stafiStorage.getInt(_key); }
function getBytes32(bytes32 _key) internal view returns (bytes32) { return stafiStorage.getBytes32(_key); }
function getAddressS(string memory _key) internal view returns (address) { return stafiStorage.getAddress(keccak256(abi.encodePacked(_key))); }
function getUintS(string memory _key) internal view returns (uint256) { return stafiStorage.getUint(keccak256(abi.encodePacked(_key))); }
function getStringS(string memory _key) internal view returns (string memory) { return stafiStorage.getString(keccak256(abi.encodePacked(_key))); }
function getBytesS(string memory _key) internal view returns (bytes memory) { return stafiStorage.getBytes(keccak256(abi.encodePacked(_key))); }
function getBoolS(string memory _key) internal view returns (bool) { return stafiStorage.getBool(keccak256(abi.encodePacked(_key))); }
function getIntS(string memory _key) internal view returns (int256) { return stafiStorage.getInt(keccak256(abi.encodePacked(_key))); }
function getBytes32S(string memory _key) internal view returns (bytes32) { return stafiStorage.getBytes32(keccak256(abi.encodePacked(_key))); }
function setAddress(bytes32 _key, address _value) internal { stafiStorage.setAddress(_key, _value); }
function setUint(bytes32 _key, uint256 _value) internal { stafiStorage.setUint(_key, _value); }
function setString(bytes32 _key, string memory _value) internal { stafiStorage.setString(_key, _value); }
function setBytes(bytes32 _key, bytes memory _value) internal { stafiStorage.setBytes(_key, _value); }
function setBool(bytes32 _key, bool _value) internal { stafiStorage.setBool(_key, _value); }
function setInt(bytes32 _key, int256 _value) internal { stafiStorage.setInt(_key, _value); }
function setBytes32(bytes32 _key, bytes32 _value) internal { stafiStorage.setBytes32(_key, _value); }
function setAddressS(string memory _key, address _value) internal { stafiStorage.setAddress(keccak256(abi.encodePacked(_key)), _value); }
function setUintS(string memory _key, uint256 _value) internal { stafiStorage.setUint(keccak256(abi.encodePacked(_key)), _value); }
function setStringS(string memory _key, string memory _value) internal { stafiStorage.setString(keccak256(abi.encodePacked(_key)), _value); }
function setBytesS(string memory _key, bytes memory _value) internal { stafiStorage.setBytes(keccak256(abi.encodePacked(_key)), _value); }
function setBoolS(string memory _key, bool _value) internal { stafiStorage.setBool(keccak256(abi.encodePacked(_key)), _value); }
function setIntS(string memory _key, int256 _value) internal { stafiStorage.setInt(keccak256(abi.encodePacked(_key)), _value); }
function setBytes32S(string memory _key, bytes32 _value) internal { stafiStorage.setBytes32(keccak256(abi.encodePacked(_key)), _value); }
function deleteAddress(bytes32 _key) internal { stafiStorage.deleteAddress(_key); }
function deleteUint(bytes32 _key) internal { stafiStorage.deleteUint(_key); }
function deleteString(bytes32 _key) internal { stafiStorage.deleteString(_key); }
function deleteBytes(bytes32 _key) internal { stafiStorage.deleteBytes(_key); }
function deleteBool(bytes32 _key) internal { stafiStorage.deleteBool(_key); }
function deleteInt(bytes32 _key) internal { stafiStorage.deleteInt(_key); }
function deleteBytes32(bytes32 _key) internal { stafiStorage.deleteBytes32(_key); }
function deleteAddressS(string memory _key) internal { stafiStorage.deleteAddress(keccak256(abi.encodePacked(_key))); }
function deleteUintS(string memory _key) internal { stafiStorage.deleteUint(keccak256(abi.encodePacked(_key))); }
function deleteStringS(string memory _key) internal { stafiStorage.deleteString(keccak256(abi.encodePacked(_key))); }
function deleteBytesS(string memory _key) internal { stafiStorage.deleteBytes(keccak256(abi.encodePacked(_key))); }
function deleteBoolS(string memory _key) internal { stafiStorage.deleteBool(keccak256(abi.encodePacked(_key))); }
function deleteIntS(string memory _key) internal { stafiStorage.deleteInt(keccak256(abi.encodePacked(_key))); }
function deleteBytes32S(string memory _key) internal { stafiStorage.deleteBytes32(keccak256(abi.encodePacked(_key))); }
function roleHas(string memory _role, address _address) internal view returns (bool) {
return getBool(keccak256(abi.encodePacked("access.role", _role, _address)));
}
}
文件 14 的 14:StafiDistributor.sol
pragma solidity 0.7.6;
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/cryptography/MerkleProof.sol";
import "../StafiBase.sol";
import "../interfaces/IStafiEther.sol";
import "../interfaces/deposit/IStafiUserDeposit.sol";
import "../interfaces/settings/IStafiNetworkSettings.sol";
import "../interfaces/reward/IStafiFeePool.sol";
import "../interfaces/reward/IStafiSuperNodeFeePool.sol";
import "../interfaces/reward/IStafiDistributor.sol";
import "../interfaces/IStafiEtherWithdrawer.sol";
import "../interfaces/node/IStafiNodeManager.sol";
import "../types/ClaimType.sol";
contract StafiDistributor is StafiBase, IStafiEtherWithdrawer, IStafiDistributor {
using SafeMath for uint256;
event Claimed(
uint256 index,
address account,
uint256 claimableReward,
uint256 claimableDeposit,
ClaimType claimType
);
event VoteProposal(bytes32 indexed proposalId, address voter);
event ProposalExecuted(bytes32 indexed proposalId);
event DistributeFee(uint256 dealedHeight, uint256 userAmount, uint256 nodeAmount, uint256 platformAmount);
event DistributeSuperNodeFee(uint256 dealedHeight, uint256 userAmount, uint256 nodeAmount, uint256 platformAmount);
event DistributeSlash(uint256 dealedHeight, uint256 slashAmount);
event SetMerkleRoot(uint256 dealedEpoch, bytes32 merkleRoot);
event SetPlatformTotalAmount(uint256 dealedEpoch, uint256 totalAmount);
constructor(address _stafiStorageAddress) StafiBase(_stafiStorageAddress) {
version = 1;
}
receive() external payable {}
function receiveEtherWithdrawal()
external
payable
override
onlyLatestContract("stafiDistributor", address(this))
onlyLatestContract("stafiEther", msg.sender)
{}
function distributeWithdrawals() external payable override onlyLatestContract("stafiDistributor", address(this)) {
require(msg.value > 0, "zero amount");
IStafiEther stafiEther = IStafiEther(getContractAddress("stafiEther"));
stafiEther.depositEther{value: msg.value}();
}
function getCurrentNodeDepositAmount() public view returns (uint256) {
return getUint("settings.node.deposit.amount");
}
function getMerkleDealedEpoch() public view returns (uint256) {
return getUint(keccak256(abi.encodePacked("stafiDistributor.merkleRoot.dealedEpoch")));
}
function getTotalClaimedReward(address _account) public view returns (uint256) {
return getUint(keccak256(abi.encodePacked("stafiDistributor.node.totalClaimedReward", _account)));
}
function getTotalClaimedDeposit(address _account) public view returns (uint256) {
return getUint(keccak256(abi.encodePacked("stafiDistributor.node.totalClaimedDeposit", _account)));
}
function getMerkleRoot() public view returns (bytes32) {
return getBytes32(keccak256(abi.encodePacked("stafiDistributor.merkleRoot")));
}
function getDistributeFeeDealedHeight() public view returns (uint256) {
return getUint(keccak256(abi.encodePacked("stafiDistributor.distributeFee.dealedHeight")));
}
function getDistributeSuperNodeFeeDealedHeight() public view returns (uint256) {
return getUint(keccak256(abi.encodePacked("stafiDistributor.distributeSuperNodeFee.dealedHeight")));
}
function getDistributeSlashDealedHeight() public view returns (uint256) {
return getUint(keccak256(abi.encodePacked("stafiDistributor.distributeSlashAmount.dealedHeight")));
}
function getPlatformTotalAmount() public view returns (uint256) {
return getUint(keccak256(abi.encodePacked("stafiDistributor.platform.totalAmount")));
}
function getPlatformTotalClaimedAmount() public view returns (uint256) {
return getUint(keccak256(abi.encodePacked("stafiDistributor.platform.totalClaimedAmount")));
}
function updateMerkleRoot(
bytes32 _merkleRoot
) external onlyLatestContract("stafiDistributor", address(this)) onlySuperUser {
setMerkleRoot(_merkleRoot);
}
function claimPlatformFee(
address _receiver
) external onlyLatestContract("stafiDistributor", address(this)) onlySuperUser {
uint256 totalAmount = getPlatformTotalAmount();
uint256 willClaimAmount = totalAmount.sub(getPlatformTotalClaimedAmount());
if (willClaimAmount > 0) {
setPlatformTotalClaimedAmount(totalAmount);
IStafiEther stafiEther = IStafiEther(getContractAddress("stafiEther"));
stafiEther.withdrawEther(willClaimAmount);
(bool success, ) = _receiver.call{value: willClaimAmount}("");
require(success, "failed to claim ETH");
}
}
function distributeFee(
uint256 _dealedHeight,
uint256 _userAmount,
uint256 _nodeAmount,
uint256 _platformAmount
) external onlyLatestContract("stafiDistributor", address(this)) onlyTrustedNode(msg.sender) {
uint256 totalAmount = _userAmount.add(_nodeAmount).add(_platformAmount);
require(totalAmount > 0, "zero amount");
require(_dealedHeight > getDistributeFeeDealedHeight(), "height already dealed");
bytes32 proposalId = keccak256(
abi.encodePacked("distributeFee", _dealedHeight, _userAmount, _nodeAmount, _platformAmount)
);
bool needExe = _voteProposal(proposalId);
if (needExe) {
IStafiFeePool feePool = IStafiFeePool(getContractAddress("stafiFeePool"));
IStafiUserDeposit stafiUserDeposit = IStafiUserDeposit(getContractAddress("stafiUserDeposit"));
IStafiEther stafiEther = IStafiEther(getContractAddress("stafiEther"));
feePool.withdrawEther(address(this), totalAmount);
uint256 nodeAndPlatformAmount = _nodeAmount.add(_platformAmount);
if (_userAmount > 0) {
stafiUserDeposit.recycleDistributorDeposit{value: _userAmount}();
}
if (nodeAndPlatformAmount > 0) {
stafiEther.depositEther{value: nodeAndPlatformAmount}();
}
setDistributeFeeDealedHeight(_dealedHeight);
_afterExecProposal(proposalId);
emit DistributeFee(_dealedHeight, _userAmount, _nodeAmount, _platformAmount);
}
}
function distributeSuperNodeFee(
uint256 _dealedHeight,
uint256 _userAmount,
uint256 _nodeAmount,
uint256 _platformAmount
) external onlyLatestContract("stafiDistributor", address(this)) onlyTrustedNode(msg.sender) {
uint256 totalAmount = _userAmount.add(_nodeAmount).add(_platformAmount);
require(totalAmount > 0, "zero amount");
require(_dealedHeight > getDistributeSuperNodeFeeDealedHeight(), "height already dealed");
bytes32 proposalId = keccak256(
abi.encodePacked("distributeSuperNodeFee", _dealedHeight, _userAmount, _nodeAmount, _platformAmount)
);
bool needExe = _voteProposal(proposalId);
if (needExe) {
IStafiFeePool feePool = IStafiFeePool(getContractAddress("stafiSuperNodeFeePool"));
IStafiUserDeposit stafiUserDeposit = IStafiUserDeposit(getContractAddress("stafiUserDeposit"));
IStafiEther stafiEther = IStafiEther(getContractAddress("stafiEther"));
feePool.withdrawEther(address(this), totalAmount);
uint256 nodeAndPlatformAmount = _nodeAmount.add(_platformAmount);
if (_userAmount > 0) {
stafiUserDeposit.recycleDistributorDeposit{value: _userAmount}();
}
if (nodeAndPlatformAmount > 0) {
stafiEther.depositEther{value: nodeAndPlatformAmount}();
}
setDistributeSuperNodeFeeDealedHeight(_dealedHeight);
_afterExecProposal(proposalId);
emit DistributeSuperNodeFee(_dealedHeight, _userAmount, _nodeAmount, _platformAmount);
}
}
function distributeSlashAmount(
uint256 _dealedHeight,
uint256 _amount
) external onlyLatestContract("stafiDistributor", address(this)) onlyTrustedNode(msg.sender) {
require(_amount > 0, "zero amount");
require(_dealedHeight > getDistributeSlashDealedHeight(), "height already dealed");
bytes32 proposalId = keccak256(abi.encodePacked("distributeSlashAmount", _dealedHeight, _amount));
bool needExe = _voteProposal(proposalId);
if (needExe) {
IStafiEther stafiEther = IStafiEther(getContractAddress("stafiEther"));
IStafiUserDeposit stafiUserDeposit = IStafiUserDeposit(getContractAddress("stafiUserDeposit"));
stafiEther.withdrawEther(_amount);
stafiUserDeposit.recycleDistributorDeposit{value: _amount}();
setDistributeSlashDealedHeight(_dealedHeight);
_afterExecProposal(proposalId);
emit DistributeSlash(_dealedHeight, _amount);
}
}
function setMerkleRoot(
uint256 _dealedEpoch,
bytes32 _merkleRoot
) external onlyLatestContract("stafiDistributor", address(this)) onlyTrustedNode(msg.sender) {
uint256 predealedEpoch = getMerkleDealedEpoch();
require(_dealedEpoch > predealedEpoch, "epoch already dealed");
bytes32 proposalId = keccak256(abi.encodePacked("setMerkleRoot", _dealedEpoch, _merkleRoot));
bool needExe = _voteProposal(proposalId);
if (needExe) {
setMerkleRoot(_merkleRoot);
setMerkleDealedEpoch(_dealedEpoch);
_afterExecProposal(proposalId);
emit SetMerkleRoot(_dealedEpoch, _merkleRoot);
}
}
function setPlatformTotalAmount(
uint256 _dealedEpoch,
uint256 _totalAmount
) external onlyLatestContract("stafiDistributor", address(this)) onlyTrustedNode(msg.sender) {
bytes32 proposalId = keccak256(abi.encodePacked("setPlatformTotalAmount", _dealedEpoch, _totalAmount));
bool needExe = _voteProposal(proposalId);
if (needExe) {
setPlatformTotalAmount(_totalAmount);
_afterExecProposal(proposalId);
emit SetPlatformTotalAmount(_dealedEpoch, _totalAmount);
}
}
function claim(
uint256 _index,
address _account,
uint256 _totalRewardAmount,
uint256 _totalExitDepositAmount,
bytes32[] calldata _merkleProof,
ClaimType _claimType
) external onlyLatestContract("stafiDistributor", address(this)) {
uint256 claimableReward = _totalRewardAmount.sub(getTotalClaimedReward(_account));
uint256 claimableDeposit = _totalExitDepositAmount.sub(getTotalClaimedDeposit(_account));
bytes32 node = keccak256(abi.encodePacked(_index, _account, _totalRewardAmount, _totalExitDepositAmount));
require(MerkleProof.verify(_merkleProof, getMerkleRoot(), node), "invalid proof");
uint256 willClaimAmount;
if (_claimType == ClaimType.CLAIMREWARD) {
require(claimableReward > 0, "no claimable reward");
setTotalClaimedReward(_account, _totalRewardAmount);
willClaimAmount = claimableReward;
} else if (_claimType == ClaimType.CLAIMDEPOSIT) {
require(claimableDeposit > 0, "no claimable deposit");
setTotalClaimedDeposit(_account, _totalExitDepositAmount);
willClaimAmount = claimableDeposit;
} else if (_claimType == ClaimType.CLAIMTOTAL) {
willClaimAmount = claimableReward.add(claimableDeposit);
require(willClaimAmount > 0, "no claimable amount");
setTotalClaimedReward(_account, _totalRewardAmount);
setTotalClaimedDeposit(_account, _totalExitDepositAmount);
} else {
revert("unknown claimType");
}
IStafiEther stafiEther = IStafiEther(getContractAddress("stafiEther"));
stafiEther.withdrawEther(willClaimAmount);
(bool success, ) = _account.call{value: willClaimAmount}("");
require(success, "failed to claim ETH");
emit Claimed(_index, _account, claimableReward, claimableDeposit, _claimType);
}
function setTotalClaimedReward(address _account, uint256 _totalAmount) internal {
setUint(keccak256(abi.encodePacked("stafiDistributor.node.totalClaimedReward", _account)), _totalAmount);
}
function setTotalClaimedDeposit(address _account, uint256 _totalAmount) internal {
setUint(keccak256(abi.encodePacked("stafiDistributor.node.totalClaimedDeposit", _account)), _totalAmount);
}
function setMerkleDealedEpoch(uint256 _dealedEpoch) internal {
setUint(keccak256(abi.encodePacked("stafiDistributor.merkleRoot.dealedEpoch")), _dealedEpoch);
}
function setMerkleRoot(bytes32 _merkleRoot) internal {
setBytes32(keccak256(abi.encodePacked("stafiDistributor.merkleRoot")), _merkleRoot);
}
function setDistributeFeeDealedHeight(uint256 _dealedHeight) internal {
setUint(keccak256(abi.encodePacked("stafiDistributor.distributeFee.dealedHeight")), _dealedHeight);
}
function setDistributeSuperNodeFeeDealedHeight(uint256 _dealedHeight) internal {
setUint(keccak256(abi.encodePacked("stafiDistributor.distributeSuperNodeFee.dealedHeight")), _dealedHeight);
}
function setDistributeSlashDealedHeight(uint256 _dealedHeight) internal {
setUint(keccak256(abi.encodePacked("stafiDistributor.distributeSlashAmount.dealedHeight")), _dealedHeight);
}
function setPlatformTotalAmount(uint256 _totalAmount) internal {
setUint(keccak256(abi.encodePacked("stafiDistributor.platform.totalAmount")), _totalAmount);
}
function setPlatformTotalClaimedAmount(uint256 _totalClaimedAmount) internal {
setUint(keccak256(abi.encodePacked("stafiDistributor.platform.totalClaimedAmount")), _totalClaimedAmount);
}
function _voteProposal(bytes32 _proposalId) internal returns (bool) {
bytes32 proposalNodeKey = keccak256(
abi.encodePacked("stafiDistributor.proposal.node.key", _proposalId, msg.sender)
);
bytes32 proposalKey = keccak256(abi.encodePacked("stafiDistributor.proposal.key", _proposalId));
require(!getBool(proposalKey), "proposal already executed");
require(!getBool(proposalNodeKey), "duplicate vote");
setBool(proposalNodeKey, true);
uint256 voteCount = getUint(proposalKey).add(1);
setUint(proposalKey, voteCount);
emit VoteProposal(_proposalId, msg.sender);
uint256 calcBase = 1 ether;
IStafiNodeManager stafiNodeManager = IStafiNodeManager(getContractAddress("stafiNodeManager"));
IStafiNetworkSettings stafiNetworkSettings = IStafiNetworkSettings(getContractAddress("stafiNetworkSettings"));
uint256 threshold = stafiNetworkSettings.getNodeConsensusThreshold();
if (calcBase.mul(voteCount) >= stafiNodeManager.getTrustedNodeCount().mul(threshold)) {
return true;
}
return false;
}
function _afterExecProposal(bytes32 _proposalId) internal {
bytes32 proposalKey = keccak256(abi.encodePacked("stafiDistributor.proposal.key", _proposalId));
setBool(proposalKey, true);
emit ProposalExecuted(_proposalId);
}
}
{
"compilationTarget": {
"contracts/reward/StafiDistributor.sol": "StafiDistributor"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_stafiStorageAddress","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"index","type":"uint256"},{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"uint256","name":"claimableReward","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"claimableDeposit","type":"uint256"},{"indexed":false,"internalType":"enum ClaimType","name":"claimType","type":"uint8"}],"name":"Claimed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"dealedHeight","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"userAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"nodeAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"platformAmount","type":"uint256"}],"name":"DistributeFee","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"dealedHeight","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"slashAmount","type":"uint256"}],"name":"DistributeSlash","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"dealedHeight","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"userAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"nodeAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"platformAmount","type":"uint256"}],"name":"DistributeSuperNodeFee","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"proposalId","type":"bytes32"}],"name":"ProposalExecuted","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"dealedEpoch","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"merkleRoot","type":"bytes32"}],"name":"SetMerkleRoot","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"dealedEpoch","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"totalAmount","type":"uint256"}],"name":"SetPlatformTotalAmount","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"proposalId","type":"bytes32"},{"indexed":false,"internalType":"address","name":"voter","type":"address"}],"name":"VoteProposal","type":"event"},{"inputs":[{"internalType":"uint256","name":"_index","type":"uint256"},{"internalType":"address","name":"_account","type":"address"},{"internalType":"uint256","name":"_totalRewardAmount","type":"uint256"},{"internalType":"uint256","name":"_totalExitDepositAmount","type":"uint256"},{"internalType":"bytes32[]","name":"_merkleProof","type":"bytes32[]"},{"internalType":"enum ClaimType","name":"_claimType","type":"uint8"}],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_receiver","type":"address"}],"name":"claimPlatformFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_dealedHeight","type":"uint256"},{"internalType":"uint256","name":"_userAmount","type":"uint256"},{"internalType":"uint256","name":"_nodeAmount","type":"uint256"},{"internalType":"uint256","name":"_platformAmount","type":"uint256"}],"name":"distributeFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_dealedHeight","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"distributeSlashAmount","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_dealedHeight","type":"uint256"},{"internalType":"uint256","name":"_userAmount","type":"uint256"},{"internalType":"uint256","name":"_nodeAmount","type":"uint256"},{"internalType":"uint256","name":"_platformAmount","type":"uint256"}],"name":"distributeSuperNodeFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"distributeWithdrawals","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"getCurrentNodeDepositAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getDistributeFeeDealedHeight","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getDistributeSlashDealedHeight","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getDistributeSuperNodeFeeDealedHeight","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getMerkleDealedEpoch","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getMerkleRoot","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPlatformTotalAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPlatformTotalClaimedAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"getTotalClaimedDeposit","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"getTotalClaimedReward","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"receiveEtherWithdrawal","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_dealedEpoch","type":"uint256"},{"internalType":"bytes32","name":"_merkleRoot","type":"bytes32"}],"name":"setMerkleRoot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_dealedEpoch","type":"uint256"},{"internalType":"uint256","name":"_totalAmount","type":"uint256"}],"name":"setPlatformTotalAmount","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_merkleRoot","type":"bytes32"}],"name":"updateMerkleRoot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"version","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]