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’?