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

For much larger games the authority (or permissionless) oracle may want to be resized. #30

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/cli/src/matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ programCommand("create_or_update_oracle")
tokenTransferRoot: config.oracleState.tokenTransferRoot,
tokenTransfers: config.oracleState.tokenTransfers,
space: config.space ? new BN(config.space) : new BN(150),
resize: config.resize ? new BN(config.resize) : new BN(150),
finalized: config.oracleState.finalized,
});
});
Expand Down
52 changes: 52 additions & 0 deletions rust/matches/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub struct CreateOrUpdateOracleArgs {
finalized: bool,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct ResizeOracleArgs {
resize: u64,
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct DrainOracleArgs {
seed: Pubkey,
Expand Down Expand Up @@ -95,6 +99,41 @@ pub mod matches {

use super::*;

pub fn resize_oracle<'a, 'b, 'c, 'info>(
ctx: Context<'a, 'b, 'c, 'info, ReizeOracle<'info>>,
args: ResizeOracleArgs,
) -> Result<()> {

let win_oracle = &mut ctx.accounts.oracle;

let win_oracle_account = win_oracle.to_account_info();

if args.resize as usize > win_oracle_account.data.borrow().len() {
let system_program = &ctx.accounts.system_program;
let payer = &ctx.accounts.payer;
let payer_account = payer.to_account_info();
let new_size = args.resize as usize;

let rent = Rent::get()?;
let new_minimum_balance = rent.minimum_balance(new_size);

let lamports_diff = new_minimum_balance.saturating_sub(win_oracle_account.lamports());
invoke(
&system_instruction::transfer(payer_account.key, win_oracle_account.key, lamports_diff),
&[
payer_account.clone(),
win_oracle_account.clone(),
system_program.to_account_info().clone(),
],
)?;

win_oracle_account.realloc(new_size, false)?;

}

Ok(())
}

pub fn create_or_update_oracle<'a, 'b, 'c, 'info>(
ctx: Context<'a, 'b, 'c, 'info, CreateOrUpdateOracle<'info>>,
args: CreateOrUpdateOracleArgs,
Expand All @@ -121,6 +160,7 @@ pub mod matches {

win_oracle.finalized = finalized;
win_oracle.token_transfer_root = token_transfer_root.clone();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this newline

win_oracle.token_transfers = token_transfers.clone();

return Ok(());
Expand Down Expand Up @@ -755,6 +795,18 @@ pub struct LeaveMatch<'info> {
token_program: Program<'info, Token>,
}

// https://github.com/raindrops-protocol/raindrops/pull/27

#[derive(Accounts)]
#[instruction(args: ResizeOracleArgs)]
pub struct ResizeOracle<'info> {
#[account(mut, seeds=[PREFIX.as_bytes(), payer.key().as_ref(), args.seed.as_ref()])]
oracle: Account<'info, WinOracle>,
#[account(mut)]
payer: Signer<'info>,
system_program: Program<'info, System>,
rent: Sysvar<'info, Rent>,
}
/// While not required to be an account owned by this program, we provide an easy
/// set of endpoitns to create oracles using the program if you don't want to do it yourself.
#[derive(Accounts)]
Expand Down