diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..dee1db6 Binary files /dev/null and b/.DS_Store differ diff --git a/lib/src/constants/mod.rs b/lib/src/constants/mod.rs index c0611aa..d5b2d26 100644 --- a/lib/src/constants/mod.rs +++ b/lib/src/constants/mod.rs @@ -9,6 +9,7 @@ pub const DEFAULT_AUTH_SCRIPT: &str = pub const CLIENT_CONFIG_FILE_NAME: &str = "miden-client.toml"; pub const BUY_IN_AMOUNT: u64 = 1000; +pub const SMALL_BUY_IN_AMOUNT: u8 = 100; pub const TRANSFER_AMOUNT: u64 = 59; pub const SMALL_BLIND_AMOUNT: u8 = 5; pub const PLAYER_INITIAL_BALANCE: u8 = 30; diff --git a/lib/src/utils/mod.rs b/lib/src/utils/mod.rs index 986c040..624dcae 100644 --- a/lib/src/utils/mod.rs +++ b/lib/src/utils/mod.rs @@ -1,37 +1,45 @@ use miden_objects::{ - accounts::{ Account, AccountCode, AccountId, AccountStorage, SlotItem }, - assembly::{ ModuleAst, ProgramAst }, - assets::{ Asset, AssetVault, FungibleAsset }, + accounts::{Account, AccountCode, AccountId, AccountStorage, SlotItem}, + assembly::{ModuleAst, ProgramAst}, + assets::{Asset, AssetVault, FungibleAsset, TokenSymbol}, crypto::{ dsa::rpo_falcon512::SecretKey, + rand::{FeltRng, RpoRandomCoin}, utils::Serializable, - rand::FeltRng, - rand::RpoRandomCoin, }, - notes::{ Note, NoteId, NoteScript }, + notes::{Note, NoteId, NoteScript, NoteType}, transaction::{ - ChainMmr, - ExecutedTransaction, - InputNote, - InputNotes, - ProvenTransaction, - TransactionInputs, + ChainMmr, ExecutedTransaction, InputNote, InputNotes, ProvenTransaction, TransactionInputs, }, - BlockHeader, - Felt, - Word, + BlockHeader, Felt, Word, +}; + +use crate::{ + client::{AzeAccountTemplate, AzeClient, AzeGameMethods}, + constants::{ + BUY_IN_AMOUNT, CURRENT_TURN_INDEX_SLOT, HIGHEST_BET, NO_OF_PLAYERS, PLAYER_INITIAL_BALANCE, + SMALL_BLIND_AMOUNT, SMALL_BUY_IN_AMOUNT, + }, + notes::{consume_notes, mint_note}, + storage::GameStorageSlotData, +}; +use ::rand::Rng; +use figment::{ + providers::{Format, Toml}, + Figment, }; -use std::{ env::temp_dir, fs, time::Duration }; use miden_client::{ - client::{ rpc::NodeRpcClient, Client }, + client::{ + accounts::{AccountStorageMode, AccountTemplate}, + rpc::NodeRpcClient, + Client, + }, config::ClientConfig, - errors::{ ClientError, NoteIdPrefixFetchError }, - store::{ sqlite_store::SqliteStore, InputNoteRecord, NoteFilter as ClientNoteFilter, Store }, + errors::{ClientError, NoteIdPrefixFetchError}, + store::{sqlite_store::SqliteStore, InputNoteRecord, NoteFilter as ClientNoteFilter, Store}, }; use std::path::Path; -use figment::{ providers::{ Format, Toml }, Figment }; -use ::rand::Rng; -use crate::client::AzeClient; +use std::{env::temp_dir, fs, time::Duration}; // use uuid::Uuid; @@ -57,7 +65,12 @@ pub fn create_aze_store_path() -> std::path::PathBuf { pub fn load_config(config_file: &Path) -> Result { Figment::from(Toml::file(config_file)) .extract() - .map_err(|err| format!("Failed to load {} config file: {err}", config_file.display())) + .map_err(|err| { + format!( + "Failed to load {} config file: {err}", + config_file.display() + ) + }) } pub fn get_random_coin() -> RpoRandomCoin { @@ -71,15 +84,98 @@ pub fn get_random_coin() -> RpoRandomCoin { // TODO hide this methods under debug feature pub async fn log_account_status(client: &AzeClient, account_id: AccountId) { let (regular_account, _seed) = client.get_account(account_id).unwrap(); - println!("Account asset count --> {:?}", regular_account.vault().assets().count()); - println!("Account storage root --> {:?}", regular_account.storage().root()); - println!("Account slot 100 --> {:?}", regular_account.storage().get_item(100)); - println!("Account slot 101 --> {:?}", regular_account.storage().get_item(101)); + println!( + "Account asset count --> {:?}", + regular_account.vault().assets().count() + ); + println!( + "Account storage root --> {:?}", + regular_account.storage().root() + ); + println!( + "Account slot 100 --> {:?}", + regular_account.storage().get_item(100) + ); + println!( + "Account slot 101 --> {:?}", + regular_account.storage().get_item(101) + ); } pub async fn log_slots(client: &AzeClient, account_id: AccountId) { let (regular_account, _seed) = client.get_account(account_id).unwrap(); for i in 1..100 { - println!("Account slot {:?} --> {:?}", i, regular_account.storage().get_item(i)); + println!( + "Account slot {:?} --> {:?}", + i, + regular_account.storage().get_item(i) + ); } } + +pub async fn setup_accounts( + mut client: &mut AzeClient, +) -> (FungibleAsset, AccountId, AccountId, GameStorageSlotData) { + let slot_data = GameStorageSlotData::new( + SMALL_BLIND_AMOUNT, + SMALL_BUY_IN_AMOUNT, + NO_OF_PLAYERS, + CURRENT_TURN_INDEX_SLOT, + HIGHEST_BET, + PLAYER_INITIAL_BALANCE, + ); + + let (game_account, _) = client + .new_game_account( + AzeAccountTemplate::GameAccount { + mutable_code: false, + storage_mode: AccountStorageMode::Local, // for now + }, + Some(slot_data.clone()), + ) + .unwrap(); + let game_account_id = game_account.id(); + log_slots(&client, game_account_id).await; + + let (player_account, _) = client + .new_game_account( + AzeAccountTemplate::PlayerAccount { + mutable_code: false, + storage_mode: AccountStorageMode::Local, // for now + }, + None, + ) + .unwrap(); + let player_account_id = player_account.id(); + + let (faucet_account, _) = client + .new_account(AccountTemplate::FungibleFaucet { + token_symbol: TokenSymbol::new("MATIC").unwrap(), + decimals: 8, + max_supply: 1_000_000_000, + storage_mode: AccountStorageMode::Local, + }) + .unwrap(); + let faucet_account_id = faucet_account.id(); + + let note = mint_note( + &mut client, + player_account_id, + faucet_account_id, + NoteType::Public, + ) + .await; + println!("Minted note"); + consume_notes(&mut client, player_account_id, &[note]).await; + + let fungible_asset = FungibleAsset::new(faucet_account_id, BUY_IN_AMOUNT).unwrap(); + let sender_account_id = player_account_id; + let target_account_id = game_account_id; + + return ( + fungible_asset, + sender_account_id, + target_account_id, + slot_data, + ); +} diff --git a/node/src/api/action.rs b/node/src/api/action.rs index 7c25537..1cff7e0 100644 --- a/node/src/api/action.rs +++ b/node/src/api/action.rs @@ -1,108 +1,53 @@ use actix_web::{ error::ResponseError, get, - http::{ header::ContentType, StatusCode }, - post, - put, + http::{header::ContentType, StatusCode}, + post, put, web::Data, web::Json, web::Path, HttpResponse, }; -use derive_more::Display; -use aze_types::actions::{ GameActionError, GameActionResponse }; -use aze_lib::utils::{ log_account_status, log_slots }; -use aze_lib::storage::GameStorageSlotData; -use aze_lib::executor::execute_tx_and_sync; -use aze_lib::constants::BUY_IN_AMOUNT; use aze_lib::client::{ - AzeClient, - AzeAccountTemplate, - create_aze_client, + create_aze_client, AzeAccountTemplate, AzeClient, AzeGameMethods, AzeTransactionTemplate, + PlayCallTransactionData, PlayCheckTransactionData, PlayFoldTransactionData, PlayRaiseTransactionData, - PlayCallTransactionData, - PlayFoldTransactionData, - PlayCheckTransactionData, - AzeTransactionTemplate, - AzeGameMethods, }; +use aze_lib::constants::BUY_IN_AMOUNT; +use aze_lib::executor::execute_tx_and_sync; +use aze_lib::notes::{consume_notes, mint_note}; +use aze_lib::storage::GameStorageSlotData; +use aze_lib::utils::{setup_accounts, log_account_status, log_slots}; +use aze_types::actions::{GameActionError, GameActionResponse}; +use derive_more::Display; use miden_client::client::{ - accounts::{ AccountStorageMode, AccountTemplate }, + accounts::{AccountStorageMode, AccountTemplate}, transactions::transaction_request::TransactionTemplate, }; -use miden_objects::{ assets::{ TokenSymbol, Asset, FungibleAsset }, notes::NoteType }; -use aze_lib::notes::{ consume_notes, mint_note }; +use miden_objects::{ + assets::{Asset, FungibleAsset, TokenSymbol}, + notes::NoteType, +}; #[post("/v1/game/action")] pub async fn aze_poker_game_action() -> Result, GameActionError> { let mut client: AzeClient = create_aze_client(); - let small_blind_amt = 5u8; - let buy_in_amt = 100u8; - let no_of_players = 4u8; - let current_turn_index = 64u8; - let player_balance = 10u8; - - let slot_data = GameStorageSlotData::new( - small_blind_amt, - buy_in_amt, - no_of_players, - current_turn_index, - small_blind_amt, - player_balance - ); - - let (game_account, _) = client - .new_game_account( - AzeAccountTemplate::GameAccount { - mutable_code: false, - storage_mode: AccountStorageMode::Local, // for now - }, - Some(slot_data) - ) - .unwrap(); - let game_account_id = game_account.id(); - log_slots(&client, game_account_id).await; - - let (player_account, _) = client - .new_game_account( - AzeAccountTemplate::PlayerAccount { - mutable_code: false, - storage_mode: AccountStorageMode::Local, // for now - }, - None - ) - .unwrap(); - let player_account_id = player_account.id(); - - let (faucet_account, _) = client - .new_account(AccountTemplate::FungibleFaucet { - token_symbol: TokenSymbol::new("MATIC").unwrap(), - decimals: 8, - max_supply: 1_000_000_000, - storage_mode: AccountStorageMode::Local, - }) - .unwrap(); - let faucet_account_id = faucet_account.id(); - - let note = mint_note(&mut client, player_account_id, faucet_account_id, NoteType::Public).await; - println!("Minted note"); - consume_notes(&mut client, player_account_id, &[note]).await; + let (fungible_asset, sender_account_id, target_account_id, slot_data) = + setup_accounts(&mut client).await; - let fungible_asset = FungibleAsset::new(faucet_account_id, BUY_IN_AMOUNT).unwrap(); - let sender_account_id = player_account_id; - let target_account_id = game_account_id; - - let player_bet = small_blind_amt; + let player_bet = slot_data.buy_in_amt(); let playraise_txn_data = PlayRaiseTransactionData::new( Asset::Fungible(fungible_asset), sender_account_id, - game_account_id, - player_bet + target_account_id, + player_bet, ); let transaction_template = AzeTransactionTemplate::PlayRaise(playraise_txn_data); - let txn_request = client.build_aze_play_raise_tx_request(transaction_template).unwrap(); + let txn_request = client + .build_aze_play_raise_tx_request(transaction_template) + .unwrap(); execute_tx_and_sync(&mut client, txn_request.clone()).await; let note_id = txn_request.expected_output_notes()[0].id(); @@ -122,69 +67,19 @@ pub async fn aze_poker_game_action() -> Result, GameAct pub async fn aze_poker_game_call() -> Result, GameActionError> { let mut client: AzeClient = create_aze_client(); - let small_blind_amt = 5u8; - let buy_in_amt = 100u8; - let no_of_players = 4u8; - let current_turn_index = 64u8; - let player_balance = 10u8; - - let slot_data = GameStorageSlotData::new( - small_blind_amt, - buy_in_amt, - no_of_players, - current_turn_index, - small_blind_amt, - player_balance - ); - - let (game_account, _) = client - .new_game_account( - AzeAccountTemplate::GameAccount { - mutable_code: false, - storage_mode: AccountStorageMode::Local, // for now - }, - Some(slot_data) - ) - .unwrap(); - let game_account_id = game_account.id(); - log_slots(&client, game_account_id).await; - - let (player_account, _) = client - .new_game_account( - AzeAccountTemplate::PlayerAccount { - mutable_code: false, - storage_mode: AccountStorageMode::Local, // for now - }, - None - ) - .unwrap(); - let player_account_id = player_account.id(); - - let (faucet_account, _) = client - .new_account(AccountTemplate::FungibleFaucet { - token_symbol: TokenSymbol::new("MATIC").unwrap(), - decimals: 8, - max_supply: 1_000_000_000, - storage_mode: AccountStorageMode::Local, - }) - .unwrap(); - let faucet_account_id = faucet_account.id(); - - let note = mint_note(&mut client, player_account_id, faucet_account_id, NoteType::Public).await; - println!("Minted note"); - consume_notes(&mut client, player_account_id, &[note]).await; - - let fungible_asset = FungibleAsset::new(faucet_account_id, BUY_IN_AMOUNT).unwrap(); - let sender_account_id = player_account_id; - let target_account_id = game_account_id; + let (fungible_asset, sender_account_id, target_account_id, slot_data) = + setup_accounts(&mut client).await; + let playcall_txn_data = PlayCallTransactionData::new( Asset::Fungible(fungible_asset), sender_account_id, - game_account_id + target_account_id, ); let transaction_template = AzeTransactionTemplate::PlayCall(playcall_txn_data); - let txn_request = client.build_aze_play_call_tx_request(transaction_template).unwrap(); + let txn_request = client + .build_aze_play_call_tx_request(transaction_template) + .unwrap(); execute_tx_and_sync(&mut client, txn_request.clone()).await; let note_id = txn_request.expected_output_notes()[0].id(); @@ -204,69 +99,17 @@ pub async fn aze_poker_game_call() -> Result, GameActio pub async fn aze_poker_game_fold() -> Result, GameActionError> { let mut client: AzeClient = create_aze_client(); - let small_blind_amt = 5u8; - let buy_in_amt = 100u8; - let no_of_players = 4u8; - let current_turn_index = 64u8; - let player_balance = 10u8; - - let slot_data = GameStorageSlotData::new( - small_blind_amt, - buy_in_amt, - no_of_players, - current_turn_index, - small_blind_amt, - player_balance - ); - - let (game_account, _) = client - .new_game_account( - AzeAccountTemplate::GameAccount { - mutable_code: false, - storage_mode: AccountStorageMode::Local, // for now - }, - Some(slot_data) - ) - .unwrap(); - let game_account_id = game_account.id(); - log_slots(&client, game_account_id).await; - - let (player_account, _) = client - .new_game_account( - AzeAccountTemplate::PlayerAccount { - mutable_code: false, - storage_mode: AccountStorageMode::Local, // for now - }, - None - ) - .unwrap(); - let player_account_id = player_account.id(); - - let (faucet_account, _) = client - .new_account(AccountTemplate::FungibleFaucet { - token_symbol: TokenSymbol::new("MATIC").unwrap(), - decimals: 8, - max_supply: 1_000_000_000, - storage_mode: AccountStorageMode::Local, - }) - .unwrap(); - let faucet_account_id = faucet_account.id(); - - let note = mint_note(&mut client, player_account_id, faucet_account_id, NoteType::Public).await; - println!("Minted note"); - consume_notes(&mut client, player_account_id, &[note]).await; - - let fungible_asset = FungibleAsset::new(faucet_account_id, BUY_IN_AMOUNT).unwrap(); - let sender_account_id = player_account_id; - let target_account_id = game_account_id; - + let (fungible_asset, sender_account_id, target_account_id, slot_data) = + setup_accounts(&mut client).await; let playcall_txn_data = PlayFoldTransactionData::new( Asset::Fungible(fungible_asset), sender_account_id, - game_account_id + target_account_id, ); let transaction_template = AzeTransactionTemplate::PlayFold(playcall_txn_data); - let txn_request = client.build_aze_play_fold_tx_request(transaction_template).unwrap(); + let txn_request = client + .build_aze_play_fold_tx_request(transaction_template) + .unwrap(); execute_tx_and_sync(&mut client, txn_request.clone()).await; let note_id = txn_request.expected_output_notes()[0].id(); @@ -286,69 +129,18 @@ pub async fn aze_poker_game_fold() -> Result, GameActio pub async fn aze_poker_game_check() -> Result, GameActionError> { let mut client: AzeClient = create_aze_client(); - let small_blind_amt = 5u8; - let buy_in_amt = 100u8; - let no_of_players = 4u8; - let current_turn_index = 64u8; - let player_balance = 10u8; - - let slot_data = GameStorageSlotData::new( - small_blind_amt, - buy_in_amt, - no_of_players, - current_turn_index, - small_blind_amt, - player_balance - ); - - let (game_account, _) = client - .new_game_account( - AzeAccountTemplate::GameAccount { - mutable_code: false, - storage_mode: AccountStorageMode::Local, // for now - }, - Some(slot_data) - ) - .unwrap(); - let game_account_id = game_account.id(); - log_slots(&client, game_account_id).await; - - let (player_account, _) = client - .new_game_account( - AzeAccountTemplate::PlayerAccount { - mutable_code: false, - storage_mode: AccountStorageMode::Local, // for now - }, - None - ) - .unwrap(); - let player_account_id = player_account.id(); - - let (faucet_account, _) = client - .new_account(AccountTemplate::FungibleFaucet { - token_symbol: TokenSymbol::new("MATIC").unwrap(), - decimals: 8, - max_supply: 1_000_000_000, - storage_mode: AccountStorageMode::Local, - }) - .unwrap(); - let faucet_account_id = faucet_account.id(); - - let note = mint_note(&mut client, player_account_id, faucet_account_id, NoteType::Public).await; - println!("Minted note"); - consume_notes(&mut client, player_account_id, &[note]).await; + let (fungible_asset, sender_account_id, target_account_id, slot_data) = + setup_accounts(&mut client).await; - let fungible_asset = FungibleAsset::new(faucet_account_id, BUY_IN_AMOUNT).unwrap(); - let sender_account_id = player_account_id; - let target_account_id = game_account_id; - let playcheck_txn_data = PlayCheckTransactionData::new( Asset::Fungible(fungible_asset), sender_account_id, - game_account_id + target_account_id, ); let transaction_template = AzeTransactionTemplate::PlayCheck(playcheck_txn_data); - let txn_request = client.build_aze_play_check_tx_request(transaction_template).unwrap(); + let txn_request = client + .build_aze_play_check_tx_request(transaction_template) + .unwrap(); execute_tx_and_sync(&mut client, txn_request.clone()).await; let note_id = txn_request.expected_output_notes()[0].id(); @@ -362,4 +154,4 @@ pub async fn aze_poker_game_check() -> Result, GameActi log_slots(&client, target_account_id).await; Ok(Json(GameActionResponse { is_taken: true })) -} \ No newline at end of file +}