0

Having issues with a contract creating another contract using Truffle dev environment. Here are my steps -

truffle console
var node = Node.deployed()
node.then(function(instance){node=instance})
var relay = node.createRelay()

Getting this response after createRelay:

{ tx: 
'0x5172bd954f72d31bb73278a10343ddfbd613c4938162b5ae9574b2e173271d03',
  receipt:
   { transactionHash: 
'0x5172bd954f72d31bb73278a10343ddfbd613c4938162b5ae9574b2e173271d03',
     transactionIndex: 0,
     blockHash: 
'0xcae09bb248baf2bc0cd9065c629958336874c2ae8c5b1e5fbd5f867600d63c56',
     blockNumber: 40,
     gasUsed: 159039,
     cumulativeGasUsed: 159039,
     contractAddress: null,
     logs: [] },
   logs: [] }

As you can see, contractAddress is null.

Here are my contracts:

pragma solidity ^0.4.2;
import "./Relay.sol";

contract Node {

uint public fee;
address public owner;
address[] activeRelays;

  function Node() {
    owner = msg.sender;
    fee = 10;
  }

  function createRelay () returns (address) {
    address relay = new Relay();
    activeRelays.push(relay);
    return relay;
  }
}

Relay:

pragma solidity ^0.4.2;

contract Relay {
address public owner;

  function Relay() {
    owner = msg.sender;
  }

  function fundRelay() payable returns (bool) {

  }

  function releaseFunds () {

  }
}

Thank you for any help.

0

Missing a step. Simple way forward:

First, address[] public publicRelays; // easy access with free getter using "public"

Second, it's normal to get a transaction hash when you submit a transaction. We don't expect a result. Ergo, we won't get the contract address that way.

Instead, wait for the transaction to be mined. At the outset, just watch your node/miner. When you're sure a block has been mined, then it follows that you should then be able to:

> var relay;
> node.publicRelays.call(0).then(function(response) { relay=response; });
> relay
0x123...

Meaning, 1) interrogate the publicRelay() getter and ask or row 0 (because it's an array). 2) Wait for the response ... 3) see the response.

Hope it helps.

  • 1
    Rob is totally right, just let me add why: Transactions return only transaction IDs. So even though you've set ` createRelay ` to return an address, you will indeed just get the tx ID back. Returning data from a function outside of the EVM works only with calls (reading), whereas functions within contracts can get data returned. – Nikita Fuchs Apr 16 '17 at 11:57

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.