1inch swap gas fee

Hi moralis team, i was using the 1inch moralis api to develop a swap, but i cant figure out how to show gas fees of transactions using react-moralis, can someone help me on this?

How are you using 1inch in code for swap transactions?

The gas fee will be available if you log the transaction receipt as you make a transaction e.g. with const receipt = await Moralis.Plugins.oneInch.swap({.

Alternatively for past transactions, you can use getTransaction to look up transaction hashes to get the gas.

import { useMoralis } from "react-moralis";
import { Button, Fade } from "@chakra-ui/react";
import { useOneInchSwap } from "react-moralis";
const SwapButton = ({
  fromToken,
  fromAmount,
  toToken,
  toAmount,
  selectedChain,
}) => {
  const { swap, data, error } = useOneInchSwap({
    chain: selectedChain,
    fromToken: fromToken,
    toToken: toToken,
    fromAmount: fromAmount,
    slippage: 1,
  });
  const { user, authenticate } = useMoralis();

  return (
    <Fade in={fromToken && fromAmount && toToken}>
      <Button
        onClick={user?.id ? swap : authenticate}
        mt={toToken && toAmount ? "10" : "10"}
        isFullWidth={true}
        backgroundColor="#00ffff"
        color="#2a2c32"
        padding="25px"
      >
        {user?.id ? "Swap" : "Connect Wallet"}
      </Button>
    </Fade>
  );
};

Can you please tell me how would i be able to display gas in this ?

You get an estimated gas when you get quotes for the tokens to swap. After the transaction is completed, you also get the gas used too. Logging the transaction receipt as @glad mentioned will show that

yea, but i dont get how to implement that function in react as it is different in react. can you tell me what code I would have to use in reactjs and what values would i have to pass?

if i also need to import something for it to work, please mention that too. Thank you!

You should get that in the data returned from the quote and also the data from the swap as you have in your code. You can log both data and get the values from there

Hi, thank you so much for helping out. I just had one more question, how can we use the 1inch plugin or moralis api to check the balance in userโ€™s metamask and block his transaction (by showing he has insufficient amount) if the amount entered is less than his balance?

You can get the user native balance here. Also when the swap fails, thereโ€™re some information regarding the reason why it failed