Simpelest way to fetch & store state variables and display them on website

Hi,

I’m trying to understand what would be the easiest way to do the following:
My contract has e few state variables that won’t change a lot. Therefore it feels overly complicated to fetch these values every time a user connects to his dashboard.

Do I understand correctly that the easiest way to go about this would be to use the sync and store contract events? This would, if i understand correctly, store the state variables in a local database?

If this is indeed the correct way to go about this, is it required to create an event for every setter function in order to keep the database updated or is there a way to for example fetch the contract variable every few hours?

If I didn’t understand correctly, what would be the best way to store state variables that don’t change often and display them on my react website?

Thanks a lot

I didn’t completely understand the question, how do you get those state variables now?

you can also store them in your server database when you get them somehow and get them from the database on later calls (like using a cache)

Hi,

Thanks for the quick reply. So right now I use a fetch function inside a useEffect with Moralis.executeFunction(stateVariable) inside the component that shows the value. If this is the most efficient way that would be ok, because the function is working. But I was wondering if it would be easier and faster to fetch it from a db instead of the smart contract.

I’m mostly a solidity dev with react knowledge. The serverside thing is where I’m struggling with

if there is an event when that value changes, then you could try to use an event sync, it will be more complicated to do that only to read a state variable from the contract

if you want it to work even when the user doesn’t have metamask connected, that can also be done with Moralis.web3API.native.runContractFunction, or by using a cloud function

Oh that’s great! I was indeed looking for a way to fetch it without metamask connection.

Thank you!

you can find here example on how to use runContractFunction in react
https://docs.moralis.io/moralis-dapp/web3-sdk/native#runcontractfunction

it will use the web3api quota for your account, you may need to use a caching system if you intend to have a lot of users for you application