[SOLVED] How to interact with variables of smart contract

Can anyone help me that how we can interact with variables that are set public in smart contact using Moralis.

You could try to query the smart contract for those variables if the contract has functions that returns that info.

you can try to use Moralis.executeFunction

https://docs.moralis.io/moralis-dapp/web3/web3#executefunction

you will need to know the ABI for that contract in order to know what functions to call

bro by using executefunction we can only interact with the functions of smart contract? Isn’t it. But i am trying to interact with the variables of smart contract. Hope you understand!

can you share me the documentation which you are talking about?

You need to call it as read function as shown in this doc
https://docs.moralis.io/moralis-dapp/web3/web3#executefunction

Inplace of the function name, it will be the public variable name.
This is an example, where I am reading the public variable named data.

const options = {
  contractAddress: "0x36d53FB0c43a124a7b0f4a23E80C1a3980E67dCB",
  functionName: "data",
  abi: [
	{
		"inputs": [],
		"name": "data",
		"outputs": [
			{
				"internalType": "string",
				"name": "",
				"type": "string"
			}
		],
		"stateMutability": "view",
		"type": "function"
	}
],
};
const transaction = await Moralis.executeFunction(options);
console.log(transaction)

^As an example, the following uses the wETH contract to get its public name variable of “Wrapped Ether”:

const readOptions = {
  contractAddress: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
  functionName: 'name',
  abi: ABI,
};

const message = await Moralis.executeFunction(readOptions);
console.log(message);

You can also use the runContractFunction API.

Thank you bro it works. Thanks a lot.

just to know, this is a call i need to replicate for each public variable on the contract?

Yes, you have to call it for every variable and update it for every public variable

great! thanks for the answer. just one more… can i trust on V1 apis? or they are going to be deprecated?

what do you mean with v1 apis? in this case it is a call that uses current wallet like metamask with Moralis.executeFunction