Skip to content

Commit

Permalink
[event v2] add coin register event
Browse files Browse the repository at this point in the history
  • Loading branch information
lightmark committed Oct 17, 2024
1 parent 026164a commit ee92517
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 deletions.
56 changes: 50 additions & 6 deletions aptos-move/framework/aptos-framework/doc/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Resource `Account`](#0x1_account_Account)
- [Struct `KeyRotationEvent`](#0x1_account_KeyRotationEvent)
- [Struct `CoinRegisterEvent`](#0x1_account_CoinRegisterEvent)
- [Struct `CoinRegister`](#0x1_account_CoinRegister)
- [Struct `CapabilityOffer`](#0x1_account_CapabilityOffer)
- [Struct `RotationCapability`](#0x1_account_RotationCapability)
- [Struct `SignerCapability`](#0x1_account_SignerCapability)
Expand Down Expand Up @@ -276,6 +277,40 @@ Resource representing an account.
</dl>


</details>

<a id="0x1_account_CoinRegister"></a>

## Struct `CoinRegister`



<pre><code>#[<a href="event.md#0x1_event">event</a>]
<b>struct</b> <a href="account.md#0x1_account_CoinRegister">CoinRegister</a> <b>has</b> drop, store
</code></pre>



<details>
<summary>Fields</summary>


<dl>
<dt>
<code><a href="account.md#0x1_account">account</a>: <b>address</b></code>
</dt>
<dd>

</dd>
<dt>
<code><a href="../../aptos-stdlib/doc/type_info.md#0x1_type_info">type_info</a>: <a href="../../aptos-stdlib/doc/type_info.md#0x1_type_info_TypeInfo">type_info::TypeInfo</a></code>
</dt>
<dd>

</dd>
</dl>


</details>

<a id="0x1_account_CapabilityOffer"></a>
Expand Down Expand Up @@ -2109,12 +2144,21 @@ Coin management methods.

<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="account.md#0x1_account_register_coin">register_coin</a>&lt;CoinType&gt;(account_addr: <b>address</b>) <b>acquires</b> <a href="account.md#0x1_account_Account">Account</a> {
<b>let</b> <a href="account.md#0x1_account">account</a> = <b>borrow_global_mut</b>&lt;<a href="account.md#0x1_account_Account">Account</a>&gt;(account_addr);
<a href="event.md#0x1_event_emit_event">event::emit_event</a>&lt;<a href="account.md#0x1_account_CoinRegisterEvent">CoinRegisterEvent</a>&gt;(
&<b>mut</b> <a href="account.md#0x1_account">account</a>.coin_register_events,
<a href="account.md#0x1_account_CoinRegisterEvent">CoinRegisterEvent</a> {
<a href="../../aptos-stdlib/doc/type_info.md#0x1_type_info">type_info</a>: <a href="../../aptos-stdlib/doc/type_info.md#0x1_type_info_type_of">type_info::type_of</a>&lt;CoinType&gt;(),
},
);
<b>if</b> (std::features::module_event_migration_enabled()) {
<a href="event.md#0x1_event_emit">event::emit</a>(
<a href="account.md#0x1_account_CoinRegister">CoinRegister</a> {
<a href="account.md#0x1_account">account</a>: account_addr,
<a href="../../aptos-stdlib/doc/type_info.md#0x1_type_info">type_info</a>: <a href="../../aptos-stdlib/doc/type_info.md#0x1_type_info_type_of">type_info::type_of</a>&lt;CoinType&gt;(),
},
);
} <b>else</b> {
<a href="event.md#0x1_event_emit_event">event::emit_event</a>&lt;<a href="account.md#0x1_account_CoinRegisterEvent">CoinRegisterEvent</a>&gt;(
&<b>mut</b> <a href="account.md#0x1_account">account</a>.coin_register_events,
<a href="account.md#0x1_account_CoinRegisterEvent">CoinRegisterEvent</a> {
<a href="../../aptos-stdlib/doc/type_info.md#0x1_type_info">type_info</a>: <a href="../../aptos-stdlib/doc/type_info.md#0x1_type_info_type_of">type_info::type_of</a>&lt;CoinType&gt;(),
},
);
}
}
</code></pre>

Expand Down
27 changes: 21 additions & 6 deletions aptos-move/framework/aptos-framework/sources/account.move
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ module aptos_framework::account {
type_info: TypeInfo,
}

#[event]
struct CoinRegister has drop, store {
account: address,
type_info: TypeInfo,
}

struct CapabilityOffer<phantom T> has store { for: Option<address> }

struct RotationCapability has drop, store { account: address }
Expand Down Expand Up @@ -798,12 +804,21 @@ module aptos_framework::account {

public(friend) fun register_coin<CoinType>(account_addr: address) acquires Account {
let account = borrow_global_mut<Account>(account_addr);
event::emit_event<CoinRegisterEvent>(
&mut account.coin_register_events,
CoinRegisterEvent {
type_info: type_info::type_of<CoinType>(),
},
);
if (std::features::module_event_migration_enabled()) {
event::emit(
CoinRegister {
account: account_addr,
type_info: type_info::type_of<CoinType>(),
},
);
} else {
event::emit_event<CoinRegisterEvent>(
&mut account.coin_register_events,
CoinRegisterEvent {
type_info: type_info::type_of<CoinType>(),
},
);
}
}

///////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit ee92517

Please sign in to comment.