Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TicTacToe example: trying to use ctx inside Game #74

Closed
netpoe opened this issue Apr 21, 2022 · 1 comment
Closed

TicTacToe example: trying to use ctx inside Game #74

netpoe opened this issue Apr 21, 2022 · 1 comment

Comments

@netpoe
Copy link

netpoe commented Apr 21, 2022

Following the TicTacToe example, I'm trying to pass ctx to a public method flip of Game:

// instructions/play.rs
pub fn play(ctx: Context<Play>, data: FlipArgs) -> Result<()> {
    ctx.accounts.game.flip(&ctx, data)
}
pub fn flip(&mut self, ctx: Context<Play>, data: FlipArgs) -> Result<()> {
    self.charge_fee(ctx);

    match data.heads_or_tails {
        true => self.play_heads(ctx),
        false => self.play_tails(ctx),
    }
}

fn charge_fee(&self, ctx: Context<Play>) -> Result<()> {
    let player = &ctx.accounts.player;

    if **player.try_borrow_lamports()? < self.static_amount {
        return Err(CoinflipError::InsufficientFunds.into());
    }

    let fee = self.calculate_fee();

    **player.try_borrow_mut_lamports()? -= fee as u64;

    Ok(())
}

However, this results in an error:

anchor_lang::context::Context<instructions::play::Play>
ctx: Context<Play>
anchor_lang::context::Context<instructions::play::Play>
ctx: Context<Play>
mismatched types

expected struct `anchor_lang::context::Context`, found reference

So, how to make ctx work from inside Game in order to access the ctx.accounts.player?

@paul-schaaf
Copy link
Contributor

pls use discord for support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants