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);
tokenNames.push(_name);
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(
"exampleTokenName"
).encodeABI();
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),
gasPrice,
gasLimit,
to,
value: toHex(value),
data
}
const tx = new Tx(rawTx);
tx.sign(pvtKey);
const serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', resolve) <---- Only returns the Receipt object no call data
.catch(reject);