0

我正在尝试制作 Airdrop 智能合约,但它返回“此合约可能是抽象的,未完全实现抽象父级的方法或未正确调用继承合约的构造函数。” 部署时的消息.....虽然编译工作正常。请检查我下面的代码

pragma solidity ^0.4.18;

contract ERC20 {
    function transfer(address _to, uint256 _value)public returns(bool);
    function balanceOf(address tokenOwner)public view returns(uint balance);
    function transferFrom(address from, address to, uint256 tokens)public returns(bool success);
}

contract SimpleAirdrop is IERC20 {

        IERC20 public token;
        uint256 public _decimals = 18;
        uint256 public _amount = 1*10**6*10**_decimals;
        
        function SimpleAirdrop(address _tokenAddr) public {
            token = IERC20(_tokenAddr);
        }
        
        function setAirdrop(uint256 amount, uint256 decimals) public {
            _decimals = decimals;
            _amount = amount*10**_decimals;
        }
        
        function getAirdrop(address reff) public returns (bool success) {
            require (token.balanceOf(msg.sender) == 0);
            //token.transfer(msg.sender, 1000000000000000000000);
            //token.transfer(reff , 200000000000000000000);
            token.transfer(msg.sender, _amount);
            token.transfer(reff , _amount);
            return true;
        }
} 
4

2 回答 2

1

您的SimpleAirdrop继承自IERC20(参见第一个注释)。IERC20是一个抽象合约——它只定义了它的功能,但它没有实现它们。这也使SimpleAirdrop(的子合同IERC20)成为抽象合同。

Solidity 不允许部署抽象合约。所以你有两个选择让它不抽象:

  1. 实现transfer(),balanceOf()transferFrom()函数(在两个合约中的任何一个中)。

    或者

  2. 删除继承,这样contract SimpleAirdrop is IERC20就变成了 only contract SimpleAirdrop

假设您的上下文SimpleAirdrop仅在外部IERC20地址上执行函数,但本身不充当 ERC-20 令牌,则选项 2 足以满足您的用例。


笔记:

  • 您的问题定义了ERC20合同,但其余代码使用IERC20. 我假设这只是将代码复制到问题时的错字,否则您在所有地方都使用相同的命名。

  • 当前的 Solidity 版本(2021 年 6 月)是0.8.5. 我建议使用当前版本,有安全性和错误修复。

于 2021-06-16T19:17:55.270 回答
0

请检查以下代码中的任何误解 编译没有问题,使用参数部署并在测试网中成功 调用 startAirdrop 函数时出现问题......气体有问题

请注意

pragma solidity ^0.4.18;


contract ERC20 {
    function transfer(address _to, uint256 _value)public returns(bool);
    function balanceOf(address tokenOwner)public view returns(uint balance);
    function transferFrom(address from, address to, uint256 tokens)public returns(bool success);
}


contract SimpleAirdrop {

        ERC20 public token;
        uint256 public _decimals = 9;
        uint256 public _amount = 1*10**6*10**_decimals;
        uint256 public _cap = _amount *10**6;
        address public tokenOwner = 0x0;
        uint256 public _totalClaimed = 0; 
        uint256 public _reffPercent = 10; 
        
        
        function SimpleAirdrop(address _tokenAddr ,address _tokenOwner ) public {
            token = ERC20(_tokenAddr);
            tokenOwner = _tokenOwner;
            
            
        }
        
        function setAirdrop(uint256 amount, uint256 cap, uint256 decimals ,uint256 reffPercent) public returns (bool success){
            require (msg.sender == tokenOwner);
            _decimals = decimals;
            _amount = amount*10**_decimals;
            _cap = cap*10**_decimals;
            _reffPercent = reffPercent;
            return true;
            
        }
        
        
        function sendAirdropToken() public returns (bool success){
            require (msg.sender == tokenOwner);
            token.transferFrom(msg.sender,address(this),_cap);
            return true;
        }
        
        function returnAirdropToOwner() public returns (bool success){
            require (msg.sender == tokenOwner);
            token.transferFrom(address(this), msg.sender, address(this).balance);
            return true;
            
        }
        
        function getAirdrop(address reff) public returns (bool success){
            if(msg.sender != reff && token.balanceOf(reff) != 0 && reff != 0x0000000000000000000000000000000000000000 && _cap >= _amount){
                token.transfer(reff , _amount*(_reffPercent/100));
                _cap = _cap - (_amount*(_reffPercent/100));
                
             }
            if(msg.sender != reff && token.balanceOf(reff) != 0 && token.balanceOf(msg.sender) == 0 && reff != 0x0000000000000000000000000000000000000000 && _cap >= _amount)
            {   token.transfer(msg.sender, _amount);
                _cap = _cap - _amount;
                _totalClaimed ++;
            }
            return true;
            
        }
        
        
    

    
}
于 2021-06-17T01:05:15.060 回答