[英]React get metamask latest transaction status
我正在使用它,通过metamask在智能合约中进行智能合约中的函数调用:
export default class EthereumForm1 extends Component {
constructor (props) {
super (props);
const MyContract = window.web3.eth.contract(ContractABI);
this.state = {
ContractInstance: MyContract.at('ContractAddress')
}
this.doPause = this.doPause.bind (this);
}
doPause() {
const { pause } = this.state.ContractInstance;
pause (
{
gas: 30000,
gasPrice: 32000000000,
value: window.web3.toWei (0, 'ether')
},
(err) => {
if (err) console.error ('Error1::::', err);
console.log ('Contract should be paused');
})
}
我想要的是通过加载gif运行jQuery代码: $(".Loading").show();
在处理交易时将其删除。 另外,最好在之后的div中添加事务状态,例如在metamask中(通过或拒绝)。
您不需要jquery来执行此操作。 React state可以自动管理进度状态。 您要做的就是调用setState
函数。
class EthereumFrom1 extends React.Component {
state = {
loading: false
};
constructor(props) {
super(props);
this.doPause = this.doPause.bind(this);
}
doPause() {
const {pause} = this.state.ContractInstance;
pause(
{
gas: 30000,
gasPrice: 32000000000,
value: window.web3.toWei(0, 'ether')
},
(err) => {
if (err) console.error('Error1::::', err);
this.setState({loading: true});
})
}
render() {
return (
<div>
{this.state.loading ?
<div className="Loading">
Loading...
</div> : <div>done</div>
}
</div>
)
}
}
帮助我的是每隔几秒钟检查一次交易是否完成,如果可以,则隐藏加载程序。
if (latestTx != null) {
window.web3.eth.getTransactionReceipt(latestTx, function (error, result) {
if (error) {
$(".Loading").hide();
console.error ('Error1::::', error);
}
console.log(result);
if(result != null){
latestTx = null;
$(".Loading").hide();
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.