Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create_token method in token.move #8825

Merged
merged 11 commits into from
Jun 26, 2023
28 changes: 20 additions & 8 deletions aptos-move/framework/aptos-token-objects/doc/aptos_token.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ The key features are:


<pre><code><b>use</b> <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error">0x1::error</a>;
<b>use</b> <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/features.md#0x1_features">0x1::features</a>;
<b>use</b> <a href="../../aptos-framework/doc/object.md#0x1_object">0x1::object</a>;
<b>use</b> <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option">0x1::option</a>;
<b>use</b> <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">0x1::signer</a>;
Expand Down Expand Up @@ -471,14 +472,25 @@ With an existing collection, directly mint a soul bound token into the recipient
property_types: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;String&gt;,
property_values: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;,
): ConstructorRef <b>acquires</b> <a href="aptos_token.md#0x4_aptos_token_AptosCollection">AptosCollection</a> {
<b>let</b> constructor_ref = <a href="token.md#0x4_token_create_from_account">token::create_from_account</a>(
creator,
<a href="collection.md#0x4_collection">collection</a>,
description,
name,
<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_none">option::none</a>(),
uri,
);
<b>let</b> constructor_ref = <b>if</b> (std::features::auids_enabled()) {
<a href="token.md#0x4_token_create">token::create</a>(
creator,
<a href="collection.md#0x4_collection">collection</a>,
description,
name,
<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_none">option::none</a>(),
uri,
)
} <b>else</b> {
<a href="token.md#0x4_token_create_from_account">token::create_from_account</a>(
creator,
<a href="collection.md#0x4_collection">collection</a>,
description,
name,
<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_none">option::none</a>(),
uri,
)
};

<b>let</b> object_signer = <a href="../../aptos-framework/doc/object.md#0x1_object_generate_signer">object::generate_signer</a>(&constructor_ref);

Expand Down
40 changes: 39 additions & 1 deletion aptos-move/framework/aptos-token-objects/doc/token.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ token are:
- [Struct `MutationEvent`](#0x4_token_MutationEvent)
- [Constants](#@Constants_0)
- [Function `create_common`](#0x4_token_create_common)
- [Function `create`](#0x4_token_create)
- [Function `create_named_token`](#0x4_token_create_named_token)
- [Function `create_from_account`](#0x4_token_create_from_account)
- [Function `create_token_address`](#0x4_token_create_token_address)
Expand Down Expand Up @@ -361,6 +362,42 @@ The token name is over the maximum length



</details>

<a name="0x4_token_create"></a>

## Function `create`

Creates a new token object with a unique address and returns the ConstructorRef
for additional specialization.


<pre><code><b>public</b> <b>fun</b> <a href="token.md#0x4_token_create">create</a>(creator: &<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, collection_name: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, description: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, name: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, <a href="royalty.md#0x4_royalty">royalty</a>: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_Option">option::Option</a>&lt;<a href="royalty.md#0x4_royalty_Royalty">royalty::Royalty</a>&gt;, uri: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>): <a href="../../aptos-framework/doc/object.md#0x1_object_ConstructorRef">object::ConstructorRef</a>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="token.md#0x4_token_create">create</a>(
creator: &<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
collection_name: String,
description: String,
name: String,
<a href="royalty.md#0x4_royalty">royalty</a>: Option&lt;Royalty&gt;,
uri: String,
): ConstructorRef {
<b>let</b> creator_address = <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(creator);
<b>let</b> constructor_ref = <a href="../../aptos-framework/doc/object.md#0x1_object_create_object">object::create_object</a>(creator_address);
<a href="token.md#0x4_token_create_common">create_common</a>(&constructor_ref, creator_address, collection_name, description, name, <a href="royalty.md#0x4_royalty">royalty</a>, uri);
constructor_ref
}
</code></pre>



</details>

<a name="0x4_token_create_named_token"></a>
Expand Down Expand Up @@ -409,7 +446,8 @@ Creates a new token object from an account GUID and returns the ConstructorRef f
additional specialization.


<pre><code><b>public</b> <b>fun</b> <a href="token.md#0x4_token_create_from_account">create_from_account</a>(creator: &<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, collection_name: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, description: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, name: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, <a href="royalty.md#0x4_royalty">royalty</a>: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_Option">option::Option</a>&lt;<a href="royalty.md#0x4_royalty_Royalty">royalty::Royalty</a>&gt;, uri: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>): <a href="../../aptos-framework/doc/object.md#0x1_object_ConstructorRef">object::ConstructorRef</a>
<pre><code>#[deprecated]
<b>public</b> <b>fun</b> <a href="token.md#0x4_token_create_from_account">create_from_account</a>(creator: &<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, collection_name: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, description: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, name: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>, <a href="royalty.md#0x4_royalty">royalty</a>: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option_Option">option::Option</a>&lt;<a href="royalty.md#0x4_royalty_Royalty">royalty::Royalty</a>&gt;, uri: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/string.md#0x1_string_String">string::String</a>): <a href="../../aptos-framework/doc/object.md#0x1_object_ConstructorRef">object::ConstructorRef</a>
</code></pre>


Expand Down
27 changes: 19 additions & 8 deletions aptos-move/framework/aptos-token-objects/sources/aptos_token.move
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,25 @@ module aptos_token_objects::aptos_token {
property_types: vector<String>,
property_values: vector<vector<u8>>,
): ConstructorRef acquires AptosCollection {
let constructor_ref = token::create_from_account(
creator,
collection,
description,
name,
option::none(),
uri,
);
let constructor_ref = if (std::features::auids_enabled()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can you add an import for std::features and just call features::auids_enabled here to keep it consistent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

token::create(
creator,
collection,
description,
name,
option::none(),
uri,
)
} else {
token::create_from_account(
creator,
collection,
description,
name,
option::none(),
uri,
)
};

let object_signer = object::generate_signer(&constructor_ref);

Expand Down
48 changes: 47 additions & 1 deletion aptos-move/framework/aptos-token-objects/sources/token.move
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ module aptos_token_objects::token {
};
}

/// Creates a new token object with a unique address and returns the ConstructorRef
/// for additional specialization.
public fun create(
creator: &signer,
collection_name: String,
description: String,
name: String,
royalty: Option<Royalty>,
uri: String,
): ConstructorRef {
let creator_address = signer::address_of(creator);
let constructor_ref = object::create_object(creator_address);
create_common(&constructor_ref, creator_address, collection_name, description, name, royalty, uri);
constructor_ref
}

/// Creates a new token object from a token name and returns the ConstructorRef for
/// additional specialization.
public fun create_named_token(
Expand All @@ -124,6 +140,7 @@ module aptos_token_objects::token {
constructor_ref
}

#[deprecated]
/// Creates a new token object from an account GUID and returns the ConstructorRef for
/// additional specialization.
public fun create_from_account(
Expand Down Expand Up @@ -514,7 +531,7 @@ module aptos_token_objects::token {
}

#[test(creator = @0x123)]
fun test_burn_and_delete(creator: &signer) acquires Token {
fun test_create_from_account_burn_and_delete(creator: &signer) acquires Token {
use aptos_framework::account;

let collection_name = string::utf8(b"collection name");
Expand All @@ -538,6 +555,35 @@ module aptos_token_objects::token {
assert!(!object::is_object(token_addr), 2);
}

#[test(creator = @0x123,fx = @std)]
fun test_create_burn_and_delete(creator: &signer, fx: signer) acquires Token {
use aptos_framework::account;
use std::features;

let feature = features::get_auids();
features::change_feature_flags(&fx, vector[feature], vector[]);

let collection_name = string::utf8(b"collection name");
let token_name = string::utf8(b"token name");

create_collection_helper(creator, collection_name, 1);
account::create_account_for_test(signer::address_of(creator));
let constructor_ref = create(
creator,
collection_name,
string::utf8(b"token description"),
token_name,
option::none(),
string::utf8(b"token uri"),
);
let burn_ref = generate_burn_ref(&constructor_ref);
let token_addr = object::address_from_constructor_ref(&constructor_ref);
assert!(exists<Token>(token_addr), 0);
burn(burn_ref);
assert!(!exists<Token>(token_addr), 1);
assert!(!object::is_object(token_addr), 2);
}

#[test_only]
fun create_collection_helper(creator: &signer, collection_name: String, max_supply: u64) {
collection::create_fixed_collection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module token_objects::ambassador {
// is used to generate the refs of the token.
let uri = base_uri;
string::append(&mut uri, string::utf8(RANK_BRONZE));
let constructor_ref = token::create_named_token(
let constructor_ref = token::create(
creator,
collection,
description,
Expand Down