A Time-Lock Smart contract That generates funds for devs.
Tags: solidity, blockchain, web3, smartcontract
Published: 2023-06-02 18:44:08

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dkzzkmkfeua78bhpqf83.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x80jer1t3ps9tfqgwjw6.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eougodxuct3o6b810p1u.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwi2k9po1sisygl9hs0r.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wev2x3q019csfh8hh9vx.png)

If you've followed the article all through, big up's. You can find the code on [github](https://github.com/crudeboy/Time_Lock_Wallet). You can reach me on [linkedIn](https://www.linkedin.com/in/jahswill-apata/) and [twitter](https://twitter.com/Ogbeni_jazzwill). 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.