Search code examples
rustsolana

Solana Rust How to get return value from current CPI function call?


From Anchor's tutorial https://project-serum.github.io/anchor/tutorials/tutorial-3.html#return-values, I know currently Solana has no way to return values from CPI...

But for porting this code from Solidity:

uint256 sharesAdded = IStrategy(addr1).deposit(sender, wantAmt);
uint256 sharesRemoved = IStrategy(addr1).withdraw(pid, wantAmt);

I need to get the return values "sharesAdded" and "sharesRemoved" from another Solana program and that value is specific to current function call, then save this value into another account... Do I have to break the above Solidity code into two function calls in Solana?

  1. Save the return value in a program account
  2. Make a second function call to get it, while hoping this value has not been changed between my 1st and 2nd function call

Is this approach / workaround correct?


Solution

  • one workaround:

    Step1: Fetch data from the target account where the target sol program saves its state variables

    Step2: Calculate the return values in your own sol program, assuming the data from step1 have not been changed