捕获交易拒绝-moralis

Posted

技术标签:

【中文标题】捕获交易拒绝-moralis【英文标题】:Catch transaction rejection- moralis 【发布时间】:2021-12-05 01:36:52 【问题描述】:

您好,我正在使用 1inch 插件与 Moralis 合作。我很难理解在哪里捕获交易拒绝错误。 IE 当我拒绝交易时,我想给一些信息。谢谢。

async function trySwap() 
      let address = Moralis.User.current().get("ethAddress");
      let amount = Number(fromAmount) * 10**fromTokenDecimals;
      if (tokenSymbol !== "ETH") 
        const allowance = await Moralis.Plugins.oneInch.hasAllowance(
          chain: "eth", // The blockchain you want to use (eth/bsc/polygon)
          fromTokenAddress: tokenAddress, // The token you want to swap
          fromAddress: address, // Your wallet address
          amount: amount,
        );
        if (!allowance) 
          await Moralis.Plugins.oneInch.approve(
            chain: "eth", // The blockchain you want to use (eth/bsc/polygon)
            tokenAddress: tokenAddress, // The token you want to swap
            fromAddress: address, // Your wallet address
          );
        
      
      try 
        await doSwap(address, amount);
        alert("Swap Complete");
       catch (error) 
        console.log(error)
        alert('Insufficient ETH')
      
    
    
    function doSwap(userAddress, amount) 
      return Moralis.Plugins.oneInch.swap(
        chain: "eth", // The blockchain you want to use (eth/bsc/polygon)
        fromTokenAddress: tokenAddress, // The token you want to swap
        toTokenAddress: totokenAddress, // The token you want to receive
        amount: amount,
        fromAddress: userAddress, // Your wallet address
        slippage: 1,
      )
    

这是错误

MetaMask - RPC Error: MetaMask Tx Signature: User denied transaction signature. 
Object  code: 4001, message: "MetaMask Tx Signature: User denied transaction signature." 
inpage.js:1:51499
Uncaught (in promise) 
Object  code: 4001, message: "MetaMask Tx Signature: User denied transaction signature.", stack:

【问题讨论】:

【参考方案1】:

您需要稍微编辑一下您的代码:添加catchthen。这是一个例子:

async function trySwap(params) 
        const  fromToken, fromAmount, chain  = params;
        const amount = Moralis.Units.Token(fromAmount, fromToken.decimals).toString();
        if (fromToken.address !== "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") 
          await Moralis.Plugins.oneInch
            .hasAllowance(
              chain, // The blockchain you want to use (eth/bsc/polygon)
              fromTokenAddress: fromToken.address, // The token you want to swap
              fromAddress: walletAddress, // Your wallet address
              amount,
            )
            .then(async (allowance) => 
              console.log(allowance);
              if (!allowance) 
                await Moralis.Plugins.oneInch.approve(
                  chain, // The blockchain you want to use (eth/bsc/polygon)
                  tokenAddress: fromToken.address, // The token you want to swap
                  fromAddress: walletAddress, // Your wallet address
                );
              
            )
            .catch((e) => alert(e.message));
        

【讨论】:

谢谢,这正是我所需要的!

以上是关于捕获交易拒绝-moralis的主要内容,如果未能解决你的问题,请参考以下文章

Paypal 商家接受或拒绝每笔交易

让交易被拒绝出售交易

信用卡交易是不是会因测试交易而被拒绝?

Braintree Paypal 交易处理器被拒绝(货币代码无效)

是否有可能在 Promise.all 中捕获所有被拒绝的承诺? [复制]

接受/拒绝未捕获的对话信号