我需要访问也更改状态的函数返回的数据。
示例:我在testnet上有一个契约,其中包含一个函数,该函数创建并返回新的ERC20令牌。
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);
}
}如何在从newToken调用contract.methods.createNewToken之后访问返回的web3。我猜我需要某种替代sendSignedTransaction的D4,但我还没有找到。
我目前的做法是:
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);发布于 2019-07-09 10:06:43
如果您发出一个具有名称和地址的事件,那么您只需在挖掘事务时查看ContractInstance.allEvents就会容易得多。
另一个选项是等待挖掘事务并获得结果:等待事务被挖掘并得到结果的正确方法是什么?
https://ethereum.stackexchange.com/questions/72705
复制相似问题