Skip to main content

ERC1155Staking

import "@thirdweb-dev/contracts/extension/Staking1155.sol";

The Staking1155 smart contract extension implements NFT staking mechanism for ERC1155. With this extension you can setup a staking contract for NFT holders of your ERC1155 Collection. Users can stake their NFTs and earn ERC20 tokens as rewards.

Usage​

This is an example smart contract demonstrating how to inherit from this extension and override the functions to add (optional) custom functionality.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@thirdweb-dev/contracts/extension/Staking1155.sol";
import "@thirdweb-dev/contracts/eip/interface/IERC20.sol";

contract MyContract is Staking1155 {
// ERC20 Reward Token address. See {_mintRewards}.
address public rewardToken;

/**
* We store the contract deployer's address only for the purposes of the example
* in the code comment below.
*
* Doing this is not necessary to use the `Staking1155` extension.
*/
address public deployer;

constructor(
uint256 _defaultTimeUnit,
uint256 _defaultRewardsPerUnitTime,
address _stakingToken,
address _rewardToken
) Staking1155(_stakingToken) {
_setDefaultStakingCondition(_defaultTimeUnit, _defaultRewardsPerUnitTime);

rewardToken = _rewardToken;
deployer = msg.sender;
}

/**
* @dev Mint/Transfer ERC20 rewards to the staker. Must override.
*
* @param _staker Address for sending rewards to.
* @param _rewards Amount of tokens to be given out as reward.
*
*/
function _mintRewards(address _staker, uint256 _rewards) internal override {
IERC20(rewardToken).transfer(_staker, _rewards);
}

// Returns whether staking restrictions can be set in given execution context.
function _canSetStakeConditions() internal view override returns (bool) {
return msg.sender == deployer;
}
}

Base Contracts Implementing This Extension​

Full API Reference​

stake
function stake(uint256 tokenId, uint256 amount) external;
  • Lets a user stake a number of ERC1155 tokens of a specific token-Id.
  • Parameter tokenId: ERC1155 token-id to stake.
  • Parameter amount: Amount to stake.
withdraw
function stake(uint256 tokenId, uint256 amount) external;
  • Un-stake and withdraw NFTs from the contract.
  • Parameter tokenId: ERC1155 token-id to withdraw.
  • Parameter amount: Amount to withdraw.
claimRewards
function claimRewards(uint256 tokenId) external;
  • Claim accumulated rewards. This claim method allows for a pull mechanism where users must initiate claiming of rewards.
  • Parameter tokenId: ERC1155 token-id to claim rewards for.
getStakeInfo
function getStakeInfo(address staker) external view returns (uint256[] memory tokensStaked, uint256[] memory tokenAmounts, uint256 totalRewards)
  • View number of NFTs staked and total rewards available for a user.
  • Parameter staker: Account address of staker.
getStakeInfoForToken
function getStakeInfoForToken(uint256 tokenId, address staker) external view returns (uint256 tokensStaked, uint256 rewards)
  • View number of NFTs staked and total rewards available for a user.
  • Parameter tokenId: ERC1155 token-id to view rewards for.
  • Parameter staker: Account address of staker.
setRewardsPerUnitTime
function setRewardsPerUnitTime(uint256 tokenId, uint256 rewardsPerUnitTime) external;
  • Allows an authorized account to set rewards per unit of time. Interpreted as x rewards per second/per day/etc based on time-unit.
  • Parameter tokenId: ERC1155 token-id to set this for.
  • Parameter rewardsPerUnitTime: New rewards per unit time.
setTimeUnit
function setTimeUnit(uint256 tokenId, uint256 timeUnit) external;
  • Allows an authorized account to set time unit as a number of seconds. For e.g. 1 hour can be set as 3600 seconds - setting the reward frequency as per hour.
  • Parameter tokenId: ERC1155 token-id to set this for.
  • Parameter timeUnit: New time unit.
setDefaultRewardsPerUnitTime
function setDefaultRewardsPerUnitTime(uint256 defaultRewardsPerUnitTime) external;
  • Allows an authorized account to set rewards per unit of time applicable for all token-Ids by default.
  • Parameter defaultRewardsPerUnitTime: New rewards per unit time.
setDefaultTimeUnit
function setDefaultTimeUnit(uint256 defaultTimeUnit) external;
  • Allows an authorized account to set time unit applicable for all token-Ids by default.
  • Parameter defaultTimeUnit: New time unit.