A Guide to Understanding Locking Mechanism for ERC20 Token

Posted By : Vishal

Aug 31, 2020

In this article, acquaint yourself with the technical intricacies involved in the locking and unlocking mechanism of the ERC20 token within Ethereum blockchain development, while exploring its pros and cons.

 

Understanding ERC20 Token

 

ERC20 is a cryptocurrency token that is used to develop Ethereum-based tokens. An ERC20 token is a blockchain-based asset that is similar to the ether, Bitcoin, and Bitcoin cash. ERC20 token runs over the Ethereum network.

 

Ethereum is a decentralized network similar to Bitcoin, provides lots of functionalities, and has its currency known as ether. It also provides users/companies to create their digital currencies, in return, they need to spend some transaction fees while doing any transaction on the Ethereum network. ERC20 is the most popular standard for creating its token/asset over the network. Ethereum provides a variety of tools for developers to create tokens and other functionality using smart contracts.

 

Suggested Read | ERC-20 Token Standard | Development Essentials

 

Locking Mechanisms

 

Locking mechanisms help the owner to control the supply of tokens. By using this mechanism, a user can lock and later unlock their tokens. Usually, they use it when they want to Lock their tokens, similar to savings.   

 

The locking mechanism works in two parts.

 

First is the locking of tokens. We can only unlock them once they are locked. Usually, while deploying the contract, you can lock them. It is not necessary to Lock them at the time of deployment. We can Lock them at any other time. In this flow, we are Locking at the time of deployment. We can set the owner or any other user as an admin that will handle the locking/unlocking part.

 

 

Second is the unlocking of these tokens. Your tokens get unlocked when the lock time is completed. Also, the admin can unlock them. You remove this condition from the code, but it will create a security issue as anyone can unlock them.

 

 

Check It Out | Exploring Token Standards Beyond Ethereum

 

Advantages of the Locking Mechanism

 

  • Users can lock their tokens. Suppose a user wants to reduce their extra spending then, in that case, they can lock their token. 
  • The mechanism can come into use in DeFi, where the user can lock their token and will get the returns for the same.
  • It is used in reward distribution mechanisms like the lottery system.

 

Disadvantages of the Locking Mechanism

 

  • Once your tokens get locked, you cannot unlock them until the time is completed.

 

Also, Discover | ERC-4337: Ethereum's Account Abstraction Proposal

 

Steps to Achieve a Locking Mechanism

 

Below are the steps that you can follow to achieve the locking mechanism:

 

Step 1: Open remx.ethereum.org and connect your Metamask wallet to it.

 

Step 2: Import the required dependency for the contract.

 

import "./tests/standardcontract/StandardToken.sol";  //Import standard token file available on Oppenzepplin
import "./tests/ERC1132.sol";   // Import ERC1132 standard file. This is the main file for the locking mechanism

 

Step 3: Lock and unlock function for tokens.

 

// Lock functions
 function lock(bytes32 _reason, uint256 _amount, uint256 _time)
        public
        returns (bool)
    {
        uint256 validUntil = now.add(_time); 
        require(tokensLocked(msg.sender, _reason) == 0, ALREADY_LOCKED);
        require(_amount != 0, AMOUNT_ZERO);
        if (locked[msg.sender][_reason].amount == 0)
            lockReason[msg.sender].push(_reason);
        transfer(address(this), _amount);
        locked[msg.sender][_reason] = lockToken(_amount, validUntil, false);
        emit Locked(msg.sender, _reason, _amount, validUntil);
        return true;
    }
  // Unlock function
      function unlock(address _of)
        public
        returns (uint256 unlockableTokens)
    {
        uint256 lockedTokens;
        for (uint256 i = 0; i < lockReason[_of].length; i++) {
            lockedTokens = tokensUnlockable(_of, lockReason[_of][i]);
            if (lockedTokens > 0) {
                unlockableTokens = unlockableTokens.add(lockedTokens);
                locked[_of][lockReason[_of][i]].claimed = true;
                emit Unlocked(_of, lockReason[_of][i], lockedTokens);
            }
        }  
        if (unlockableTokens > 0)
            this.transfer(_of, unlockableTokens);
    }

 

Step 4: Get the Locked amount and extend the lock

 

 // Extend lock 
 function extendLock(bytes32 _reason, uint256 _time)
        public
        returns (bool)
    {
        require(tokensLocked(msg.sender, _reason) > 0, NOT_LOCKED);
        locked[msg.sender][_reason].validity = locked[msg.sender][_reason].validity.add(_time);
        emit Locked(msg.sender, _reason, locked[msg.sender][_reason].amount, locked[msg.sender][_reason].validity);
        return true;
    }
 // Get locked amount
  function tokensLocked(address _of, bytes32 _reason)
        public
        view
        returns (uint256 amount)
    {
        if (!locked[_of][_reason].claimed)
            amount = locked[_of][_reason].amount;
    }

 

You May Also Like | ERC-20 Token Standard | Things You Must Know

 

Conclusion

 

By following the above steps, you can integrate the locking mechanism on your ERC20 without the need to be dependent on any external service. If you are interested in developing an ERC20 token, then connect with our Ethereum blockchain developers

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

November 23, 2024 at 01:07 am

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button

Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text