Looking up DEX contract name from address

So I’d like to look up a name from from an address for…

...reasons
import { useEffect, useState } from "react";
import { useMoralis } from "react-moralis";

const emptyList = [];

export const useTransactions = (props) => {
  const { isAuthenticated, Moralis, user } = useMoralis();
  const [address, setAddress] = useState("0x");
  const [Txs, setTxs] = useState(emptyList);
  const [isLoading, setIsLoading] = useState(true);
  console.groupCollapsed("useTransactions");
  console.log(
    isAuthenticated !== "0x"
      ? address + " is authenticated."
      : "Unauthenticated."
  );

  useEffect(() => {
    if (isAuthenticated) {
      setAddress(user.attributes[props.chain + "Address"]);
      Moralis.Web3.getTransactions({ usePost: true }).then((userTrans) => {
        let newTxs = userTrans.map((Tx) => {
          const output = { ...Tx };
          switch (address) {
            case Tx.from_address:
              output.counterparty = Tx.to_address;
              output.amount = -1 * parseFloat(Tx.value);
              break;
            case Tx.to_address:
              output.counterparty = Tx.from_address;
              output.amount = 1 * parseFloat(Tx.value);
              break;
            case undefined:
              output.counterparty = undefined;
              output.amount = undefined;
              break;
            default:
              output.counterparty = null;
              output.amount = null;
              break;
          }
          Moralis.Web3.eth
            .contract({ address: output.counterparty })
            .then((pinger) => {
              if (pinger) {
                output.counterparty = pinger.name();
              }
            });
          return output;
        });
        setTxs(newTxs);
        setIsLoading(false);
      });
    } else {
      setTxs(emptyList);
      setIsLoading(true);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [Moralis.Web3, isAuthenticated]);

  console.log(isLoading ? "Transactions loading..." : "Returning Txs: ", Txs);
  console.groupEnd();

  return { Txs, isLoading };
};

I’d like to write this:

Moralis.Web3.eth
  .contract({ address: <some DEX address>})
  .then((pinger) => {
    if (pinger) {
       output.dex = pinger.name();
    }
});

But Unhandled Rejection (TypeError): Cannot read property 'contract' of undefined says this is not the way.

Complication: I expect I’m looking at DEX addresses like 1Inch or Uniswap. The CoinGecko API only identifies token contracts. It brings back zilch on all the addresses in my Moralis.Web3.getTransactions() output.

Anybody know ‘the way’?

You should provide the ABI for getting acces to the contract info.

Take a look at web3.eth.Contract

Ok so if I presume DEX’s are an ERC20-derived species and I add:

import ERC20ABI from "../data/ERC20.json";

So I can:

 Moralis.Web3.eth
  .contract(ERC20ABI, output.counterparty)
  .then((pinger) => {
    if (pinger) {
      output.counterparty = pinger.name();
   }
});

I still get
Unhandled Rejection (TypeError): Cannot read property 'contract' of undefined

Which can mean one of two things:

  1. Web3 doesn’t have a .eth.
  2. The .contract signature is wrong.
  3. There is no spoon. Er…Moralis.Web3.

I tried asking nicely:

Moralis.Web3.eth
    .contract({ jsonInterface: ERC20ABI, address: output.counterparty })

to no avail. Capitalizing ‘Contract’ doesn’t work either. Nix on .Eth too, or omitting .eth altogether.

Thoughts?

1 Like

Hi @TheBubbleGuy

In React you should use web3 from useMoralis(). But first you need to enable web3.

const EnableWeb3 = ({user, score}) => {
  const { web3, enableWeb3, isWeb3Enabled, isWeb3EnableLoading, web3EnableError } = useMoralis()

  if(isWeb3Enabled){
    return null
  }

  return <div>
    {web3EnableError && <ErrorMessage error={web3EnableError} />}
    <button onClick={() => enableWeb3()} disabled={isWeb3EnableLoading}>Enable web3</button>
  </div>
}

Affter that you can use the web3 functionality:

const contract = new web3.eth.contract(ERC20ABI, output.counterparty);
//do what you want with the  contract;

I didn’t understand the logic a little with pinger.

Hope this helps :wink:

1 Like