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:

Hi, please is there any way to get the balance of wallet?

And please where is the full document of the User class

You can create a new forum thread.

Documentation can be found at docs.moralis.io

you can check in the moralis react library

thank you man, I will make a new thread

thank you man, I will check it