Skip to content

Commit

Permalink
using default constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhay-2811 committed May 27, 2024
1 parent b1d7f4f commit 5c69cce
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 47 deletions.
1 change: 1 addition & 0 deletions lib/src/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
116 changes: 74 additions & 42 deletions lib/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
use miden_objects::{
accounts::{ Account, AccountCode, AccountId, AccountStorage, SlotItem },
assembly::{ ModuleAst, ProgramAst },
assets::{ Asset, AssetVault, FungibleAsset, TokenSymbol },
accounts::{Account, AccountCode, AccountId, AccountStorage, SlotItem},
assembly::{ModuleAst, ProgramAst},
assets::{Asset, AssetVault, FungibleAsset, TokenSymbol},
crypto::{
dsa::rpo_falcon512::SecretKey, rand::{FeltRng, RpoRandomCoin}, utils::Serializable
dsa::rpo_falcon512::SecretKey,
rand::{FeltRng, RpoRandomCoin},
utils::Serializable,
},
notes::{ Note, NoteId, NoteScript, NoteType },
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 std::{ env::temp_dir, fs, time::Duration };
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 miden_client::{
client::{ accounts::{AccountStorageMode, AccountTemplate}, 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::{AzeAccountTemplate, AzeClient, AzeGameMethods}, constants::BUY_IN_AMOUNT, notes::{consume_notes, mint_note}, storage::GameStorageSlotData};
use std::{env::temp_dir, fs, time::Duration};

// use uuid::Uuid;

Expand All @@ -55,7 +65,12 @@ pub fn create_aze_store_path() -> std::path::PathBuf {
pub fn load_config(config_file: &Path) -> Result<ClientConfig, String> {
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 {
Expand All @@ -69,33 +84,45 @@ 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 initial_setup(mut client: &mut AzeClient) -> (FungibleAsset, AccountId, AccountId, GameStorageSlotData){
let small_blind_amt = 5u8;
let buy_in_amt = 100u8;
let no_of_players = 4u8;
let current_turn_index = 64u8;
let player_balance = 10u8;

pub async fn setup_accounts(
mut client: &mut AzeClient,
) -> (FungibleAsset, AccountId, AccountId, GameStorageSlotData) {
let slot_data = GameStorageSlotData::new(
small_blind_amt,
buy_in_amt,
no_of_players,
current_turn_index,
small_blind_amt,
player_balance,
SMALL_BLIND_AMOUNT,
SMALL_BUY_IN_AMOUNT,
NO_OF_PLAYERS,
CURRENT_TURN_INDEX_SLOT,
HIGHEST_BET,
PLAYER_INITIAL_BALANCE,
);

let (game_account, _) = client
Expand Down Expand Up @@ -145,5 +172,10 @@ pub async fn initial_setup(mut client: &mut AzeClient) -> (FungibleAsset, Accoun
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)
}
return (
fungible_asset,
sender_account_id,
target_account_id,
slot_data,
);
}
10 changes: 5 additions & 5 deletions node/src/api/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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::{initial_setup, log_account_status, log_slots};
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::{
Expand All @@ -34,7 +34,7 @@ pub async fn aze_poker_game_action() -> Result<Json<GameActionResponse>, GameAct
let mut client: AzeClient = create_aze_client();

let (fungible_asset, sender_account_id, target_account_id, slot_data) =
initial_setup(&mut client).await;
setup_accounts(&mut client).await;

let player_bet = slot_data.buy_in_amt();

Expand Down Expand Up @@ -68,7 +68,7 @@ pub async fn aze_poker_game_call() -> Result<Json<GameActionResponse>, GameActio
let mut client: AzeClient = create_aze_client();

let (fungible_asset, sender_account_id, target_account_id, slot_data) =
initial_setup(&mut client).await;
setup_accounts(&mut client).await;


let playcall_txn_data = PlayCallTransactionData::new(
Expand Down Expand Up @@ -100,7 +100,7 @@ pub async fn aze_poker_game_fold() -> Result<Json<GameActionResponse>, GameActio
let mut client: AzeClient = create_aze_client();

let (fungible_asset, sender_account_id, target_account_id, slot_data) =
initial_setup(&mut client).await;
setup_accounts(&mut client).await;
let playcall_txn_data = PlayFoldTransactionData::new(
Asset::Fungible(fungible_asset),
sender_account_id,
Expand Down Expand Up @@ -130,7 +130,7 @@ pub async fn aze_poker_game_check() -> Result<Json<GameActionResponse>, GameActi
let mut client: AzeClient = create_aze_client();

let (fungible_asset, sender_account_id, target_account_id, slot_data) =
initial_setup(&mut client).await;
setup_accounts(&mut client).await;

let playcheck_txn_data = PlayCheckTransactionData::new(
Asset::Fungible(fungible_asset),
Expand Down

0 comments on commit 5c69cce

Please sign in to comment.