Verified contractPartialMIT
Campaigns
Address0x3b929708ccfbd49f34497ca5a8979ff2132ce2a4
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.19
- Same code
Only this contract
Details
- Compiler
- solc 0.8.19+commit.7dd6d404
- Language
- solidity
- Optimization
- Yes, 800 runs
- EVM version
- paris
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 19,267,970
- Creation transaction
- 0x382bb56001…a9fcdab647
17 files
- contracts/campaigns/Campaigns.solmain21.09 KB
- @openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol3.15 KB
- @openzeppelin/contracts-upgradeable/interfaces/IERC5267Upgradeable.sol749 B
- @openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol6.64 KB
- @openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol3.66 KB
- @openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol2.75 KB
- @openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20PermitUpgradeable.sol2.27 KB
- @openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol7.08 KB
- @openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol9.38 KB
- @openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol1.35 KB
- @openzeppelin/contracts-upgradeable/utils/StringsUpgradeable.sol2.85 KB
- @openzeppelin/contracts-upgradeable/utils/cryptography/ECDSAUpgradeable.sol9.16 KB
- @openzeppelin/contracts-upgradeable/utils/cryptography/EIP712Upgradeable.sol8.04 KB
- @openzeppelin/contracts-upgradeable/utils/math/MathUpgradeable.sol12.8 KB
- @openzeppelin/contracts-upgradeable/utils/math/SignedMathUpgradeable.sol1.27 KB
- contracts/protocol-rewards/ProtocolRewards.sol3.73 KB
- contracts/protocol-rewards/RewardsSplitter.sol3.59 KB
contracts/campaigns/Campaigns.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
/* solhint-disable max-line-length */
import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import { IERC20Upgradeable, SafeERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import { ReentrancyGuardUpgradeable } from "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import { EIP712Upgradeable } from "@openzeppelin/contracts-upgradeable/utils/cryptography/EIP712Upgradeable.sol";
import { ECDSAUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/cryptography/ECDSAUpgradeable.sol";
import { RewardsSplitter } from "../protocol-rewards/RewardsSplitter.sol";
/* solhint-enable max-line-length */
/// @title Campaigns
/// @author Coinvise
/// @notice Create ERC20 token / native currency campaigns that are claimable with a signature from a trusted address
contract Campaigns is OwnableUpgradeable, ReentrancyGuardUpgradeable, EIP712Upgradeable, RewardsSplitter {
using SafeERC20Upgradeable for IERC20Upgradeable;
/// @notice Emitted when trying to set `claimFee` to zero
error InvalidFee();
/// @notice Emitted when trying to create campaign with zero `_maxClaims` or `_amountPerClaim`
error InvalidCount();
/// @notice Emitted when incorrect msg.value is passed during purchase or renewal
error IncorrectValue();
/// @notice Emitted when trying to claim non existent campaign
error NonExistentCampaign();
/// @notice Emitted when trying to claim or withdraw an inactive campaign
error InactiveCampaign();
/// @notice Emitted when user tries to claim campaign more than once
error AlreadyClaimed();
/// @notice Emitted when user tries to claim a campaign that's fully claimed
error ExceedsMaxClaims();
/// @notice Emitted when ether transfer reverted
error TransferFailed();
/// @notice Emitted when `campaignManager` creates a campaign with `campaignId`
/// @param campaignManager creator of the campaign
/// @param campaignId id of the campaign created under the creator address
event CampaignCreated(address indexed campaignManager, uint256 indexed campaignId);
/// @notice Emitted when `campaignManager` withdraws a campaign with `campaignId`
/// @param campaignManager creator of the campaign
/// @param campaignId id of the campaign being withdrawn
event CampaignWithdrawn(address indexed campaignManager, uint256 indexed campaignId);
/// @notice Emitted when `claimer` claims a campaign by `campaignManager` with `campaignId`
/// @param campaignManager creator of the campaign
/// @param campaignId id of the campaign being claimed
/// @param claimer address of the claimer
/// @param tokenAddress address of token being claimed
/// @param amount amount of tokens being claimed
event CampaignClaimed(
address indexed campaignManager,
uint256 indexed campaignId,
address indexed claimer,
address tokenAddress,
uint256 amount
);
/// @notice Emitted when fees are withdrawn to `treasury`
/// @param amount amount of funds withdrawn to `treasury`
/// @param treasury treasury address to which funds are withdrawn
event Withdrawal(uint256 amount, address indexed treasury);
/// @notice Emitted when sponsored claim fees are paid during createCampaign
/// @param sponsoredClaims number of sponsored claims
/// @param claimFee sponsored claim fees paid
/// @param claimFeeRecipient sponsored claim fee recipient
event SponsoredClaimFeesPaid(uint256 sponsoredClaims, uint256 claimFee, address claimFeeRecipient);
/// @notice Emitted when mint fee is paid during mint
/// @param claimFee claim fee paid
/// @param claimFeePayer token claimer who paid claimFee
/// @param claimFeeRecipient claim fee recipient
/// @param campaignManager campaignManager of campaign
/// @param campaignId campaignId of campaign
/// @param referrer referrer of token claimer
event ClaimFeePaid(
uint256 claimFee,
address claimFeePayer,
address claimFeeRecipient,
address campaignManager,
uint256 campaignId,
address referrer
);
struct Campaign {
address tokenAddress; // address of token used in campaign
uint256 maxClaims; // max no. of claims possible for the campaign
uint256 noOfClaims; // no. of times a campaign has been claimed
uint256 amountPerClaim; // amount of tokens received per claim
uint256 isInactive; // whether campaign has been withdrawn, and is now inactive
uint256 maxSponsoredClaims; // max allowed sponsored mints
uint256 noOfSponsoredClaims; // no. of sponsored claims
}
/// @dev ETH pseudo-address used to represent native currency campaigns
address private constant ETHAddress = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
bytes32 private constant _CLAIM_TYPEHASH =
keccak256("Claim(address campaignManager,uint256 campaignId,address claimer)");
/// @dev Address used for signatures
address internal trustedAddress;
/// @notice Claim Fee
uint256 public claimFee;
/// @notice Total claim fees accrued that is withdrawable
uint256 public totalClaimFees;
/// @notice Mapping to store next campaignId for a user: campaignManager => campaignId
/// @dev campaignId is only unique to each campaignManager, not global
mapping(address => uint256) internal nextCampaignId;
/// @notice Mapping to store campaigns: campaignManager => campaignId => Campaign
/// @dev Helps avoid writing repeated campaignManager addresses in storage.
/// campaignId is only unique to each campaignManager, not global
mapping(address => mapping(uint256 => Campaign)) public campaigns;
/// @notice Mapping to store addresses who've claimed a campaign:
/// keccak256(abi.encode(campaignManager, campaignId, claimer)) => claimed
/// @dev Helps avoid multiple hashing & inputs for nested mapping
mapping(bytes32 => uint256) public hasClaimed;
/// @notice Sponsored Claim Fee
uint256 public sponsoredClaimFee;
/// @notice Treasury address
/// @dev Receives sponsored claim fees during createCampaign
address public treasury;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor(address _protocolRewards) RewardsSplitter(_protocolRewards) {
_disableInitializers();
}
/// @dev Initialize proxy for Campaigns
/// @param _trustedAddress Address used for signatures
/// @param _claimFee Claim fee
function initialize(address _trustedAddress, uint256 _claimFee) public initializer {
__Ownable_init();
__EIP712_init("Campaigns", "1.0");
assembly {
// if (_trustedAddress == address(0))
if iszero(_trustedAddress) {
mstore(0x00, 0xe6c4247b) // revert InvalidAddress();
revert(0x1c, 0x04)
}
// Clean the upper 96 bits
// Write to storage
sstore(trustedAddress.slot, shr(96, shl(96, _trustedAddress))) // trustedAddress = _trustedAddress;
sstore(claimFee.slot, _claimFee) // claimFee = _claimFee;
}
}
/// @notice Create a new campaign
/// @dev Stores a new `Campaign` to `campaigns[campaignManager][campaignId]`.
/// Transfers the total required tokens from creator to contract.
/// Reverts if `_tokenAddress` is zero.
/// Reverts if `_maxClaims` | `_amountPerClaim` is not greater than zero.
/// Emits `CampaignCreated`
/// @param _tokenAddress address of token used in campaign
/// @param _maxClaims max no. of claims possible for the campaign
/// @param _amountPerClaim amount of tokens received per claim
/// @param _maxSponsoredClaims no. of sponsored claims
function createCampaign(
address _tokenAddress,
uint256 _maxClaims,
uint256 _amountPerClaim,
uint256 _maxSponsoredClaims
) external payable nonReentrant returns (uint256 _campaignId) {
// Revert if InvalidAddress / InvalidCount
assembly {
// if (_tokenAddress == address(0))
if iszero(_tokenAddress) {
mstore(0x00, 0xe6c4247b) // revert InvalidAddress();
revert(0x1c, 0x04)
}
// if (!(_maxClaims > 0) || !(_amountPerClaim > 0))
if or(iszero(_maxClaims), iszero(_amountPerClaim)) {
mstore(0x00, 0x3492aef6) // revert InvalidCount();
revert(0x1c, 0x04)
}
if gt(_maxSponsoredClaims, _maxClaims) {
mstore(0x00, 0xe38a9e81) // revert ExceedsMaxClaims();
revert(0x1c, 0x04)
}
}
// Create & store new Campaign
/// @dev practically hard for nextCampaignId to overflow type(uint256).max
unchecked {
_campaignId = nextCampaignId[msg.sender]++;
Campaign storage _campaign = campaigns[msg.sender][_campaignId];
_campaign.tokenAddress = _tokenAddress;
_campaign.maxClaims = _maxClaims;
_campaign.noOfClaims = 0;
_campaign.amountPerClaim = _amountPerClaim;
_campaign.isInactive = 0;
_campaign.maxSponsoredClaims = _maxSponsoredClaims;
_campaign.noOfSponsoredClaims = 0;
}
uint256 totalValue;
uint256 totalSponsoredClaimFees = _maxSponsoredClaims * sponsoredClaimFee;
uint256 totalCampaignAmount = _amountPerClaim * _maxClaims;
if (_tokenAddress != ETHAddress) {
IERC20Upgradeable(_tokenAddress).safeTransferFrom(msg.sender, address(this), totalCampaignAmount);
totalValue = totalSponsoredClaimFees;
} else {
totalValue = totalSponsoredClaimFees + totalCampaignAmount;
}
assembly {
if iszero(eq(callvalue(), totalValue)) {
mstore(0x00, 0xd2ade556) // revert IncorrectValue();
revert(0x1c, 0x04)
}
}
if (totalSponsoredClaimFees > 0) {
_paySponsoredClaimFees(_maxSponsoredClaims);
}
// Emit event
/* emit CampaignCreated(msg.sender, _campaignId); */
assembly {
log3(
0x00,
0x00, // no data
0xfc5b9d1c2c1134048e1792e3ae27d4eee04f460d341711c7088000d2ca218621, // CampaignCreated(address,uint256)
caller(), // campaignManager
_campaignId // campaignId
)
}
return _campaignId;
}
/// @notice Claim a campaign
/// @dev Transfers campaign.amountPerClaim tokens to claimer.
/// Reverts if
/// - campaign does not exist.
/// - campaign is inactive.
/// - claimer has already claimed before.
/// - campaign is fully claimed.
/// - `claimFee` is not paid.
/// - signature verification fails.
/// Emits `CampaignClaimed`
/// @param _campaignManager creator of the campaign to claim
/// @param _campaignId id of the campaign to claim. unique to campaignManager, not global
/// @param r r component of claim signature from `trustedAddress`
/// @param s s component of claim signature from `trustedAddress`
/// @param v v component of claim signature from `trustedAddress`
/// @param _referrer referrer for the claim
function claim(
address _campaignManager,
uint256 _campaignId,
bytes32 r,
bytes32 s,
uint8 v,
address _referrer
) external payable nonReentrant {
assembly {
// if (msg.sender == _referrer)
if eq(shr(96, shl(96, caller())), shr(96, shl(96, _referrer))) {
mstore(0x00, 0xe6c4247b) // revert InvalidAddress();
revert(0x1c, 0x04)
}
}
Campaign storage _campaign = campaigns[_campaignManager][_campaignId];
// Check non-existent Campaign
/// @dev campaignId could be zero for non-existent campaign
// but tokenAddress will not be zero for an existent campaign
if (_campaign.tokenAddress == address(0)) {
assembly {
mstore(0x00, 0xf456b659) // revert NonExistentCampaign();
revert(0x1c, 0x04)
}
}
// Check campaign is inactive
if (_campaign.isInactive != 0) {
assembly {
mstore(0x00, 0x952ce6dd) // revert InactiveCampaign();
revert(0x1c, 0x04)
}
}
// Check caller already claimed
bytes32 _hasClaimedKey = keccak256(abi.encode(_campaignManager, _campaignId, msg.sender));
if (hasClaimed[_hasClaimedKey] != 0) {
assembly {
mstore(0x00, 0x646cf558) // revert AlreadyClaimed();
revert(0x1c, 0x04)
}
}
// Check Campaign fully claimed
if (_campaign.noOfClaims == _campaign.maxClaims) {
assembly {
mstore(0x00, 0xe38a9e81) // revert ExceedsMaxClaims();
revert(0x1c, 0x04)
}
}
// Check claimFee paid
// Don't charge if sponsored claims are available
bool _isSponsoredClaim = _campaign.noOfSponsoredClaims < _campaign.maxSponsoredClaims;
uint256 _claimFee = _isSponsoredClaim ? 0 : claimFee;
if (msg.value != _claimFee) {
assembly {
mstore(0x00, 0x58d620b3) // revert InvalidFee();
revert(0x1c, 0x04)
}
}
if (_claimFee > 0) {
_payClaimFee(_claimFee, _campaignManager, _campaignId, _referrer);
}
if (_isSponsoredClaim) {
_campaign.noOfSponsoredClaims++;
}
// Verify signature
if (
ECDSAUpgradeable.recover(
_hashTypedDataV4(keccak256(abi.encode(_CLAIM_TYPEHASH, _campaignManager, _campaignId, msg.sender))),
v,
r,
s
) != trustedAddress
) {
assembly {
mstore(0x00, 0xe6c4247b) // revert InvalidAddress();
revert(0x1c, 0x04)
}
}
// Update Campaign state (before external calls)
/// @dev Practically hard for this to overflow type(uint256).max
/// Max value for noOfClaims is maxClaims which has max value type(uint256).max
unchecked {
_campaign.noOfClaims++;
}
hasClaimed[_hasClaimedKey] = 1;
totalClaimFees += msg.value;
address _tokenAddress = _campaign.tokenAddress;
uint256 _amountPerClaim = _campaign.amountPerClaim;
emit CampaignClaimed(_campaignManager, _campaignId, msg.sender, _tokenAddress, _amountPerClaim);
// Transfer tokens
_transferFunds(_tokenAddress, msg.sender, _amountPerClaim);
}
/// @notice Withdraw / Cancel a campaign
/// @dev Marks campaign as inactive.
/// Transfers remaining tokens of the campaign to the creator
/// Reverts if campaign does not exist.
/// Reverts if campaign is inactive
/// Emits `CampaignWithdrawn`
/// @param _campaignId id of the campaign being withdrawn / cancelled
function withdrawCampaign(uint256 _campaignId) external nonReentrant {
Campaign storage _campaign = campaigns[msg.sender][_campaignId];
// Check non-existent Campaign
/// @dev campaignId could be zero for non-existent campaign
// but tokenAddress will not be zero for an existent campaign
if (_campaign.tokenAddress == address(0)) {
assembly {
mstore(0x00, 0xf456b659) // revert NonExistentCampaign();
revert(0x1c, 0x04)
}
}
// Check campaign is inactive
if (_campaign.isInactive != 0) {
assembly {
mstore(0x00, 0x952ce6dd) // revert InactiveCampaign();
revert(0x1c, 0x04)
}
}
// Mark as inactive
_campaign.isInactive = 1;
// Emit event
/* emit CampaignWithdrawn(msg.sender, _campaignId); */
assembly {
log3(
0x00,
0x00, // no data
0x06a0982d8b0bd87e1ae43f31a116ca52b1353fbc3dc30a1a97cf143da800cb0d, // CampaignWithdrawn(address,uint256)
caller(), // campaignManager
_campaignId // campaignId
)
}
// Transfer remaining tokens
_transferFunds(
_campaign.tokenAddress,
msg.sender,
(_campaign.maxClaims - _campaign.noOfClaims) * _campaign.amountPerClaim
);
}
/// @notice Increase no. of sponsored claims
/// @dev Reverts if exceeds max claims
/// @param _additionalSponsoredClaims Number to increase current sponsored claims by
function increaseMaxSponsoredClaims(
address _campaignManager,
uint256 _campaignId,
uint256 _additionalSponsoredClaims
) external payable nonReentrant {
Campaign storage _campaign = campaigns[_campaignManager][_campaignId];
uint256 newSponsoredClaims = _campaign.maxSponsoredClaims + _additionalSponsoredClaims;
if (newSponsoredClaims > _campaign.maxClaims) {
assembly {
mstore(0x00, 0xe38a9e81) // revert ExceedsMaxClaims();
revert(0x1c, 0x04)
}
}
_campaign.maxSponsoredClaims = newSponsoredClaims;
_paySponsoredClaimFees(_additionalSponsoredClaims);
}
/// @notice Withdraw total claim fees collected
/// @dev Transfers `totalClaimFees` to `_treasury` iff it is > 0
/// Callable only by `owner`.
/// Emits `Withdrawal`
/// @param _treasury treasury address to which funds are withdrawn
function withdrawTotalClaimFees(address _treasury) external payable onlyOwner nonReentrant {
uint256 amount = totalClaimFees;
if (amount > 0) {
// reset totalClaimFees
totalClaimFees = 0;
assembly {
/* emit Withdrawal(amount, _treasury); */
let memPtr := mload(64)
mstore(memPtr, amount)
log2(
memPtr,
32, // _amount
0xd964a27d45f595739c13d8b1160b57491050cacf3a2e5602207277d6228f64ee, // Withdrawal(uint256,address)
_treasury // treasury
)
// (bool success, ) = _treasury.call{ value: amount }("");
// if (!success)
if iszero(call(gas(), _treasury, amount, 0, 0, 0, 0)) {
mstore(0x00, 0x90b8ec18) // revert TransferFailed();
revert(0x1c, 0x04)
}
}
}
}
/// @notice Set `trustedAddress`
/// @dev Callable only by `owner`.
/// Reverts if `_trustedAddress` is address(0).
/// @param _trustedAddress Address to be used for signatures
function setTrustedAddress(address _trustedAddress) external payable onlyOwner {
assembly {
// if (_trustedAddress == address(0))
if iszero(_trustedAddress) {
mstore(0x00, 0xe6c4247b) // revert InvalidAddress();
revert(0x1c, 0x04)
}
// Clean the upper 96 bits
// Write to storage
sstore(trustedAddress.slot, shr(96, shl(96, _trustedAddress))) // trustedAddress = _trustedAddress;
}
}
/// @notice Set `treasury`
/// @dev Callable only by `owner`.
/// Reverts if `_treasury` is address(0).
/// @param _treasury Address to be used for signatures
function setTreasury(address _treasury) external payable onlyOwner {
assembly {
if iszero(_treasury) {
mstore(0x00, 0xe6c4247b) // revert InvalidAddress();
revert(0x1c, 0x04)
}
sstore(treasury.slot, shr(96, shl(96, _treasury))) // treasury = _treasury;
}
}
/// @notice Set `claimFee`
/// @dev Callable only by `owner`.
/// @param _claimFee Claim fee
function setClaimFee(uint256 _claimFee) external payable onlyOwner {
assembly {
sstore(claimFee.slot, _claimFee) // claimFee = _claimFee;
}
}
/// @notice Set `sponsoredClaimFee`
/// @dev Callable only by `owner`.
/// @param _sponsoredClaimFee Claim fee
function setSponsoredClaimFee(uint256 _sponsoredClaimFee) external payable onlyOwner {
assembly {
sstore(sponsoredClaimFee.slot, _sponsoredClaimFee) // sponsoredClaimFee = _sponsoredClaimFee;
}
}
/// @dev Internal function to pay claim fee
/// Splits and deposits into protocol rewards
/// Emits ClaimFeePaid
function _payClaimFee(uint256 _claimFee, address _campaignManager, uint256 _campaignId, address _referrer) internal {
splitAndDepositRewards(
_claimFee,
treasury, // platform
_campaignManager, // creator
_referrer // referrer
);
emit ClaimFeePaid(_claimFee, msg.sender, treasury, _campaignManager, _campaignId, _referrer);
}
/// @dev Internal function to pay sponsored claim fees
/// Emits SponsoredClaimFeesPaid
function _paySponsoredClaimFees(uint256 _sponsoredClaims) internal {
uint256 totalSponsoredClaimFees = _sponsoredClaims * sponsoredClaimFee;
// ensure sponsored claim fees is paid in current transaction
if (msg.value < totalSponsoredClaimFees) {
assembly {
mstore(0x00, 0xd2ade556) // revert IncorrectValue();
revert(0x1c, 0x04)
}
}
_transferFunds(ETHAddress, treasury, totalSponsoredClaimFees);
emit SponsoredClaimFeesPaid(_sponsoredClaims, sponsoredClaimFee, treasury);
}
/// @dev Utility method to handle ether or token transfers.
/// Reverts if transfer fails
/// @param tokenAddress address of token to transfer
/// @param recipient recipient of the ether / token transfer
/// @param amount amount of ether / token to transfer in wei
function _transferFunds(address tokenAddress, address recipient, uint256 amount) internal {
if (tokenAddress != ETHAddress) {
IERC20Upgradeable(tokenAddress).safeTransfer(recipient, amount);
} else {
assembly {
// (bool success, ) = recipient.call{ value: amount }("");
// if (!success)
if iszero(call(gas(), recipient, amount, 0, 0, 0, 0)) {
mstore(0x00, 0x90b8ec18) // revert TransferFailed();
revert(0x1c, 0x04)
}
}
}
}
}
@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/ContextUpgradeable.sol";
import "../proxy/utils/Initializable.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.
*/
abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
function __Ownable_init() internal onlyInitializing {
__Ownable_init_unchained();
}
function __Ownable_init_unchained() internal onlyInitializing {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
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) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
}
@openzeppelin/contracts-upgradeable/interfaces/IERC5267Upgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (interfaces/IERC5267.sol)
pragma solidity ^0.8.0;
interface IERC5267Upgradeable {
/**
* @dev MAY be emitted to signal that the domain could have changed.
*/
event EIP712DomainChanged();
/**
* @dev returns the fields and values that describe the domain separator used by this contract for EIP-712
* signature.
*/
function eip712Domain()
external
view
returns (
bytes1 fields,
string memory name,
string memory version,
uint256 chainId,
address verifyingContract,
bytes32 salt,
uint256[] memory extensions
);
}
@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (proxy/utils/Initializable.sol)
pragma solidity ^0.8.2;
import "../../utils/AddressUpgradeable.sol";
/**
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
* behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
*
* The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
* reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
* case an upgrade adds a module that needs to be initialized.
*
* For example:
*
* [.hljs-theme-light.nopadding]
* ```solidity
* contract MyToken is ERC20Upgradeable {
* function initialize() initializer public {
* __ERC20_init("MyToken", "MTK");
* }
* }
*
* contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
* function initializeV2() reinitializer(2) public {
* __ERC20Permit_init("MyToken");
* }
* }
* ```
*
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
* possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
*
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
*
* [CAUTION]
* ====
* Avoid leaving a contract uninitialized.
*
* An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
* contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
* the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
*
* [.hljs-theme-light.nopadding]
* ```
* /// @custom:oz-upgrades-unsafe-allow constructor
* constructor() {
* _disableInitializers();
* }
* ```
* ====
*/
abstract contract Initializable {
/**
* @dev Indicates that the contract has been initialized.
* @custom:oz-retyped-from bool
*/
uint8 private _initialized;
/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool private _initializing;
/**
* @dev Triggered when the contract has been initialized or reinitialized.
*/
event Initialized(uint8 version);
/**
* @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
* `onlyInitializing` functions can be used to initialize parent contracts.
*
* Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a
* constructor.
*
* Emits an {Initialized} event.
*/
modifier initializer() {
bool isTopLevelCall = !_initializing;
require(
(isTopLevelCall && _initialized < 1) || (!AddressUpgradeable.isContract(address(this)) && _initialized == 1),
"Initializable: contract is already initialized"
);
_initialized = 1;
if (isTopLevelCall) {
_initializing = true;
}
_;
if (isTopLevelCall) {
_initializing = false;
emit Initialized(1);
}
}
/**
* @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
* contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
* used to initialize parent contracts.
*
* A reinitializer may be used after the original initialization step. This is essential to configure modules that
* are added through upgrades and that require initialization.
*
* When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
* cannot be nested. If one is invoked in the context of another, execution will revert.
*
* Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
* a contract, executing them in the right order is up to the developer or operator.
*
* WARNING: setting the version to 255 will prevent any future reinitialization.
*
* Emits an {Initialized} event.
*/
modifier reinitializer(uint8 version) {
require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
_initialized = version;
_initializing = true;
_;
_initializing = false;
emit Initialized(version);
}
/**
* @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
* {initializer} and {reinitializer} modifiers, directly or indirectly.
*/
modifier onlyInitializing() {
require(_initializing, "Initializable: contract is not initializing");
_;
}
/**
* @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
* Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
* to any version. It is recommended to use this to lock implementation contracts that are designed to be called
* through proxies.
*
* Emits an {Initialized} event the first time it is successfully executed.
*/
function _disableInitializers() internal virtual {
require(!_initializing, "Initializable: contract is initializing");
if (_initialized != type(uint8).max) {
_initialized = type(uint8).max;
emit Initialized(type(uint8).max);
}
}
/**
* @dev Returns the highest version that has been initialized. See {reinitializer}.
*/
function _getInitializedVersion() internal view returns (uint8) {
return _initialized;
}
/**
* @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
*/
function _isInitializing() internal view returns (bool) {
return _initializing;
}
}
@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
import "../proxy/utils/Initializable.sol";
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuardUpgradeable is Initializable {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
function __ReentrancyGuard_init() internal onlyInitializing {
__ReentrancyGuard_init_unchained();
}
function __ReentrancyGuard_init_unchained() internal onlyInitializing {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be _NOT_ENTERED
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[49] private __gap;
}
@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20Upgradeable {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed 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.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (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.
*/
function transfer(address to, uint256 amount) external returns (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.
*/
function allowance(address owner, address spender) external view returns (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.
*/
function approve(address spender, uint256 amount) external returns (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.
*/
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
@openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20PermitUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/extensions/IERC20Permit.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*/
interface IERC20PermitUpgradeable {
/**
* @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
* given ``owner``'s signed approval.
*
* IMPORTANT: The same issues {IERC20-approve} has related to transaction
* ordering also apply here.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `deadline` must be a timestamp in the future.
* - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
* over the EIP712-formatted function arguments.
* - the signature must use ``owner``'s current nonce (see {nonces}).
*
* For more information on the signature format, see the
* https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
* section].
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Returns the current nonce for `owner`. This value must be
* included whenever a signature is generated for {permit}.
*
* Every successful call to {permit} increases ``owner``'s nonce by one. This
* prevents a signature from being used multiple times.
*/
function nonces(address owner) external view returns (uint256);
/**
* @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
*/
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.0;
import "../IERC20Upgradeable.sol";
import "../extensions/IERC20PermitUpgradeable.sol";
import "../../../utils/AddressUpgradeable.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20Upgradeable {
using AddressUpgradeable for address;
/**
* @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeTransfer(IERC20Upgradeable token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
/**
* @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
* calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
*/
function safeTransferFrom(IERC20Upgradeable token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(IERC20Upgradeable token, address spender, uint256 value) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
/**
* @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeIncreaseAllowance(IERC20Upgradeable token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
}
/**
* @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeDecreaseAllowance(IERC20Upgradeable token, address spender, uint256 value) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
}
}
/**
* @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful. Compatible with tokens that require the approval to be set to
* 0 before setting it to a non-zero value.
*/
function forceApprove(IERC20Upgradeable token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
_callOptionalReturn(token, approvalCall);
}
}
/**
* @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`.
* Revert on invalid signature.
*/
function safePermit(
IERC20PermitUpgradeable token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20Upgradeable token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*
* This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
*/
function _callOptionalReturnBool(IERC20Upgradeable token, bytes memory data) private returns (bool) {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
// and not revert is the subcall reverts.
(bool success, bytes memory returndata) = address(token).call(data);
return
success && (returndata.length == 0 || abi.decode(returndata, (bool))) && AddressUpgradeable.isContract(address(token));
}
}
@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
pragma solidity ^0.8.1;
/**
* @dev Collection of functions related to the address type
*/
library AddressUpgradeable {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
*
* Furthermore, `isContract` will also return true if the target contract within
* the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
* which only has an effect at the end of a transaction.
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0
// for contracts in construction, since the code is only stored at the end
// of the constructor execution.
return account.code.length > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
* the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
*
* _Available since v4.8._
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
// only check isContract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
/**
* @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason or using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
pragma solidity ^0.8.0;
import "../proxy/utils/Initializable.sol";
/**
* @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.
*/
abstract contract ContextUpgradeable is Initializable {
function __Context_init() internal onlyInitializing {
}
function __Context_init_unchained() internal onlyInitializing {
}
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[50] private __gap;
}
@openzeppelin/contracts-upgradeable/utils/StringsUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Strings.sol)
pragma solidity ^0.8.0;
import "./math/MathUpgradeable.sol";
import "./math/SignedMathUpgradeable.sol";
/**
* @dev String operations.
*/
library StringsUpgradeable {
bytes16 private constant _SYMBOLS = "0123456789abcdef";
uint8 private constant _ADDRESS_LENGTH = 20;
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/
function toString(uint256 value) internal pure returns (string memory) {
unchecked {
uint256 length = MathUpgradeable.log10(value) + 1;
string memory buffer = new string(length);
uint256 ptr;
/// @solidity memory-safe-assembly
assembly {
ptr := add(buffer, add(32, length))
}
while (true) {
ptr--;
/// @solidity memory-safe-assembly
assembly {
mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
}
value /= 10;
if (value == 0) break;
}
return buffer;
}
}
/**
* @dev Converts a `int256` to its ASCII `string` decimal representation.
*/
function toString(int256 value) internal pure returns (string memory) {
return string(abi.encodePacked(value < 0 ? "-" : "", toString(SignedMathUpgradeable.abs(value))));
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
function toHexString(uint256 value) internal pure returns (string memory) {
unchecked {
return toHexString(value, MathUpgradeable.log256(value) + 1);
}
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
/**
* @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation.
*/
function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
}
/**
* @dev Returns true if the two strings are equal.
*/
function equal(string memory a, string memory b) internal pure returns (bool) {
return keccak256(bytes(a)) == keccak256(bytes(b));
}
}
@openzeppelin/contracts-upgradeable/utils/cryptography/ECDSAUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/cryptography/ECDSA.sol)
pragma solidity ^0.8.0;
import "../StringsUpgradeable.sol";
/**
* @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
*
* These functions can be used to verify that a message was signed by the holder
* of the private keys of a given address.
*/
library ECDSAUpgradeable {
enum RecoverError {
NoError,
InvalidSignature,
InvalidSignatureLength,
InvalidSignatureS,
InvalidSignatureV // Deprecated in v4.8
}
function _throwError(RecoverError error) private pure {
if (error == RecoverError.NoError) {
return; // no error: do nothing
} else if (error == RecoverError.InvalidSignature) {
revert("ECDSA: invalid signature");
} else if (error == RecoverError.InvalidSignatureLength) {
revert("ECDSA: invalid signature length");
} else if (error == RecoverError.InvalidSignatureS) {
revert("ECDSA: invalid signature 's' value");
}
}
/**
* @dev Returns the address that signed a hashed message (`hash`) with
* `signature` or error string. This address can then be used for verification purposes.
*
* The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
* this function rejects them by requiring the `s` value to be in the lower
* half order, and the `v` value to be either 27 or 28.
*
* IMPORTANT: `hash` _must_ be the result of a hash operation for the
* verification to be secure: it is possible to craft signatures that
* recover to arbitrary addresses for non-hashed data. A safe way to ensure
* this is by receiving a hash of the original message (which may otherwise
* be too long), and then calling {toEthSignedMessageHash} on it.
*
* Documentation for signature generation:
* - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js]
* - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers]
*
* _Available since v4.3._
*/
function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
if (signature.length == 65) {
bytes32 r;
bytes32 s;
uint8 v;
// ecrecover takes the signature parameters, and the only way to get them
// currently is to use assembly.
/// @solidity memory-safe-assembly
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
return tryRecover(hash, v, r, s);
} else {
return (address(0), RecoverError.InvalidSignatureLength);
}
}
/**
* @dev Returns the address that signed a hashed message (`hash`) with
* `signature`. This address can then be used for verification purposes.
*
* The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
* this function rejects them by requiring the `s` value to be in the lower
* half order, and the `v` value to be either 27 or 28.
*
* IMPORTANT: `hash` _must_ be the result of a hash operation for the
* verification to be secure: it is possible to craft signatures that
* recover to arbitrary addresses for non-hashed data. A safe way to ensure
* this is by receiving a hash of the original message (which may otherwise
* be too long), and then calling {toEthSignedMessageHash} on it.
*/
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, signature);
_throwError(error);
return recovered;
}
/**
* @dev Overload of {ECDSA-tryRecover} that receives the `r` and `vs` short-signature fields separately.
*
* See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures]
*
* _Available since v4.3._
*/
function tryRecover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address, RecoverError) {
bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
uint8 v = uint8((uint256(vs) >> 255) + 27);
return tryRecover(hash, v, r, s);
}
/**
* @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.
*
* _Available since v4.2._
*/
function recover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, r, vs);
_throwError(error);
return recovered;
}
/**
* @dev Overload of {ECDSA-tryRecover} that receives the `v`,
* `r` and `s` signature fields separately.
*
* _Available since v4.3._
*/
function tryRecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address, RecoverError) {
// EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
// unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
// the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most
// signatures from current libraries generate a unique signature with an s-value in the lower half order.
//
// If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
// with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
// vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
// these malleable signatures as well.
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
return (address(0), RecoverError.InvalidSignatureS);
}
// If the signature is valid (and not malleable), return the signer address
address signer = ecrecover(hash, v, r, s);
if (signer == address(0)) {
return (address(0), RecoverError.InvalidSignature);
}
return (signer, RecoverError.NoError);
}
/**
* @dev Overload of {ECDSA-recover} that receives the `v`,
* `r` and `s` signature fields separately.
*/
function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, v, r, s);
_throwError(error);
return recovered;
}
/**
* @dev Returns an Ethereum Signed Message, created from a `hash`. This
* produces hash corresponding to the one signed with the
* https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
* JSON-RPC method as part of EIP-191.
*
* See {recover}.
*/
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32 message) {
// 32 is the length in bytes of hash,
// enforced by the type signature above
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, "\x19Ethereum Signed Message:\n32")
mstore(0x1c, hash)
message := keccak256(0x00, 0x3c)
}
}
/**
* @dev Returns an Ethereum Signed Message, created from `s`. This
* produces hash corresponding to the one signed with the
* https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
* JSON-RPC method as part of EIP-191.
*
* See {recover}.
*/
function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n", StringsUpgradeable.toString(s.length), s));
}
/**
* @dev Returns an Ethereum Signed Typed Data, created from a
* `domainSeparator` and a `structHash`. This produces hash corresponding
* to the one signed with the
* https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`]
* JSON-RPC method as part of EIP-712.
*
* See {recover}.
*/
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32 data) {
/// @solidity memory-safe-assembly
assembly {
let ptr := mload(0x40)
mstore(ptr, "\x19\x01")
mstore(add(ptr, 0x02), domainSeparator)
mstore(add(ptr, 0x22), structHash)
data := keccak256(ptr, 0x42)
}
}
/**
* @dev Returns an Ethereum Signed Data with intended validator, created from a
* `validator` and `data` according to the version 0 of EIP-191.
*
* See {recover}.
*/
function toDataWithIntendedValidatorHash(address validator, bytes memory data) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x00", validator, data));
}
}
@openzeppelin/contracts-upgradeable/utils/cryptography/EIP712Upgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/cryptography/EIP712.sol)
pragma solidity ^0.8.8;
import "./ECDSAUpgradeable.sol";
import "../../interfaces/IERC5267Upgradeable.sol";
import "../../proxy/utils/Initializable.sol";
/**
* @dev https://eips.ethereum.org/EIPS/eip-712[EIP 712] is a standard for hashing and signing of typed structured data.
*
* The encoding specified in the EIP is very generic, and such a generic implementation in Solidity is not feasible,
* thus this contract does not implement the encoding itself. Protocols need to implement the type-specific encoding
* they need in their contracts using a combination of `abi.encode` and `keccak256`.
*
* This contract implements the EIP 712 domain separator ({_domainSeparatorV4}) that is used as part of the encoding
* scheme, and the final step of the encoding to obtain the message digest that is then signed via ECDSA
* ({_hashTypedDataV4}).
*
* The implementation of the domain separator was designed to be as efficient as possible while still properly updating
* the chain id to protect against replay attacks on an eventual fork of the chain.
*
* NOTE: This contract implements the version of the encoding known as "v4", as implemented by the JSON RPC method
* https://docs.metamask.io/guide/signing-data.html[`eth_signTypedDataV4` in MetaMask].
*
* NOTE: In the upgradeable version of this contract, the cached values will correspond to the address, and the domain
* separator of the implementation contract. This will cause the `_domainSeparatorV4` function to always rebuild the
* separator from the immutable values, which is cheaper than accessing a cached version in cold storage.
*
* _Available since v3.4._
*
* @custom:storage-size 52
*/
abstract contract EIP712Upgradeable is Initializable, IERC5267Upgradeable {
bytes32 private constant _TYPE_HASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
/// @custom:oz-renamed-from _HASHED_NAME
bytes32 private _hashedName;
/// @custom:oz-renamed-from _HASHED_VERSION
bytes32 private _hashedVersion;
string private _name;
string private _version;
/**
* @dev Initializes the domain separator and parameter caches.
*
* The meaning of `name` and `version` is specified in
* https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator[EIP 712]:
*
* - `name`: the user readable name of the signing domain, i.e. the name of the DApp or the protocol.
* - `version`: the current major version of the signing domain.
*
* NOTE: These parameters cannot be changed except through a xref:learn::upgrading-smart-contracts.adoc[smart
* contract upgrade].
*/
function __EIP712_init(string memory name, string memory version) internal onlyInitializing {
__EIP712_init_unchained(name, version);
}
function __EIP712_init_unchained(string memory name, string memory version) internal onlyInitializing {
_name = name;
_version = version;
// Reset prior values in storage if upgrading
_hashedName = 0;
_hashedVersion = 0;
}
/**
* @dev Returns the domain separator for the current chain.
*/
function _domainSeparatorV4() internal view returns (bytes32) {
return _buildDomainSeparator();
}
function _buildDomainSeparator() private view returns (bytes32) {
return keccak256(abi.encode(_TYPE_HASH, _EIP712NameHash(), _EIP712VersionHash(), block.chainid, address(this)));
}
/**
* @dev Given an already https://eips.ethereum.org/EIPS/eip-712#definition-of-hashstruct[hashed struct], this
* function returns the hash of the fully encoded EIP712 message for this domain.
*
* This hash can be used together with {ECDSA-recover} to obtain the signer of a message. For example:
*
* ```solidity
* bytes32 digest = _hashTypedDataV4(keccak256(abi.encode(
* keccak256("Mail(address to,string contents)"),
* mailTo,
* keccak256(bytes(mailContents))
* )));
* address signer = ECDSA.recover(digest, signature);
* ```
*/
function _hashTypedDataV4(bytes32 structHash) internal view virtual returns (bytes32) {
return ECDSAUpgradeable.toTypedDataHash(_domainSeparatorV4(), structHash);
}
/**
* @dev See {EIP-5267}.
*
* _Available since v4.9._
*/
function eip712Domain()
public
view
virtual
override
returns (
bytes1 fields,
string memory name,
string memory version,
uint256 chainId,
address verifyingContract,
bytes32 salt,
uint256[] memory extensions
)
{
// If the hashed name and version in storage are non-zero, the contract hasn't been properly initialized
// and the EIP712 domain is not reliable, as it will be missing name and version.
require(_hashedName == 0 && _hashedVersion == 0, "EIP712: Uninitialized");
return (
hex"0f", // 01111
_EIP712Name(),
_EIP712Version(),
block.chainid,
address(this),
bytes32(0),
new uint256[](0)
);
}
/**
* @dev The name parameter for the EIP712 domain.
*
* NOTE: This function reads from storage by default, but can be redefined to return a constant value if gas costs
* are a concern.
*/
function _EIP712Name() internal virtual view returns (string memory) {
return _name;
}
/**
* @dev The version parameter for the EIP712 domain.
*
* NOTE: This function reads from storage by default, but can be redefined to return a constant value if gas costs
* are a concern.
*/
function _EIP712Version() internal virtual view returns (string memory) {
return _version;
}
/**
* @dev The hash of the name parameter for the EIP712 domain.
*
* NOTE: In previous versions this function was virtual. In this version you should override `_EIP712Name` instead.
*/
function _EIP712NameHash() internal view returns (bytes32) {
string memory name = _EIP712Name();
if (bytes(name).length > 0) {
return keccak256(bytes(name));
} else {
// If the name is empty, the contract may have been upgraded without initializing the new storage.
// We return the name hash in storage if non-zero, otherwise we assume the name is empty by design.
bytes32 hashedName = _hashedName;
if (hashedName != 0) {
return hashedName;
} else {
return keccak256("");
}
}
}
/**
* @dev The hash of the version parameter for the EIP712 domain.
*
* NOTE: In previous versions this function was virtual. In this version you should override `_EIP712Version` instead.
*/
function _EIP712VersionHash() internal view returns (bytes32) {
string memory version = _EIP712Version();
if (bytes(version).length > 0) {
return keccak256(bytes(version));
} else {
// If the version is empty, the contract may have been upgraded without initializing the new storage.
// We return the version hash in storage if non-zero, otherwise we assume the version is empty by design.
bytes32 hashedVersion = _hashedVersion;
if (hashedVersion != 0) {
return hashedVersion;
} else {
return keccak256("");
}
}
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[48] private __gap;
}
@openzeppelin/contracts-upgradeable/utils/math/MathUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library MathUpgradeable {
enum Rounding {
Down, // Toward negative infinity
Up, // Toward infinity
Zero // Toward zero
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds up instead
* of rounding down.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b - 1) / b can overflow on addition, so we distribute.
return a == 0 ? 0 : (a - 1) / b + 1;
}
/**
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
// Solidity will revert if denominator == 0, unlike the div opcode on its own.
// The surrounding unchecked block does not change this fact.
// See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
return prod0 / denominator;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
require(denominator > prod1, "Math: mulDiv overflow");
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 twos = denominator & (~denominator + 1);
assembly {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [prod1 prod0] by twos.
prod0 := div(prod0, twos)
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * twos;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
// in modular arithmetic, doubling the correct bits in each step.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/**
* @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
//
// We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
// `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
//
// This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
// → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
// → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
//
// Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1 << (log2(a) >> 1);
// At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
// since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
// every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
// into the expected uint128 result.
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
/**
* @notice Calculates sqrt(a), following the selected rounding direction.
*/
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
/**
* @dev Return the log in base 2, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 10, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 256, rounded down, of a positive value.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 256, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
}
}
}
@openzeppelin/contracts-upgradeable/utils/math/SignedMathUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SignedMath.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard signed math utilities missing in the Solidity language.
*/
library SignedMathUpgradeable {
/**
* @dev Returns the largest of two signed numbers.
*/
function max(int256 a, int256 b) internal pure returns (int256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two signed numbers.
*/
function min(int256 a, int256 b) internal pure returns (int256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two signed numbers without overflow.
* The result is rounded towards zero.
*/
function average(int256 a, int256 b) internal pure returns (int256) {
// Formula from the book "Hacker's Delight"
int256 x = (a & b) + ((a ^ b) >> 1);
return x + (int256(uint256(x) >> 255) & (a ^ b));
}
/**
* @dev Returns the absolute unsigned value of a signed value.
*/
function abs(int256 n) internal pure returns (uint256) {
unchecked {
// must be unchecked in order to support `n = type(int256).min`
return uint256(n >= 0 ? n : -n);
}
}
}
contracts/protocol-rewards/ProtocolRewards.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
/// @title ProtocolRewards
/// @author Coinvise
/// @notice Contract for distributing protocol rewards
contract ProtocolRewards {
/// @notice Thrown if total ether transfer doesn't match constituent rewards in `depositRewards`
error InvalidAmount();
/// @notice Thrown if ether transfer fails in `withdrawRewards`
error TransferFailed();
/// @notice Emitted when rewards are deposited
/// @param sender sender address
/// @param _platform platform address
/// @param _platformReward platform reward
/// @param _creator creator address
/// @param _creatorReward creator reward
/// @param _referrer referrer address
/// @param _referrerReward referrer reward
event RewardsDeposited(
address sender,
address indexed _platform,
uint256 _platformReward,
address indexed _creator,
uint256 _creatorReward,
address indexed _referrer,
uint256 _referrerReward
);
/// @notice Emitted when rewards are withdrawn
/// @param _account account address
/// @param _amount amount withdrawn
event Withdrawal(address indexed _account, uint256 _amount);
/// @notice Mapping of account to rewards balance
mapping(address => uint256) public balanceOf;
/// @notice Deposit rewards
/// @dev Increments balance of each account
/// @param _platform platform address
/// @param _platformReward platform reward
/// @param _creator creator address
/// @param _creatorReward creator reward
/// @param _referrer referrer address
/// @param _referrerReward referrer reward
function depositRewards(
address _platform,
uint256 _platformReward,
address _creator,
uint256 _creatorReward,
address _referrer,
uint256 _referrerReward
) external payable {
if (msg.value != (_platformReward + _creatorReward + _referrerReward)) {
assembly {
mstore(0x00, 0x2c5211c6) // revert InvalidAmount();
revert(0x1c, 0x04)
}
}
if (_platform != address(0) && _platformReward > 0) balanceOf[_platform] += _platformReward;
if (_creator != address(0) && _creatorReward > 0) balanceOf[_creator] += _creatorReward;
if (_referrer != address(0) && _referrerReward > 0) balanceOf[_referrer] += _referrerReward;
assembly {
let memPtr := mload(64)
mstore(memPtr, caller())
mstore(add(memPtr, 32), _platformReward)
mstore(add(memPtr, 64), _creatorReward)
mstore(add(memPtr, 96), _referrerReward)
log4(
memPtr,
128, // sender, _platformReward, _creatorReward, _referrerReward
0xea2fdf7406d73ac5543899b986199b33f5d424510e1cef6a9d120ce546868bca, // RewardsDeposited
shr(96, shl(96, _platform)), // _platform
shr(96, shl(96, _creator)), // _creator
shr(96, shl(96, _referrer)) // _referrer
)
}
}
/// @notice Withdraw rewards
/// @dev Decrements balance of account and transfers ether
/// @param _account account address
function withdrawRewards(address payable _account) external {
uint256 _amount = balanceOf[_account];
assembly {
// if (_amount == 0) return
if iszero(_amount) {
return(0, 0)
}
}
balanceOf[_account] = 0;
assembly {
// _account.call{value: _amount}("");
if iszero(call(gas(), _account, _amount, 0, 0, 0, 0)) {
mstore(0x00, 0x90b8ec18) // revert TransferFailed();
revert(0x1c, 0x04)
}
let memPtr := mload(64)
mstore(memPtr, _amount)
log2(
memPtr,
32, // _amount
0x7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65, // Withdrawal(address,uint256)
shr(96, shl(96, _account)) // _account
)
}
}
}
contracts/protocol-rewards/RewardsSplitter.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import { ProtocolRewards } from "./ProtocolRewards.sol";
/// @title RewardsSplitter
/// @notice Contract for splitting and depositing rewards
abstract contract RewardsSplitter {
/// @notice Thrown if zero address or creator and referrer are the same
error InvalidAddress();
/// @notice Platform share
/// @dev 60% = 6000 bps = 0.6e4
uint256 internal constant PLATFORM_SHARE = 0.6e4;
/// @notice Creator share
/// @dev 30% = 3000 bps = 0.3e4
uint256 internal constant CREATOR_SHARE = 0.3e4;
/// @notice Referrer share
/// @dev 10% = 1000 bps = 0.1e4
uint256 internal constant REFERRER_SHARE = 0.1e4;
/// @notice Basis points denominator
/// @dev 100% = 10000 bps = 1e4
uint256 internal constant BASIS_POINTS = 1e4;
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
/// @notice ProtocolRewards contract
ProtocolRewards public immutable protocolRewards;
/// @notice RewardsSplitter constructor
/// @param _protocolRewards ProtocolRewards contract
/// @custom:oz-upgrades-unsafe-allow constructor
/// @dev Since only sets immutable variable
constructor(address _protocolRewards) {
protocolRewards = ProtocolRewards(_protocolRewards);
assert(PLATFORM_SHARE + CREATOR_SHARE + REFERRER_SHARE == BASIS_POINTS);
}
/// @notice Split and deposit rewards
/// @dev Calls `splitRewards` and `ProtocolRewards#depositRewards`
/// @param _amount total amount to split and deposit
/// @param _platform platform address
/// @param _creator creator address
/// @param _referrer referrer address
function splitAndDepositRewards(
uint256 _amount,
address _platform,
address _creator,
address _referrer
) public payable returns (uint256, uint256, uint256) {
(uint256 _platformReward, uint256 _creatorReward, uint256 _referrerReward) = splitRewards(
_amount,
_platform,
_creator,
_referrer
);
protocolRewards.depositRewards{ value: _amount }(
_platform,
_platformReward,
_creator,
_creatorReward,
_referrer,
_referrerReward
);
return (_platformReward, _creatorReward, _referrerReward);
}
/// @notice Split rewards
/// @dev Splits `_amount` into platform, creator, and referrer rewards,
/// based on `PLATFORM_SHARE`, `CREATOR_SHARE`, and `REFERRER_SHARE`
/// @param _amount total amount to split
/// @param _platform platform address
/// @param _creator creator address
/// @param _referrer referrer address
function splitRewards(
uint256 _amount,
address _platform,
address _creator,
address _referrer
) public pure returns (uint256, uint256, uint256) {
assembly {
_platform := shr(96, shl(96, _platform))
_creator := shr(96, shl(96, _creator))
_referrer := shr(96, shl(96, _referrer))
if or(
or(
iszero(_platform), // _platform == address(0)
iszero(_creator) // _creator == address(0)
),
eq(_referrer, _creator) // _referrer == _creator
) {
mstore(0x00, 0xe6c4247b) // revert InvalidAddress();
revert(0x1c, 0x04)
}
}
uint256 _platformReward = (_amount * PLATFORM_SHARE) / BASIS_POINTS;
uint256 _creatorReward = (_amount * CREATOR_SHARE) / BASIS_POINTS;
uint256 _referrerReward = (_amount * REFERRER_SHARE) / BASIS_POINTS;
if (_referrer == address(0)) {
_platformReward += _referrerReward;
_referrerReward = 0;
}
return (_platformReward, _creatorReward, _referrerReward);
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "none"
},
"libraries": {},
"optimizer": {
"runs": 800,
"enabled": true
},
"evmVersion": "paris",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.