From c4a69b2f5367085846a8ada48d2cbd7ef45ac7d4 Mon Sep 17 00:00:00 2001 From: Jarett Dunn Date: Tue, 16 Aug 2022 21:02:41 -0300 Subject: [PATCH 1/5] try this again; I suck at collaborative dev --- js/cli/src/matches.ts | 1 + rust/matches/src/lib.rs | 52 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/js/cli/src/matches.ts b/js/cli/src/matches.ts index 88047463..bdd8bd9d 100755 --- a/js/cli/src/matches.ts +++ b/js/cli/src/matches.ts @@ -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, }); }); diff --git a/rust/matches/src/lib.rs b/rust/matches/src/lib.rs index c0666a62..57b47e5d 100644 --- a/rust/matches/src/lib.rs +++ b/rust/matches/src/lib.rs @@ -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, @@ -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, @@ -121,6 +160,7 @@ pub mod matches { win_oracle.finalized = finalized; win_oracle.token_transfer_root = token_transfer_root.clone(); + win_oracle.token_transfers = token_transfers.clone(); return Ok(()); @@ -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)] From e85c53c82e688d44ac795e74aaa1200c8745a6b2 Mon Sep 17 00:00:00 2001 From: Jarett Dunn Date: Tue, 23 Aug 2022 11:54:02 +0000 Subject: [PATCH 2/5] changes as req --- .gitpod.yml | 8 +++++ js/cli/src/matches.ts | 30 ++++++++++++++++++- js/lib/src/contract/matches.ts | 53 +++++++++++++++++++++++++++++++++- rust/matches/src/lib.rs | 10 +++---- 4 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 .gitpod.yml diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 00000000..eae2dd77 --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,8 @@ +# This configuration file was automatically generated by Gitpod. +# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file) +# and commit this file to your remote git repository to share the goodness with others. + +tasks: + - init: yarn install + + diff --git a/js/cli/src/matches.ts b/js/cli/src/matches.ts index bdd8bd9d..ab328cbe 100755 --- a/js/cli/src/matches.ts +++ b/js/cli/src/matches.ts @@ -403,6 +403,35 @@ programCommand("drain_oracle") ); }); + programCommand("resize_oracle") + .requiredOption( + "-cp, --config-path ", + "JSON file with match settings" + ) + .action(async (files: string[], cmd) => { + const { keypair, env, configPath, rpcUrl } = cmd.opts(); + + const walletKeyPair = loadWalletKey(keypair); + const anchorProgram = await getMatchesProgram(walletKeyPair, env, rpcUrl); + + if (configPath === undefined) { + throw new Error("The configPath is undefined"); + } + const configString = fs.readFileSync(configPath); + + //@ts-ignore + const config = JSON.parse(configString); + + await anchorProgram.resizeOracle({ + authority: config.oracleState.authority + ? new web3.PublicKey(config.oracleState.authority) + : walletKeyPair.publicKey, + seed: config.oracleState.seed, + resize: config.resize ? new BN(config.resize) : new BN(150), + + }); + }); + programCommand("create_or_update_oracle") .requiredOption( "-cp, --config-path ", @@ -430,7 +459,6 @@ 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, }); }); diff --git a/js/lib/src/contract/matches.ts b/js/lib/src/contract/matches.ts index e8d5a839..ae49c7ab 100644 --- a/js/lib/src/contract/matches.ts +++ b/js/lib/src/contract/matches.ts @@ -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,37 @@ export class MatchesInstruction { signers: [], }; } + + async resizeOracle( + args: ResizeOracleArgs, + _accounts = {}, + _additionalArgs = {} + ) { + 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 +780,22 @@ export class MatchesProgram { signers ); } + + async resizeOracle( + args: ResizeOracleArgs, + _accounts = {}, + _additionalArgs = {} + ) { + 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( diff --git a/rust/matches/src/lib.rs b/rust/matches/src/lib.rs index 57b47e5d..4a3cf87a 100644 --- a/rust/matches/src/lib.rs +++ b/rust/matches/src/lib.rs @@ -100,7 +100,7 @@ pub mod matches { use super::*; pub fn resize_oracle<'a, 'b, 'c, 'info>( - ctx: Context<'a, 'b, 'c, 'info, ReizeOracle<'info>>, + ctx: Context<'a, 'b, 'c, 'info, ResizeOracle<'info>>, args: ResizeOracleArgs, ) -> Result<()> { @@ -795,13 +795,13 @@ 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(constraint=oracle.key() == match_instance.win_oracle)] + 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>, From 683caf50a04aeb2b55d22add1385ea2757b19d06 Mon Sep 17 00:00:00 2001 From: Jarett Dunn Date: Tue, 23 Aug 2022 16:32:13 +0000 Subject: [PATCH 3/5] fixing some suggestions --- .gitignore | 1 + js/cli/src/matches.ts | 5 ++++- rust/matches/src/lib.rs | 6 +++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index a56689a2..6dc063cf 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ .DS_Store .env .idea +.gitpod.yml ./package-lock.json .vscode bin diff --git a/js/cli/src/matches.ts b/js/cli/src/matches.ts index ab328cbe..575134cc 100755 --- a/js/cli/src/matches.ts +++ b/js/cli/src/matches.ts @@ -422,12 +422,15 @@ programCommand("drain_oracle") //@ts-ignore const config = JSON.parse(configString); + if (config.resize === undefined) { + throw new Error("Must specify a new size"); + } await anchorProgram.resizeOracle({ authority: config.oracleState.authority ? new web3.PublicKey(config.oracleState.authority) : walletKeyPair.publicKey, seed: config.oracleState.seed, - resize: config.resize ? new BN(config.resize) : new BN(150), + resize: new BN(config.resize), }); }); diff --git a/rust/matches/src/lib.rs b/rust/matches/src/lib.rs index 4a3cf87a..91fe0d67 100644 --- a/rust/matches/src/lib.rs +++ b/rust/matches/src/lib.rs @@ -104,7 +104,7 @@ pub mod matches { args: ResizeOracleArgs, ) -> Result<()> { - let win_oracle = &mut ctx.accounts.oracle; + let win_oracle = &mut ctx.accounts.winOracle; let win_oracle_account = win_oracle.to_account_info(); @@ -798,8 +798,8 @@ pub struct LeaveMatch<'info> { #[derive(Accounts)] #[instruction(args: ResizeOracleArgs)] pub struct ResizeOracle<'info> { - #[account(constraint=oracle.key() == match_instance.win_oracle)] - oracle: UncheckedAccount<'info>, + #[account(constraint=winOracle.key() == match_instance.win_oracle)] + winOracle: 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)] From 032629c2cde035134d1b038efd93782707421d2b Mon Sep 17 00:00:00 2001 From: Jarett Dunn Date: Tue, 23 Aug 2022 16:32:44 +0000 Subject: [PATCH 4/5] fixing some suggestions --- .gitpod.yml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .gitpod.yml diff --git a/.gitpod.yml b/.gitpod.yml deleted file mode 100644 index eae2dd77..00000000 --- a/.gitpod.yml +++ /dev/null @@ -1,8 +0,0 @@ -# This configuration file was automatically generated by Gitpod. -# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file) -# and commit this file to your remote git repository to share the goodness with others. - -tasks: - - init: yarn install - - From c4dea65eb93b5a80d7f56a594ac001fbe8351611 Mon Sep 17 00:00:00 2001 From: Jarett Dunn Date: Tue, 23 Aug 2022 17:36:07 +0000 Subject: [PATCH 5/5] commit and commit often --- js/cli/src/matches.ts | 3 +- js/lib/src/contract/matches.ts | 2 - rust/matches/src/lib.rs | 70 ++++++++++++++++------------------ 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/js/cli/src/matches.ts b/js/cli/src/matches.ts index 575134cc..c52ff5d4 100755 --- a/js/cli/src/matches.ts +++ b/js/cli/src/matches.ts @@ -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({ @@ -431,7 +431,6 @@ programCommand("drain_oracle") : walletKeyPair.publicKey, seed: config.oracleState.seed, resize: new BN(config.resize), - }); }); diff --git a/js/lib/src/contract/matches.ts b/js/lib/src/contract/matches.ts index ae49c7ab..9fb1302f 100644 --- a/js/lib/src/contract/matches.ts +++ b/js/lib/src/contract/matches.ts @@ -545,8 +545,6 @@ export class MatchesInstruction { async resizeOracle( args: ResizeOracleArgs, - _accounts = {}, - _additionalArgs = {} ) { const [oracle, _oracleBump] = await getOracle( new web3.PublicKey(args.seed), diff --git a/rust/matches/src/lib.rs b/rust/matches/src/lib.rs index 91fe0d67..f512cd09 100644 --- a/rust/matches/src/lib.rs +++ b/rust/matches/src/lib.rs @@ -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, @@ -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<()> { + + 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() { + + + 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, @@ -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)] + 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)]