Question calling a function of a smart contract

I have this working for the transfer event (don’t worry it’s on Ropsten)


      let options = {
        type: "erc20",
        tokenId: "",
        amount: Moralis.Units.Token(ntoken, 18), 
        receiver: eth2,
        contractAddress: "0x294e1BFd460053C77B991A3ed22482060b54278A"}
  
        let result = await (Moralis.Web3 as any).transfer(options);
        if(result){... etc....

I need an example of how to call a simpler method like totalSupply or BalanceOf - (the standard openzeppelin functions on same contract ? Can I use moralis for this too, ? Or should I use web3?

you can use runContractFunction for that https://docs.moralis.io/moralis-server/web3-sdk/native#runcontractfunction

executeFunction would also work but it will need web3 to be enabled

this will probably work. I still can’t figure out where to get the ethAddress of signed into metamask account

there should be various ways to get that address, do you have access to a web3 or etherjs instance?

doesnt this

 const Web3Api = useMoralisWeb3Api();

get me that instance ?

Because metamask isn’t injectedWeb3 anymore right ? Doesn’t this mess things up ?

what is not web3, that is web3api

So I will have to add another library , just to get users eth address ?

Hey if you’re using React, you can use this

cosnt { account } = useMoralis();

to get the current metamask/web3 wallet address connected :raised_hands:

yes ! thanks to you guys both

1 Like
const { account } = useMoralis();

givesme this error

Property ‘account’ does not exist on type ‘MoralisContextValue’.ts(2339)

and yet all this works fine
const {
authenticate,
isAuthenticated,
logout,
} = useMoralis();

I can get this info from JSON.stringify(user);

{"username":"yMWhe6Tlk2nlTnzg5eIz6CPW5","authData":{"moralisEth":{"id":"0x63a22a955534635e612765a50febfc3759eb81b9","signature":"0x0643a83b6bfdbb1811a5251aa8a15cd44ac4f31c6bee7ec6bb7c5d1ac36540e5110328375e19e702e934040c09b09335d9ce752f600aec22ee25bfe277aa3ca41b","data":"Moralis Authentication\n\nId: raQNzqEd2flUnZWcMplhSZzCBps4cUNeFkgEb1qZ:1643975685827"}},"createdAt":"2021-12-12T05:49:07.313Z","updatedAt":"2022-02-04T11:54:48.957Z","accounts":["0x63a22a955534635e612765a50febfc3759eb81b9"],"ethAddress":"0x63a22a955534635e612765a50febfc3759eb81b9","ACL":{"jaWgBM7QtrssvPBJ5QTjxOJg":{"read":true,"write":true}},"sessionToken":"r:22733931f624a46708ca0fadc594093a","objectId":"jaWgBM7QtrssvPBJ5QTjxOJg"}

Any ideas on how I can extract the ethAddress ?having a tough time

which version of the SDK are you using?

I do not even know how to find that info.

I got it using this code

let puser =  JSON.stringify(user);

     let uArray  = puser.split(",");
     for (let element of uArray) 
     {     
       console.log("--->" + element);
     }

you can check in your package.json, also did you enableWeb3 in your app before calling this?

Not so sure, it looks quite complicated that you stringify it, but if it works then great :raised_hands: