2

Suppose I am developing an application that allows users to input their current portfolio amount and the annual percentage gains they receive. In Solidity, I can create a function called get_portfolio, which calculates the return percentage based on the current date and the growth rate entered by the user. Then, the function adds the calculated return value to the original amount and returns it.

However, in Solana, instructions cannot return data. Therefore, if I retrieve a user's on-chain account data for their portfolio, it will only show the original portfolio amount that the user entered along with the growth rate. It will not show the current portfolio balance with the added the amount they got from growth rate calculation. To include the user's growth amount in the data, I need to create a separate instruction that updates the user's on-chain data with the growth return amount based on when it's called according to the yearly return initially entered. Then, I can fetch the updated user data account.

The problem with this approach is that I don't want to rely on the user/client to call the update instruction each time before getting the latest portfolio balance, if the instruction is not called, then the data will not have the returned gains the next time it is fetched. What could be a potential solution for this issue?

2 Answers 2

3

So i'm not super familiar with solidity, but there the state is in the program right? On solana its in the accounts. So you would have a PDA with the account balance i guess. You could then in the client listen to that account via a websocket onAccountChange RPC function and whenever the player logs in in the client you would call update on your program and you would get the updated onchain value pushed directly.

But it sounded like you don't want to rely on the clients to call something. So other people will write the clients?

Then a possible onchain solution would for example be clockwork.xyz and have a thread which calls these update account instruction in a certain time interval or when another account changes.

But actually the thing you are trying to do could also be done with a next.js api that you host on vercel and just fetch the onchain data there and do the calculation on top in js and return the calculated value.

Edit: You could probably also log what you want and then parse the transaction logs like so: https://docs.solana.com/api/http#gettransaction

2
  • Yes there's a PDA with account balance. And yes, I don't want a client side solution because then, we can just get the balance from chain through our program and just perform the calculations to get the return rate on client directly instead of having to call a seperate update function first to update the balance and then fetch it. Can you share the clockwork thread? That might help. Also, I don't want to build something like this practically. I just gave an example to understand what could be the solution to this sort of problem, since program instructions in Solana do not return data.
    – Gmer
    May 12, 2023 at 0:01
  • Clockwork an open source program which lets you trigger transactions on certain triggers: docs.clockwork.xyz/developers/threads/triggers here is some examples: github.com/clockwork-xyz/examples and here a video: youtube.com/watch?v=ax0Si3Vkvbo What you could do to get a result from a program is logging it and parsing the transaction logs in the client, but feels a bit hacky. quicknode.com/guides/solana-development/transactions/…
    – Jonas H.
    May 12, 2023 at 0:12
0

You can implement onchain event trigger in your smart contract. In client side, you can fetch target account state in the first load and then it will listen to the onchain event in realtime to update initially loaded account state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.