0

I'm trying to write a test with ganache and truffle to create a contract instance from another contract.

Everything works in Remix but it doesn't in ganache. This is the code

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

const compiledFactory = require('../build/contracts/EntityFactory.json');
const compiledEntity = require('../build/contracts/Entity.json');

let accounts;
let factory;

beforeEach(async () => {
  accounts = await web3.eth.getAccounts();

  factory = await new web3.eth.Contract(compiledFactory.abi)
    .deploy({ data: compiledFactory.bytecode })
    .send({ from: accounts[0], gas: '6721975' });

  // create rootEntity
  console.log('create root entity from account: ' + accounts[1]);
  try {
    await factory.methods.createEntity().send({
      from: accounts[1],
      gas: '1000000'
    });
  } catch (e) {
    console.log(e);
  }
  console.log('created root entity');
});

describe('Entities', () => {
  it('deploys a factory and an entity', () => {
    console.log(factory.options.address);
  });
});

The console output I receive after runnung "truffle test" is:

create root entity from account: 0x5DE926288f995e7F916f2348894C200545804714
c: VM Exception while processing transaction: revert
    at Function.c.fromResults (/Users/....../ethereum/transparency/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:157333)
    at A.processBlock (/Users/....../ethereum/transparency/node_modules/ganache-cli/build/ganache-core.node.cli.js:17:121734)
    at processTicksAndRejections (internal/process/task_queues.js:89:5) {
  name: 'c',
  results: {
    '0xeed3e78f724bd4c079efda4192df9b95684a45f3789e9c93594dce663a42ccff': {
      error: 'revert',
      program_counter: 2198,
      return: '0x',
      reason: undefined
    }
  },
  hashes: [
    '0xeed3e78f724bd4c079efda4192df9b95684a45f3789e9c93594dce663a42ccff'
  ],
  message: 'VM Exception while processing transaction: revert'
}
created root entity
assign address root
assigned address root
create child entity
    1) "before each" hook for "deploys a factory and an entity"


  0 passing (533ms)
  1 failing

  1) "before each" hook for "deploys a factory and an entity":
     c: VM Exception while processing transaction: revert
      at Function.c.fromResults (node_modules/ganache-cli/build/ganache-core.node.cli.js:2:157333)
      at A.processBlock (node_modules/ganache-cli/build/ganache-core.node.cli.js:17:121734)
      at processTicksAndRejections (internal/process/task_queues.js:89:5)

Here's the EntityFactory code:

pragma solidity ^0.4.17;

contract EntityFactory {
    Entity[] public deployedEntities;
    mapping(address => bool) public deployedEntitiesMap;

    function createEntity() public {
        Entity newEntity = new Entity(msg.sender);
        deployedEntitiesMap[newEntity] = true;
        deployedEntities.push(newEntity);
    }

    function getDeployedEntities() public view returns(Entity[]) {
        return deployedEntities;
    }
}

The Entity's constructor:

function Entity(address creatorAcc) public {
    creator = creatorAcc;
    manager = creatorAcc;
    isManaged = false;
}

I'm completely new with ethereum, just learning! Thanks

  • the createEntity is relevant , please post its code – Majd TL Feb 16 at 15:13
  • I've added it at the end @MajdTL – SG87 Feb 16 at 15:30
  • The Entity constructor is also relevant, please post its code. – goodvibration Feb 16 at 21:13
  • @goodvibration here it is as I said, with remix no issue at all – SG87 Feb 16 at 22:05
  • if i try to use truffle console to create the factory i get this after creating it and calling it { address: [Getter/Setter], jsonInterface: [Getter/Setter], data: undefined, from: undefined, gasPrice: undefined, gas: undefined } – SG87 Feb 16 at 23:33
0

Your code looks as if it was not written for Truffle:

  1. The following is not needed, since it is already provided by the framework:
const assert = require('assert');
const web3 = new Web3(ganache.provider());
accounts = await web3.eth.getAccounts();
  1. You can and should use the source contract instead of the compiled contract:
const compiledFactory = require('../build/contracts/EntityFactory.json');
const compiledEntity = require('../build/contracts/Entity.json');

For example, try this:

const EntityFactory = artifacts.require('EntityFactory');
const Entity = artifacts.require('Entity');

let factory;

contract('Test', (accounts) => {
    beforeEach(async () => {
        factory = await EntityFactory.new({from: accounts[0]});
        await factory.createEntity({from: accounts[1]});
    });

    it('test 1', async () => {
        ...
        assert(...);
    });

    it('test 2', async () => {
        ...
        assert(...);
    });

    it('test 3', async () => {
        ...
        assert(...);
    });
});

Finally, before you run truffle test, start ganache-cli, for example:

ganache-cli --port=8545 --gasPrice=20000000000 --gasLimit=6721975

This will provide you with a Ganache node with 10 unlocked accounts.

| improve this answer | |
  • Great! I was following an outdated tutorial while I was trying to use truffle. I removed the lines you said and adapted the tests and now everything works! thanks a lot – SG87 Feb 17 at 11:14
  • @SG87: You're welcome :) – goodvibration Feb 17 at 14:07

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.