// SPDX-License-Identifier: MIT// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)pragmasolidity ^0.8.0;/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/abstractcontractContext{
function_msgSender() internalviewvirtualreturns (address) {
returnmsg.sender;
}
function_msgData() internalviewvirtualreturns (bytescalldata) {
returnmsg.data;
}
}
Contract Source Code
File 2 of 4: 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);
}
Contract Source Code
File 3 of 4: MultiDistributor.sol
// SPDX-License-Identifier: ISCpragmasolidity ^0.8.13;pragmaabicoderv2;import {IERC20} from"lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {Ownable} from"lib/openzeppelin-contracts/contracts/access/Ownable.sol";
/**
* @title Token Distributor contract.
* @dev Whitelisted addresses can create batched claims for tokens.
* Contract owner can approve / remove these claims.
*
* @author Lyra
*/contractMultiDistributorisOwnable{
// Details for a specific batchIdstructBatch {
IERC20 token;
bool approved;
}
// Batch Idsuint nextId =1;
mapping(address=>bool) public whitelisted; // whitelisted addresses approved for creating claimsmapping(uint=> Batch) public batchApprovals; // batchId -> Batch detailsmapping(uint=>mapping(address=>uint)) public amountToClaim; // batchId -> User -> amountmapping(address=>mapping(IERC20 =>uint)) public totalClaimed; // User -> Token -> Amount claimed/////////////////////// Constructor ///////////////////////constructor() Ownable() {}
//////////////////////////////// Owner-only Functions /////////////////////////////////**
* @notice Setter to whitelist an address to create claims.
*
* @param _user The address to be added or removed.
* @param _whitelist Boolean on whether the address is being added or removed.
*/functionsetWhitelistAddress(address _user, bool _whitelist) externalonlyOwner{
whitelisted[_user] = _whitelist;
emit WhitelistAddressSet(_user, _whitelist);
}
/**
* @notice Allows owner to approve or unapprove batchIds.
*
* @param batchIds The list of batchIds to approve or unapprove
* @param approve Bool on whether the ids are being approved or not
*/functionapproveClaims(uint[] memory batchIds, bool approve) externalonlyOwner{
for (uint i =0; i < batchIds.length; i++) {
batchApprovals[batchIds[i]].approved = approve;
emit ClaimApproved(batchIds[i]);
}
}
//////////////////////////////////// Whitelist-only Functions /////////////////////////////////////**
* @notice Allows whitelisted addresses to create a batch of new claims.
*
* @param tokenAmounts List of token amounts to add for the batch
* @param users List of user addresses that correspond to the tokenAmounts
* @param token The reward token
* @param epochTimestamp The timestamp for the epoch
* @param tag Data relating to the claim
*/functionaddToClaims(uint[] memory tokenAmounts,
address[] memory users,
IERC20 token,
uint epochTimestamp,
stringmemory tag
) external{
if (whitelisted[msg.sender] !=true) revert MD_NotWhitelisted(msg.sender);
if (tokenAmounts.length!= users.length) revert MD_InvalidArrayLength(tokenAmounts.length, users.length);
for (uint i =0; i < users.length; i++) {
amountToClaim[nextId][users[i]] += tokenAmounts[i];
emit ClaimAdded(token, users[i], tokenAmounts[i], nextId, epochTimestamp, tag);
}
batchApprovals[nextId++].token = token;
}
////////////////////////////// External Functions ///////////////////////////////**
* @notice Allows user to redeem a list of batchIds.
* @dev Users can only claim their own rewards.
* @param claimList List of batchIds to claim
*/functionclaim(uint[] memory claimList) external{
for (uint i =0; i < claimList.length; i++) {
uint batchId = claimList[i];
if (batchApprovals[batchId].approved !=true) revert MD_BatchNotApproved(batchId);
uint balanceToClaim = amountToClaim[batchId][msg.sender];
if (balanceToClaim ==0) {
continue;
}
amountToClaim[batchId][msg.sender] =0;
totalClaimed[msg.sender][batchApprovals[batchId].token] += balanceToClaim;
batchApprovals[batchId].token.transfer(msg.sender, balanceToClaim);
emit Claimed(batchApprovals[batchId].token, msg.sender, batchId, balanceToClaim);
}
}
/**
* @notice Returns the claimable amount of a a specific token for an address.
* @param batchIds The list of batchIds to claim
* @param user The addresses claimable amount
* @param token The claimable amount for this token
*/functiongetClaimableAmountForUser(uint[] memory batchIds, address user, IERC20 token)
externalviewreturns (uint amount)
{
for (uint i =0; i < batchIds.length; i++) {
uint balanceToClaim = amountToClaim[batchIds[i]][user];
if (
balanceToClaim >0&& batchApprovals[batchIds[i]].approved ==true&& batchApprovals[batchIds[i]].token == token
) {
amount += balanceToClaim;
}
}
}
/**
* @notice Returns the claimable ids for an address.
* @param batchIds The list of batchIds to check
* @param user The addresses claimable amount
*/functiongetClaimableIdsForUser(uint[] memory batchIds, address user)
externalviewreturns (uint[] memory claimableIds)
{
uint count;
claimableIds =newuint[](batchIds.length);
for (uint i =0; i < batchIds.length; i++) {
if (amountToClaim[batchIds[i]][user] >0&& batchApprovals[batchIds[i]].approved ==true) {
claimableIds[count++] = batchIds[i];
}
}
}
////////////// Events //////////////eventWhitelistAddressSet(address user, bool whitelisted);
eventClaimed(IERC20 indexed rewardToken, addressindexed claimer, uintindexed batchId, uint amount);
eventClaimAdded(
IERC20 rewardToken,
addressindexed claimer,
uint amount,
uintindexed batchId,
uintindexed epochTimestamp,
string tag
);
eventClaimApproved(uintindexed batchId);
////////////// Errors //////////////errorMD_NotWhitelisted(address user);
errorMD_InvalidArrayLength(uint tokenLength, uint userLength);
errorMD_BatchNotApproved(uint batchId);
}
Contract Source Code
File 4 of 4: Ownable.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)pragmasolidity ^0.8.0;import"../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/abstractcontractOwnableisContext{
addressprivate _owner;
eventOwnershipTransferred(addressindexed previousOwner, addressindexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/modifieronlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/functionowner() publicviewvirtualreturns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/function_checkOwner() internalviewvirtual{
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/functionrenounceOwnership() publicvirtualonlyOwner{
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/functiontransferOwnership(address newOwner) publicvirtualonlyOwner{
require(newOwner !=address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/function_transferOwnership(address newOwner) internalvirtual{
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}