I need access to data returned by function that also changes state.

Example: I have a contract live on testnet with a function that creates and then returns a new ERC20 token.

contract MyContract {
  function createNewToken(string _name) payable returns (address newToken) {
    require(msg.value == 1 ether);
    ERC20 newToken = new ERC20();
    return address(newToken);

How do I get access to the returned newToken after calling contract.methods.createNewToken from web3. I'm guessing I need some kind of call alternative to sendSignedTransaction But I haven't found any.

My current approach:

const nonce = await web3.eth.getTransactionCount(PUB_KEY);
const data = myContract.methods.createNewToken(
const pvtKey = new Buffer.from(PVT_KEY, 'hex');

const gasPrice = toHex(toWei("1", "gwei"));
const gasLimit = toHex((10 ** 6).toString());
const value = toWei("1", "ether");

const rawTx = {
    nonce: toHex(nonce),
    value: toHex(value),

const tx = new Tx(rawTx);

const serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', resolve) <---- Only returns the Receipt object no call data

1 Answer 1


It will be much easier if you emit an event with the name and address, then you can just watch ContractInstance.allEvents when the transaction is mined.

The other option is to wait for the transaction to be mined and get the result: What's the proper way to wait for a transaction to be mined and get the results?

  • Alright I was hoping there was an other way but I'll just go with the event emitting.
    – jasper
    Commented Jul 9, 2019 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.