This commit is contained in:
Sewmina Dilshan 2025-03-31 15:30:51 +05:30
commit 15cc9a6976
22 changed files with 3457 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.anchor
.DS_Store
target
**/*.rs.bk
node_modules
test-ledger
.yarn

7
.prettierignore Normal file
View File

@ -0,0 +1,7 @@
.anchor
.DS_Store
target
node_modules
dist
build
test-ledger

18
Anchor.toml Normal file
View File

@ -0,0 +1,18 @@
[toolchain]
[features]
resolution = true
skip-lint = false
[programs.localnet]
bets = "HxsDuhD7wcPxcMsrYdteMYxkffuwff8HoxhZ7NuFtM37"
[registry]
url = "https://api.apr.dev"
[provider]
cluster = "Localnet"
wallet = "~/.config/solana/id.json"
[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

1952
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

14
Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[workspace]
members = [
"programs/*"
]
resolver = "2"
[profile.release]
overflow-checks = true
lto = "fat"
codegen-units = 1
[profile.release.build-override]
opt-level = 3
incremental = false
codegen-units = 1

12
migrations/deploy.ts Normal file
View File

@ -0,0 +1,12 @@
// Migrations are an early feature. Currently, they're nothing more than this
// single deploy script that's invoked from the CLI, injecting a provider
// configured from the workspace's Anchor.toml.
const anchor = require("@coral-xyz/anchor");
module.exports = async function (provider) {
// Configure client to use the provider.
anchor.setProvider(provider);
// Add your deploy script here.
};

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"license": "ISC",
"scripts": {
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
},
"dependencies": {
"@coral-xyz/anchor": "^0.30.1"
},
"devDependencies": {
"chai": "^4.3.4",
"mocha": "^9.0.3",
"ts-mocha": "^10.0.0",
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0",
"typescript": "^4.3.5",
"prettier": "^2.6.2"
}
}

20
programs/bets/Cargo.toml Normal file
View File

@ -0,0 +1,20 @@
[package]
name = "bets"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"
[lib]
crate-type = ["cdylib", "lib"]
name = "bets"
[features]
default = []
cpi = ["no-entrypoint"]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
idl-build = ["anchor-lang/idl-build"]
[dependencies]
anchor-lang = "0.30.1"

2
programs/bets/Xargo.toml Normal file
View File

@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -0,0 +1,4 @@
use anchor_lang::prelude::*;
#[constant]
pub const SEED: &str = "anchor";

View File

@ -0,0 +1,14 @@
use anchor_lang::prelude::*;
#[error_code]
pub enum ErrorCode {
#[msg("Custom error message")]
CustomError,
}
#[error_code]
pub enum BettingError {
#[msg("Bet is not filled yet!")]
BetNotFilled,
}

View File

@ -0,0 +1,49 @@
use anchor_lang::prelude::*;
use crate::*;
pub fn create(ctx: Context<CreateBet>, wager: u64, game_id:String, _nonce:u64) -> Result<()> {
let bets_list = &mut ctx.accounts.bets_list;
let bet_vault = &mut ctx.accounts.bet_vault;
let payer = &ctx.accounts.payer;
let system_program = &ctx.accounts.system_program;
// Store bet details
bet_vault.game_id = game_id;
bet_vault.owner = payer.key();
bet_vault.wager = wager;
// Transfer SOL from the payer to the bet vault
let cpi_accounts = anchor_lang::system_program::Transfer {
from: payer.to_account_info(),
to: bet_vault.to_account_info(),
};
let cpi_ctx = CpiContext::new(system_program.to_account_info(), cpi_accounts);
anchor_lang::system_program::transfer(cpi_ctx, wager)?;
// Add this bet to the global list
bets_list.bets.push(bet_vault.key());
msg!("New bet {} created with {} lamports!", bet_vault.key(), wager);
Ok(())
}
#[derive(Accounts)]
#[instruction(wager: u64, game_id: String, _nonce:u64)]
pub struct CreateBet<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(mut)]
pub bets_list: Account<'info, BetsList>,
#[account(
init,
payer = payer,
space = 8 + BetVault::INIT_SPACE, // Owner (Pubkey) + Wager (u64)
seeds = [b"bet_vault", payer.key().as_ref(), &game_id.as_bytes(), &_nonce.to_le_bytes()],
bump
)]
pub bet_vault: Account<'info, BetVault>,
pub system_program: Program<'info, System>,
}

View File

@ -0,0 +1,24 @@
use anchor_lang::prelude::*;
use crate::*;
pub fn init(ctx: Context<InitializeBetsList>) -> Result<()> {
ctx.accounts.bets_list.bets = vec![];
msg!("Initialized Bets List!");
Ok(())
}
#[derive(Accounts)]
pub struct InitializeBetsList<'info> {
#[account(
init,
payer = payer,
space = 8 + 4 + (1000 * 8), // Allow storing up to 100 bets
seeds = [b"bets_list"],
bump
)]
pub bets_list: Account<'info, BetsList>,
#[account(mut)]
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
}

View File

@ -0,0 +1,40 @@
use anchor_lang::prelude::*;
use crate::*;
pub fn join(ctx: Context<JoinBet>, _game_id:String) ->Result<()>{
let bet_vault = &mut ctx.accounts.bet_vault;
let payer= &ctx.accounts.payer;
let ix = anchor_lang::solana_program::system_instruction::transfer(
&payer.key(),
&bet_vault.key(),
bet_vault.wager,
);
anchor_lang::solana_program::program::invoke(
&ix,
&[
payer.to_account_info(),
bet_vault.to_account_info(),
],
)?;
bet_vault.joiner = payer.key();
msg!("Joined bet {}!", bet_vault.key());
Ok(())
}
#[derive(Accounts)]
#[instruction(_game_id:String)]
pub struct JoinBet<'info>{
#[account(
mut
)]
pub bet_vault: Account<'info, BetVault>,
#[account(mut)]
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
}

View File

@ -0,0 +1,8 @@
pub mod initialize_bets_list;
pub use initialize_bets_list::*;
pub mod create_bet;
pub use create_bet::*;
pub mod join_bet;
pub use join_bet::*;

33
programs/bets/src/lib.rs Normal file
View File

@ -0,0 +1,33 @@
pub mod constants;
pub mod error;
pub mod instructions;
pub mod state;
use anchor_lang::prelude::*;
pub use constants::*;
pub use instructions::*;
pub use state::*;
declare_id!("HxsDuhD7wcPxcMsrYdteMYxkffuwff8HoxhZ7NuFtM37");
#[program]
pub mod bets {
use super::*;
pub fn initialize(ctx: Context<InitializeBetsList>) -> Result<()> {
initialize_bets_list::init(ctx)
}
pub fn create_bet(ctx: Context<CreateBet>, wager:u64, game_id:String)-> 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)
}
pub fn join_bet(ctx: Context<JoinBet>, game_id:String) -> Result<()>{
join_bet::join(ctx, game_id)
}
}

View File

@ -0,0 +1,12 @@
use anchor_lang::*;
use crate::*;
#[account]
#[derive(InitSpace)]
pub struct BetVault {
#[max_len(10)]
pub game_id: String,
pub owner: Pubkey,
pub joiner: Pubkey,
pub wager:u64
}

View File

@ -0,0 +1,9 @@
use anchor_lang::*;
use crate::*;
#[account]
#[derive(InitSpace)]
pub struct BetsList {
#[max_len(1000,)]
pub bets: Vec<Pubkey>, // Stores bet IDs
}

View File

@ -0,0 +1,5 @@
pub mod bets_list;
pub use bets_list::*;
pub mod bet_vault;
pub use bet_vault::*;

16
tests/bets.ts Normal file
View File

@ -0,0 +1,16 @@
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Bets } from "../target/types/bets";
describe("bets", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.Bets as Program<Bets>;
it("Is initialized!", async () => {
// Add your test here.
const tx = await program.methods.initialize().rpc();
console.log("Your transaction signature", tx);
});
});

10
tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"types": ["mocha", "chai"],
"typeRoots": ["./node_modules/@types"],
"lib": ["es2015"],
"module": "commonjs",
"target": "es6",
"esModuleInterop": true
}
}

1181
yarn.lock Normal file

File diff suppressed because it is too large Load Diff