2

Is it possible to return a custom value from a program function and capture this data in the client side code?

//Anchor function
pub fn new_item(ctx: Context<Item>, id: String) -> Result<String> {
   Ok("Custom return value");
}


//Client side
let customValue = await PROGRAM.methods.newItem()
            .accounts({
                item: itemPDA,
                user: user.publicKey,
            })
            .rpc();

console.log(customValue);

1 Answer 1

4

Yes! You can use set_return_data to write a slice of raw bytes to the return data.

solana_program::program::set_return_data(b"Custom return value");

When using the RPC's getTransaction method, you'll get the return data as a base64-encoded string, which you can then decode into the value, in this case a string.

You can find the return data in the transaction's meta.

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.