From e09e8f55e8cf710c578cd1af4dab4f3bd92511ca Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:02:39 -0800 Subject: [PATCH 01/12] Require lock in on top off if already locked in --- .../sources/emojicoin_arena.move | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index 60080ac17..3f5cce274 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -35,8 +35,9 @@ module arena::emojicoin_arena { const E_ENTER_COIN_BALANCE_0: u64 = 4; /// User's melee escrow has nonzero emojicoin 1 balance. const E_ENTER_COIN_BALANCE_1: u64 = 5; - /// Elected to lock in but unable to match. - const E_UNABLE_TO_LOCK_IN: u64 = 6; + /// User did not elect to lock in even though they've been matched since their most recent + /// deposit into an empty escrow. + const E_TOP_OFF_MUST_LOCK_IN: u64 = 6; /// Provided escrow coin type is invalid. const E_INVALID_ESCROW_COIN_TYPE: u64 = 7; /// User has no escrow resource. @@ -268,10 +269,16 @@ module arena::emojicoin_arena { coin::value(&escrow_ref_mut.emojicoin_0) == 0, E_ENTER_COIN_BALANCE_0 ); - // Match a portion of user's contribution if they elect to lock in. + // Verify that if user has been matched since their most recent deposit into an empty esrow, + // they lock in again. + if (escrow_ref_mut.octas_matched > 0) { + assert!(lock_in, E_TOP_OFF_MUST_LOCK_IN); + }; + + // Match a portion of user's contribution if they elect to lock in, and if there are any + // available rewards to match. let match_amount = if (lock_in) { - // Verify that user can even lock in. let match_amount = match_amount( input_amount, @@ -280,28 +287,30 @@ module arena::emojicoin_arena { registry_ref_mut, time ); - assert!(match_amount > 0, E_UNABLE_TO_LOCK_IN); + if (match_amount > 0) { - // Transfer APT to entrant. - aptos_account::transfer( - &account::create_signer_with_capability( - ®istry_ref_mut.signer_capability - ), - entrant_address, - match_amount - ); + // Transfer APT to entrant. + aptos_account::transfer( + &account::create_signer_with_capability( + ®istry_ref_mut.signer_capability + ), + entrant_address, + match_amount + ); - // Update melee state. - current_melee_ref_mut.melee_available_rewards_decrement(match_amount); - current_melee_ref_mut.melee_locked_in_entrants_add_if_not_contains( - entrant_address - ); + // Update melee state. + current_melee_ref_mut.melee_available_rewards_decrement(match_amount); + current_melee_ref_mut.melee_locked_in_entrants_add_if_not_contains( + entrant_address + ); - // Update registry state. - registry_ref_mut.registry_octas_matched_increment(match_amount); + // Update registry state. + registry_ref_mut.registry_octas_matched_increment(match_amount); + + // Update escrow state. + escrow_ref_mut.escrow_octas_matched_increment(match_amount); - // Update escrow state. - escrow_ref_mut.escrow_octas_matched_increment(match_amount); + }; match_amount From bafd7388eab94311873fcbf506ac09d30a2bbc6b Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:03:02 -0800 Subject: [PATCH 02/12] Fix typo --- src/move/emojicoin_arena/sources/emojicoin_arena.move | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index 3f5cce274..257480d5d 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -269,8 +269,8 @@ module arena::emojicoin_arena { coin::value(&escrow_ref_mut.emojicoin_0) == 0, E_ENTER_COIN_BALANCE_0 ); - // Verify that if user has been matched since their most recent deposit into an empty esrow, - // they lock in again. + // Verify that if user has been matched since their most recent deposit into an empty + // escrow, they lock in again. if (escrow_ref_mut.octas_matched > 0) { assert!(lock_in, E_TOP_OFF_MUST_LOCK_IN); }; From 37e03780a118eeed414486b2191f5c64e37f9410 Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:11:45 -0800 Subject: [PATCH 03/12] Add entry event --- .../sources/emojicoin_arena.move | 56 +++++++++++++++---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index 257480d5d..a195109a2 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -6,6 +6,7 @@ module arena::emojicoin_arena { use aptos_framework::aptos_account; use aptos_framework::aptos_coin::AptosCoin; use aptos_framework::coin::{Self, Coin}; + use aptos_framework::event; use aptos_framework::randomness::Self; use aptos_framework::timestamp; use aptos_std::math64::min; @@ -154,6 +155,18 @@ module arena::emojicoin_arena { unexited_melee_ids: SmartTable } + #[event] + /// Emitted whenever a new melee starts. + struct NewMelee has copy, drop, store { + melee_id: u64, + market_metadatas: vector, + start_time: u64, + duration: u64, + max_match_percentage: u64, + max_match_amount: u64, + available_rewards: u64 + } + public entry fun fund_vault(arena: &signer, amount: u64) acquires Registry { aptos_account::transfer( arena, @@ -830,22 +843,30 @@ module arena::emojicoin_arena { sorted_unique_market_ids: vector ) { let melee_id = n_melees_before_registration + 1; + let market_metadatas = + sorted_unique_market_ids.map_ref(|market_id_ref| { + option::destroy_some( + emojicoin_dot_fun::market_metadata_by_market_id(*market_id_ref) + ) + }); + let start_time = + last_period_boundary( + timestamp::now_microseconds(), registry_ref_mut.next_melee_duration + ); + let duration = registry_ref_mut.next_melee_duration; + let max_match_percentage = registry_ref_mut.next_melee_max_match_percentage; + let max_match_amount = registry_ref_mut.next_melee_max_match_amount; + let available_rewards = registry_ref_mut.next_melee_available_rewards; registry_ref_mut.melees_by_id.add( melee_id, Melee { melee_id, - market_metadatas: sorted_unique_market_ids.map_ref(|market_id_ref| { - option::destroy_some( - emojicoin_dot_fun::market_metadata_by_market_id(*market_id_ref) - ) - }), - start_time: last_period_boundary( - timestamp::now_microseconds(), registry_ref_mut.next_melee_duration - ), - duration: registry_ref_mut.next_melee_duration, - max_match_percentage: registry_ref_mut.next_melee_max_match_percentage, - max_match_amount: registry_ref_mut.next_melee_max_match_amount, - available_rewards: registry_ref_mut.next_melee_available_rewards, + market_metadatas, + start_time, + duration, + max_match_percentage, + max_match_amount, + available_rewards, all_entrants: smart_table::new(), active_entrants: smart_table::new(), exited_entrants: smart_table::new(), @@ -857,6 +878,17 @@ module arena::emojicoin_arena { } ); registry_ref_mut.melee_ids_by_market_ids.add(sorted_unique_market_ids, melee_id); + event::emit( + NewMelee { + melee_id, + market_metadatas, + start_time, + duration, + max_match_percentage, + max_match_amount, + available_rewards + } + ) } inline fun registry_all_entrants_add_if_not_contains( From aac694bed2e9c363b22007bb4d388d4237581d7e Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:29:38 -0800 Subject: [PATCH 04/12] Add vault balance update event --- .../sources/emojicoin_arena.move | 58 ++++++++++++++----- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index a195109a2..13d8474fc 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -167,14 +167,18 @@ module arena::emojicoin_arena { available_rewards: u64 } - public entry fun fund_vault(arena: &signer, amount: u64) acquires Registry { - aptos_account::transfer( - arena, - account::get_signer_capability_address( - &borrow_registry_ref_checked(arena).signer_capability - ), - amount - ); + #[event] + /// Emitted whenever the vault balance is updated, except for when the vault is funded by + /// sending funds directly to the vault address instead of by using the `fund_vault` function. + struct VaultBalanceUpdate has copy, drop, store { + new_balance: u64 + } + + public entry fun fund_vault(funder: &signer, amount: u64) acquires Registry { + let vault_address = + account::get_signer_capability_address(&Registry[@arena].signer_capability); + aptos_account::transfer(funder, vault_address, amount); + emit_vault_balance_update_with_vault_address(vault_address); } public entry fun set_next_melee_available_rewards( @@ -201,13 +205,14 @@ module arena::emojicoin_arena { } public entry fun withdraw_from_vault(arena: &signer, amount: u64) acquires Registry { + let signer_capability_ref = + &borrow_registry_ref_mut_checked(arena).signer_capability; aptos_account::transfer( - &account::create_signer_with_capability( - &borrow_registry_ref_checked(arena).signer_capability - ), + &account::create_signer_with_capability(signer_capability_ref), @arena, amount ); + emit_vault_balance_update_with_singer_capability_ref(signer_capability_ref); } #[randomness] @@ -303,13 +308,15 @@ module arena::emojicoin_arena { if (match_amount > 0) { // Transfer APT to entrant. + let signer_capability_ref = ®istry_ref_mut.signer_capability; aptos_account::transfer( - &account::create_signer_with_capability( - ®istry_ref_mut.signer_capability - ), + &account::create_signer_with_capability(signer_capability_ref), entrant_address, match_amount ); + emit_vault_balance_update_with_singer_capability_ref( + signer_capability_ref + ); // Update melee state. current_melee_ref_mut.melee_available_rewards_decrement(match_amount); @@ -570,6 +577,28 @@ module arena::emojicoin_arena { (cranked, registry_ref_mut, time, n_melees_before_cranking) } + inline fun emit_vault_balance_update_with_singer_capability_ref( + signer_capability_ref: &SignerCapability + ) { + event::emit( + VaultBalanceUpdate { + new_balance: coin::balance( + account::get_signer_capability_address(signer_capability_ref) + ) + } + ); + } + + inline fun emit_vault_balance_update_with_vault_address( + vault_address: address + ) { + event::emit( + VaultBalanceUpdate { + new_balance: coin::balance(vault_address) + } + ); + } + inline fun escrow_n_swaps_increment( self: &mut Escrow ) { @@ -629,6 +658,7 @@ module arena::emojicoin_arena { ®istry_ref_mut.signer_capability ); aptos_account::transfer(participant, vault_address, octas_matched); + emit_vault_balance_update_with_vault_address(vault_address); // Update melee state. exited_melee_ref_mut.melee_available_rewards_increment(octas_matched); From 1f23951d296206fd0e967539250ae10c7f2ae3c4 Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:43:42 -0800 Subject: [PATCH 05/12] Add escrow state event --- .../sources/emojicoin_arena.move | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index 13d8474fc..84c45d967 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -155,6 +155,19 @@ module arena::emojicoin_arena { unexited_melee_ids: SmartTable } + #[event] + /// Emitted after a user enters, swaps, or exits, representing the final state of their escrow. + struct EscrowState has copy, drop, store { + user: address, + melee_id: u64, + emojicoin_0_balance: u64, + emojicoin_1_balance: u64, + n_swaps: u64, + swaps_volume: u128, + octas_entered: u64, + octas_matched: u64 + } + #[event] /// Emitted whenever a new melee starts. struct NewMelee has copy, drop, store { @@ -396,6 +409,8 @@ module arena::emojicoin_arena { user_melees_ref_mut.user_melees_unexited_melee_ids_add_if_not_contains(melee_id); user_melees_ref_mut.user_melees_exited_melee_ids_remove_if_contains(melee_id); + // Emit escrow state event. + escrow_ref_mut.emit_escrow_state(entrant_address); } #[randomness] @@ -492,13 +507,15 @@ module arena::emojicoin_arena { escrow_ref_mut.escrow_n_swaps_increment(); escrow_ref_mut.escrow_swaps_volume_increment(quote_volume_u128); + // Exit as needed, emitting escrow state event if not exiting, since exit inner function + // emits the event if exiting. if (exit_once_done) exit_inner( swapper, swapper_address, registry_ref_mut, false - ); + ) else escrow_ref_mut.emit_escrow_state(swapper_address); } @@ -577,6 +594,24 @@ module arena::emojicoin_arena { (cranked, registry_ref_mut, time, n_melees_before_cranking) } + inline fun emit_escrow_state( + self: &Escrow, + participant_address: address + ) { + event::emit( + EscrowState { + user: participant_address, + melee_id: self.melee_id, + emojicoin_0_balance: coin::value(&self.emojicoin_0), + emojicoin_1_balance: coin::value(&self.emojicoin_1), + n_swaps: self.n_swaps, + swaps_volume: self.swaps_volume, + octas_entered: self.octas_entered, + octas_matched: self.octas_matched + } + ); + } + inline fun emit_vault_balance_update_with_singer_capability_ref( signer_capability_ref: &SignerCapability ) { @@ -691,6 +726,9 @@ module arena::emojicoin_arena { let user_melees_ref_mut = &mut UserMelees[participant_address]; user_melees_ref_mut.user_melees_exited_melee_ids_add_if_not_contains(melee_id); user_melees_ref_mut.user_melees_unexited_melee_ids_remove_if_contains(melee_id); + + // Emit escrow state event. + escrow_ref_mut.emit_escrow_state(participant_address); } inline fun get_n_registered_markets(): u64 { From af849d699aae4cfcc9095d7c546bff3b782336bf Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:00:01 -0800 Subject: [PATCH 06/12] Add melee state event emission --- .../sources/emojicoin_arena.move | 57 ++++++++++++++++--- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index 84c45d967..e7f78ea23 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -2,7 +2,7 @@ module arena::emojicoin_arena { use aptos_framework::account::{Self, SignerCapability}; - use aptos_framework::aggregator_v2::{Self, Aggregator}; + use aptos_framework::aggregator_v2::{Self, Aggregator, AggregatorSnapshot}; use aptos_framework::aptos_account; use aptos_framework::aptos_coin::AptosCoin; use aptos_framework::coin::{Self, Coin}; @@ -168,6 +168,21 @@ module arena::emojicoin_arena { octas_matched: u64 } + #[event] + /// Emitted after a user enters, swaps, or exits, representing the final state of the melee. + struct MeleeState has drop, store { + melee_id: u64, + available_rewards: u64, + n_all_entrants: u64, + n_active_entrants: u64, + n_exited_entrants: u64, + n_locked_in_entrants: u64, + n_swaps: AggregatorSnapshot, + swaps_volume: AggregatorSnapshot, + emojicoin_0_locked: AggregatorSnapshot, + emojicoin_1_locked: AggregatorSnapshot + } + #[event] /// Emitted whenever a new melee starts. struct NewMelee has copy, drop, store { @@ -409,8 +424,8 @@ module arena::emojicoin_arena { user_melees_ref_mut.user_melees_unexited_melee_ids_add_if_not_contains(melee_id); user_melees_ref_mut.user_melees_exited_melee_ids_remove_if_contains(melee_id); - // Emit escrow state event. - escrow_ref_mut.emit_escrow_state(entrant_address); + // Emit state events. + emit_state(escrow_ref_mut, current_melee_ref_mut, entrant_address); } #[randomness] @@ -507,15 +522,15 @@ module arena::emojicoin_arena { escrow_ref_mut.escrow_n_swaps_increment(); escrow_ref_mut.escrow_swaps_volume_increment(quote_volume_u128); - // Exit as needed, emitting escrow state event if not exiting, since exit inner function - // emits the event if exiting. + // Exit as needed, emitting state if not exiting, since exit inner function emits state. if (exit_once_done) exit_inner( swapper, swapper_address, registry_ref_mut, false - ) else escrow_ref_mut.emit_escrow_state(swapper_address); + ) + else emit_state(escrow_ref_mut, swap_melee_ref_mut, swapper_address); } @@ -612,6 +627,32 @@ module arena::emojicoin_arena { ); } + inline fun emit_melee_state(self: &Melee) { + event::emit( + MeleeState { + melee_id: self.melee_id, + available_rewards: self.available_rewards, + n_all_entrants: self.all_entrants.length(), + n_active_entrants: self.active_entrants.length(), + n_exited_entrants: self.exited_entrants.length(), + n_locked_in_entrants: self.locked_in_entrants.length(), + n_swaps: aggregator_v2::snapshot(&self.n_swaps), + swaps_volume: aggregator_v2::snapshot(&self.swaps_volume), + emojicoin_0_locked: aggregator_v2::snapshot(&self.emojicoin_0_locked), + emojicoin_1_locked: aggregator_v2::snapshot(&self.emojicoin_1_locked) + } + ); + } + + inline fun emit_state( + escrow_state_ref: &Escrow, + melee_state_ref: &Melee, + participant_address: address + ) { + escrow_state_ref.emit_escrow_state(participant_address); + melee_state_ref.emit_melee_state(); + } + inline fun emit_vault_balance_update_with_singer_capability_ref( signer_capability_ref: &SignerCapability ) { @@ -727,8 +768,8 @@ module arena::emojicoin_arena { user_melees_ref_mut.user_melees_exited_melee_ids_add_if_not_contains(melee_id); user_melees_ref_mut.user_melees_unexited_melee_ids_remove_if_contains(melee_id); - // Emit escrow state event. - escrow_ref_mut.emit_escrow_state(participant_address); + // Emit state events. + emit_state(escrow_ref_mut, exited_melee_ref_mut, participant_address); } inline fun get_n_registered_markets(): u64 { From 3945e192934699c32f9d03c6a92d418983dacba5 Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:13:14 -0800 Subject: [PATCH 07/12] Add registry events --- .../sources/emojicoin_arena.move | 66 +++++++++++++++---- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index e7f78ea23..1f09ec61a 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -156,7 +156,7 @@ module arena::emojicoin_arena { } #[event] - /// Emitted after a user enters, swaps, or exits, representing the final state of their escrow. + /// Emitted after a user enters, swaps, or exits, representing the updated escrow state. struct EscrowState has copy, drop, store { user: address, melee_id: u64, @@ -169,7 +169,7 @@ module arena::emojicoin_arena { } #[event] - /// Emitted after a user enters, swaps, or exits, representing the final state of the melee. + /// Emitted after a user enters, swaps, or exits, representing the updated melee state. struct MeleeState has drop, store { melee_id: u64, available_rewards: u64, @@ -183,6 +183,16 @@ module arena::emojicoin_arena { emojicoin_1_locked: AggregatorSnapshot } + #[event] + /// Emitted after a user enters, swaps, or exits, representing the updated registry state. + struct RegistryState has drop, store { + n_melees: u64, + n_entrants: u64, + n_swaps: AggregatorSnapshot, + swaps_volume: AggregatorSnapshot, + octas_matched: u64 + } + #[event] /// Emitted whenever a new melee starts. struct NewMelee has copy, drop, store { @@ -425,7 +435,12 @@ module arena::emojicoin_arena { user_melees_ref_mut.user_melees_exited_melee_ids_remove_if_contains(melee_id); // Emit state events. - emit_state(escrow_ref_mut, current_melee_ref_mut, entrant_address); + emit_state( + registry_ref_mut, + current_melee_ref_mut, + escrow_ref_mut, + entrant_address + ); } #[randomness] @@ -530,7 +545,13 @@ module arena::emojicoin_arena { registry_ref_mut, false ) - else emit_state(escrow_ref_mut, swap_melee_ref_mut, swapper_address); + else + emit_state( + registry_ref_mut, + swap_melee_ref_mut, + escrow_ref_mut, + swapper_address + ); } @@ -610,7 +631,7 @@ module arena::emojicoin_arena { } inline fun emit_escrow_state( - self: &Escrow, + self: &mut Escrow, participant_address: address ) { event::emit( @@ -627,7 +648,8 @@ module arena::emojicoin_arena { ); } - inline fun emit_melee_state(self: &Melee) { + /// Uses mutable references to avoid borrowing issues. + inline fun emit_melee_state(self: &mut Melee) { event::emit( MeleeState { melee_id: self.melee_id, @@ -644,13 +666,30 @@ module arena::emojicoin_arena { ); } + /// Uses mutable references to avoid borrowing issues. + inline fun emit_registry_state(self: &mut Registry) { + event::emit( + RegistryState { + n_melees: self.melees_by_id.length(), + n_entrants: self.all_entrants.length(), + n_swaps: aggregator_v2::snapshot(&self.n_swaps), + swaps_volume: aggregator_v2::snapshot(&self.swaps_volume), + octas_matched: self.octas_matched + } + ); + } + + /// Uses mutable references to avoid borrowing issues. Emitted in ascending hierarchy order, + /// since registry state must be emitted after melee state. inline fun emit_state( - escrow_state_ref: &Escrow, - melee_state_ref: &Melee, + registry_ref_mut: &mut Registry, + melee_ref_mut: &mut Melee, + escrow_ref_mut: &mut Escrow, participant_address: address ) { - escrow_state_ref.emit_escrow_state(participant_address); - melee_state_ref.emit_melee_state(); + escrow_ref_mut.emit_escrow_state(participant_address); + melee_ref_mut.emit_melee_state(); + registry_ref_mut.emit_registry_state(); // Must emit after melee state for borrow checker. } inline fun emit_vault_balance_update_with_singer_capability_ref( @@ -769,7 +808,12 @@ module arena::emojicoin_arena { user_melees_ref_mut.user_melees_unexited_melee_ids_remove_if_contains(melee_id); // Emit state events. - emit_state(escrow_ref_mut, exited_melee_ref_mut, participant_address); + emit_state( + registry_ref_mut, + exited_melee_ref_mut, + escrow_ref_mut, + participant_address + ); } inline fun get_n_registered_markets(): u64 { From 4ae045ff25d42f2401f5f9ff0873af50873157c7 Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:20:09 -0800 Subject: [PATCH 08/12] Add enter event --- .../sources/emojicoin_arena.move | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index 1f09ec61a..38848e673 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -155,6 +155,18 @@ module arena::emojicoin_arena { unexited_melee_ids: SmartTable } + #[event] + /// Emitted whenever a user enters. + struct Enter has copy, drop, store { + user: address, + melee_id: u64, + input_amount: u64, + quote_volume: u64, + match_amount: u64, + emojicoin_0_proceeds: u64, + emojicoin_1_proceeds: u64 + } + #[event] /// Emitted after a user enters, swaps, or exits, representing the updated escrow state. struct EscrowState has copy, drop, store { @@ -377,6 +389,7 @@ module arena::emojicoin_arena { // Execute a swap then immediately move funds into escrow, updating total emojicoin locked // values based on side. let input_amount_after_matching = input_amount + match_amount; + let (emojicoin_0_proceeds, emojicoin_1_proceeds) = (0, 0); let quote_volume = if (buy_coin_0) { let (net_proceeds, quote_volume) = @@ -392,6 +405,7 @@ module arena::emojicoin_arena { current_melee_ref_mut.melee_emojicoin_0_locked_increment( coin::value(emojicoin_0_ref_mut) ); + emojicoin_0_proceeds = net_proceeds; quote_volume } else { let (net_proceeds, quote_volume) = @@ -407,6 +421,7 @@ module arena::emojicoin_arena { current_melee_ref_mut.melee_emojicoin_0_locked_increment( coin::value(emojicoin_1_ref_mut) ); + emojicoin_1_proceeds = net_proceeds; quote_volume }; @@ -434,6 +449,19 @@ module arena::emojicoin_arena { user_melees_ref_mut.user_melees_unexited_melee_ids_add_if_not_contains(melee_id); user_melees_ref_mut.user_melees_exited_melee_ids_remove_if_contains(melee_id); + // Emit enter event. + event::emit( + Enter { + user: entrant_address, + melee_id, + input_amount, + quote_volume, + match_amount, + emojicoin_0_proceeds, + emojicoin_1_proceeds + } + ); + // Emit state events. emit_state( registry_ref_mut, From 4204fd0c13f975e4e364429b179c337da899465f Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:28:43 -0800 Subject: [PATCH 09/12] Add exit event --- .../sources/emojicoin_arena.move | 43 ++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index 38848e673..c5ab48989 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -156,7 +156,7 @@ module arena::emojicoin_arena { } #[event] - /// Emitted whenever a user enters. + /// Emitted whenever a user enters, thereby executing a single-route swap into escrow. struct Enter has copy, drop, store { user: address, melee_id: u64, @@ -167,6 +167,18 @@ module arena::emojicoin_arena { emojicoin_1_proceeds: u64 } + #[event] + /// Emitted whenever a user exits, thereby withdrawing from escrow. + struct Exit has copy, drop, store { + user: address, + melee_id: u64, + emojicoin_0_proceeds: u64, + emojicoin_1_proceeds: u64, + octas_entered: u64, + octas_matched: u64, + tap_out_fee: u64 + } + #[event] /// Emitted after a user enters, swaps, or exits, representing the updated escrow state. struct EscrowState has copy, drop, store { @@ -793,14 +805,17 @@ module arena::emojicoin_arena { let exited_melee_ref_mut = registry_ref_mut.melees_by_id.borrow_mut(melee_id); // Charge tap out fee if applicable. + let octas_entered = escrow_ref_mut.octas_entered; + let tap_out_fee = 0; + let octas_matched = escrow_ref_mut.octas_matched; if (melee_is_current) { - let octas_matched = escrow_ref_mut.octas_matched; if (octas_matched > 0) { let vault_address = account::get_signer_capability_address( ®istry_ref_mut.signer_capability ); aptos_account::transfer(participant, vault_address, octas_matched); + tap_out_fee = octas_matched; emit_vault_balance_update_with_vault_address(vault_address); // Update melee state. @@ -815,11 +830,16 @@ module arena::emojicoin_arena { }; // Withdraw emojicoin balances from escrow. + let (emojicoin_0_proceeds, emojicoin_1_proceeds) = (0, 0); if (coin::value(&escrow_ref_mut.emojicoin_0) > 0) { - withdraw_from_escrow(participant_address, &mut escrow_ref_mut.emojicoin_0); + let emojicoin_0_ref_mut = &mut escrow_ref_mut.emojicoin_0; + emojicoin_0_proceeds = coin::value(emojicoin_0_ref_mut); + withdraw_from_escrow(participant_address, emojicoin_0_ref_mut); } else { - assert!(coin::value(&escrow_ref_mut.emojicoin_1) > 0, E_EXIT_NO_FUNDS); - withdraw_from_escrow(participant_address, &mut escrow_ref_mut.emojicoin_1); + let emojicoin_1_ref_mut = &mut escrow_ref_mut.emojicoin_1; + emojicoin_1_proceeds = coin::value(emojicoin_1_ref_mut); + assert!(emojicoin_1_proceeds > 0, E_EXIT_NO_FUNDS); + withdraw_from_escrow(participant_address, emojicoin_1_ref_mut); }; // Update melee state. @@ -835,6 +855,19 @@ module arena::emojicoin_arena { user_melees_ref_mut.user_melees_exited_melee_ids_add_if_not_contains(melee_id); user_melees_ref_mut.user_melees_unexited_melee_ids_remove_if_contains(melee_id); + // Emit exit event. + event::emit( + Exit { + user: participant_address, + melee_id, + emojicoin_0_proceeds, + emojicoin_1_proceeds, + octas_entered, + octas_matched, + tap_out_fee + } + ); + // Emit state events. emit_state( registry_ref_mut, From c265fd4829bcebb184d58e18a9736972f11ffa2b Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:35:19 -0800 Subject: [PATCH 10/12] Add swap event --- .../sources/emojicoin_arena.move | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index c5ab48989..fc023a2d7 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -179,6 +179,18 @@ module arena::emojicoin_arena { tap_out_fee: u64 } + #[event] + /// Emmitted whenever a user swaps within their escrow. + struct Swap has copy, drop, store { + user: address, + melee_id: u64, + quote_volume: u64, + emojicoin_0_in: u64, + emojicoin_0_proceeds: u64, + emojicoin_1_in: u64, + emojicoin_1_proceeds: u64 + } + #[event] /// Emitted after a user enters, swaps, or exits, representing the updated escrow state. struct EscrowState has copy, drop, store { @@ -523,8 +535,11 @@ module arena::emojicoin_arena { let emojicoin_1_ref_mut = &mut escrow_ref_mut.emojicoin_1; let emojicoin_0_locked_before_swap = coin::value(emojicoin_0_ref_mut); let emojicoin_1_locked_before_swap = coin::value(emojicoin_1_ref_mut); + let (emojicoin_0_in, emojicoin_0_proceeds, emojicoin_1_in, emojicoin_1_proceeds) = + (0, 0, 0, 0); let quote_volume = if (emojicoin_0_locked_before_swap > 0) { + emojicoin_0_in = emojicoin_0_locked_before_swap; let quote_volume = swap_within_escrow( swapper, @@ -534,18 +549,13 @@ module arena::emojicoin_arena { emojicoin_0_ref_mut, emojicoin_1_ref_mut ); - swap_melee_ref_mut.melee_emojicoin_0_locked_decrement( - emojicoin_0_locked_before_swap - ); - swap_melee_ref_mut.melee_emojicoin_1_locked_increment( - coin::value(emojicoin_1_ref_mut) - ); + swap_melee_ref_mut.melee_emojicoin_0_locked_decrement(emojicoin_0_in); + emojicoin_1_proceeds = coin::value(emojicoin_1_ref_mut); + swap_melee_ref_mut.melee_emojicoin_1_locked_increment(emojicoin_1_proceeds); quote_volume } else { assert!(emojicoin_1_locked_before_swap > 0, E_SWAP_NO_FUNDS); - swap_melee_ref_mut.melee_emojicoin_1_locked_decrement( - emojicoin_1_locked_before_swap - ); + emojicoin_1_in = emojicoin_1_locked_before_swap; let quote_volume = swap_within_escrow( swapper, @@ -555,12 +565,9 @@ module arena::emojicoin_arena { emojicoin_1_ref_mut, emojicoin_0_ref_mut ); - swap_melee_ref_mut.melee_emojicoin_1_locked_decrement( - emojicoin_1_locked_before_swap - ); - swap_melee_ref_mut.melee_emojicoin_0_locked_increment( - coin::value(emojicoin_0_ref_mut) - ); + swap_melee_ref_mut.melee_emojicoin_1_locked_decrement(emojicoin_1_in); + emojicoin_0_proceeds = coin::value(emojicoin_0_ref_mut); + swap_melee_ref_mut.melee_emojicoin_0_locked_increment(emojicoin_0_proceeds); quote_volume }; @@ -577,6 +584,19 @@ module arena::emojicoin_arena { escrow_ref_mut.escrow_n_swaps_increment(); escrow_ref_mut.escrow_swaps_volume_increment(quote_volume_u128); + // Emit swap event. + event::emit( + Swap { + user: swapper_address, + melee_id: escrow_ref_mut.melee_id, + quote_volume, + emojicoin_0_in, + emojicoin_0_proceeds, + emojicoin_1_in, + emojicoin_1_proceeds + } + ); + // Exit as needed, emitting state if not exiting, since exit inner function emits state. if (exit_once_done) exit_inner( From 0bec0543a9ffeeef765957f8fd811e64e881163a Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:36:10 -0800 Subject: [PATCH 11/12] Reorder fields for consistency --- src/move/emojicoin_arena/sources/emojicoin_arena.move | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index fc023a2d7..43384133d 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -172,11 +172,11 @@ module arena::emojicoin_arena { struct Exit has copy, drop, store { user: address, melee_id: u64, - emojicoin_0_proceeds: u64, - emojicoin_1_proceeds: u64, octas_entered: u64, octas_matched: u64, - tap_out_fee: u64 + tap_out_fee: u64, + emojicoin_0_proceeds: u64, + emojicoin_1_proceeds: u64 } #[event] From 5a79a12ed11938934a51cf785ce40c04628cf8d2 Mon Sep 17 00:00:00 2001 From: alnoki <43892045+alnoki@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:44:24 -0800 Subject: [PATCH 12/12] Address cspell --- src/move/emojicoin_arena/sources/emojicoin_arena.move | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/move/emojicoin_arena/sources/emojicoin_arena.move b/src/move/emojicoin_arena/sources/emojicoin_arena.move index 43384133d..1cdafc6d7 100644 --- a/src/move/emojicoin_arena/sources/emojicoin_arena.move +++ b/src/move/emojicoin_arena/sources/emojicoin_arena.move @@ -1,3 +1,4 @@ +// cspell:word funder // cspell:word unexited module arena::emojicoin_arena { @@ -180,7 +181,7 @@ module arena::emojicoin_arena { } #[event] - /// Emmitted whenever a user swaps within their escrow. + /// Emitted whenever a user swaps within their escrow. struct Swap has copy, drop, store { user: address, melee_id: u64,