What's new

Welcome to uruad | Welcome My Forum

Join us now to get access to all our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, and so, so much more. It's also quick and totally free, so what are you waiting for?

Timelock in Solidity – EatTheBlocks

Hoca

Administrator
Staff member
Joined
Mar 22, 2024
Messages
193
Reaction score
0
Points
6
A timelock is a chunk of code that locks performance on an utility till a sure period of time has handed. It was broadly used as a kind of sensible contract primitive in lots of Bitcoin scripts as a way to lock-up bitcoins normally for durations of months or years.

Later, the ICO craze introduced many rip-off tasks the place the founders dumped their tokens as quickly as they might get some revenue. Legit tasks began implementing timelocks to sign dedication from the founders, because the timelock labored as a vesting mechanism broadly used within the startup world.

Yield farming and DeFi have introduced a brand new collection of ponzinomics, dump and run schemes, and governance points. Sushiswap had a latest episode the place the founder dumped hundreds of thousands of {dollars} on Sushi Tokens from the dev pool and affected Sushi holders, improper or not a timelock might have prevented this habits.

Code:
pragma solidity ^0.7.0;
contract DeFiProtocol {
    deal with public ERC20Token;
    deal with public uniswapPool;
    uint256 public burnFee;
    perform setERC20(deal with _erc) public {
        ERC20Token = _erc;
    }

    perform setUniswapPool(deal with _pool) public {
        uniswapPool = _pool;
    }

    perform setFee(uint256 _fee) public {
        burnFee = _fee;
    }
}

Options of a timelock:
– Extending from Ownable, enable us so as to add the onlyOwner modifier to our features which permit solely the proprietor of the contract to execute it.
– The enum permits us to entry the time-locked perform in a extra readable means.
– The _TIMELOCK fixed defines how a lot we wish our features to attend till they are often executed.
– We additionally create a timelock mapping which helps us to see which perform is presently unlocked and create our personal modifiers accordingly

Timelock perform in solidity

Code:
pragma solidity ^0.7.0;

import "@openzeppelin/contracts/entry/Ownable.sol";

contract DeFiProtocol is Ownable {

   deal with public ERC20Token;
   deal with public uniswapPool;
   uint256 public burnFee;

   enum Capabilities { ERC, POOL, FEE }    
   uint256 non-public fixed _TIMELOCK = 1 days;
   mapping(Capabilities => uint256) public timelock;

   modifier notLocked(Capabilities _fn) {
     require(timelock[_fn] != 0 && timelock[_fn] <= block.timestamp, "Operate is timelocked");
     _;
   }

  //unlock timelock
  perform unlockFunction(Capabilities _fn) public onlyOwner {
    timelock[_fn] = block.timestamp + _TIMELOCK;
  }

  //lock timelock
  perform lockFunction(Capabilities _fn) public onlyOwner {
    timelock[_fn] = 0;
  }

  perform setERC20(deal with _erc) public onlyOwner notLocked(Capabilities.ERC) {
      ERC20Token = _erc;
      timelock[ERC] = 0;
  }

  perform setUniswapPool(deal with _pool) public onlyOwner notLocked(Capabilities.POOL) {
      uniswapPool = _pool;
      timelock[POOL] = 0;
  }

  perform setFee(uint256 _fee) public onlyOwner notLocked(Capabilities.FEE) {
      burnFee = _fee;
      timelock[FEE] = 0;
  }
}
 
Top Bottom