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
3 changes: 1 addition & 2 deletions js/cli/src/matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ programCommand("drain_oracle")
//@ts-ignore
const config = JSON.parse(configString);

if (config.resize === undefined) {
if (!config.resize) {
throw new Error("Must specify a new size");
}
await anchorProgram.resizeOracle({
Expand All @@ -431,7 +431,6 @@ programCommand("drain_oracle")
: walletKeyPair.publicKey,
seed: config.oracleState.seed,
resize: new BN(config.resize),

});
});

Expand Down
2 changes: 0 additions & 2 deletions js/lib/src/contract/matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,6 @@ export class MatchesInstruction {

async resizeOracle(
args: ResizeOracleArgs,
_accounts = {},
_additionalArgs = {}
) {
const [oracle, _oracleBump] = await getOracle(
new web3.PublicKey(args.seed),
Expand Down
70 changes: 33 additions & 37 deletions rust/matches/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,41 +99,6 @@ pub mod matches {

use super::*;

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

let win_oracle = &mut ctx.accounts.winOracle;

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 Down Expand Up @@ -166,6 +131,37 @@ pub mod matches {
return Ok(());
}

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove extra newline

let win_oracle_account = &mut ctx.accounts.win_oracle.to_account_info();

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


Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove these two new lines

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_match<'a, 'b, 'c, 'info>(
ctx: Context<'a, 'b, 'c, 'info, CreateMatch<'info>>,
args: CreateMatchArgs,
Expand Down Expand Up @@ -798,8 +794,8 @@ pub struct LeaveMatch<'info> {
#[derive(Accounts)]
#[instruction(args: ResizeOracleArgs)]
pub struct ResizeOracle<'info> {
#[account(constraint=winOracle.key() == match_instance.win_oracle)]
winOracle: UncheckedAccount<'info>,
#[account(address == match_instance.win_oracle)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
#[account(address == match_instance.win_oracle)]
#[account(address = match_instance.win_oracle)]

win_oracle: UncheckedAccount<'info>,
#[account(mut, seeds=[PREFIX.as_bytes(), match_instance.win_oracle.as_ref()], bump=match_instance.bump)]
match_instance: Account<'info, Match>,
#[account(mut)]
Expand Down