4

Having a instruction that return a number:

    pub fn get_value(ctx: Context<DataGet>) -> Result<u64> {
        let ret = 0;
         // ... 
        Ok(ret)
    }

then, how can I get that Instruction's return value from JS client after send the transaction?

    const tx = await configProgram.methods
      .getValue()
      .accounts({  ...  })
      .rpc();  // ? how can I get the return value of 'get_value' instruction ?

Is it possible to archive this ?

1 Answer 1

4

On Solana you would usually store data or state in an account and then read that from js after you changed it in a transaction. I would rethink the problem you are having and check if you really need a view function. Usually if you are just calculating something you can probably also do it in the client or if it is a persistent thing you may want to save it in an account.

There are some way on how you can achieve this though. You can for example log a value and then parse the logs or use a view function in case you are using anchor.

.view() 

Here is a similar question answering the anchor part: View function in Solana

1
  • 1
    Thanks. I know I can pass value through log or parse the transaction. But I just expect a simple/easy way to get the return value. .view() is new to me, I will try it later. Commented Mar 26 at 17:30

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.