Add some methods and datastructures to contract implementation

This commit is contained in:
jahooma 2021-12-08 11:26:12 -06:00
parent 634c0af85b
commit eb3fed171d

View File

@ -5,39 +5,88 @@ declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod dpm {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> ProgramResult {
pub fn create_contract(ctx: Context<CreateContract>, props: NewContractProps) -> ProgramResult {
let contract = &mut ctx.accounts.contract;
contract.bets = 0;
Ok(())
}
pub fn add_bet(ctx: Context<AddBet>) -> ProgramResult {
let contract = &mut ctx.accounts.contract;
contract.bets += 1;
pub fn place_bet(ctx: Context<PlaceBet>, props: BetProps) -> ProgramResult {
Ok(())
}
}
}
#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
pub struct NewContractProps {
pub id: String,
pub creator_id: String,
pub question: String,
pub description: String,
pub seed_amounts: SeedAmounts,
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = user, space = 9000)]
pub struct CreateContract<'info> {
#[account(init, payer = creator, space = 9000)]
pub contract: Account<'info, Contract>,
#[account(mut)]
pub user: Signer<'info>,
pub creator: Signer<'info>,
pub system_program: Program <'info, System>,
pub system_program: Program<'info, System>,
}
#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
pub struct BetProps {
pub amount: u64,
pub user_id: String,
pub outcome: BetOutcome,
}
#[derive(Accounts)]
pub struct AddBet<'info> {
#[account(mut)]
pub contract: Account<'info, Contract>,
pub struct PlaceBet<'info> {
#[account(mut)]
pub contract: Account<'info, Contract>,
}
#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
pub enum BetOutcome {
Yes,
No,
}
#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
pub enum ResolutionOutcome {
Yes,
No,
Cancel,
}
#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
pub struct SeedAmounts {
yes: u64,
no: u64,
}
#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
pub struct Resolution {
time: u64,
outcome: ResolutionOutcome,
}
#[account]
pub struct Contract {
pub bets: u64,
pub id: String,
pub creator_id: String,
pub question: String,
pub description: String,
pub seed_amounts: SeedAmounts,
pub created_time: u64,
pub last_updated_time: u64,
pub resolution: Option<Resolution>
// pub bets: Vec<Bet>
}