Skip to content

Commit

Permalink
feat: add proc fold
Browse files Browse the repository at this point in the history
  • Loading branch information
Created-for-a-purpose committed May 10, 2024
1 parent 8347ec3 commit d551424
Show file tree
Hide file tree
Showing 8 changed files with 488 additions and 8 deletions.
81 changes: 81 additions & 0 deletions lib/contracts/core/game.masm
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,87 @@ export.play_raise
# []
end


export.play_fold
# make is_fold of player 1
# inc turn
push.CURRENT_TURN_INDEX exec.account::get_item
# => [0, 0, 0, current_turn_index]

drop drop drop
# => [current_turn_index]

push.9
# 9, current_turn_index
add
# => [current_turn_index + 9]
dup #for getting the get item
dup #for updating the turn
# => [current_turn_index + 9, current_turn_index + 9, current_turn_index + 9]

# getting the is_fold of player
exec.account::get_item
drop drop drop
# => [is_fold, current_turn_index + 9, current_turn_index + 9]

# making sure that is_fold is zero currently
assertz
# => [current_turn_index + 9, current_turn_index + 9]
push.1
# => [1, current_turn_index + 9, current_turn_index + 9]

swap padw drop movup.3
# => [current_turn_index + 9, 0, 0, 0, 1, current_turn_index + 9]
exec.account::set_item
dropw drop drop drop
# => [current_turn_index + 9]

# adding 4 will give the slot of next player
push.4 add
push.1
# => [1, next_turn_index]

while.true

dup push.9 add
# => [next_turn_index + 9, next_turn_index]
exec.account::get_item
drop drop drop
# => [is_fold, next_turn_index]

if.true
# if player has folded
dup push.103 lte
# => [0/1, next_turn_index]

if.true
push.13 add
push.1
# => [1, next_turn_index + 13]
else
push.0
end
else
# checking is fold
# => [next_turn_index]

padw drop
# => [0, 0, 0, next_turn_index]

push.CURRENT_TURN_INDEX # slot of current turn
# => [CURRENT_TURN_INDEX, 0, 0, 0, next_turn_index]

exec.account::set_item
dropw dropw
# => [...]
push.0
end
end
drop
# => [...]
end


# some basic account methods

export.basic_wallet::receive_asset
Expand Down
99 changes: 99 additions & 0 deletions lib/contracts/notes/game/fold.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use.miden::account
use.miden::note
use.miden::contracts::wallets::basic->wallet

const.CURRENT_TURN_INDEX=60

proc.play_fold
push.CURRENT_TURN_INDEX exec.account::get_item
# => [0, 0, 0, current_turn_index]

drop drop drop
# => [current_turn_index]

push.9
# 9, current_turn_index
add
# => [current_turn_index + 9]
dup #for getting the get item
dup #for updating the turn
# => [current_turn_index + 9, current_turn_index + 9, current_turn_index + 9]

# getting the is_fold of player
exec.account::get_item
drop drop drop
# => [is_fold, current_turn_index + 9, current_turn_index + 9]

# making sure that is_fold is zero currently
assertz
# => [current_turn_index + 9, current_turn_index + 9]
push.1
# => [1, current_turn_index + 9, current_turn_index + 9]

swap padw drop movup.3
# => [current_turn_index + 9, 0, 0, 0, 1, current_turn_index + 9]
exec.account::set_item
dropw drop drop drop
# => [current_turn_index + 9]

# adding 4 will give the slot of next player
push.4 add
push.1
# => [1, next_turn_index]

while.true

dup push.9 add
# => [next_turn_index + 9, next_turn_index]
exec.account::get_item
drop drop drop
# => [is_fold, next_turn_index]

if.true
# if player has folded
dup push.103 lte
# => [0/1, next_turn_index]

if.true
push.13 add
push.1
# => [1, next_turn_index + 13]
else
push.0
end
else
# checking is fold
# => [next_turn_index]

padw drop
# => [0, 0, 0, next_turn_index]

push.CURRENT_TURN_INDEX # slot of current turn
# => [CURRENT_TURN_INDEX, 0, 0, 0, next_turn_index]

exec.account::set_item
dropw dropw
# => [...]
push.0
end
end
drop
# => [...]
end

begin
dropw

call.play_fold
# => [...]

dropw
exec.note::get_assets drop mem_loadw
# => [ASSET, ...]

# load the asset and add it to the account
call.wallet::receive_asset
# => [...]

dropw
end
4 changes: 2 additions & 2 deletions lib/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ fn construct_game_constructor_storage(
];
player_pub_keys.extend(player_slots);

slot_index += 8; // since the mid 9 elements would cover the player stats and initially all those values are zero
slot_index += 13; // since the mid 9 elements would cover the player stats and initially all those values are zero
}

// merghe player_id with card_suit
// merge player_id with card_suit
game_info.push(auth_slot);
game_info.extend(cards);
game_info.extend(game_stats);
Expand Down
97 changes: 95 additions & 2 deletions lib/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use crate::accounts::{ create_basic_aze_game_account, create_basic_aze_player_account };
use crate::utils::{ create_aze_store_path, load_config };
use crate::notes::{ create_send_card_note, create_play_raise_note, create_play_call_note };
use crate::notes::{
create_send_card_note,
create_play_raise_note,
create_play_call_note,
create_play_fold_note,
};
use crate::constants::CLIENT_CONFIG_FILE_NAME;
use miden_client::client::rpc::NodeRpcClient;
use miden_client::{ client, store };
Expand Down Expand Up @@ -64,6 +69,13 @@ pub struct PlayCallTransactionData {
target_account_id: AccountId,
}

#[derive(Clone)]
pub struct PlayFoldTransactionData {
asset: Asset,
sender_account_id: AccountId,
target_account_id: AccountId,
}

impl SendCardTransactionData {
pub fn account_id(&self) -> AccountId {
self.sender_account_id
Expand Down Expand Up @@ -115,6 +127,19 @@ impl PlayCallTransactionData {
}
}

impl PlayFoldTransactionData {
pub fn account_id(&self) -> AccountId {
self.sender_account_id
}
pub fn new(asset: Asset, sender_account_id: AccountId, target_account_id: AccountId) -> Self {
Self {
asset,
sender_account_id,
target_account_id,
}
}
}

pub trait AzeGameMethods {
// fn get_tx_executor(&self) -> TransactionExecutor<ClientDataStore<D>>;
fn store(&self) -> SqliteStore;
Expand All @@ -141,6 +166,11 @@ pub trait AzeGameMethods {
// auth_info: AuthInfo,
transaction_template: AzeTransactionTemplate
) -> Result<TransactionRequest, ClientError>;
fn build_aze_play_fold_tx_request(
&mut self,
// auth_info: AuthInfo,
transaction_template: AzeTransactionTemplate
) -> Result<TransactionRequest, ClientError>;
fn new_game_account(
&mut self,
template: AzeAccountTemplate,
Expand Down Expand Up @@ -473,6 +503,67 @@ impl<N: NodeRpcClient, R: FeltRng, S: Store> AzeGameMethods for Client<N, R, S>
)
}

fn build_aze_play_fold_tx_request(
&mut self,
// auth_info: AuthInfo,
transaction_template: AzeTransactionTemplate
) -> Result<TransactionRequest, ClientError> {
let account_id = transaction_template.account_id();
let account_auth = self.store().get_account_auth(account_id)?;

let (sender_account_id, target_account_id, asset) = match transaction_template {
AzeTransactionTemplate::PlayFold(
PlayFoldTransactionData { asset, sender_account_id, target_account_id },
) => (sender_account_id, target_account_id, asset),
_ => panic!("Invalid transaction template"),
};

let random_coin = self.get_random_coin();

let created_note = create_play_fold_note(
self,
sender_account_id,
target_account_id,
[asset].to_vec(),
NoteType::Public,
random_coin
)?;

let recipient = created_note
.recipient_digest()
.iter()
.map(|x| x.as_int().to_string())
.collect::<Vec<_>>()
.join(".");

let note_tag = created_note.metadata().tag().inner();
let note_type = NoteType::Public;

let tx_script = ProgramAst::parse(
&transaction_request::AUTH_SEND_ASSET_SCRIPT
.replace("{recipient}", &recipient)
.replace("{note_type}", &Felt::new(note_type as u64).to_string())
.replace("{tag}", &Felt::new(note_tag.into()).to_string())
.replace("{asset}", &prepare_word(&asset.into()).to_string())
).expect("shipped MASM is well-formed");

let tx_script = {
let script_inputs = vec![account_auth.into_advice_inputs()];
self.compile_tx_script(tx_script, script_inputs, vec![])?
};

println!("Created txn script");

Ok(
TransactionRequest::new(
sender_account_id,
BTreeMap::new(),
vec![created_note],
Some(tx_script)
)
)
}

fn new_send_card_transaction(
&mut self,
asset: Asset,
Expand All @@ -498,6 +589,7 @@ pub enum AzeTransactionTemplate {
SendCard(SendCardTransactionData),
PlayRaise(PlayRaiseTransactionData),
PlayCall(PlayCallTransactionData),
PlayFold(PlayFoldTransactionData),
}

impl AzeTransactionTemplate {
Expand All @@ -507,6 +599,7 @@ impl AzeTransactionTemplate {
AzeTransactionTemplate::SendCard(p) => p.account_id(),
AzeTransactionTemplate::PlayRaise(p) => p.account_id(),
AzeTransactionTemplate::PlayCall(p) => p.account_id(),
AzeTransactionTemplate::PlayFold(p) => p.account_id(),
}
}
}
Expand All @@ -516,4 +609,4 @@ pub(crate) fn prepare_word(word: &Word) -> String {
.map(|x| x.as_int().to_string())
.collect::<Vec<_>>()
.join(".")
}
}
24 changes: 24 additions & 0 deletions lib/src/notes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ pub fn create_play_call_note<R: FeltRng, N: NodeRpcClient, S: Store>(
Ok(Note::new(vault, metadata, recipient))
}

pub fn create_play_fold_note<R: FeltRng, N: NodeRpcClient, S: Store>(
client: &mut Client<N, R, S>,
sender_account_id: AccountId,
target_account_id: AccountId,
assets: Vec<Asset>,
note_type: NoteType,
mut rng: RpoRandomCoin
) -> Result<Note, NoteError> {
let note_script = include_str!("../../contracts/notes/game/fold.masm");
let script_ast = ProgramAst::parse(note_script).unwrap();
let note_script = client.compile_note_script(script_ast, vec![]).unwrap();

let note_inputs = NoteInputs::new(vec![]).unwrap();
let tag = NoteTag::from_account_id(target_account_id, NoteExecutionMode::Local)?;
let serial_num = rng.draw_word();
let aux = ZERO;

let metadata = NoteMetadata::new(sender_account_id, NoteType::Public, tag, aux)?;
let vault = NoteAssets::new(assets)?;
let recipient = NoteRecipient::new(serial_num, note_script, note_inputs);

Ok(Note::new(vault, metadata, recipient))
}

// TODO: remove this function after testing
pub async fn mint_note(
client: &mut AzeClient,
Expand Down
Loading

0 comments on commit d551424

Please sign in to comment.