Skip to content

Commit

Permalink
Merge pull request #24 from RizeLabs/feat-refactor
Browse files Browse the repository at this point in the history
Adding Utility funtion to setup user, game accounts
  • Loading branch information
nlok5923 authored May 28, 2024
2 parents a655ca4 + 5c69cce commit 4c7d120
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 280 deletions.
Binary file added .DS_Store
Binary file not shown.
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
152 changes: 124 additions & 28 deletions lib/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -57,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 @@ -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,
);
}
Loading

0 comments on commit 4c7d120

Please sign in to comment.