1

I'm creating "Trade" contract inside "Token" contract. I used an array to store each "Trade" contracts. Used web3, truffle.

Token.sol:

mapping (address => Trade[]) private _Trade;
event tradeMade(address maker);
...
function createTrade(params...)
public{
    Trade newTrade = new Trade(params...); // line A
    _Trade[msg.sender].push(newTrade);     // line B

    emit tradeMade(msg.sender);
}

Trade.sol:

constructor(params..)
public {
    // Set variables. Parameters are correct
}

Run.js:

Token.createTrade(params..., function(err, res){
    // Do jobs
});
var event = Token.tradeMade();
event.watch(function(err, res){
    if(!err) console.log("Maker: " + res.args.maker);
});

Compiling / Migrating: Succeed.

Running js file: Failed with the following Error

> Error: VM Exception while processing transaction: revert

Truffle debugging screen:

Token.sol:createTrade() -> Roles.sol:add() -> Roles.sol:remove():require(has(role, account));
> TypeError: Cannot read property 'replace' of undefined
      at process._tickCallback (internal/process/next_tick.js:68:7)
      at Object.interpreter (C:\Users\cosgenio\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-core\lib\commands\debug.js:695:1)
      at printState (C:\Users\cosgenio\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-core\lib\commands\debug.js:167:1)
      at Object.formatRangeLines (C:\Users\cosgenio\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-debug-utils\index.js:233:1)
      at Object.formatLineNumberPrefix (C:\Users\cosgenio\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-debug-utils\index.js:157:1)

and if I continue, it stops with the following error...

Roles.sol:
> Transaction halted with a RUNTIME ERROR.
  This is likely due to an intentional halting expression, like assert(), require() or revert(). It can also be due to out-of-gas exceptions. Please inspect your transaction parameters and contract code to determine the meaning of this error.

When I delete line A and B, the js listener works fine(printing address). But when I add line A or both, the error happens.

What is making errors now and how can I fix it? How can I create new contract inside another contract?

  • What is Token in Run.js? Typically it should be a contract instance whose methods are called like instance.createTrade.call(params). Check out this question for more info. – sfmiller940 Apr 1 at 19:52

Your Answer

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

Browse other questions tagged or ask your own question.