Skip to content

Commit

Permalink
Progress
Browse files Browse the repository at this point in the history
Made the change deployed to devnet. Added placing bets and recording a result.
  • Loading branch information
jobyid committed Nov 17, 2021
1 parent f088d47 commit 897a55e
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Anchor.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
102 changes: 97 additions & 5 deletions programs/myepicproject/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
declare_id!("Cq1TSA1obVQZzw2YYxvFN6Q5ia5TYxSbwyZQ9JwQCbBL");


#[program]
pub mod myepicproject {
Expand All @@ -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<AddGif>) -> 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<AddGif>, 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<String> = 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<AddGif>, 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<AddGif>, 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>,
Expand All @@ -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<String>,
pub close_date_time: u32,
pub verify_url: String,
pub owner_fee: u8,
pub result: String,
pub closed: bool,
pub entries: Vec<EntryStruct>
}
#[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<PoolStruct>,
}
35 changes: 35 additions & 0 deletions tests/myepicproject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 897a55e

Please sign in to comment.