简体   繁体   中英

Is there a way to check sells and buys in transfer function on Solidity?

My objective is to make a contract on PancakeSwap that allow people to buy tokens and only sell these after a predefined time.

For example the contract is deploy at 09:00 pm, people can buy it but don't sell it yet, and at 09:10 pm the sell is on.

I was thinking about add a modifier on a sell function but i don't understand how to do a separate function.

I was also thinking about add some checks in transfer function but this is still the same, i don't know how to check who is sell and who is the buyer.

Here is one of my try to perform it:

pragma solidity ^0.8.2;

contract Token {
    mapping(address => uint256) public balances;
    mapping(address => mapping(address => uint256)) public allowance;

    uint256 public decimals = 18;
    uint256 public totalSupply = 1000000000000 * 10**decimals;
    string public name = "Maxi Pump Token Try 2";
    string public symbol = "$MPT2";
    uint256 public allowedSellTime;
    uint256 public totalTime = 30;

    modifier sellNotAllowed() {
        require(
            block.timestamp > allowedSellTime,
            "You are not allowed to sell yet"
        );
        _;
    }

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(
        address indexed owner,
        address indexed spender,
        uint256 value
    );

    constructor() {
        balances[msg.sender] = totalSupply;
        allowedSellTime = block.timestamp + totalTime;
    }

    function balanceOf(address owner) public view returns (uint256) {
        return balances[owner];
    }

    function transfer(address to, uint256 value)
        public
        sellNotAllowed
        returns (bool)
    {
        require(balanceOf(msg.sender) >= value, "balance too low buddy");
        // Check if user wants to sell his tokens, if true then check time

        balances[to] += value;
        balances[msg.sender] -= value;
        emit Transfer(msg.sender, to, value);
        return true;
    }

    function transferFrom(
        address from,
        address to,
        uint256 value
    ) public returns (bool) {
        require(balanceOf(from) >= value, "balance too low buddy");
        require(allowance[from][msg.sender] >= value, "allowance too low");
        balances[to] += value;
        balances[from] -= value;
        emit Transfer(from, to, value);
        return true;
    }

    function approve(address spender, uint256 value) public returns (bool) {
        allowance[msg.sender][spender] = value;
        return true;
    }
}

First, it's going to be impossible getting an exact time using blocktime. You can roughly estimate a timespan with blocktime, for precise timing use an oracle. "now" is the same as "block.timestamp". You need to store the time when the buy function is called for that address. Then, you can check "now" when the sell function is called and compare it to the time of the buy to check if enough time passed.

A check like this in the sell function, or maybe, the transfer function, otherwise addresses could transfer to other addresses to sell.

require(now > timeOfBuy + 10 min && timeOfBuy > 0, "Not enough time -10 min- has passed");

You might be able to store the time of buy for each address that buys using a mapping. However, the mapping will initialize all possible addresses to equal 0.

mapping (address => uint) addrToTimeOfBuy;

So in the buy function:

addrToTimeOfBuy[msg.sender] = now;

will set the mapping to the blockstamp.time when that address buys.

The wait time is 10 minutes, but that could be a variable as well, with a separate helper function to set it.

With the current logic, once an address gets a buyTime set for it and it waits through the wait time, it can sell anytime, as much as it wants. Depends on your application if that's what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM