-
Notifications
You must be signed in to change notification settings - Fork 25
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
.DS_Store | ||
.env | ||
.idea | ||
.gitpod.yml | ||
./package-lock.json | ||
.vscode | ||
bin | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,7 +119,11 @@ export interface CreateMatchAdditionalArgs { | |
tokenTransferRoot: null; | ||
tokenTransfers: null | AnchorTokenDelta[]; | ||
} | ||
|
||
export interface ResizeOracleArgs { | ||
seed: string; | ||
authority: web3.PublicKey; | ||
resize: BN; | ||
} | ||
export interface CreateOrUpdateOracleArgs { | ||
seed: string; | ||
authority: web3.PublicKey; | ||
|
@@ -538,6 +542,35 @@ export class MatchesInstruction { | |
signers: [], | ||
}; | ||
} | ||
|
||
async resizeOracle( | ||
args: ResizeOracleArgs, | ||
) { | ||
const [oracle, _oracleBump] = await getOracle( | ||
new web3.PublicKey(args.seed), | ||
args.authority | ||
); | ||
|
||
const match = (await getMatch(oracle))[0]; | ||
|
||
return { | ||
instructions: [ | ||
await this.program.methods | ||
.resizeOracle({ | ||
...args, | ||
}) | ||
.accounts({ | ||
oracle, | ||
matchInstance: match, | ||
payer: (this.program.provider as AnchorProvider).wallet.publicKey, | ||
systemProgram: SystemProgram.programId, | ||
rent: web3.SYSVAR_RENT_PUBKEY, | ||
}) | ||
.instruction(), | ||
], | ||
signers: [], | ||
}; | ||
} | ||
} | ||
|
||
export class MatchesProgram { | ||
|
@@ -745,6 +778,22 @@ export class MatchesProgram { | |
signers | ||
); | ||
} | ||
|
||
async resizeOracle( | ||
args: ResizeOracleArgs, | ||
_accounts = {}, | ||
_additionalArgs = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you removed these ones from the |
||
) { | ||
const { instructions, signers } = | ||
await this.instruction.resizeOracle(args); | ||
|
||
await sendTransactionWithRetry( | ||
(this.program.provider as AnchorProvider).connection, | ||
(this.program.provider as AnchorProvider).wallet, | ||
instructions, | ||
signers | ||
); | ||
} | ||
} | ||
|
||
export async function getMatchesProgram( | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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, | ||||||
|
@@ -121,11 +125,43 @@ pub mod matches { | |||||
|
||||||
win_oracle.finalized = finalized; | ||||||
win_oracle.token_transfer_root = token_transfer_root.clone(); | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this newline |
||||||
win_oracle.token_transfers = token_transfers.clone(); | ||||||
|
||||||
return Ok(()); | ||||||
} | ||||||
|
||||||
pub fn resize_oracle<'a, 'b, 'c, 'info>( | ||||||
ctx: Context<'a, 'b, 'c, 'info, ResizeOracle<'info>>, | ||||||
args: ResizeOracleArgs, | ||||||
) -> Result<()> { | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||||||
|
||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||||||
|
@@ -755,6 +791,18 @@ pub struct LeaveMatch<'info> { | |||||
token_program: Program<'info, Token>, | ||||||
} | ||||||
|
||||||
#[derive(Accounts)] | ||||||
#[instruction(args: ResizeOracleArgs)] | ||||||
pub struct ResizeOracle<'info> { | ||||||
#[account(address == match_instance.win_oracle)] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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)] | ||||||
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)] | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename this to
matchInstance
, simplifies the accounts struct below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget about this one