// SPDX-License-Identifier: Apache-2.0pragmasolidity 0.8.14;import"@openzeppelin/contracts/token/ERC20/IERC20.sol";
contractDevFund{
/// --- External contracts// token to be awarded as reward to holders
IERC20 public rewardToken;
// token to claim rewards
IERC20 public holderToken;
/// --- Storage// total reward on contractuint256public totalReward;
// previously seen reward balance on contractuint256public prevBalance;
// User's address => Reward at time of withdrawmapping(address=>uint256) public rewardAtTimeOfWithdraw;
// User's address => Reward which can be withdrawnmapping(address=>uint256) public owed;
/**
* @dev Contract constructor sets reward token address.
*/constructor(address _rewardToken) {
rewardToken = IERC20(_rewardToken);
}
/**
* @dev Setup function sets dev holder token address and owner, callable only once.
* @param _holderToken Dev holder Token address.
*/functionsetup(address _holderToken) externalreturns (bool){
require(address(holderToken) ==address(0), "DevFund: already setup");
holderToken = IERC20(_holderToken);
returntrue;
}
/// --- Contract functions/**
* @dev Withdraws reward for holder. Returns reward.
*/functionwithdrawReward() publicreturns (uint256) {
updateTotalReward();
uint256 value = calcReward(msg.sender) + owed[msg.sender];
rewardAtTimeOfWithdraw[msg.sender] = totalReward;
owed[msg.sender] =0;
require(value >0, "DevFund: withdrawReward nothing to transfer");
rewardToken.transfer(msg.sender, value);
prevBalance = rewardToken.balanceOf(address(this));
return value;
}
/**
* @dev Credits reward to owed balance.
* @param forAddress holder's address.
*/functionsoftWithdrawRewardFor(address forAddress) externalreturns (uint256){
updateTotalReward();
uint256 value = calcReward(forAddress);
rewardAtTimeOfWithdraw[forAddress] = totalReward;
owed[forAddress] += value;
return value;
}
/**
* @dev View remaining reward for an address.
* @param forAddress holder's address.
*/functionrewardFor(address forAddress) publicviewreturns (uint256) {
uint256 _currentBalance = rewardToken.balanceOf(address(this));
uint256 _totalReward = totalReward +
(_currentBalance > prevBalance ? _currentBalance - prevBalance : 0);
return
owed[forAddress] +
(holderToken.balanceOf(forAddress) *
(_totalReward - rewardAtTimeOfWithdraw[forAddress])) /
holderToken.totalSupply();
}
/**
* @dev if we got some tokens update the totalReward, has to be called on every withdraw
*/functionupdateTotalReward() internalreturns (uint256) {
uint256 currentBalance = rewardToken.balanceOf(address(this));
if (currentBalance > prevBalance) {
totalReward += currentBalance - prevBalance;
}
return totalReward;
}
/**
* @dev Compute reward for holder. Returns reward.
* @param forAddress holder address.
*/functioncalcReward(address forAddress) internalviewreturns (uint256) {
return
(holderToken.balanceOf(forAddress) *
(totalReward - rewardAtTimeOfWithdraw[forAddress])) /
holderToken.totalSupply();
}
// 0 ETH transfers to trigger withdrawRewardfallback() external{withdrawReward();}
}
Contract Source Code
File 2 of 2: IERC20.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)pragmasolidity ^0.8.0;/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/interfaceIERC20{
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/eventTransfer(addressindexedfrom, addressindexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/eventApproval(addressindexed owner, addressindexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/functiontotalSupply() externalviewreturns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/functionbalanceOf(address account) externalviewreturns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/functiontransfer(address to, uint256 amount) externalreturns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/functionallowance(address owner, address spender) externalviewreturns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/functionapprove(address spender, uint256 amount) externalreturns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/functiontransferFrom(addressfrom,
address to,
uint256 amount
) externalreturns (bool);
}