v1
This commit is contained in:
parent
15cc9a6976
commit
2568a215e9
|
|
@ -11,4 +11,10 @@ pub enum ErrorCode {
|
||||||
pub enum BettingError {
|
pub enum BettingError {
|
||||||
#[msg("Bet is not filled yet!")]
|
#[msg("Bet is not filled yet!")]
|
||||||
BetNotFilled,
|
BetNotFilled,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[error_code]
|
||||||
|
pub enum BetError {
|
||||||
|
#[msg("The winner must be either the owner or the joiner of the bet.")]
|
||||||
|
InvalidWinner,
|
||||||
}
|
}
|
||||||
36
programs/bets/src/instructions/close_bet.rs
Normal file
36
programs/bets/src/instructions/close_bet.rs
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
use anchor_lang::prelude::*;
|
||||||
|
use crate::{error::BetError, *};
|
||||||
|
|
||||||
|
pub fn close(ctx: Context<CloseBet>, winner:Pubkey)->Result<()>{
|
||||||
|
let bet_vault = &mut ctx.accounts.bet_vault;
|
||||||
|
require!(
|
||||||
|
bet_vault.owner == winner || bet_vault.joiner == winner,
|
||||||
|
BetError::InvalidWinner
|
||||||
|
);
|
||||||
|
|
||||||
|
let bets_list = &mut ctx.accounts.bets_list;
|
||||||
|
// Remove the bet_vault public key from the list
|
||||||
|
bets_list.bets.retain(|&bet| bet != bet_vault.key());
|
||||||
|
|
||||||
|
msg!("Bet {} closed by {}", bet_vault.key(), winner);
|
||||||
|
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Accounts)]
|
||||||
|
pub struct CloseBet<'info>{
|
||||||
|
#[account(mut)]
|
||||||
|
pub bets_list: Account<'info, BetsList>,
|
||||||
|
|
||||||
|
#[account(mut, close=winner)]
|
||||||
|
pub bet_vault: Account<'info, BetVault>,
|
||||||
|
|
||||||
|
#[account(mut)]
|
||||||
|
pub winner: SystemAccount<'info>,
|
||||||
|
|
||||||
|
#[account(mut)]
|
||||||
|
pub payer: Signer<'info>,
|
||||||
|
pub system_program: Program<'info, System>
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,7 @@ pub fn join(ctx: Context<JoinBet>, _game_id:String) ->Result<()>{
|
||||||
|
|
||||||
msg!("Joined bet {}!", bet_vault.key());
|
msg!("Joined bet {}!", bet_vault.key());
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Accounts)]
|
#[derive(Accounts)]
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,7 @@ pub mod create_bet;
|
||||||
pub use create_bet::*;
|
pub use create_bet::*;
|
||||||
|
|
||||||
pub mod join_bet;
|
pub mod join_bet;
|
||||||
pub use join_bet::*;
|
pub use join_bet::*;
|
||||||
|
|
||||||
|
pub mod close_bet;
|
||||||
|
pub use close_bet::*;
|
||||||
|
|
@ -19,15 +19,15 @@ pub mod bets {
|
||||||
initialize_bets_list::init(ctx)
|
initialize_bets_list::init(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_bet(ctx: Context<CreateBet>, wager:u64, game_id:String)-> Result<()>{
|
pub fn create_bet(ctx: Context<CreateBet>, wager:u64, game_id:String, nonce:u64)-> Result<()>{
|
||||||
let clock = Clock::get().unwrap();
|
|
||||||
let seed = clock.unix_timestamp as u64;
|
|
||||||
let nonce = seed % 100000;
|
|
||||||
|
|
||||||
create_bet::create(ctx, wager,game_id,nonce)
|
create_bet::create(ctx, wager,game_id,nonce)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn join_bet(ctx: Context<JoinBet>, game_id:String) -> Result<()>{
|
pub fn join_bet(ctx: Context<JoinBet>, game_id:String) -> Result<()>{
|
||||||
join_bet::join(ctx, game_id)
|
join_bet::join(ctx, game_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn close_bet(ctx:Context<CloseBet>, winner:Pubkey)->Result<()> {
|
||||||
|
close_bet::close(ctx, winner)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user