From 897a55edc55be4e89d90e3922dc3c5a056d12bfc Mon Sep 17 00:00:00 2001 From: Joby Ingram-Dodd Date: Wed, 17 Nov 2021 20:10:37 +0000 Subject: [PATCH] Progress Made the change deployed to devnet. Added placing bets and recording a result. --- Anchor.toml | 6 +- programs/myepicproject/src/lib.rs | 102 ++++++++++++++++++++++++++++-- tests/myepicproject.js | 35 ++++++++++ 3 files changed, 135 insertions(+), 8 deletions(-) diff --git a/Anchor.toml b/Anchor.toml index 00beedb..7c38204 100644 --- a/Anchor.toml +++ b/Anchor.toml @@ -1,11 +1,11 @@ -[programs.localnet] -myepicproject = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS" +[programs.devnet] +myepicproject = "Cq1TSA1obVQZzw2YYxvFN6Q5ia5TYxSbwyZQ9JwQCbBL" [registry] url = "https://anchor.projectserum.com" [provider] -cluster = "localnet" +cluster = "devnet" wallet = "/Users/jobymacbookpro/.config/solana/id.json" [scripts] diff --git a/programs/myepicproject/src/lib.rs b/programs/myepicproject/src/lib.rs index a6d98ce..de44d52 100644 --- a/programs/myepicproject/src/lib.rs +++ b/programs/myepicproject/src/lib.rs @@ -1,6 +1,7 @@ use anchor_lang::prelude::*; -declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); +declare_id!("Cq1TSA1obVQZzw2YYxvFN6Q5ia5TYxSbwyZQ9JwQCbBL"); + #[program] pub mod myepicproject { @@ -9,19 +10,81 @@ pub mod myepicproject { // get a referance to the account let base_account = &mut ctx.accounts.base_account; // initliase total_count. - base_account.total_pools =0; + base_account.total_pools = 0; Ok(()) } - pub fn add_gif(ctx: Context) -> ProgramResult { + // old name which I don't want to change as used in other places function is add_pool + pub fn add_gif(ctx: Context, image_link: String, pool_name: String, pool_desc: String, win_opt:String, close: u32, verify:String, fee:u8) -> ProgramResult { let base_account = &mut ctx.accounts.base_account; + let wins: Vec = win_opt.split(';').map(|s| s.trim().to_string()).collect(); //chars().filter(|c| !c.is_whitespace()).collect() + let pool = PoolStruct{ + pool_id: base_account.total_pools, + image_link: image_link.to_string(), + user_address: *base_account.to_account_info().key, + pool_name: pool_name.to_string(), + pool_balance: 0, + pool_description: pool_desc.to_string(), + win_options: wins, + close_date_time: close, + verify_url:verify.to_string(), + owner_fee: fee, + result: "".to_string(), + closed: false, + entries: Vec::new() + }; + base_account.pool_list.push(pool); base_account.total_pools += 1; Ok(()) } + + pub fn add_result(ctx: Context, pool_id:u32, result:String) -> ProgramResult{ + //TODO; Make this only callable by the pool owner. + let base_account = &mut ctx.accounts.base_account; + let mut i = 0; + let mut found = false; + for p in &base_account.pool_list { + if p.pool_id == pool_id { + found = true; + break; + } + i += 1; + } + if found { + base_account.pool_list[i].closed = true; + base_account.pool_list[i].result = result.to_string(); + }; + Ok(()) + } + + pub fn place_bet(ctx: Context, pool_id:u32, stake_bal:u32, user:String, pred: String) -> ProgramResult { + let base_account = &mut ctx.accounts.base_account; + // TODO: check prediction is one of possible options + // TODO: Add payment to this function + let bet = EntryStruct{ + user: user, + prediction: pred, + stake_bal: stake_bal + }; + let mut i = 0; + let mut found = false; + for p in &base_account.pool_list{ + if p.pool_id == pool_id { + found = true; + break; + } + i += 1; + }; + if found{ + base_account.pool_list[i].pool_balance += stake_bal as u64; + base_account.pool_list[i].entries.push(bet); + } + Ok(()) + } } #[derive(Accounts)] pub struct StartStuffOff<'info> { - #[account(init, payer = user, space= 9000)] + #[account(init, payer = user, space= 10240)] pub base_account: Account<'info, BaseAccount>, #[account(mut)] pub user: Signer<'info>, @@ -33,9 +96,38 @@ pub struct AddGif<'info> { #[account(mut)] pub base_account: Account<'info, BaseAccount>, } +#[derive(Accounts)] +pub struct PlaceBet<'info>{ + #[account(mut)] + pub base_account: Account<'info, BaseAccount>, +} + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct PoolStruct{ + pub pool_id: u32, + pub image_link: String, + pub user_address: Pubkey, + pub pool_name: String, + pub pool_balance: u64, + pub pool_description: String, + pub win_options: Vec, + pub close_date_time: u32, + pub verify_url: String, + pub owner_fee: u8, + pub result: String, + pub closed: bool, + pub entries: Vec +} +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct EntryStruct{ + pub user: String, + pub prediction: String, + pub stake_bal: u32, +} //Tell solana we want to store on this account #[account] pub struct BaseAccount { - pub total_pools: u64, + pub total_pools: u32, + pub pool_list: Vec, } \ No newline at end of file diff --git a/tests/myepicproject.js b/tests/myepicproject.js index 041845f..e4abf54 100644 --- a/tests/myepicproject.js +++ b/tests/myepicproject.js @@ -24,6 +24,41 @@ const main = async() => { let account = await program.account.baseAccount.fetch(baseAccount.publicKey); console.log('👀 Total Pools', account.totalPools.toString()) + + //call add_gif + await program.rpc.addGif("insert image linke here", "Test Pool","This is a test Pool", "option 1; option 2", 9897, "Verify here", 5,{ + accounts: { + baseAccount: baseAccount.publicKey, + } + }); + await program.rpc.addGif("insert image linke here 3", "Test Pool 2","This is a test Pool 2", "2option 1; 2option 2", 789897, "2Verify here", 2,{ + accounts: { + baseAccount: baseAccount.publicKey, + } + }); + await program.rpc.addResult(0,"winner 1",{ + accounts: { + baseAccount: baseAccount.publicKey, + } + }); + await program.rpc.placeBet(1,125,"user", "winner 1", { + accounts: { + baseAccount: baseAccount.publicKey, + } + }) + + account = await program.account.baseAccount.fetch(baseAccount.publicKey); + console.log("Total Pools count ", account.totalPools.toString()) + + console.log("Pool list: ", account.poolList) + + // await program.rpc.addResult(0,"winner 1",{ + // accounts: { + // baseAccount: baseAccount.publicKey, + // } + // }); + + //console.log("Pool list: ", account.poolList) }; const runMain = async () => {