Erc20 time locking explained

Parishilan Rayamajhi
4 min readMar 11, 2019

Before starting we need to understand the concept of owner of a smart contract.

The address by which you deploy a contract now becomes a owner address,When you deploy a ERC20 contract you first transfer all of the total supply to owner address in my case it is

owner : 0xca35b7d915458ef540ade6068dfe2f44e8fa733c

code source : https://ethfiddle.com/5WOwwx4u05

1.Total Supply

Gives the total supply of the particular ERC20

initially the total supply is defined to be :

uint256 private constant totalsupply_ = 1000000000

When we deployed the contract, we have mentioned in our constructor that

constructor() public{

balanceof_[msg.sender] = totalsupply_;
owner = msg.sender;
}

total supply is assigned to the balance of owner address,
so at this point(just after deploying a contract) balance of my owner address is 1000000000

2. Transfer

function transfer(address to_, uint256 value_) external returns (bool){

This function transfer tokens from one address to another

ip:

to_address → the receiver address (for instance our to_address is : 0xdd870fa1b7c4700f2bd7f44238821c26f7392148 )

value → no of token to be sent

eg:

ip parameters:

“0xdd870fa1b7c4700f2bd7f44238821c26f7392148",10

op:

true/false

3)BalanceOf(address)

Function gives the balance of particular address

earlier balance of to_address(0xdd870fa1b7c4700f2bd7f44238821c26f7392148 )was 0

now after i called transfer function my balance of to_address(0xdd870fa1b7c4700f2bd7f44238821c26f7392148) address is 10

4) approve

In some cases owner can approve thrid party address to transfer balance from owner’s address to xyz address. For that first of all owner need to assign third party address the right , and he can do so by calling approve function..

function approve(address spender_, uint256 value_) external returns (bool){

here spender_ is the third party address that need to be given rights of owner

lets say that address is : 0x583031d1113ad414f02576bd6afabfb302140225 , and he wants to allow spender to send 5000 coins

to do so owner need to call approve function with following parameter

ip: approve(“0x583031d1113ad414f02576bd6afabfb302140225”,5000)

op: true

5) allowance

This function validate how many tokens are actually allowed by owner to third_party address so that he can transfer on behalf of owner:

for eg: in step 4 we assign 5000 tokens as sendable by address : 0x583031d1113ad414f02576bd6afabfb302140225 which we can validate using allowance function

ip: allowance (“0xca35b7d915458ef540ade6068dfe2f44e8fa733c”,”0x583031d1113ad414f02576bd6afabfb302140225")

op: 5000

here

0xca35b7d915458ef540ade6068dfe2f44e8fa733c → address of owner

0x583031d1113ad414f02576bd6afabfb302140225 → address of sender

6) transferFrom

the use of this function is kind of trikky,

If you think this is normal function where from and to address is given and by calling this function you can transfer funds from one address to another address then thats wrong. You cannot manipulate balance from other address though you are the contract owner, unless someone approves you by calling approve function(like we did it before)

Now to test this function:

instead of ower calling the function we should call the function with the third_party address(0x583031d1113ad414f02576bd6afabfb302140225) but and we will transfer funds from owner address to xyz address i.e (0xdd870fa1b7c4700f2bd7f44238821c26f7392148). So lets transfer 4 tokens

ip:

transferFrom(“0xca35b7d915458ef540ade6068dfe2f44e8fa733c”,”0xdd870fa1b7c4700f2bd7f44238821c26f7392148”,4)

op: true

** point to remember: we have allowed third party address 5000 tokens to transfer but here he only sent 4 token, so now if you check allowance then 5000–4 = 4996 tokens(he can tranfer to other address as well as his own address)

This is how a ERC20 works.

Now after having clear understanding about ERC20, we can move ahead to undersatand how the concept of locking is achived in smart contracts.

7) lock_erc(value,releasetime)

Only owner can use this functionality i.e in ower case if any other address invoke this function other than 0xca35b7d915458ef540ade6068dfe2f44e8fa733c then that will throw exception

to envoke this functionality you need to call lock_erc function with following ip:

a) total tokens you want to lock

b) total time in epoch you want to lock the contract till

you can convert date time to epoch time anytime using : https://www.epochconverter.com/

lets say i want to lock 5000 tokens till 1552315106(this is the date time of today)

then i will envoke function like:

ip: lock_erc(5000,1552315106)

op: index (1,2,3….)

now after response is some integer, 5000 tokens has been locked, and if you check the balance of owner it is reduced by 5000.

lets say we locked output of above lock_erc(5000,1552315106) is 1

i.e in 1 index 5000 tokens are locked till epoch time 1552315106

  • *imp point: The index no that is returned by the function lock_erc is very crucial to release the token, while releasing the locked tokens you need to pass the index no where you initially locked the n no of tokens

8)release_erc(index_no)

to release erc all you need to remember is the index no that is returned when we invoked lock_erc() function, that mens you tokens are locked on particular indexes .

if the op of lock_erc(5000,1552315106) was 1

then our current ip would be:

release_erc(1)

op: true / false

if its true: if the release time satisfy the condition then automatically 5000 tokens would be allocated to owners address i.e 0xca35b7d915458ef540ade6068dfe2f44e8fa733c in our case.

and if you check the balance of owner now it will show incresed balance by 5000

thanks all!!

--

--