简体   繁体   English

有没有办法在 Solidity 的转移函数中检查买卖?

[英]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.我的目标是在 PancakeSwap 上签订合同,允许人们购买代币,并且只能在预定的时间后出售这些代币。

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.例如,合约在晚上 09:00 部署,人们可以购买但尚未出售,并且在晚上 09:10 开始出售。

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.您可以使用 blocktime 粗略估计时间跨度,为了精确计时,请使用 oracle。 "now" is the same as "block.timestamp". “现在”与“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.但是,映射会将所有可能的地址初始化为等于 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.将在该地址购买时将映射设置为 blockstamp.time。

The wait time is 10 minutes, but that could be a variable as well, with a separate helper function to set it.等待时间是 10 分钟,但这也可以是一个变量,有一个单独的辅助函数来设置它。

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.如果这是您想要的,则取决于您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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