# create ERC-20 token from a contract function

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 transfer(address to, uint tokens) public returns (bool success) ;
function approve(address spender, uint tokens) public returns (bool success);

}
contract MainContrat is SubToken {

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));
newSubToken.transfer(msg.sender, _totalSupply);

}
*/
/*
SubToken st = new SubToken();
contracts.push(st);
}
*/

}

contract factory{

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

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

• because you're trying to instantiate an abstract contract. Jul 23, 2019 at 8:42
• what should I do ? Can you help me? Jul 23, 2019 at 8:45
• Well yes I can create new contract and its give me the inspiration :) Jul 23, 2019 at 10:57
• @BadrBellaj hey I changed my code and I got same results can you help me again please :) Jul 23, 2019 at 11:28
• @BadrBellaj ethereum.stackexchange.com/questions/73209/… Jul 23, 2019 at 11:56

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 transfer(address to, uint tokens) public returns (bool success) ;
function approve(address spender, uint tokens) public returns (bool success);

}
contract token is SubToken {

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

//    return approve[_owner][_spender];
//}

}

contract factory{