Solidity
嘗試創建抽象合約的實例
pragma solidity ^0.5.0; import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol'; contract SubToken{ mapping(address => uint) balances; string symbol; string name; uint8 decimals; uint totalSupply; using SafeMath for uint; //subtoken_struct public sub_tokens; //mapping(address => subtoken_struct) public mySubTokens ; address contract_address; constructor(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply) public{ name = _name; symbol = _symbol; decimals=_decimals; totalSupply = _totalSupply; balances[msg.sender]=_totalSupply; } //function totalSupply() public view returns (uint); //function balanceOf(address tokenOwner) public view returns (uint balance); //function allowance(address tokenOwner, address spender) public view returns (uint remaining); //function transfer(address to, uint tokens) public returns (bool success) ; //function approve(address spender, uint tokens) public returns (bool success); function transferFrom(address from, address to, uint tokens) public returns (bool success); event Transfer(address indexed from, address indexed to, uint tokens); event Approval(address indexed tokenOwner, address indexed spender, uint tokens); } contract MainContrat { mapping(address => uint) balances; mapping(address => mapping(address => uint)) allowed; mapping(address => address[]) public created; mapping(address => SubToken) public tokenlist; address[] public contracts; address owner = msg.sender; using SafeMath for uint; address contract_address; function createNewContract(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply) public returns(address newAddres){ SubToken mc = new SubToken(_name,_symbol,_decimals,_totalSupply); created[msg.sender].push(address(mc)); contracts.push(address(mc)); return address(mc); } function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { emit Transfer(_from, _to, _value); return true; } event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value); }
嘿伙計們,我有一份主契約,並且有一個 creteContract 功能。這個函式創建一個新的子令牌,所以我像介面一樣使用 subToken。
當我編譯我的契約時,我收到了這樣的錯誤:
TypeError:試圖創建一個抽象合約的實例。SubToken mc = new SubToken(_name,_symbol,_decimals,_totalSupply); ^———-^ browser/crtcont7.sol:32:5: 缺少實現:函式 transferFrom(address from, address to, uint tokens) public return (bool success); ^————————————————- —————————————————-^
pragma solidity ^0.5.0; import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol'; contract SubToken{ mapping(address => uint) balances; string symbol; string name; uint8 decimals; uint totalSupply; using SafeMath for uint; address contract_address; constructor(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply) public{ name = _name; symbol = _symbol; decimals=_decimals; totalSupply = _totalSupply; balances[msg.sender]=_totalSupply; } //function transferFrom(address from, address to, uint tokens) public returns (bool success); //function allowance(address tokenOwner, address spender) public view returns (uint remaining); //function transfer(address to, uint tokens) public returns (bool success) ; //function approve(address spender, uint tokens) public returns (bool success); //function transferFrom(address from, address to, uint tokens) public returns (bool success); event Transfer(address indexed from, address indexed to, uint tokens); event Approval(address indexed tokenOwner, address indexed spender, uint tokens); } contract MainContract { mapping(address => uint) balances; mapping(address => mapping(address => uint)) allowed; mapping(address => address[]) public created; mapping(address => SubToken) public tokenlist; address[] public contracts; address owner = msg.sender; using SafeMath for uint; address contract_address; /* function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { emit Transfer(_from, _to, _value); return true; } */ function createNewContract(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply) public returns(address){ SubToken st = new SubToken(_name,_symbol,_decimals,_totalSupply); //st = new SubToken(); created[msg.sender].push(address(st)); contracts.push(address(st)); //return address(mc); } event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value); }
您可以查看OpenZeppelin Contracts ERC20 代幣文件,看看這是否滿足您對 ERC20 代幣的需求。您可能還想使用OpenZeppelin IERC20 介面。
請注意,您應該只使用在官方版本中發布的 OpenZeppelin 程式碼,因此在使用 Remix 時您應該在導入中包含版本標記,或者您可以查看入門指南以獲取有關如何設置開發環境的資訊。
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/math/SafeMath.sol";
您還可以在社區論壇中詢問有關如何使用 OpenZeppelin 合約的問題
披露:我是 OpenZeppelin 的社區經理