Solidity

從合約函式創建 ERC-20 代幣

  • July 23, 2019

我想做一個帶有 ERC-20 介面的函式創建合約。但我有幾個問題。

如果有人幫助我,我將不勝感激。

這是程式碼:

pragma solidity ^0.5.0;
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol';
/*
library SafeMath {
   function add(uint a, uint b) internal pure returns (uint c) {
       c = a + b;
       require(c >= a);
   }
   function sub(uint a, uint b) internal pure returns (uint c) {
       require(b <= a);
       c = a - b;
   }
   function mul(uint a, uint b) internal pure returns (uint c) {
       c = a * b;
       require(a == 0 || c / a == b);
   }
   function div(uint a, uint b) internal pure returns (uint c) {
       require(b > 0);
       c = a / b;
   }
}
*/
contract SubToken{
   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 is SubToken {
   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;

   string public symbol;
   string public  name;
   uint8 public decimals;
   uint _totalSupply;

   constructor(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply)  public{
       name = _name;
       symbol = _symbol;
       decimals=_decimals;
       balances[msg.sender]=_totalSupply;
   }    

   function totalSupply() public view returns (uint256) {
       return _totalSupply;
   }

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


   function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
       emit Transfer(_from, _to, _value);
       return true;
   }
/*
   function createNew(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply) public returns(address newToken){
       SubToken newSubToken = (new SubToken(_name,_symbol,_decimals,_totalSupply));
       created[msg.sender].push(address(newSubToken));
       newSubToken.transfer(msg.sender, _totalSupply);
       return address(newSubToken);

   }
   */
   /*
   function createNewContract() public returns(address){
   SubToken st = new SubToken();
   contracts.push(st);
   address(st);
   }
   */
   event Transfer(address indexed _from, address indexed _to, uint256 _value);

   event Approval(address indexed _owner, address indexed _spender, uint256 _value);


}

contract factory{
   address[] public contracts;
   mapping(address => address[]) public created;

   function createNewContract(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply) public returns(address){
   MainContrat st = new MainContrat(_name,_symbol,_decimals,_totalSupply);
   contracts.push(address(st));
   return address(st);
   }
}

我的 createContract 函式有錯誤。錯誤是:

TypeError:試圖創建一個抽象合約的實例。SubToken st = new SubToken(); ^———-^ browser/crtcont2.sol:29:5:缺少實現:函式批准(地址支出者,uint令牌)公共返回(布爾成功);^————————————————- ————————–^ browser/crtcont2.sol:26:5: 缺少實現:函式 balanceOf(address tokenOwner) 公共視圖返回 (單位餘額);^————————————————- ————————————^ browser/crtcont2.sol:25:5:缺少實現:函式totalSupply()公共視圖返回(uint);^———————————————— ^ browser/crtcont2.sol:28:5:缺少實現:函式傳輸(地址到,uint令牌)公共返回(布爾成功);^————————————————- ———————-^ browser/crtcont2.sol:30:5: 缺少實現:函式 transferFrom(address from, address to, uint tokens) 公共返回(布爾成功);^————————————————- —————————————————-^

您可以改為使用在此設計中創建令牌的工廠類,如下所示子令牌是一個抽像類,由令牌類實現,最後是一個工廠類,它將創建令牌類的不同實例。

pragma solidity ^0.5.0;

library SafeMath {
   function add(uint a, uint b) internal pure returns (uint c) {
       c = a + b;
       require(c >= a);
   }
   function sub(uint a, uint b) internal pure returns (uint c) {
       require(b <= a);
       c = a - b;
   }
   function mul(uint a, uint b) internal pure returns (uint c) {
       c = a * b;
       require(a == 0 || c / a == b);
   }
   function div(uint a, uint b) internal pure returns (uint c) {
       require(b > 0);
       c = a / b;
   }
}
contract SubToken{
   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 token is SubToken {
   mapping(address => uint) _balances;
   mapping(address => mapping(address => uint)) allowed;
   mapping(address => SubToken) public tokenlist;
   address[] public contracts;
   address owner = msg.sender;

   using SafeMath for uint;
   string public symbol;
   string public  name;
   uint8 public decimals;
   uint _totalSupply;

   //constructor(string memory name,string memory symbol,uint8 decimals,uint256 _totalSupply) public{
   //    _balances[msg.sender]=_totalSupply;
   //}    

   function totalSupply() public view returns (uint256) {
       return _totalSupply;
   }

   function balanceOf(address _owner) public view  returns (uint256 balance) {
       return _balances[_owner];
   }

   function transfer(address _to, uint256 _value) public returns (bool success) {

       return true;

   }

   function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
       emit Transfer(_from, _to, _value);
       return true;
   }

   function approve(address _spender, uint256 _value) public returns (bool success) {
       return true;
   }

   //function allowance(address _owner, address _spender) public view returns (uint256 remaining) {
   //    return approve[_owner][_spender];
   //}



   event Transfer(address indexed _from, address indexed _to, uint256 _value);

   event Approval(address indexed _owner, address indexed _spender, uint256 _value);

}

contract factory{
   address[] public contracts;

   function createNewContract() public returns(address){
   token st = new token();
   contracts.push(address(st));
   address(st);
   }
}

您可以在https://github.com/ConsenSys/Tokens/tree/master/contracts/eip20下找到更好的範例

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