Retrieving user address

Ok, I’ve a stupid question I can’t find the answer to. I need to get the current user’s wallet address(s?). I’m trying to simplify the transactions display by filtering out ‘home base’ and adjusting the sign of the value according to if tokens coming or going. But my usrAddress keeps coming back undefined regardless of how I try to spell or punctuate Moralis.User.Address. I’m expecting some function like that to return to me an array of addresses, or even a chain array of arrays of addresses on each chain. But I’m getting nothing. Does this functionality exist somewhere? I can’t find it in the docs.

Here’s some nifty ReactJS hook code on the topic to make me look smarter while asking this really dumb question:

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

const emptyList = [];

export const useTransactions = (props) => {
  const { isAuthenticated, Moralis } = useMoralis();
  const [Txs, setTxs] = useState(emptyList);
  const [isLoading, setIsLoading] = useState(true);
  const usrAddress = Moralis.User.Address;
  console.groupCollapsed("useTransactions");
  console.log(
    isAuthenticated ? usrAddress + " is authenticated." : "Unauthenticated."
  );

  useEffect(() => {
    if (isAuthenticated) {
      Moralis.Web3.getTransactions({ usePost: true }).then((userTrans) => {
        let newTxs = userTrans.map((Tx) => {
          const output = { ...Tx };
          if (Tx.from_address === usrAddress) {
            output.counterparty = Tx.to_address;
            output.amount = -1 * parseFloat(Tx.value);
          } else {
            output.counterparty = Tx.from_address;
            output.amount = parseFloat(Tx.value);
          }
          return output;
        });
        setTxs(newTxs);
        setIsLoading(false);
      });
    } else {
      setTxs(emptyList);
      setIsLoading(true);
    }
  }, [Moralis.Web3, isAuthenticated, usrAddress]);

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

  return { Txs, isLoading };
};

But all it ever gives me is “undefined is authenticated” and a list of nothing but deposits that should be withdraws (and half the time I’m my own counterparty). I see my address in the output. But how do you tell React which one is mine?

Thoughts?

1 Like

Hey @TheBubbleGuy

In React you should use user:

const { user } = useMoralis();

export const useTransactions = (props) => {
const [address, setAddress] = useState();
  useEffect(() => {
    if (isAuthenticated) {
      setAddress(user.attributes.ethAddress);
    }
  }, [isAuthenticated]);
};

Also you can get an array of his addresses:

user.attributes.accounts

One of the ways to get the networks in which there were transactions is to check whether the transactions of tokens from this address were in the required network or not. Usually we use the same ethadress in different networks.

And stop scolding yourself. You are making great progress! :muscle:

1 Like

You can also pass ethAddressas as a prop instead of calling the user.attributes.ethAddress several times.

Ah. “attributes” was the string I was missing in my documents search. Just searching on ‘user address’ does not bring back:
https://docs.moralis.io/getting-started/quick-start#user

This thread evolved from a ‘how to get the user address from Moralis’ to ‘how do you compare user addresses?’. I’m continuing the problem under the ‘JavaScript’ category here: