Skip to main content

Staking ERC721 NFTs

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

Setup staking feature for your NFT Collection.

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

Note: This is a Beta release.

Availability in base contracts

The Staking721 is already available in the following base contracts.

Implementing the Contract

Import the contract and make your contract inherit it.

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

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

contract MyContract is Staking721 {
// 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 `Staking721` extension.
*/
address public deployer;

constructor(
uint256 _timeUnit,
uint256 _rewardsPerUnitTime,
address _nftCollection,
address _rewardToken
) Staking721(_nftCollection) {
_setTimeUnit(_timeUnit);
_setRewardsPerUnitTime(_rewardsPerUnitTime);

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;
}
}

Full API Reference

stake

function stake(uint256[] calldata tokenIds) external;
  • Lets a user stake a number of ERC721 tokens by passing in token-Ids.
  • Parameter tokenIds: List of token-Ids to stake.

withdraw

function withdraw(uint256[] calldata tokenIds) external;
  • Un-stake and withdraw NFTs from the contract.
  • Parameter tokenIds: List of token-Ids to withdraw.

claimRewards

function claimRewards() external;
  • Claim accumulated rewards. This claim method allows for a pull mechanism where users must initiate claiming of rewards.

getStakeInfo

function getStakeInfo(address staker) external view returns (uint256 tokensStaked, uint256 rewards);
  • View number of NFTs staked and total rewards available for a user.
  • Parameter staker: Account address of staker.

setRewardsPerUnitTime

function setRewardsPerUnitTime(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 rewardsPerUnitTime: New rewards per unit time.

setTimeUnit

function setTimeUnit(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 timeUnit: New time unit.