DEV Community

Cover image for A Time-Lock Smart contract That generates funds for devs.
Jazz_Baba
Jazz_Baba

Posted on

A Time-Lock Smart contract That generates funds for devs.

A time lock smart contract is a tool that locks tokens or crypto currencies into a smart contract for a particular time frame. This is a very useful feature in prevention of impulsive token sales or transfer on a whim.
The smart contract implemented allows external users to lock their Ethers in the smart contract and pay a small fee which goes directly to the developer of the smart contract. This could be a passive income generator for a smart contract developer.
Many technical concepts are implemented in this smart contract, some are:

  1. Using modifiers
  2. Preventing re-entrancy attacks
  3. Working with Dates in smart contracts.
  4. Using Structs and Mappings

The smart contract has the following features which can be used as a guide in understanding smart contracts and developing ideas which builds on this. The features are:

  1. A taxing system
  2. A time lock feature
  3. Funds transfer feature
  4. Authorization

Also included is simple string concatenation in smart contracts and using emojis.

Transferring Ethers to the Smart contract

The initial step to activating the smart contract lock feature is to credit the smart contract. The smart contract has a pay function that is "payable". The first check in the pay function is to ensure the amount inputed is that same as the value the user pays into the smart contract. A require statement is used to achieve this. The second check is to ensure that the credit amount is at least the fixed tax associated with the funds lock functionality. This particular smart contract implements a fixed tax system which would be explained further in the article.

Image description

The smart contract allows seamless withdrawal of funds that are transferred to the smart contract when the lock feature has not been triggered. No lock fee or tax is imposed only transaction gas is deducted by the EVM.

Triggering the Funds Lock feature

The main course in this project is the Time Lock feature. This feature implements a struct that holds essential details of the user: the start time for the funds lock, the Lock interval, user balance. As soon as a user locks their funds in the contract, the variable "hasLockedFunds" is set to true this is stored to be used to ensure the user has locked funds and has permission to trigger the withdraw functionality.

Image description

The image above shows the use of a struct to encapsulate all the user details in the smart contract.
The power of structs in smart contracts is unleashed when used with mappings and other structs. This is the simplest use case of a struct and a mapping.

Image description

Above is the implementation of the funds lock functionality. the code above uses a time of 15 seconds which is for testing while implementing the smart contract. The smart contract lock period is fixed at 365 days, this could be easily changed to accept a "number of days" variable value.

To test the time Lock functionality you could set the lock time to "15" seconds, this would help the developer validate the time count down functionality.

Time lapse feature

To help users of the smart contract know how far along their funds lock is there's a "howManyDays" feature. This feature helps the user get the number of days left before their funds are available for withdrawal. The functionality depends on a countdown timer which sets the "funds release time", which is when the funds would become available. The countdown timer ensures the "block.timestamp" hasn't yet equalled or exceeded the set "funds release time". The functionality uses "int256" and does a type casting to convert the difference "fundsLockTime" and the "presentTime". The difference between these two variables would eventually spill into a negative vale and the "uint256" type would error on variable subtraction.
Below is the code snippet for this:

Image description

The Tax System(payday for devs)

Smart contracts and tokens often implements a tax system that ensures the service rendered by the smart contract is billed to the end users of the contract. There are various taxing models that could be used, on this safe lock project a fixed tax system is implemented , which bills any Ether lock into the smart contract. A base fee of 0.00055ETH(which is about $10 as at writing this smart contract).
This fee can only be withdrawn by the smart contract developer. The code includes a snip-bit of how to use emojis in a smart contract for you perusal.

Image description

If you've followed the article all through, big up's. You can find the code on github. You can reach me on linkedIn and twitter. Looking forward to your comments and questions.
The smart contract would work seamlessly on the Remix IDE. Further set ups would be required for a local IDE.

See you soon, be on the look out for my next article on creating your own ERC20 token.

Top comments (1)

Collapse
 
crudeboy profile image
Jazz_Baba

I'd be ever glad to respond to your questions and comments ...