Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
Catch transaction rejection- moralis

Hi I am working with Moralis using the 1inch plugin. I am having a hard time understanding where to catch a transaction rejection error. IE when I reject the transaction I would like to give a message of sorts. Thanks.

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,
      })
    }

and this is the error

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:
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to edit your code a bit : add catch with then. Here is a example:

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));
        }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda