Skip to content

Commit

Permalink
Add collection address, old and new values to collection mutation eve…
Browse files Browse the repository at this point in the history
…nts (#12807) (#12812)
  • Loading branch information
JohnChangUK authored Apr 8, 2024
1 parent ac71277 commit d43fc86
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
41 changes: 36 additions & 5 deletions aptos-move/framework/aptos-token-objects/doc/collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ directly understand the behavior in a writeset.
</dt>
<dd>

</dd>
<dt>
<code><a href="collection.md#0x4_collection">collection</a>: <a href="../../aptos-framework/doc/object.md#0x1_object_Object">object::Object</a>&lt;<a href="collection.md#0x4_collection_Collection">collection::Collection</a>&gt;</code>
</dt>
<dd>

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

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

</dd>
</dl>

Expand Down Expand Up @@ -612,7 +630,13 @@ If max_value is not set to U64_MAX, this ensures that a limited number of tokens

</dd>
<dt>
<code>max_supply: u64</code>
<code>old_max_supply: u64</code>
</dt>
<dd>

</dd>
<dt>
<code>new_max_supply: u64</code>
</dt>
<dd>

Expand Down Expand Up @@ -1598,10 +1622,14 @@ Once the collection has been created, the collection address should be saved for
<pre><code><b>public</b> <b>fun</b> <a href="collection.md#0x4_collection_set_name">set_name</a>(mutator_ref: &<a href="collection.md#0x4_collection_MutatorRef">MutatorRef</a>, name: String) <b>acquires</b> <a href="collection.md#0x4_collection_Collection">Collection</a> {
<b>assert</b>!(<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_length">string::length</a>(&name) &lt;= <a href="collection.md#0x4_collection_MAX_COLLECTION_NAME_LENGTH">MAX_COLLECTION_NAME_LENGTH</a>, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_out_of_range">error::out_of_range</a>(<a href="collection.md#0x4_collection_ECOLLECTION_NAME_TOO_LONG">ECOLLECTION_NAME_TOO_LONG</a>));
<b>let</b> <a href="collection.md#0x4_collection">collection</a> = <a href="collection.md#0x4_collection_borrow_mut">borrow_mut</a>(mutator_ref);
<b>let</b> old_name = <a href="collection.md#0x4_collection">collection</a>.name;
<a href="collection.md#0x4_collection">collection</a>.name = name;
<a href="../../aptos-framework/doc/event.md#0x1_event_emit">event::emit</a>(
<a href="collection.md#0x4_collection_Mutation">Mutation</a> { mutated_field_name: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(b"name") },
);
<a href="../../aptos-framework/doc/event.md#0x1_event_emit">event::emit</a>(<a href="collection.md#0x4_collection_Mutation">Mutation</a> {
mutated_field_name: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_utf8">string::utf8</a>(b"name") ,
<a href="collection.md#0x4_collection">collection</a>: <a href="../../aptos-framework/doc/object.md#0x1_object_address_to_object">object::address_to_object</a>(mutator_ref.self),
old_value: old_name,
new_value: name,
});
}
</code></pre>

Expand Down Expand Up @@ -1687,6 +1715,7 @@ Once the collection has been created, the collection address should be saved for
<pre><code><b>public</b> <b>fun</b> <a href="collection.md#0x4_collection_set_max_supply">set_max_supply</a>(mutator_ref: &<a href="collection.md#0x4_collection_MutatorRef">MutatorRef</a>, max_supply: u64) <b>acquires</b> <a href="collection.md#0x4_collection_ConcurrentSupply">ConcurrentSupply</a>, <a href="collection.md#0x4_collection_FixedSupply">FixedSupply</a> {
<b>let</b> <a href="collection.md#0x4_collection">collection</a> = <a href="../../aptos-framework/doc/object.md#0x1_object_address_to_object">object::address_to_object</a>&lt;<a href="collection.md#0x4_collection_Collection">Collection</a>&gt;(mutator_ref.self);
<b>let</b> collection_address = <a href="../../aptos-framework/doc/object.md#0x1_object_object_address">object::object_address</a>(&<a href="collection.md#0x4_collection">collection</a>);
<b>let</b> old_max_supply;

<b>if</b> (<b>exists</b>&lt;<a href="collection.md#0x4_collection_ConcurrentSupply">ConcurrentSupply</a>&gt;(collection_address)) {
<b>let</b> supply = <b>borrow_global_mut</b>&lt;<a href="collection.md#0x4_collection_ConcurrentSupply">ConcurrentSupply</a>&gt;(collection_address);
Expand All @@ -1695,6 +1724,7 @@ Once the collection has been created, the collection address should be saved for
max_supply &gt;= current_supply,
<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_out_of_range">error::out_of_range</a>(<a href="collection.md#0x4_collection_EINVALID_MAX_SUPPLY">EINVALID_MAX_SUPPLY</a>),
);
old_max_supply = <a href="../../aptos-framework/doc/aggregator_v2.md#0x1_aggregator_v2_max_value">aggregator_v2::max_value</a>(&supply.current_supply);
supply.current_supply = <a href="../../aptos-framework/doc/aggregator_v2.md#0x1_aggregator_v2_create_aggregator">aggregator_v2::create_aggregator</a>(max_supply);
<a href="../../aptos-framework/doc/aggregator_v2.md#0x1_aggregator_v2_add">aggregator_v2::add</a>(&<b>mut</b> supply.current_supply, current_supply);
} <b>else</b> <b>if</b> (<b>exists</b>&lt;<a href="collection.md#0x4_collection_FixedSupply">FixedSupply</a>&gt;(collection_address)) {
Expand All @@ -1703,12 +1733,13 @@ Once the collection has been created, the collection address should be saved for
max_supply &gt;= supply.current_supply,
<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_out_of_range">error::out_of_range</a>(<a href="collection.md#0x4_collection_EINVALID_MAX_SUPPLY">EINVALID_MAX_SUPPLY</a>),
);
old_max_supply = supply.max_supply;
supply.max_supply = max_supply;
} <b>else</b> {
<b>abort</b> <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_invalid_argument">error::invalid_argument</a>(<a href="collection.md#0x4_collection_ENO_MAX_SUPPLY_IN_COLLECTION">ENO_MAX_SUPPLY_IN_COLLECTION</a>)
};

<a href="../../aptos-framework/doc/event.md#0x1_event_emit">event::emit</a>(<a href="collection.md#0x4_collection_SetMaxSupply">SetMaxSupply</a> { <a href="collection.md#0x4_collection">collection</a>, max_supply });
<a href="../../aptos-framework/doc/event.md#0x1_event_emit">event::emit</a>(<a href="collection.md#0x4_collection_SetMaxSupply">SetMaxSupply</a> { <a href="collection.md#0x4_collection">collection</a>, old_max_supply, new_max_supply: max_supply });
}
</code></pre>

Expand Down
30 changes: 23 additions & 7 deletions aptos-move/framework/aptos-token-objects/sources/collection.move
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ module aptos_token_objects::collection {
/// directly understand the behavior in a writeset.
struct Mutation has drop, store {
mutated_field_name: String,
collection: Object<Collection>,
old_value: String,
new_value: String,
}

#[resource_group_member(group = aptos_framework::object::ObjectGroup)]
Expand Down Expand Up @@ -171,7 +174,8 @@ module aptos_token_objects::collection {
#[event]
struct SetMaxSupply has drop, store {
collection: Object<Collection>,
max_supply: u64,
old_max_supply: u64,
new_max_supply: u64,
}

/// Creates a fixed-sized collection, or a collection that supports a fixed amount of tokens.
Expand Down Expand Up @@ -618,10 +622,14 @@ module aptos_token_objects::collection {
public fun set_name(mutator_ref: &MutatorRef, name: String) acquires Collection {
assert!(string::length(&name) <= MAX_COLLECTION_NAME_LENGTH, error::out_of_range(ECOLLECTION_NAME_TOO_LONG));
let collection = borrow_mut(mutator_ref);
let old_name = collection.name;
collection.name = name;
event::emit(
Mutation { mutated_field_name: string::utf8(b"name") },
);
event::emit(Mutation {
mutated_field_name: string::utf8(b"name") ,
collection: object::address_to_object(mutator_ref.self),
old_value: old_name,
new_value: name,
});
}

public fun set_description(mutator_ref: &MutatorRef, description: String) acquires Collection {
Expand All @@ -647,6 +655,7 @@ module aptos_token_objects::collection {
public fun set_max_supply(mutator_ref: &MutatorRef, max_supply: u64) acquires ConcurrentSupply, FixedSupply {
let collection = object::address_to_object<Collection>(mutator_ref.self);
let collection_address = object::object_address(&collection);
let old_max_supply;

if (exists<ConcurrentSupply>(collection_address)) {
let supply = borrow_global_mut<ConcurrentSupply>(collection_address);
Expand All @@ -655,6 +664,7 @@ module aptos_token_objects::collection {
max_supply >= current_supply,
error::out_of_range(EINVALID_MAX_SUPPLY),
);
old_max_supply = aggregator_v2::max_value(&supply.current_supply);
supply.current_supply = aggregator_v2::create_aggregator(max_supply);
aggregator_v2::add(&mut supply.current_supply, current_supply);
} else if (exists<FixedSupply>(collection_address)) {
Expand All @@ -663,12 +673,13 @@ module aptos_token_objects::collection {
max_supply >= supply.current_supply,
error::out_of_range(EINVALID_MAX_SUPPLY),
);
old_max_supply = supply.max_supply;
supply.max_supply = max_supply;
} else {
abort error::invalid_argument(ENO_MAX_SUPPLY_IN_COLLECTION)
};

event::emit(SetMaxSupply { collection, max_supply });
event::emit(SetMaxSupply { collection, old_max_supply, new_max_supply: max_supply });
}

// Tests
Expand Down Expand Up @@ -776,6 +787,9 @@ module aptos_token_objects::collection {
assert!(new_collection_name == name(collection), 1);
event::was_event_emitted<Mutation>(&Mutation {
mutated_field_name: string::utf8(b"name"),
collection,
old_value: collection_name,
new_value: new_collection_name,
});
}

Expand Down Expand Up @@ -826,7 +840,8 @@ module aptos_token_objects::collection {

event::was_event_emitted<SetMaxSupply>(&SetMaxSupply {
collection: object::address_to_object<Collection>(collection_address),
max_supply: new_max_supply,
old_max_supply: max_supply,
new_max_supply,
});
}

Expand Down Expand Up @@ -860,7 +875,8 @@ module aptos_token_objects::collection {

event::was_event_emitted<SetMaxSupply>(&SetMaxSupply {
collection: object::address_to_object<Collection>(collection_address),
max_supply: current_supply,
old_max_supply: current_supply,
new_max_supply: current_supply,
});
}

Expand Down

0 comments on commit d43fc86

Please sign in to comment.