0

I would like to do a function create contract with ERC-20 interface. But I have a few problems.

If anyone help me I’ll be appreciated.

Here’s the code :

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);
    }
}

I have errors at my createContract functions. Errors is :

TypeError: Trying to create an instance of an abstract contract. SubToken st = new SubToken(); ^----------^ browser/crtcont2.sol:29:5: Missing implementation: function approve(address spender, uint tokens) public returns (bool success); ^---------------------------------------------------------------------------^ browser/crtcont2.sol:26:5: Missing implementation: function balanceOf(address tokenOwner) public view returns (uint balance); ^------------------------------------------------------------------------^ browser/crtcont2.sol:25:5: Missing implementation: function totalSupply() public view returns (uint); ^------------------------------------------------^ browser/crtcont2.sol:28:5: Missing implementation: function transfer(address to, uint tokens) public returns (bool success); ^-----------------------------------------------------------------------^ browser/crtcont2.sol:30:5: Missing implementation: function transferFrom(address from, address to, uint tokens) public returns (bool success); ^-----------------------------------------------------------------------------------------^

1

you can instead use a factory class which creates token as follows in this design subtoken is an abstract class, implemented by token class and in the end a factory class which will create different instances of token class.

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);
    }
}

you can find a better example under https://github.com/ConsenSys/Tokens/tree/master/contracts/eip20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.