[英]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.