Verified contractExactMIT
ZealStakingContract
Address0x40238d07cfa59efb674c0b379cecd4da8fbd3143
- Balance
0 ETH
- Transactions
143
- Compiler
solc 0.8.19
- Same code
Only this contract
Details
- Compiler
- solc 0.8.19+commit.7dd6d404
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- paris
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 19,313,163
- Creation transaction
- 0x2b31c29071…a330c8b44c
18 files
- contracts/StakingContract.solmain625 B
- @thirdweb-dev/contracts/base/Staking20Base.sol7.76 KB
- @thirdweb-dev/contracts/eip/interface/IERC20.sol832 B
- @thirdweb-dev/contracts/eip/interface/IERC20Metadata.sol367 B
- @thirdweb-dev/contracts/extension/ContractMetadata.sol1.62 KB
- @thirdweb-dev/contracts/extension/Multicall.sol1.02 KB
- @thirdweb-dev/contracts/extension/Ownable.sol1.74 KB
- @thirdweb-dev/contracts/extension/Staking20.sol15.73 KB
- @thirdweb-dev/contracts/extension/interface/IContractMetadata.sol836 B
- @thirdweb-dev/contracts/extension/interface/IMulticall.sol413 B
- @thirdweb-dev/contracts/extension/interface/IOwnable.sol807 B
- @thirdweb-dev/contracts/extension/interface/IStaking20.sol3.3 KB
- @thirdweb-dev/contracts/external-deps/openzeppelin/security/ReentrancyGuard.sol883 B
- @thirdweb-dev/contracts/external-deps/openzeppelin/token/ERC20/utils/SafeERC20.sol3.95 KB
- @thirdweb-dev/contracts/external-deps/openzeppelin/utils/math/SafeMath.sol6.78 KB
- @thirdweb-dev/contracts/infra/interface/IWETH.sol249 B
- @thirdweb-dev/contracts/lib/CurrencyTransferLib.sol3.39 KB
- @thirdweb-dev/contracts/lib/TWAddress.sol8.22 KB
contracts/StakingContract.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@thirdweb-dev/contracts/base/Staking20Base.sol";
contract ZealStakingContract is Staking20Base {
constructor(
uint80 _timeUnit,
address _defaultAdmin,
uint256 _rewardRatioNumerator,
uint256 _rewardRatioDenominator,
address _stakingToken,
address _rewardToken,
address _nativeTokenWrapper
) Staking20Base(
_timeUnit,
_defaultAdmin,
_rewardRatioNumerator,
_rewardRatioDenominator,
_stakingToken,
_rewardToken,
_nativeTokenWrapper
) {}
}@thirdweb-dev/contracts/base/Staking20Base.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/// @author thirdweb
import "../extension/ContractMetadata.sol";
import "../extension/Multicall.sol";
import "../extension/Ownable.sol";
import "../extension/Staking20.sol";
import "../eip/interface/IERC20.sol";
import "../eip/interface/IERC20Metadata.sol";
import { CurrencyTransferLib } from "../lib/CurrencyTransferLib.sol";
/**
*
* EXTENSION: Staking20
*
* The `Staking20Base` smart contract implements Token staking mechanism.
* Allows users to stake their ERC-20 Tokens and earn rewards in form of another ERC-20 tokens.
*
* Following features and implementation setup must be noted:
*
* - ERC-20 Tokens from only one contract can be staked.
*
* - Contract admin can choose to give out rewards by either transferring or minting the rewardToken,
* which is ideally a different ERC20 token. See {_mintRewards}.
*
* - To implement custom logic for staking, reward calculation, etc. corresponding functions can be
* overridden from the extension `Staking20`.
*
* - Ownership of the contract, with the ability to restrict certain functions to
* only be called by the contract's owner.
*
* - Multicall capability to perform multiple actions atomically.
*
*/
/// note: This contract is provided as a base contract.
// This is to support a variety of use-cases that can be build on top of this base.
//
// Additional functionality such as deposit functions, reward-minting, etc.
// must be implemented by the deployer of this contract, as needed for their use-case.
contract Staking20Base is ContractMetadata, Multicall, Ownable, Staking20 {
/// @dev ERC20 Reward Token address. See {_mintRewards} below.
address public rewardToken;
/// @dev Total amount of reward tokens in the contract.
uint256 private rewardTokenBalance;
constructor(
uint80 _timeUnit,
address _defaultAdmin,
uint256 _rewardRatioNumerator,
uint256 _rewardRatioDenominator,
address _stakingToken,
address _rewardToken,
address _nativeTokenWrapper
)
Staking20(
_nativeTokenWrapper,
_stakingToken,
IERC20Metadata(_stakingToken).decimals(),
IERC20Metadata(_rewardToken).decimals()
)
{
_setupOwner(_defaultAdmin);
_setStakingCondition(_timeUnit, _rewardRatioNumerator, _rewardRatioDenominator);
// require(_rewardToken != _stakingToken, "Reward Token and Staking Token can't be same.");
rewardToken = _rewardToken;
}
/// @dev Lets the contract receive ether to unwrap native tokens.
receive() external payable virtual {
require(msg.sender == nativeTokenWrapper, "caller not native token wrapper.");
}
/// @dev Admin deposits reward tokens.
function depositRewardTokens(uint256 _amount) external payable virtual nonReentrant {
_depositRewardTokens(_amount); // override this for custom logic.
}
/// @dev Admin can withdraw excess reward tokens.
function withdrawRewardTokens(uint256 _amount) external virtual nonReentrant {
_withdrawRewardTokens(_amount); // override this for custom logic.
}
// /// @dev Admin can withdraw their own reward tokens.
function withdrawRewardOwnerTokens(uint256 _amount) external virtual onlyOwner() {
_withdrawRewardOwnerTokens(_amount); // override this for custom logic.
}
/// @notice View total rewards available in the staking contract.
function getRewardTokenBalance() external view virtual override onlyOwner() returns (uint256) {
return rewardTokenBalance;
}
function updateRewardLockedTime(uint256 _time) external virtual onlyOwner() {
_setLockedTime(_time);
}
/*//////////////////////////////////////////////////////////////
Minting logic
//////////////////////////////////////////////////////////////*/
/**
* @dev Mint ERC20 rewards to the staker. Override for custom logic.
*
* @param _staker Address for which to calculated rewards.
* @param _rewards Amount of tokens to be given out as reward.
*
*/
function _mintRewards(address _staker, uint256 _rewards) internal virtual override {
require(_rewards <= rewardTokenBalance, "Not enough reward tokens");
rewardTokenBalance -= _rewards;
CurrencyTransferLib.transferCurrencyWithWrapper(
rewardToken,
address(this),
_staker,
_rewards,
nativeTokenWrapper
);
}
/*//////////////////////////////////////////////////////////////
Other Internal functions
//////////////////////////////////////////////////////////////*/
/// @dev Admin deposits reward tokens -- override for custom logic.
function _depositRewardTokens(uint256 _amount) internal virtual {
require(msg.sender == owner(), "Not authorized");
address _rewardToken = rewardToken == CurrencyTransferLib.NATIVE_TOKEN ? nativeTokenWrapper : rewardToken;
uint256 balanceBefore = IERC20(_rewardToken).balanceOf(address(this));
CurrencyTransferLib.transferCurrencyWithWrapper(
rewardToken,
msg.sender,
address(this),
_amount,
nativeTokenWrapper
);
uint256 actualAmount = IERC20(_rewardToken).balanceOf(address(this)) - balanceBefore;
rewardTokenBalance += actualAmount;
}
/// @dev Admin can withdraw excess reward tokens -- override for custom logic.
function _withdrawRewardTokens(uint256 _amount) internal virtual {
require(msg.sender == owner(), "Not authorized");
// to prevent locking of direct-transferred tokens
// rewardTokenBalance = _amount > rewardTokenBalance ? 0 : rewardTokenBalance - _amount;
CurrencyTransferLib.transferCurrencyWithWrapper(
rewardToken,
address(this),
owner(),
_amount,
nativeTokenWrapper
);
// The withdrawal shouldn't reduce staking token balance. `>=` accounts for any accidental transfers.
// address _stakingToken = stakingToken == CurrencyTransferLib.NATIVE_TOKEN ? nativeTokenWrapper : stakingToken;
// require(
// IERC20(_stakingToken).balanceOf(address(this)) >= stakingTokenBalance,
// "Staking token balance reduced."
// );
}
function _withdrawRewardOwnerTokens(uint256 _amount) internal virtual {
CurrencyTransferLib.transferCurrencyWithWrapper(
rewardToken,
address(this),
owner(),
_amount,
nativeTokenWrapper
);
IERC20 token = IERC20(rewardToken);
uint256 contractBalance = token.balanceOf(address(this));
require(contractBalance > 0, "No tokens to transfer");
bool success = token.transfer(owner(), contractBalance);
require(success, "Transfer failed");
}
/// @dev Returns whether staking restrictions can be set in given execution context.
function _canSetStakeConditions() internal view virtual override returns (bool) {
return msg.sender == owner();
}
/// @dev Returns whether contract metadata can be set in the given execution context.
function _canSetContractURI() internal view virtual override returns (bool) {
return msg.sender == owner();
}
/// @dev Returns whether owner can be set in the given execution context.
function _canSetOwner() internal view virtual override returns (bool) {
return msg.sender == owner();
}
}@thirdweb-dev/contracts/eip/interface/IERC20.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/**
* @title ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(
address from,
address to,
uint256 value
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
@thirdweb-dev/contracts/eip/interface/IERC20Metadata.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/**
* @title ERC20Metadata interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
interface IERC20Metadata {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
@thirdweb-dev/contracts/extension/ContractMetadata.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/// @author thirdweb
import "./interface/IContractMetadata.sol";
/**
* @title Contract Metadata
* @notice Thirdweb's `ContractMetadata` is a contract extension for any base contracts. It lets you set a metadata URI
* for you contract.
* Additionally, `ContractMetadata` is necessary for NFT contracts that want royalties to get distributed on OpenSea.
*/
abstract contract ContractMetadata is IContractMetadata {
/// @notice Returns the contract metadata URI.
string public override contractURI;
/**
* @notice Lets a contract admin set the URI for contract-level metadata.
* @dev Caller should be authorized to setup contractURI, e.g. contract admin.
* See {_canSetContractURI}.
* Emits {ContractURIUpdated Event}.
*
* @param _uri keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
*/
function setContractURI(string memory _uri) external override {
if (!_canSetContractURI()) {
revert("Not authorized");
}
_setupContractURI(_uri);
}
/// @dev Lets a contract admin set the URI for contract-level metadata.
function _setupContractURI(string memory _uri) internal {
string memory prevURI = contractURI;
contractURI = _uri;
emit ContractURIUpdated(prevURI, _uri);
}
/// @dev Returns whether contract metadata can be set in the given execution context.
function _canSetContractURI() internal view virtual returns (bool);
}
@thirdweb-dev/contracts/extension/Multicall.sol
// SPDX-License-Identifier: Apache 2.0
pragma solidity ^0.8.0;
/// @author thirdweb
import "../lib/TWAddress.sol";
import "./interface/IMulticall.sol";
/**
* @dev Provides a function to batch together multiple calls in a single external call.
*
* _Available since v4.1._
*/
contract Multicall is IMulticall {
/**
* @notice Receives and executes a batch of function calls on this contract.
* @dev Receives and executes a batch of function calls on this contract.
*
* @param data The bytes data that makes up the batch of function calls to execute.
* @return results The bytes data that makes up the result of the batch of function calls executed.
*/
function multicall(bytes[] calldata data) external virtual override returns (bytes[] memory results) {
results = new bytes[](data.length);
for (uint256 i = 0; i < data.length; i++) {
results[i] = TWAddress.functionDelegateCall(address(this), data[i]);
}
return results;
}
}
@thirdweb-dev/contracts/extension/Ownable.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/// @author thirdweb
import "./interface/IOwnable.sol";
/**
* @title Ownable
* @notice Thirdweb's `Ownable` is a contract extension to be used with any base contract. It exposes functions for setting and reading
* who the 'owner' of the inheriting smart contract is, and lets the inheriting contract perform conditional logic that uses
* information about who the contract's owner is.
*/
abstract contract Ownable is IOwnable {
/// @dev Owner of the contract (purpose: OpenSea compatibility)
address private _owner;
/// @dev Reverts if caller is not the owner.
modifier onlyOwner() {
if (msg.sender != _owner) {
revert("Not authorized");
}
_;
}
/**
* @notice Returns the owner of the contract.
*/
function owner() public view override returns (address) {
return _owner;
}
/**
* @notice Lets an authorized wallet set a new owner for the contract.
* @param _newOwner The address to set as the new owner of the contract.
*/
function setOwner(address _newOwner) external override {
if (!_canSetOwner()) {
revert("Not authorized");
}
_setupOwner(_newOwner);
}
/// @dev Lets a contract admin set a new owner for the contract. The new owner must be a contract admin.
function _setupOwner(address _newOwner) internal {
address _prevOwner = _owner;
_owner = _newOwner;
emit OwnerUpdated(_prevOwner, _newOwner);
}
/// @dev Returns whether owner can be set in the given execution context.
function _canSetOwner() internal view virtual returns (bool);
}
@thirdweb-dev/contracts/extension/Staking20.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.19;
/// @author thirdweb
import "../external-deps/openzeppelin/security/ReentrancyGuard.sol";
import "../external-deps/openzeppelin/utils/math/SafeMath.sol";
import "../eip/interface/IERC20.sol";
import "../lib/CurrencyTransferLib.sol";
import "./interface/IStaking20.sol";
abstract contract Staking20 is ReentrancyGuard, IStaking20 {
/*///////////////////////////////////////////////////////////////
State variables / Mappings
//////////////////////////////////////////////////////////////*/
/// @dev The address of the native token wrapper contract.
address internal immutable nativeTokenWrapper;
///@dev Address of ERC20 contract -- staked tokens belong to this contract.
address public immutable stakingToken;
/// @dev Decimals of staking token.
uint16 public immutable stakingTokenDecimals;
/// @dev Decimals of reward token.
uint16 public immutable rewardTokenDecimals;
///@dev Next staking condition Id. Tracks number of conditon updates so far.
uint64 private nextConditionId;
/// @dev Total amount of tokens staked in the contract.
uint256 public stakingTokenBalance;
uint256 private lockedTime;
/// @dev List of accounts that have staked that token-id.
address[] public stakersArray;
///@dev Mapping staker address to Staker struct. See {struct IStaking20.Staker}.
mapping(address => Staker) public stakers;
///@dev Mapping from condition Id to staking condition. See {struct IStaking721.StakingCondition}
mapping(uint256 => StakingCondition) private stakingConditions;
constructor(
address _nativeTokenWrapper,
address _stakingToken,
uint16 _stakingTokenDecimals,
uint16 _rewardTokenDecimals
) ReentrancyGuard() {
require(_stakingToken != address(0) && _nativeTokenWrapper != address(0), "address 0");
require(_stakingTokenDecimals != 0 && _rewardTokenDecimals != 0, "decimals 0");
nativeTokenWrapper = _nativeTokenWrapper;
stakingToken = _stakingToken;
stakingTokenDecimals = _stakingTokenDecimals;
rewardTokenDecimals = _rewardTokenDecimals;
lockedTime = 14 days;
}
/*///////////////////////////////////////////////////////////////
External/Public Functions
//////////////////////////////////////////////////////////////*/
/**
* @notice Stake ERC20 Tokens.
*
* @dev See {_stake}. Override that to implement custom logic.
*
* @param _amount Amount to stake.
*/
function stake(uint256 _amount) external payable nonReentrant {
_stake(_amount);
}
/**
* @notice Withdraw staked ERC20 tokens.
*
* @dev See {_withdraw}. Override that to implement custom logic.
*
* @param _amount Amount to withdraw.
*/
function withdraw(uint256 _amount) external nonReentrant {
_withdraw(_amount);
}
/**
* @notice Claim accumulated rewards.
*
* @dev See {_claimRewards}. Override that to implement custom logic.
* See {_calculateRewards} for reward-calculation logic.
*/
function claimRewards(ClaimOption _option) external nonReentrant {
_claimRewards(_option);
}
/**
* @notice Set time unit. Set as a number of seconds.
* Could be specified as -- x * 1 hours, x * 1 days, etc.
*
* @dev Only admin/authorized-account can call it.
*
* @param _timeUnit New time unit.
*/
function setTimeUnit(uint80 _timeUnit) external virtual {
if (!_canSetStakeConditions()) {
revert("Not authorized");
}
StakingCondition memory condition = stakingConditions[nextConditionId - 1];
require(_timeUnit != condition.timeUnit, "Time-unit unchanged.");
_setStakingCondition(_timeUnit, condition.rewardRatioNumerator, condition.rewardRatioDenominator);
emit UpdatedTimeUnit(condition.timeUnit, _timeUnit);
}
/**
* @notice Set rewards per unit of time.
* Interpreted as (numerator/denominator) rewards per second/per day/etc based on time-unit.
*
* For e.g., ratio of 1/20 would mean 1 reward token for every 20 tokens staked.
*
* @dev Only admin/authorized-account can call it.
*
* @param _numerator Reward ratio numerator.
* @param _denominator Reward ratio denominator.
*/
function setRewardRatio(uint256 _numerator, uint256 _denominator) external virtual {
if (!_canSetStakeConditions()) {
revert("Not authorized");
}
StakingCondition memory condition = stakingConditions[nextConditionId - 1];
require(
_numerator != condition.rewardRatioNumerator || _denominator != condition.rewardRatioDenominator,
"Reward ratio unchanged."
);
_setStakingCondition(condition.timeUnit, _numerator, _denominator);
emit UpdatedRewardRatio(
condition.rewardRatioNumerator,
_numerator,
condition.rewardRatioDenominator,
_denominator
);
}
/**
* @notice View amount staked and rewards for a user.
*
* @param _staker Address for which to calculated rewards.
* @return _tokensStaked Amount of tokens staked.
* @return _rewards Available reward amount.
*/
function getStakeInfo(address _staker) external view virtual returns (uint256 _tokensStaked, uint256 _rewards) {
_tokensStaked = stakers[_staker].amountStaked;
_rewards = _availableRewards(_staker);
}
function getTimeUnit() public view returns (uint256 _timeUnit) {
_timeUnit = stakingConditions[nextConditionId - 1].timeUnit;
}
function getRewardRatio() public view returns (uint256 _numerator, uint256 _denominator) {
_numerator = stakingConditions[nextConditionId - 1].rewardRatioNumerator;
_denominator = stakingConditions[nextConditionId - 1].rewardRatioDenominator;
}
/// @dev get locked time for claiming rewards
function getRewardLockedTime() public view returns(uint256 _lockedTime) {
return lockedTime;
}
/// @dev get unlock time for claiming normal rewards
function getRewardUnlockTime(address _staker) external view virtual returns (uint256 _unlockTime) {
_unlockTime = stakers[_staker].claimTime;
}
function isAbleToClaimNormal(address _staker) public view returns(bool) {
return block.timestamp >= stakers[_staker].claimTime;
}
/*///////////////////////////////////////////////////////////////
Internal Functions
//////////////////////////////////////////////////////////////*/
/// @dev Staking logic. Override to add custom logic.
function _stake(uint256 _amount) internal virtual {
require(_amount != 0, "Staking 0 tokens");
require(lockedTime != 0, "Unknown Locked Time");
address _stakingToken;
if (stakingToken == CurrencyTransferLib.NATIVE_TOKEN) {
_stakingToken = nativeTokenWrapper;
} else {
require(msg.value == 0, "Value not 0");
_stakingToken = stakingToken;
}
if (stakers[_stakeMsgSender()].amountStaked > 0) {
_updateUnclaimedRewardsForStaker(_stakeMsgSender());
} else {
stakersArray.push(_stakeMsgSender());
stakers[_stakeMsgSender()].timeOfLastUpdate = uint80(block.timestamp);
stakers[_stakeMsgSender()].conditionIdOflastUpdate = nextConditionId - 1;
}
uint256 balanceBefore = IERC20(_stakingToken).balanceOf(address(this));
CurrencyTransferLib.transferCurrencyWithWrapper(
stakingToken,
_stakeMsgSender(),
address(this),
_amount,
nativeTokenWrapper
);
uint256 actualAmount = IERC20(_stakingToken).balanceOf(address(this)) - balanceBefore;
stakers[_stakeMsgSender()].amountStaked += actualAmount;
stakers[_stakeMsgSender()].claimTime = block.timestamp + lockedTime;
stakingTokenBalance += actualAmount;
emit TokensStaked(_stakeMsgSender(), actualAmount);
}
/// @dev Withdraw logic. Override to add custom logic.
function _withdraw(uint256 _amount) internal virtual {
uint256 _amountStaked = stakers[_stakeMsgSender()].amountStaked;
require(_amount != 0, "Withdrawing 0 tokens");
require(_amountStaked >= _amount, "Withdrawing more than staked");
_updateUnclaimedRewardsForStaker(_stakeMsgSender());
if (_amountStaked == _amount) {
address[] memory _stakersArray = stakersArray;
for (uint256 i = 0; i < _stakersArray.length; ++i) {
if (_stakersArray[i] == _stakeMsgSender()) {
stakersArray[i] = _stakersArray[_stakersArray.length - 1];
stakersArray.pop();
break;
}
}
}
stakers[_stakeMsgSender()].amountStaked -= _amount;
stakingTokenBalance -= _amount;
CurrencyTransferLib.transferCurrencyWithWrapper(
stakingToken,
address(this),
_stakeMsgSender(),
_amount,
nativeTokenWrapper
);
emit TokensWithdrawn(_stakeMsgSender(), _amount);
}
/// @dev Logic for claiming rewards. Override to add custom logic.
function _claimRewards(ClaimOption _option) internal virtual {
require(_option == ClaimOption.Normal || _option == ClaimOption.Penalty, "Claim Option should be either Normal or Penalty");
uint256 rewards = stakers[_stakeMsgSender()].unclaimedRewards + _calculateRewards(_stakeMsgSender());
require(rewards != 0, "No rewards");
stakers[_stakeMsgSender()].timeOfLastUpdate = uint80(block.timestamp);
stakers[_stakeMsgSender()].unclaimedRewards = 0;
stakers[_stakeMsgSender()].conditionIdOflastUpdate = nextConditionId - 1;
if(_option == ClaimOption.Normal){
require(isAbleToClaimNormal(_stakeMsgSender()), "Rewards are still locked");
_mintRewards(_stakeMsgSender(), rewards);
stakers[_stakeMsgSender()].claimTime = block.timestamp;
emit RewardsClaimed(_stakeMsgSender(), rewards);
} else {
uint256 penaltyRewards = rewards / 2;
_mintRewards(_stakeMsgSender(), penaltyRewards);
stakers[_stakeMsgSender()].claimTime = block.timestamp;
emit RewardsClaimed(_stakeMsgSender(), penaltyRewards);
}
}
/// @dev View available rewards for a user.
function _availableRewards(address _staker) internal view virtual returns (uint256 _rewards) {
if (stakers[_staker].amountStaked == 0) {
_rewards = stakers[_staker].unclaimedRewards;
} else {
_rewards = stakers[_staker].unclaimedRewards + _calculateRewards(_staker);
}
}
/// @dev Update unclaimed rewards for a users. Called for every state change for a user.
function _updateUnclaimedRewardsForStaker(address _staker) internal virtual {
uint256 rewards = _calculateRewards(_staker);
stakers[_staker].unclaimedRewards += rewards;
stakers[_staker].timeOfLastUpdate = uint80(block.timestamp);
stakers[_staker].conditionIdOflastUpdate = nextConditionId - 1;
}
/// @dev set locked time for claiming rewards
function _setLockedTime(uint256 _time) internal virtual {
lockedTime = _time;
}
/// @dev Set staking conditions.
function _setStakingCondition(
uint80 _timeUnit,
uint256 _numerator,
uint256 _denominator
) internal virtual {
require(_denominator != 0, "divide by 0");
require(_timeUnit != 0, "time-unit can't be 0");
uint256 conditionId = nextConditionId;
nextConditionId += 1;
stakingConditions[conditionId] = StakingCondition({
timeUnit: _timeUnit,
rewardRatioNumerator: _numerator,
rewardRatioDenominator: _denominator,
startTimestamp: uint80(block.timestamp),
endTimestamp: 0
});
if (conditionId > 0) {
stakingConditions[conditionId - 1].endTimestamp = uint80(block.timestamp);
}
}
/// @dev Calculate rewards for a staker.
function _calculateRewards(address _staker) internal view virtual returns (uint256 _rewards) {
Staker memory staker = stakers[_staker];
uint256 _stakerConditionId = staker.conditionIdOflastUpdate;
uint256 _nextConditionId = nextConditionId;
for (uint256 i = _stakerConditionId; i < _nextConditionId; i += 1) {
StakingCondition memory condition = stakingConditions[i];
uint256 startTime = i != _stakerConditionId ? condition.startTimestamp : staker.timeOfLastUpdate;
uint256 endTime = condition.endTimestamp != 0 ? condition.endTimestamp : block.timestamp;
(bool noOverflowProduct, uint256 rewardsProduct) = SafeMath.tryMul(
(endTime - startTime) * staker.amountStaked,
condition.rewardRatioNumerator
);
(bool noOverflowSum, uint256 rewardsSum) = SafeMath.tryAdd(
_rewards,
(rewardsProduct / condition.timeUnit) / condition.rewardRatioDenominator
);
_rewards = noOverflowProduct && noOverflowSum ? rewardsSum : _rewards;
}
(, _rewards) = SafeMath.tryMul(_rewards, 10**rewardTokenDecimals);
_rewards /= (10**stakingTokenDecimals);
}
/*////////////////////////////////////////////////////////////////////
Optional hooks that can be implemented in the derived contract
///////////////////////////////////////////////////////////////////*/
/// @dev Exposes the ability to override the msg sender -- support ERC2771.
function _stakeMsgSender() internal virtual returns (address) {
return msg.sender;
}
/*///////////////////////////////////////////////////////////////
Virtual functions to be implemented in derived contract
//////////////////////////////////////////////////////////////*/
/**
* @notice View total rewards available in the staking contract.
*
*/
function getRewardTokenBalance() external view virtual returns (uint256 _rewardsAvailableInContract);
/**
* @dev Mint/Transfer ERC20 rewards to the staker. Must override.
*
* @param _staker Address for which to calculated rewards.
* @param _rewards Amount of tokens to be given out as reward.
*
* For example, override as below to mint ERC20 rewards:
*
* ```
* function _mintRewards(address _staker, uint256 _rewards) internal override {
*
* TokenERC20(rewardTokenAddress).mintTo(_staker, _rewards);
*
* }
* ```
*/
function _mintRewards(address _staker, uint256 _rewards) internal virtual;
/**
* @dev Returns whether staking restrictions can be set in given execution context.
* Must override.
*
*
* For example, override as below to restrict access to admin:
*
* ```
* function _canSetStakeConditions() internal override {
*
* return msg.sender == adminAddress;
*
* }
* ```
*/
function _canSetStakeConditions() internal view virtual returns (bool);
}@thirdweb-dev/contracts/extension/interface/IContractMetadata.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/// @author thirdweb
/**
* Thirdweb's `ContractMetadata` is a contract extension for any base contracts. It lets you set a metadata URI
* for you contract.
*
* Additionally, `ContractMetadata` is necessary for NFT contracts that want royalties to get distributed on OpenSea.
*/
interface IContractMetadata {
/// @dev Returns the metadata URI of the contract.
function contractURI() external view returns (string memory);
/**
* @dev Sets contract URI for the storefront-level metadata of the contract.
* Only module admin can call this function.
*/
function setContractURI(string calldata _uri) external;
/// @dev Emitted when the contract URI is updated.
event ContractURIUpdated(string prevURI, string newURI);
}
@thirdweb-dev/contracts/extension/interface/IMulticall.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @author thirdweb
/**
* @dev Provides a function to batch together multiple calls in a single external call.
*
* _Available since v4.1._
*/
interface IMulticall {
/**
* @dev Receives and executes a batch of function calls on this contract.
*/
function multicall(bytes[] calldata data) external returns (bytes[] memory results);
}
@thirdweb-dev/contracts/extension/interface/IOwnable.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/// @author thirdweb
/**
* Thirdweb's `Ownable` is a contract extension to be used with any base contract. It exposes functions for setting and reading
* who the 'owner' of the inheriting smart contract is, and lets the inheriting contract perform conditional logic that uses
* information about who the contract's owner is.
*/
interface IOwnable {
/// @dev Returns the owner of the contract.
function owner() external view returns (address);
/// @dev Lets a module admin set a new owner for the contract. The new owner must be a module admin.
function setOwner(address _newOwner) external;
/// @dev Emitted when a new Owner is set.
event OwnerUpdated(address indexed prevOwner, address indexed newOwner);
}
@thirdweb-dev/contracts/extension/interface/IStaking20.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.11;
/// @author thirdweb
interface IStaking20 {
enum ClaimOption { Normal, Penalty }
/// @dev Emitted when tokens are staked.
event TokensStaked(address indexed staker, uint256 amount);
/// @dev Emitted when a tokens are withdrawn.
event TokensWithdrawn(address indexed staker, uint256 amount);
/// @dev Emitted when a staker claims staking rewards.
event RewardsClaimed(address indexed staker, uint256 rewardAmount);
/// @dev Emitted when contract admin updates timeUnit.
event UpdatedTimeUnit(uint256 oldTimeUnit, uint256 newTimeUnit);
/// @dev Emitted when contract admin updates rewardsPerUnitTime.
event UpdatedRewardRatio(
uint256 oldNumerator,
uint256 newNumerator,
uint256 oldDenominator,
uint256 newDenominator
);
/// @dev Emitted when contract admin updates minimum staking amount.
event UpdatedMinStakeAmount(uint256 oldAmount, uint256 newAmount);
/**
* @notice Staker Info.
*
* @param amountStaked Total number of tokens staked by the staker.
*
* @param timeOfLastUpdate Last reward-update timestamp.
*
* @param unclaimedRewards Rewards accumulated but not claimed by user yet.
*
* @param conditionIdOflastUpdate Condition-Id when rewards were last updated for user.
*
* @param claimTime Timer for user to claim their staked tokens.
*/
struct Staker {
uint128 timeOfLastUpdate;
uint64 conditionIdOflastUpdate;
uint256 amountStaked;
uint256 unclaimedRewards;
uint256 claimTime;
}
/**
* @notice Staking Condition.
*
* @param timeUnit Unit of time specified in number of seconds. Can be set as 1 seconds, 1 days, 1 hours, etc.
*
* @param rewardRatioNumerator Rewards ratio is the number of reward tokens for a number of staked tokens,
* per unit of time.
*
* @param rewardRatioDenominator Rewards ratio is the number of reward tokens for a number of staked tokens,
* per unit of time.
*
* @param startTimestamp Condition start timestamp.
*
* @param endTimestamp Condition end timestamp.
*/
struct StakingCondition {
uint80 timeUnit;
uint80 startTimestamp;
uint80 endTimestamp;
uint256 rewardRatioNumerator;
uint256 rewardRatioDenominator;
}
/**
* @notice Stake ERC721 Tokens.
*
* @param amount Amount to stake.
*/
function stake(uint256 amount) external payable;
/**
* @notice Withdraw staked tokens.
*
* @param amount Amount to withdraw.
*/
function withdraw(uint256 amount) external;
/**
* @notice Claim accumulated rewards.
*
*/
function claimRewards(ClaimOption _option) external;
/**
* @notice View amount staked and total rewards for a user.
*
* @param staker Address for which to calculated rewards.
*/
function getStakeInfo(address staker) external returns (uint256 _tokensStaked, uint256 _rewards);
}
@thirdweb-dev/contracts/external-deps/openzeppelin/security/ReentrancyGuard.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
*/
modifier nonReentrant() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
@thirdweb-dev/contracts/external-deps/openzeppelin/token/ERC20/utils/SafeERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.0;
import "../../../../../eip/interface/IERC20.sol";
import "../../../../../lib/TWAddress.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 SafeERC20 {
using TWAddress for address;
function safeTransfer(
IERC20 token,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(
IERC20 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(
IERC20 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));
}
function safeIncreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
/**
* @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(IERC20 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");
if (returndata.length > 0) {
// Return data is optional
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
@thirdweb-dev/contracts/external-deps/openzeppelin/utils/math/SafeMath.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (utils/math/SafeMath.sol)
pragma solidity ^0.8.0;
// CAUTION
// This version of SafeMath should only be used with Solidity 0.8 or later,
// because it relies on the compiler's built in overflow checks.
/**
* @dev Wrappers over Solidity's arithmetic operations.
*
* NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler
* now has built in overflow checking.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the subtraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b > a) return (false, 0);
return (true, a - b);
}
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a / b);
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a % b);
}
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
return a + b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
return a * b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator.
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
unchecked {
require(b <= a, errorMessage);
return a - b;
}
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
unchecked {
require(b > 0, errorMessage);
return a / b;
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
unchecked {
require(b > 0, errorMessage);
return a % b;
}
}
}
@thirdweb-dev/contracts/infra/interface/IWETH.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
interface IWETH {
function deposit() external payable;
function withdraw(uint256 amount) external;
function transfer(address to, uint256 value) external returns (bool);
}
@thirdweb-dev/contracts/lib/CurrencyTransferLib.sol
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/// @author thirdweb
// Helper interfaces
import { IWETH } from "../infra/interface/IWETH.sol";
import "../external-deps/openzeppelin/token/ERC20/utils/SafeERC20.sol";
library CurrencyTransferLib {
using SafeERC20 for IERC20;
/// @dev The address interpreted as native token of the chain.
address public constant NATIVE_TOKEN = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/// @dev Transfers a given amount of currency.
function transferCurrency(
address _currency,
address _from,
address _to,
uint256 _amount
) internal {
if (_amount == 0) {
return;
}
if (_currency == NATIVE_TOKEN) {
safeTransferNativeToken(_to, _amount);
} else {
safeTransferERC20(_currency, _from, _to, _amount);
}
}
/// @dev Transfers a given amount of currency. (With native token wrapping)
function transferCurrencyWithWrapper(
address _currency,
address _from,
address _to,
uint256 _amount,
address _nativeTokenWrapper
) internal {
if (_amount == 0) {
return;
}
if (_currency == NATIVE_TOKEN) {
if (_from == address(this)) {
// withdraw from weth then transfer withdrawn native token to recipient
IWETH(_nativeTokenWrapper).withdraw(_amount);
safeTransferNativeTokenWithWrapper(_to, _amount, _nativeTokenWrapper);
} else if (_to == address(this)) {
// store native currency in weth
require(_amount == msg.value, "msg.value != amount");
IWETH(_nativeTokenWrapper).deposit{ value: _amount }();
} else {
safeTransferNativeTokenWithWrapper(_to, _amount, _nativeTokenWrapper);
}
} else {
safeTransferERC20(_currency, _from, _to, _amount);
}
}
/// @dev Transfer `amount` of ERC20 token from `from` to `to`.
function safeTransferERC20(
address _currency,
address _from,
address _to,
uint256 _amount
) internal {
if (_from == _to) {
return;
}
if (_from == address(this)) {
IERC20(_currency).safeTransfer(_to, _amount);
} else {
IERC20(_currency).safeTransferFrom(_from, _to, _amount);
}
}
/// @dev Transfers `amount` of native token to `to`.
function safeTransferNativeToken(address to, uint256 value) internal {
// solhint-disable avoid-low-level-calls
// slither-disable-next-line low-level-calls
(bool success, ) = to.call{ value: value }("");
require(success, "native token transfer failed");
}
/// @dev Transfers `amount` of native token to `to`. (With native token wrapping)
function safeTransferNativeTokenWithWrapper(
address to,
uint256 value,
address _nativeTokenWrapper
) internal {
// solhint-disable avoid-low-level-calls
// slither-disable-next-line low-level-calls
(bool success, ) = to.call{ value: value }("");
if (!success) {
IWETH(_nativeTokenWrapper).deposit{ value: value }();
IERC20(_nativeTokenWrapper).safeTransfer(to, value);
}
}
}
@thirdweb-dev/contracts/lib/TWAddress.sol
// SPDX-License-Identifier: Apache 2.0
pragma solidity ^0.8.0;
/// @author thirdweb
/**
* @dev Collection of functions related to the address type
*/
library TWAddress {
/**
* @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
* ====
*
* [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.
*
* [EIP1884](https://eips.ethereum.org/EIPS/eip-1884) 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://diligence.consensys.net/posts/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.5.11/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 functionCall(target, data, "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");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{ value: value }(data);
return verifyCallResult(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) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResult(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) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason 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 {
// 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
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "paris",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.