Go-Ethereum

防止買家代幣轉移的智能合約erc20程式碼片段

  • December 24, 2021

我將開始代幣預售,我希望能夠將代幣發送給買家,但阻止買家將代幣發送到另一個錢包。

我在我的 ERC20 智能合約中嘗試了以下操作,但它實際上阻止了我自己(合約創建者)轉移代幣。我需要能夠向買家發送代幣,並防止買家在未來某個時間之前轉移代幣。

這是測試智能合約合約:https ://etherscan.io/address/0x5783f4e4a1bec72f41f246c50ba3d06265d984a6#code

函式轉移(地址_to,uint_value)公共{要求(現在> 1514764800);/* 其餘函式 */ }

你可以這樣做。

modifier notPaused {
   require(now > 1514764800  || msg.sender == owner);
   _;
}

function transfer(address to, uint256 amount) public notPaused returns(bool) {
   //rest of function
}
//this is where you declare the identifier owner

address owner;

//whith this modifier, only you can call the transfer function 
//when now <= 1514764800.

modifier notPaused{
   require(now > 1514764800 || msg.sender == owner);
   _;
}

//the identifier owner is initialized once the contract is deployed and 
//set to the address of the message sender. Note that this should be in 
//the constructor of the contract, which is always the function with the
//same name as the contract.

function ERC20Token() {
   owner  = msg.sender;
   //rest of constructor
}

//by adding notPaused here, the function will only successfully execute when
//A: you the owner call the function before or after the block 1514764800 or
//B: when anyone else calls the the function after the block 1514764800

function transfer(address to, uint256 amount) public notPaused returns (bool) {
   //rest of function
}

引用自:https://ethereum.stackexchange.com/questions/29398