我有以下内容:
onTransfer = async () => {
this.setState({loading: true});
try {
const accounts = await web3.eth.getAccounts();
await this.props.myData.methods.transferBalanceToOwner().send({
from: accounts[0]
});
let contractBalance;
await web3.eth.getBalance(this.props.address).then(function(result) {
contractBalance = web3.utils.fromWei(result);
});
console.log('contract balance: ', contractBalance);
this.setState({ contractBalance });
} catch (error) {
console.log('Transfer to owner error: ',error)
}
this.setState({loading: false});
}
并且我的render()中有以下内容
if (this.state.isOwner) {
ownerUI = (
<Container>
<Divider/>
<p>Contract balance: {this.state.contractBalance}</p>
<Button loading={this.state.loading} onClick={this.onTransfer} primary>Transfer Balance</Button>
</Container>
)
}
由于某些原因,OnTransfer之后合同余额未得到更新。有人可以发现我可能做错了吗? 谢谢!
答案 0 :(得分:0)
使用async/await
的方法不正确。
async/await
,没有然后可以await
块中放入try/catch
语句以进行success/error
检查。正确的方法。
let contractBalance;
try {
//success check
contractBalance = await web3.eth.getBalance(this.props.address)
this.setState({contractBalance});
}
catch (rejectedValue) {
//error check
console.log(rejectedValue)
}