0

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
2
  • Do you want the check only when the tokens are minted to an address, or also when a user receives those tokens from another user? In both the cases, the user will be getting new tokens in his wallet. Nov 22, 2022 at 13:18
  • Why are you doing this 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 that currentTime[msg.sender] = block.timestamp; are executed before the checks. Study the Check-Effect-Interactions pattern to write better solidity code.
    – Ismael
    Dec 2, 2022 at 5:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.