Staking Protocol

Token Staking

The Token Staking feature in our asset tokenization platform allows users to stake their tokens in return for ERC20-based rewards. This staking mechanism involves locking tokens in a smart contract, choosing a staking duration, and receiving interest in a specified ERC20 token. Below is a detailed outline of the staking process, contract functionalities, and associated rules.

Overview of Token Staking Process

  1. Staking Initiation:

    • Users choose to stake their asset tokens by locking them in a predefined staking contract.

    • They specify the staking duration (ranging from 1 month to 1 year) and select an ERC20 token for staking rewards.

  2. Asset Transfer and Locking:

    • The asset tokens are transferred to the staking contract, where they are locked for the specified duration.

    • The contract verifies the 'last approved price' of the assets from the token struct to calculate the equivalent value in the chosen ERC20 token.

Staking Contract Functionalities

  1. ERC20 Minting:

    • Upon successful locking of assets, the contract mints the equivalent value of the selected ERC20 token based on the last approved asset price.

    • The minted ERC20 tokens are credited to the user's account as staking rewards.

  2. Interest Payment:

    • The generated interest from staking is paid directly to the user’s wallet.

    • Interest rates are predefined and may vary based on the chosen ERC20 token and staking duration.

  3. Early Withdrawal Penalty:

    • If a user chooses to withdraw the staked assets before the end of the staking period, a penalty is applied.

    • The penalty involves a fee, deducted from the minted ERC20 tokens or interest earned.

  4. Asset Unlocking and ERC20 Burning:

    • The asset tokens are only unlocked after the staked ERC20 coins are burned.

    • If the staking duration completes, the system automatically burns the ERC20 tokens and returns the asset tokens to the user's wallet.

Staking Contract Structure

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract StakingContract {
    IERC721 public assetTokenContract;
    IERC20 public erc20TokenContract;
    
    struct StakingInfo {
        uint256 stakedAmount;
        uint256 startTime;
        uint256 duration;
        bool isStaked;
    }

    mapping(address => StakingInfo) public stakings;

    // Stake tokens
    function stakeTokens(uint256 tokenId, uint256 duration, address erc20Token) public {
        require(duration >= 1 month && duration <= 1 year, "Invalid duration");
        require(assetTokenContract.ownerOf(tokenId) == msg.sender, "Not the owner");

        // Transfer and lock the asset token
        assetTokenContract.transferFrom(msg.sender, address(this), tokenId);

        // Mint ERC20 tokens based on the last approved price
        uint256 lastApprovedPrice = // fetch last approved price for tokenId
        erc20TokenContract = IERC20(erc20Token);
        erc20TokenContract.mint(msg.sender, lastApprovedPrice);

        // Record staking information
        stakings[msg.sender] = StakingInfo({
            stakedAmount: lastApprovedPrice,
            startTime: block.timestamp,
            duration: duration,
            isStaked: true
        });
    }

    // Unstake tokens
    function unstakeTokens(uint256 tokenId) public {
        StakingInfo storage info = stakings[msg.sender];
        require(info.isStaked, "No staked tokens");
        require(block.timestamp >= info.startTime + info.duration, "Staking period not ended");

        // Burn ERC20 tokens
        erc20TokenContract.burn(msg.sender, info.stakedAmount);

        // Unlock and return the asset token
        assetTokenContract.transfer(msg.sender, tokenId);

        // Reset staking information
        info.isStaked = false;
    }

    // Early withdrawal function with penalty goes here
    // ...

    // Additional functions and operations
    // ...
}

Key Operations and Functions

  • Stake and Unstake: Functions to allow users to stake their tokens and retrieve them after the staking period.

  • Interest and Penalties: Mechanisms to handle the interest distribution and penalties for early withdrawal.

  • ERC20 Token Integration: Interface with ERC20 tokens for minting and burning based on staking activities.

Conclusion

This staking contract provides a structured approach for users to earn rewards by staking their asset tokens. By integrating with ERC20 tokens and ensuring secure transaction mechanisms, the platform offers an attractive feature for token holders to maximize their asset value

Last updated

Logo