5

Using openzeppelin library and truffle I am trying to make a Contract Factory that creates ERC20 tokens. I prefer to test it with Truffle using solidity and not javascript.

Here is the code from the Contract

    pragma solidity ^0.4.24;

import "zos-lib/contracts/migrations/Migratable.sol";
import "openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";

contract TokenFactory is Migratable {

    address[] public deployedTokens;

    function createToken(string name, string symbol) public {
        address newToken = new Token(name,symbol,msg.sender);
        deployedTokens.push(newToken);            
    }

    function getDeployedTokens() public view returns (address[]) {
        return deployedTokens;
    }

}

contract Token is StandardToken, Ownable {
    string public name = ""; 
    string public symbol = "";
    uint public decimals = 2;
    uint public INITIAL_SUPPLY = 10000 * (10 ** decimals);

    constructor(string _name, string _symbol, address creator) {
        owner = creator;        
        name = _name;
        symbol = _symbol;
        totalSupply_ = INITIAL_SUPPLY;
    }    
}

and the Test

pragma solidity ^0.4.24;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/TokenFactory.sol";

contract TestTokenFactory {

    TokenFactory tk = TokenFactory(DeployedAddresses.TokenFactory());

    function testChildContractDeployed() public {

        tk.createToken("xxx","sym");
        address[] memory deployedTokens = tk.getDeployedTokens();
        Assert.equal(deployedTokens.length, 1, "Contact is not deployed");

    }

}

my truffle.js

'use strict';

module.exports = {
  networks: {
    local: {
      host: 'localhost',
      port: 7545,
      gas: 6000000,
      gasPrice:1,
      network_id: '*'
    }
  }
};

and the error after running truffle test --network local

1) TestTokenFactory "before all" hook: prepare suite: Error: VM Exception while processing transaction: out of gas

2
+100

It took me some time to figure out what's wrong with you code (Thanks for the riddle). Here's how i get your test working :

1- I've change truffle.js to following :

networks: {
    local: {
      host: 'localhost',
      port: 7545,
     // gas: 6000010,
     // gasPrice:10000000000,
      network_id: '*'
    }
  }

but instead out of gas i've got revert exception,

enter image description here

to deal with I've created a migration script (2_initial_migration.js):

var TokenFactory = artifacts.require("./TokenFactory.sol");

module.exports = function(deployer) {
  deployer.deploy(TokenFactory);
};

then the test passed with success

enter image description here

  • hi thanks for trying. the first part works, I get the revert message. When I put the 2_initial_migration.js in the migrations folder (I did not have any migration script before) I get the error : Error: Could not find artifacts for ./Migrations from any sources" I tried to change it to ../contracts/TokenFactory.sol and simple "TokenFactory.sol" but something is wrong. Which directory structure you have? Did you use openzeppeling? Maybe they mess something up with zos – arisalexis Jul 27 '18 at 10:58
  • I've installed openzeplin and zos using npm. try to run truffle compile-all then truffle migrate --reset then test the code. you don't have to change your contract code its working. Besides use the contract name as a file name. – Badr Bellaj Jul 27 '18 at 11:11
  • I've uploaded the truffle's project on github github.com/bellaj/SEError (along the npm modules).. – Badr Bellaj Jul 27 '18 at 11:18
  • 1
    that solved it!! many thanks!! I think I deleted Migrations.sol contract from the folder without knowing it. – arisalexis Jul 27 '18 at 11:37

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.