简体   繁体   English

如何设置加载屏幕,直到在 react web3js 上确认交易

[英]How to set loading screen until transaction confirmed on react web3js

I'm quite new to Web3 and React development.我对 Web3 和 React 开发还很陌生。 I want to call the contract function and get a loading screen until the transaction is minted:我想调用合约 function 并获取加载屏幕,直到交易完成:

let transactions = await TokenContract.methods.buy().send({
    from: account,
    value: web3.utils.toWei(inputBuyQty, 'ether')
}, function (err, transactionHash) {
    if (!err) {
        console.log(transactionHash);
        setLoadingScreen(1);
        let receipt = web3.eth.getTransactionReceipt(transactionHash);
        console.log(receipt);
        setLoadingScreen(0);
    }
});

How would I call setLoadingScreen(1) after the request is submitted, and setLoadingScreen(0) after the transaction has been minted?我将如何在提交请求后调用setLoadingScreen(1) ,并在交易生成后调用 setLoadingScreen setLoadingScreen(0)

setLoadingScreen(1);
let reciept;
try {
    reciept = await TokenContract.methods.buy().send({
        from: account,
        value: web3.utils.toWei(inputBuyQty, 'ether')
    });
} catch (err) {
    setLoadingScreen(0);
    // Print error stuff
    return;
}
setLoadingScreen(0);

contract.send resolves when the transaction receipt is available. contract.send在交易收据可用时解决。 Enable the loading screen once the user needs to submit the transaction, and disable it on error or when the receipt is available.一旦用户需要提交交易就启用加载屏幕,并在错误或收据可用时禁用它。

this should help you:这应该可以帮助您:

const hangle = () =>{
    setLoadingScreen(1);
    try {
       let reciept = await TokenContract.methods.buy().send({
            from: account,
            value: web3.utils.toWei(inputBuyQty, 'ether')
        });
    } catch (err) {
       console.log(err)
    } finally{
       setLoadingScreen(0);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM