Skip to content

Commit

Permalink
Merge pull request #31 from Entropy-Foundation/coin_event_fix
Browse files Browse the repository at this point in the history
fixes #30 add account, coin_type to events
  • Loading branch information
sjoshisupra authored Jul 25, 2024
2 parents 832a0de + fa7bea2 commit 9f69ec7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
58 changes: 41 additions & 17 deletions aptos-move/framework/supra-framework/doc/coin.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ This module provides the foundation for typesafe Coins.
- [Resource `CoinStore`](#0x1_coin_CoinStore)
- [Resource `SupplyConfig`](#0x1_coin_SupplyConfig)
- [Resource `CoinInfo`](#0x1_coin_CoinInfo)
- [Struct `DepositEvent`](#0x1_coin_DepositEvent)
- [Struct `WithdrawEvent`](#0x1_coin_WithdrawEvent)
- [Struct `CoinDeposit`](#0x1_coin_CoinDeposit)
- [Struct `CoinWithdraw`](#0x1_coin_CoinWithdraw)
- [Struct `MintCapability`](#0x1_coin_MintCapability)
- [Struct `FreezeCapability`](#0x1_coin_FreezeCapability)
- [Struct `BurnCapability`](#0x1_coin_BurnCapability)
Expand Down Expand Up @@ -204,13 +204,13 @@ These are kept in a single resource to ensure locality of data.

</dd>
<dt>
<code>deposit_events: <a href="event.md#0x1_event_EventHandle">event::EventHandle</a>&lt;<a href="coin.md#0x1_coin_DepositEvent">coin::DepositEvent</a>&gt;</code>
<code>deposit_events: <a href="event.md#0x1_event_EventHandle">event::EventHandle</a>&lt;<a href="coin.md#0x1_coin_CoinDeposit">coin::CoinDeposit</a>&gt;</code>
</dt>
<dd>

</dd>
<dt>
<code>withdraw_events: <a href="event.md#0x1_event_EventHandle">event::EventHandle</a>&lt;<a href="coin.md#0x1_coin_WithdrawEvent">coin::WithdrawEvent</a>&gt;</code>
<code>withdraw_events: <a href="event.md#0x1_event_EventHandle">event::EventHandle</a>&lt;<a href="coin.md#0x1_coin_CoinWithdraw">coin::CoinWithdraw</a>&gt;</code>
</dt>
<dd>

Expand Down Expand Up @@ -298,14 +298,14 @@ Information about a specific coin type. Stored on the creator of the coin's acco

</details>

<a id="0x1_coin_DepositEvent"></a>
<a id="0x1_coin_CoinDeposit"></a>

## Struct `DepositEvent`
## Struct `CoinDeposit`

Event emitted when some amount of a coin is deposited into an account.


<pre><code><b>struct</b> <a href="coin.md#0x1_coin_DepositEvent">DepositEvent</a> <b>has</b> drop, store
<pre><code><b>struct</b> <a href="coin.md#0x1_coin_CoinDeposit">CoinDeposit</a> <b>has</b> drop, store
</code></pre>


Expand All @@ -316,6 +316,18 @@ Event emitted when some amount of a coin is deposited into an account.

<dl>
<dt>
<code>coin_type: <a href="../../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a></code>
</dt>
<dd>

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

</dd>
<dt>
<code>amount: u64</code>
</dt>
<dd>
Expand All @@ -326,14 +338,14 @@ Event emitted when some amount of a coin is deposited into an account.

</details>

<a id="0x1_coin_WithdrawEvent"></a>
<a id="0x1_coin_CoinWithdraw"></a>

## Struct `WithdrawEvent`
## Struct `CoinWithdraw`

Event emitted when some amount of a coin is withdrawn from an account.


<pre><code><b>struct</b> <a href="coin.md#0x1_coin_WithdrawEvent">WithdrawEvent</a> <b>has</b> drop, store
<pre><code><b>struct</b> <a href="coin.md#0x1_coin_CoinWithdraw">CoinWithdraw</a> <b>has</b> drop, store
</code></pre>


Expand All @@ -344,6 +356,18 @@ Event emitted when some amount of a coin is withdrawn from an account.

<dl>
<dt>
<code>coin_type: <a href="../../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a></code>
</dt>
<dd>

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

</dd>
<dt>
<code>amount: u64</code>
</dt>
<dd>
Expand Down Expand Up @@ -1243,9 +1267,9 @@ Deposit the coin balance into the recipient's account and emit an event.
<a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_permission_denied">error::permission_denied</a>(<a href="coin.md#0x1_coin_EFROZEN">EFROZEN</a>),
);

<a href="event.md#0x1_event_emit_event">event::emit_event</a>&lt;<a href="coin.md#0x1_coin_DepositEvent">DepositEvent</a>&gt;(
<a href="event.md#0x1_event_emit_event">event::emit_event</a>&lt;<a href="coin.md#0x1_coin_CoinDeposit">CoinDeposit</a>&gt;(
&<b>mut</b> coin_store.deposit_events,
<a href="coin.md#0x1_coin_DepositEvent">DepositEvent</a> { amount: <a href="coin.md#0x1_coin">coin</a>.value },
<a href="coin.md#0x1_coin_CoinDeposit">CoinDeposit</a> { coin_type: type_name&lt;CoinType&gt;(), <a href="account.md#0x1_account">account</a>: account_addr, amount: <a href="coin.md#0x1_coin">coin</a>.value, },
);

<a href="coin.md#0x1_coin_merge">merge</a>(&<b>mut</b> coin_store.<a href="coin.md#0x1_coin">coin</a>, <a href="coin.md#0x1_coin">coin</a>);
Expand All @@ -1261,7 +1285,7 @@ Deposit the coin balance into the recipient's account and emit an event.
## Function `force_deposit`

Deposit the coin balance into the recipient's account without checking if the account is frozen.
This is for internal use only and doesn't emit an DepositEvent.
This is for internal use only and doesn't emit an CoinDeposit.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="coin.md#0x1_coin_force_deposit">force_deposit</a>&lt;CoinType&gt;(account_addr: <b>address</b>, <a href="coin.md#0x1_coin">coin</a>: <a href="coin.md#0x1_coin_Coin">coin::Coin</a>&lt;CoinType&gt;)
Expand Down Expand Up @@ -1814,8 +1838,8 @@ Returns minted <code><a href="coin.md#0x1_coin_Coin">Coin</a></code>.
<b>let</b> coin_store = <a href="coin.md#0x1_coin_CoinStore">CoinStore</a>&lt;CoinType&gt; {
<a href="coin.md#0x1_coin">coin</a>: <a href="coin.md#0x1_coin_Coin">Coin</a> { value: 0 },
frozen: <b>false</b>,
deposit_events: <a href="account.md#0x1_account_new_event_handle">account::new_event_handle</a>&lt;<a href="coin.md#0x1_coin_DepositEvent">DepositEvent</a>&gt;(<a href="account.md#0x1_account">account</a>),
withdraw_events: <a href="account.md#0x1_account_new_event_handle">account::new_event_handle</a>&lt;<a href="coin.md#0x1_coin_WithdrawEvent">WithdrawEvent</a>&gt;(<a href="account.md#0x1_account">account</a>),
deposit_events: <a href="account.md#0x1_account_new_event_handle">account::new_event_handle</a>&lt;<a href="coin.md#0x1_coin_CoinDeposit">CoinDeposit</a>&gt;(<a href="account.md#0x1_account">account</a>),
withdraw_events: <a href="account.md#0x1_account_new_event_handle">account::new_event_handle</a>&lt;<a href="coin.md#0x1_coin_CoinWithdraw">CoinWithdraw</a>&gt;(<a href="account.md#0x1_account">account</a>),
};
<b>move_to</b>(<a href="account.md#0x1_account">account</a>, coin_store);
}
Expand Down Expand Up @@ -1912,9 +1936,9 @@ Withdraw specified <code>amount</code> of coin <code>CoinType</code> from the si
<a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_permission_denied">error::permission_denied</a>(<a href="coin.md#0x1_coin_EFROZEN">EFROZEN</a>),
);

<a href="event.md#0x1_event_emit_event">event::emit_event</a>&lt;<a href="coin.md#0x1_coin_WithdrawEvent">WithdrawEvent</a>&gt;(
<a href="event.md#0x1_event_emit_event">event::emit_event</a>&lt;<a href="coin.md#0x1_coin_CoinWithdraw">CoinWithdraw</a>&gt;(
&<b>mut</b> coin_store.withdraw_events,
<a href="coin.md#0x1_coin_WithdrawEvent">WithdrawEvent</a> { amount },
<a href="coin.md#0x1_coin_CoinWithdraw">CoinWithdraw</a> { coin_type: type_name&lt;CoinType&gt;(), <a href="account.md#0x1_account">account</a>: account_addr, amount },
);

<a href="coin.md#0x1_coin_extract">extract</a>(&<b>mut</b> coin_store.<a href="coin.md#0x1_coin">coin</a>, amount)
Expand Down
30 changes: 17 additions & 13 deletions aptos-move/framework/supra-framework/sources/coin.move
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// This module provides the foundation for typesafe Coins.
module supra_framework::coin {
use std::string;
use std::string::{Self,String};
use std::error;
use std::option::{Self, Option};
use std::signer;
Expand All @@ -12,7 +12,7 @@ module supra_framework::coin {
use supra_framework::optional_aggregator::{Self, OptionalAggregator};
use supra_framework::system_addresses;

use aptos_std::type_info;
use aptos_std::type_info::{Self,type_name};

friend supra_framework::supra_coin;
friend supra_framework::genesis;
Expand Down Expand Up @@ -92,8 +92,8 @@ module supra_framework::coin {
struct CoinStore<phantom CoinType> has key {
coin: Coin<CoinType>,
frozen: bool,
deposit_events: EventHandle<DepositEvent>,
withdraw_events: EventHandle<WithdrawEvent>,
deposit_events: EventHandle<CoinDeposit>,
withdraw_events: EventHandle<CoinWithdraw>,
}

/// Maximum possible coin supply.
Expand All @@ -120,12 +120,16 @@ module supra_framework::coin {
}

/// Event emitted when some amount of a coin is deposited into an account.
struct DepositEvent has drop, store {
struct CoinDeposit has drop, store {
coin_type: String,
account: address,
amount: u64,
}

/// Event emitted when some amount of a coin is withdrawn from an account.
struct WithdrawEvent has drop, store {
struct CoinWithdraw has drop, store {
coin_type: String,
account: address,
amount: u64,
}

Expand Down Expand Up @@ -358,16 +362,16 @@ module supra_framework::coin {
error::permission_denied(EFROZEN),
);

event::emit_event<DepositEvent>(
event::emit_event<CoinDeposit>(
&mut coin_store.deposit_events,
DepositEvent { amount: coin.value },
CoinDeposit { coin_type: type_name<CoinType>(), account: account_addr, amount: coin.value, },
);

merge(&mut coin_store.coin, coin);
}

/// Deposit the coin balance into the recipient's account without checking if the account is frozen.
/// This is for internal use only and doesn't emit an DepositEvent.
/// This is for internal use only and doesn't emit an CoinDeposit.
public(friend) fun force_deposit<CoinType>(account_addr: address, coin: Coin<CoinType>) acquires CoinStore {
assert!(
is_account_registered<CoinType>(account_addr),
Expand Down Expand Up @@ -629,8 +633,8 @@ module supra_framework::coin {
let coin_store = CoinStore<CoinType> {
coin: Coin { value: 0 },
frozen: false,
deposit_events: account::new_event_handle<DepositEvent>(account),
withdraw_events: account::new_event_handle<WithdrawEvent>(account),
deposit_events: account::new_event_handle<CoinDeposit>(account),
withdraw_events: account::new_event_handle<CoinWithdraw>(account),
};
move_to(account, coin_store);
}
Expand Down Expand Up @@ -667,9 +671,9 @@ module supra_framework::coin {
error::permission_denied(EFROZEN),
);

event::emit_event<WithdrawEvent>(
event::emit_event<CoinWithdraw>(
&mut coin_store.withdraw_events,
WithdrawEvent { amount },
CoinWithdraw { coin_type: type_name<CoinType>(), account: account_addr, amount },
);

extract(&mut coin_store.coin, amount)
Expand Down

0 comments on commit 9f69ec7

Please sign in to comment.