Best practice to add view and pure functions to a deployed contract?

Hello,
I am new to smart contracts and I am learning to develop DApps using truffle and web3 client from my web app.
.
I have already deployed a contract. And I want to extend it with some view functions that the web app will be using.

I was thinking about a solution, but I am not sure if which would be good practice in a real-world project:

  • I am thinking about deploying another contract that has view/pure functions that query the data from the first one.
    But would not deploying such contracts be expensive as it cost ethers in real-world scenarios?

  • Or should I just use web3 and manipulate the data on the client-side?

Does these approaches sounds reasonable, or do you have a better ones?

Thanks

1 Like

I am not sure, but I always make a new contract and I will use only one or two functions to assemble all the data I need.

2 Likes

Hi @motia,

Welcome to the community :wave:

If your existing contract has public state variables, then you could create a wrapper contract to combine/manipulate that data and expose as view/pure functions.

The cost would be the expense of deploying the wrapper contract but this could make your interaction much easier. Gas prices are more reasonable so this could be worth it: https://www.ethgasstation.info/

Note, even read functions have a gas limit, so there will be some restrictions on what you can do.

Another approach, (less decentralized) would be to look at a service like The Graph to read data from your smart contracts.

1 Like

Thanks, @Skyge, I will take this way.

1 Like

Thanks, @abcoathup. I appreciate the extensive answer with the extra tips!

1 Like