1

I have written a function in the rust backend that I wish to return a value (I64) from and I have written it as such:

use anchor_lang::prelude::*;
use crate::state::*;

const SLOWSPEED_REWARD: i64 = 50;

#[derive(Accounts)]
pub struct SlowSpeeding<'info> {
    #[account(mut, has_one = wallet_id)]
    pub ship_account: Account<'info, Ship>,
    pub wallet_id: Signer<'info>,
    //Constraint on system_program to make sure it is official SOLANA software
    pub system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<SlowSpeeding>, berth: i64, initial: i64, expected: i64) -> Result<i64> {
    //Accounts called here are deserialized
    let ship: &mut Account<Ship> = &mut ctx.accounts.ship_account;
    let _author: &Signer = &ctx.accounts.wallet_id;

    ship.berth_time = berth;
    ship.initial_berth_time = initial;
    ship.expected_berth_time = expected;

    let mut reward: i64 = 0;
    //Check that slow speeding did occur
    if initial < berth && (expected - berth).abs() >= 1000 {
        //Give a small margin for boat to reach, dont have to be exactly on time
        msg!("Thank you for slow speeding sufficiently!");
        reward = SLOWSPEED_REWARD;
        //Add transaction (fixed amt)

    } else if initial >= berth {
        msg!("Slow speeding did not occur!");

    } else {
        msg!("Partial slow speeding detected!");
        let expected_time_reduction:i64 = expected - initial;
        let actual_time_reduction: i64 = berth - initial;
        //Arrive closer to slow speed time -> get more reward
        let fraction: f64 = (1 as f64) - (actual_time_reduction / expected_time_reduction) as f64;
        reward = (fraction * (SLOWSPEED_REWARD as f64)) as i64;
    }

    Ok(reward)
}

However, on my front-end, when I tag it to a variable such as:

const x = await program.methods.slowSpeeding(....)....

this returns the signature of the transaction instead. Is there a way for my to return the reward variable from my backend? Thank you!

1 Answer 1

1
#[program]

Add this to your above code, where your mentioning the instructions.

So it would be like this.

use anchor_lang::prelude::*;
use crate::state::*;

const SLOWSPEED_REWARD: i64 = 50;

#[derive(Accounts)]
pub struct SlowSpeeding<'info> {
    #[account(mut, has_one = wallet_id)]
    pub ship_account: Account<'info, Ship>,
    pub wallet_id: Signer<'info>,
    //Constraint on system_program to make sure it is official SOLANA software
    pub system_program: Program<'info, System>,
}

#[program]
pub fn handler(ctx: Context<SlowSpeeding>, berth: i64, initial: i64, expected: i64) -> Result<i64> {
    //Accounts called here are deserialized
    let ship: &mut Account<Ship> = &mut ctx.accounts.ship_account;
    let _author: &Signer = &ctx.accounts.wallet_id;

    ship.berth_time = berth;
    ship.initial_berth_time = initial;
    ship.expected_berth_time = expected;

    let mut reward: i64 = 0;
    //Check that slow speeding did occur
    if initial < berth && (expected - berth).abs() >= 1000 {
        //Give a small margin for boat to reach, dont have to be exactly on time
        msg!("Thank you for slow speeding sufficiently!");
        reward = SLOWSPEED_REWARD;
        //Add transaction (fixed amt)

    } else if initial >= berth {
        msg!("Slow speeding did not occur!");

    } else {
        msg!("Partial slow speeding detected!");
        let expected_time_reduction:i64 = expected - initial;
        let actual_time_reduction: i64 = berth - initial;
        //Arrive closer to slow speed time -> get more reward
        let fraction: f64 = (1 as f64) - (actual_time_reduction / expected_time_reduction) as f64;
        reward = (fraction * (SLOWSPEED_REWARD as f64)) as i64;
    }

    Ok(reward)
}

The program field is an macro , which tells the anchor that this section is an instruction section . Via which the frontend will communicate with.

1
  • 1
    I actually already did this in my lib.rs file (this is not the lib.rs file but another file containing the instructions) where I called on the all the handler functions my code would require!
    – Aengus
    Commented Sep 21, 2022 at 3:24

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.