I want to create a ERC20 contract in which inside the transfer() function I would check if the msg.sender can transfer tokens. This check will be like those we have in stock markets where when someone buys stocks , they cannot sell it on the same day . Since transfer() function could be called by anyone, we have to also keep in mind that the above check will not be applied for the owner . He can transfer as many tokens and to whoever he likes in anytime. Now , for other people who have my ERC20 token , I want that they can sell their tokens after 1 day ( or any time which I specify ) . For this functionality I want a variable to keep track of what time it is , how much time is left for these people so they can sell their tokens. I tried using block.timestamp for keeping log of time , but it didn't helped .
contract ERC20 is IERC20 {
//many other variables here
mapping(address =>uint256) public currentTime; // to be initialized when I call transfer() function
mapping(address =>uint256) public upperLimitTime; // to be calculated with the help of currentTime e.g. if I want that people can sell my tokens after a day then upperLimitTime = currentTime+24*60*60;
uint256 public LiveTime;
constructor() {
owner = msg.sender;
balance[owner] = totalSupply_;
}
function transfer(address receiver, uint256 token) public override returns (bool) {
require(token * (10**decimals) <= balance[msg.sender],"Not enough tokens in sender's account"); //msg.sender whether it is owner or just a person calling this contract should have required tokens available
currentTime[msg.sender] = block.timestamp; // storing currentTime
upperLimitTime[msg.sender] = block.timestamp + 2*60; // this is upper time limit , we want that if this upperLimitTime is crossed then any person ( excluding owner here ) are able to transfer their tokens
// LiveTime = getCurrentTimeInSeconds(); // I want this to automatically fetch current epoch time, tried using block.timeStamp but not getting result I wanted
if(msg.sender!=owner) { // if transfer() function is called by any other person than owner of the contract , I want this code to run
require(LiveTime>=upperLimitTime[msg.sender],"we can call this function after 2 minutes"); // current time i.e.LiveTime counter should've reached ahead of the upperTimeLimit I set
balance[msg.sender] = balance[msg.sender]-(token * (10**decimals));
balance[receiver] = balance[receiver]+(token * (10**decimals));
}
else if(msg.sender == owner) { // since we want contract owner to transfer token whenever he wants
balance[owner] = balance[owner]-(token * (10**decimals));
balance[receiver] = balance[receiver]+(token * (10**decimals));
}
return true;
}
//other functions here
} // end of contract
token * (10**decimals)
? It doesn't make sense inside a standard transfer function. It is hard to say what the problem is exactly because the almost the whole contract is missing, but it could be thatcurrentTime[msg.sender] = block.timestamp;
are executed before the checks. Study the Check-Effect-Interactions pattern to write better solidity code.