From cbb7cb3086d15eef0c171a5080be91dfe509fb9b Mon Sep 17 00:00:00 2001 From: CapCap Date: Fri, 23 Sep 2022 22:10:30 -0700 Subject: [PATCH 1/2] Add helpers to token --- .../framework/aptos-token/sources/token.move | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/aptos-move/framework/aptos-token/sources/token.move b/aptos-move/framework/aptos-token/sources/token.move index dacd2bd906045..3d51faf063d9a 100644 --- a/aptos-move/framework/aptos-token/sources/token.move +++ b/aptos-move/framework/aptos-token/sources/token.move @@ -358,7 +358,7 @@ module aptos_token::token { name, ); // only creator of the tokendata can mint more tokens for now - assert!(token_data_id.creator == signer::address_of(account), error::permission_denied(ENO_MINT_CAPABILITY)); + assert!(token_data_id.creator == signer::address_of(account), error::permission_denied(ENO_MINT_CAPABILITY)); mint_token( account, token_data_id, @@ -474,7 +474,7 @@ module aptos_token::token { token_data.largest_property_version = cur_property_version; // burn the orignial property_version 0 token after mutation - let Token {id: _, amount: _, token_properties: _} = token; + let Token { id: _, amount: _, token_properties: _ } = token; new_token_id } else { // only 1 copy for the token with property verion bigger than 0 @@ -635,7 +635,7 @@ module aptos_token::token { public fun split(dst_token: &mut Token, amount: u64): Token { assert!(dst_token.id.property_version == 0, error::invalid_state(ENFT_NOT_SPLITABLE)); - assert!(dst_token.amount > amount, error::invalid_argument(ETOKEN_SPLIT_AMOUNT_LARGER_THAN_TOKEN_AMOUNT)); + assert!(dst_token.amount > amount, error::invalid_argument(ETOKEN_SPLIT_AMOUNT_LARGER_THAN_TOKEN_AMOUNT)); dst_token.amount = dst_token.amount - amount; Token { id: dst_token.id, @@ -718,7 +718,7 @@ module aptos_token::token { let token_store = borrow_global_mut(account_addr); event::emit_event( &mut token_store.withdraw_events, - WithdrawEvent{ id, amount }, + WithdrawEvent { id, amount }, ); let tokens = &mut borrow_global_mut(account_addr).tokens; assert!( @@ -729,7 +729,7 @@ module aptos_token::token { let balance = &mut table::borrow_mut(tokens, id).amount; if (*balance > amount) { *balance = *balance - amount; - Token{ id, amount, token_properties: property_map::empty() } + Token { id, amount, token_properties: property_map::empty() } } else { table::remove(tokens, id) } @@ -754,7 +754,7 @@ module aptos_token::token { if (!exists(account_addr)) { move_to( creator, - Collections{ + Collections { collection_data: table::new(), token_data: table::new(), create_collection_events: account::new_event_handle(creator), @@ -772,7 +772,7 @@ module aptos_token::token { ); let mutability_config = create_collection_mutability_config(&mutate_setting); - let collection = CollectionData{ + let collection = CollectionData { description, name: *&name, uri, @@ -869,7 +869,7 @@ module aptos_token::token { largest_property_version: 0, supply: 0, uri, - royalty: Royalty{ + royalty: Royalty { royalty_points_denominator, royalty_points_numerator, payee_address: royalty_payee_address, @@ -923,7 +923,7 @@ module aptos_token::token { assert!(table::contains(all_token_data, token_data_id), error::not_found(ETOKEN_DATA_NOT_PUBLISHED)); let token_data = table::borrow(all_token_data, token_data_id); - if (token_data.maximum > 0 ) { + if (token_data.maximum > 0) { option::some(token_data.supply) } else { option::none() @@ -944,7 +944,7 @@ module aptos_token::token { } public fun create_token_mutability_config(mutate_setting: &vector): TokenMutabilityConfig { - TokenMutabilityConfig{ + TokenMutabilityConfig { maximum: *vector::borrow(mutate_setting, TOKEN_MAX_MUTABLE_IND), uri: *vector::borrow(mutate_setting, TOKEN_URI_MUTABLE_IND), royalty: *vector::borrow(mutate_setting, TOKEN_ROYALTY_MUTABLE_IND), @@ -954,7 +954,7 @@ module aptos_token::token { } public fun create_collection_mutability_config(mutate_setting: &vector): CollectionMutabilityConfig { - CollectionMutabilityConfig{ + CollectionMutabilityConfig { description: *vector::borrow(mutate_setting, COLLECTION_DESCRIPTION_MUTABLE_IND), uri: *vector::borrow(mutate_setting, COLLECTION_URI_MUTABLE_IND), maximum: *vector::borrow(mutate_setting, COLLECTION_MAX_MUTABLE_IND), @@ -1014,7 +1014,7 @@ module aptos_token::token { assert!(table::contains(all_token_data, token_data_id), error::not_found(ETOKEN_DATA_NOT_PUBLISHED)); let token_data = table::borrow_mut(all_token_data, token_data_id); - if (token_data.maximum > 0 ) { + if (token_data.maximum > 0) { assert!(token_data.supply + amount <= token_data.maximum, error::invalid_argument(EMINT_WOULD_EXCEED_TOKEN_MAXIMUM)); token_data.supply = token_data.supply + amount; }; @@ -1022,7 +1022,7 @@ module aptos_token::token { // we add more tokens with property_version 0 let token_id = create_token_id(token_data_id, 0); deposit_token(account, - Token{ + Token { id: token_id, amount, token_properties: property_map::empty(), // same as default properties no need to store @@ -1057,7 +1057,7 @@ module aptos_token::token { assert!(table::contains(all_token_data, token_data_id), error::not_found(ETOKEN_DATA_NOT_PUBLISHED)); let token_data = table::borrow_mut(all_token_data, token_data_id); - if (token_data.maximum > 0 ) { + if (token_data.maximum > 0) { assert!(token_data.supply + amount <= token_data.maximum, error::invalid_argument(EMINT_WOULD_EXCEED_TOKEN_MAXIMUM)); token_data.supply = token_data.supply + amount; }; @@ -1065,7 +1065,7 @@ module aptos_token::token { // we add more tokens with property_version 0 let token_id = create_token_id(token_data_id, 0); direct_deposit(receiver, - Token{ + Token { id: token_id, amount, token_properties: property_map::empty(), // same as default properties no need to store @@ -1190,7 +1190,7 @@ module aptos_token::token { let token_store = borrow_global_mut(signer::address_of(owner)); event::emit_event( &mut token_store.burn_events, - BurnTokenEvent { id: token_id, amount: burned_amount}, + BurnTokenEvent { id: token_id, amount: burned_amount }, ); // Decrease the supply correspondingly by the amount of tokens burned. @@ -1206,7 +1206,7 @@ module aptos_token::token { // Delete the token_data if supply drops to 0. if (token_data.supply == 0) { - let TokenData{ + let TokenData { maximum: _, largest_property_version: _, supply: _, @@ -1222,7 +1222,7 @@ module aptos_token::token { } public fun create_token_id(token_data_id: TokenDataId, property_version: u64): TokenId { - TokenId{ + TokenId { token_data_id, property_version, } @@ -1244,13 +1244,16 @@ module aptos_token::token { name: String, property_version: u64, ): TokenId { - TokenId{ + TokenId { token_data_id: create_token_data_id(creator, collection, name), property_version, } } public fun balance_of(owner: address, id: TokenId): u64 acquires TokenStore { + if (!exists(owner)) { + return 0 + }; let token_store = borrow_global(owner); if (table::contains(&token_store.tokens, id)) { table::borrow(&token_store.tokens, id).amount @@ -1259,6 +1262,10 @@ module aptos_token::token { } } + public fun has_token_store(owner: address): bool { + exists(owner) + } + public fun get_royalty(token_id: TokenId): Royalty acquires Collections { let token_data_id = token_id.token_data_id; let creator_addr = token_data_id.creator; @@ -1440,6 +1447,9 @@ module aptos_token::token { vector[false, false, false], vector[false, false, false, false, false], ); + assert!(balance_of(signer::address_of(&owner), token_id) == 0, 1); + account::create_account_for_test(signer::address_of(&owner)); + direct_transfer(&creator, &owner, token_id, 1); let token = withdraw_token(&owner, token_id, 1); deposit_token(&creator, token); @@ -1727,7 +1737,7 @@ module aptos_token::token { assert!(property_map::read_u64(&og_pm, &string::utf8(b"attack")) == 1, 1); } - #[test(framework = @0x1, creator=@0xcafe)] + #[test(framework = @0x1, creator = @0xcafe)] fun test_withdraw_with_proof(creator: &signer, framework: &signer): Token acquires TokenStore, Collections { timestamp::set_time_has_started_for_testing(framework); account::create_account_for_test(signer::address_of(creator)); @@ -1746,7 +1756,7 @@ module aptos_token::token { timestamp::update_global_time_for_test(1000000); // provide the proof to the account - let cap = create_withdraw_capability( + let cap = create_withdraw_capability( creator, // ask user to provide address to avoid ambiguity from rotated keys token_id, 1, @@ -1756,7 +1766,7 @@ module aptos_token::token { withdraw_with_capability(cap) } - #[test(creator=@0xcafe, another_creator=@0xde)] + #[test(creator = @0xcafe, another_creator = @0xde)] fun test_burn_token_from_both_limited_and_unlimited( creator: &signer, another_creator: &signer, @@ -1805,7 +1815,7 @@ module aptos_token::token { assert!(pre - aft == 1, 1); } - #[test(creator=@0xcafe, owner=@0xafe)] + #[test(creator = @0xcafe, owner = @0xafe)] fun test_mint_token_to_different_address( creator: &signer, owner: &signer, @@ -1831,7 +1841,7 @@ module aptos_token::token { assert!(balance_of(owner_addr, token_id) == 1, 1); } - #[test(creator=@0xcafe, owner=@0xafe)] + #[test(creator = @0xcafe, owner = @0xafe)] #[expected_failure(abort_code = 327696)] fun test_opt_in_direct_transfer_fail( creator: &signer, @@ -1856,7 +1866,7 @@ module aptos_token::token { transfer(creator, token_id, owner_addr, 1); } - #[test(creator=@0xcafe, owner=@0xafe)] + #[test(creator = @0xcafe, owner = @0xafe)] #[expected_failure(abort_code = 327696)] fun test_opt_in_direct_deposit_fail( creator: &signer, From d5f868889f52c44590a6b7052c3fb6923e7202b1 Mon Sep 17 00:00:00 2001 From: CapCap Date: Sat, 24 Sep 2022 20:26:30 -0700 Subject: [PATCH 2/2] lint --- .../framework/aptos-token/sources/token.move | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/aptos-move/framework/aptos-token/sources/token.move b/aptos-move/framework/aptos-token/sources/token.move index 3d51faf063d9a..ee0b5807f77d0 100644 --- a/aptos-move/framework/aptos-token/sources/token.move +++ b/aptos-move/framework/aptos-token/sources/token.move @@ -417,7 +417,7 @@ module aptos_token::token { property_map::update_property_map(&mut token_data.default_properties, keys, values, types); } - public fun mutate_one_token( + public fun mutate_one_token( account: &signer, token_owner: address, token_id: TokenId, @@ -1126,7 +1126,7 @@ module aptos_token::token { let token_store = borrow_global_mut(owner); event::emit_event( &mut token_store.burn_events, - BurnTokenEvent { id: token_id, amount: burned_amount}, + BurnTokenEvent { id: token_id, amount: burned_amount }, ); if (token_data.maximum > 0) { @@ -1134,7 +1134,7 @@ module aptos_token::token { // Delete the token_data if supply drops to 0. if (token_data.supply == 0) { - let TokenData{ + let TokenData { maximum: _, largest_property_version: _, supply: _, @@ -1179,7 +1179,7 @@ module aptos_token::token { ); assert!( - property_map::contains_key(&token_data.default_properties, &string::utf8(BURNABLE_BY_OWNER)), + property_map::contains_key(&token_data.default_properties, &string::utf8(BURNABLE_BY_OWNER)), error::permission_denied(EOWNER_CANNOT_BURN_TOKEN) ); let burn_by_owner_flag = property_map::read_bool(&token_data.default_properties, &string::utf8(BURNABLE_BY_OWNER)); @@ -1448,7 +1448,6 @@ module aptos_token::token { vector[false, false, false, false, false], ); assert!(balance_of(signer::address_of(&owner), token_id) == 0, 1); - account::create_account_for_test(signer::address_of(&owner)); direct_transfer(&creator, &owner, token_id, 1); let token = withdraw_token(&owner, token_id, 1); @@ -1492,9 +1491,9 @@ module aptos_token::token { mutate_setting ); - let default_keys = if(vector::length(&property_keys) == 0) {vector[string::utf8(b"attack"), string::utf8(b"num_of_use")]} else {property_keys}; - let default_vals = if (vector::length>(&property_values) == 0) {vector>[bcs::to_bytes(&10), bcs::to_bytes(&5)] } else {property_values}; - let default_types = if (vector::length(&property_types) == 0) {vector[string::utf8(b"u64"), string::utf8(b"u64")] } else {property_types}; + let default_keys = if (vector::length(&property_keys) == 0) { vector[string::utf8(b"attack"), string::utf8(b"num_of_use")] } else { property_keys }; + let default_vals = if (vector::length>(&property_values) == 0) { vector>[bcs::to_bytes(&10), bcs::to_bytes(&5)] } else { property_values }; + let default_types = if (vector::length(&property_types) == 0) { vector[string::utf8(b"u64"), string::utf8(b"u64")] } else { property_types }; let mutate_setting = token_mutate_setting; create_token_script( creator, @@ -1833,7 +1832,6 @@ module aptos_token::token { vector[], vector[false, false, false], vector[false, false, false, false, false], - ); let owner_addr = signer::address_of(owner); opt_in_direct_transfer(owner, true); @@ -1905,11 +1903,10 @@ module aptos_token::token { opt_in_direct_transfer(owner, true); initialize_token_store(owner); transfer(creator, token_id, signer::address_of(owner), 2); - burn_by_creator(creator, signer::address_of(owner), get_collection_name(), get_token_name(), 0,1); - + burn_by_creator(creator, signer::address_of(owner), get_collection_name(), get_token_name(), 0, 1); } - #[test(creator=@0xcafe, owner=@0x456)] + #[test(creator = @0xcafe, owner = @0x456)] #[expected_failure(abort_code = 327710)] fun test_burn_token_by_owner_without_burnable_config( creator: &signer, @@ -1933,10 +1930,10 @@ module aptos_token::token { initialize_token_store(owner); transfer(creator, token_id, signer::address_of(owner), 2); - burn(owner, signer::address_of(creator), get_collection_name(), get_token_name(), 0,1); + burn(owner, signer::address_of(creator), get_collection_name(), get_token_name(), 0, 1); } - #[test(creator=@0xcafe, owner=@0x456)] + #[test(creator = @0xcafe, owner = @0x456)] fun test_burn_token_by_owner_and_creator( creator: &signer, owner: &signer, @@ -1950,7 +1947,7 @@ module aptos_token::token { 4, 4, vector[string::utf8(BURNABLE_BY_CREATOR), string::utf8(BURNABLE_BY_OWNER)], - vector>[bcs::to_bytes(&true), bcs::to_bytes(&true)], + vector>[bcs::to_bytes(&true), bcs::to_bytes(&true)], vector[string::utf8(b"bool"), string::utf8(b"bool")], vector[false, false, false], vector[false, false, false, false, false], @@ -1959,11 +1956,11 @@ module aptos_token::token { initialize_token_store(owner); transfer(creator, token_id, signer::address_of(owner), 2); burn_by_creator(creator, signer::address_of(owner), get_collection_name(), get_token_name(), 0, 1); - burn(owner, signer::address_of(creator), get_collection_name(), get_token_name(), 0,1); + burn(owner, signer::address_of(creator), get_collection_name(), get_token_name(), 0, 1); assert!(balance_of(signer::address_of(owner), token_id) == 0, 1); } - #[test(creator=@0xcafe, owner=@0x456)] + #[test(creator = @0xcafe, owner = @0x456)] fun test_mutate_default_token_properties( creator: &signer, ) acquires Collections, TokenStore { @@ -2002,12 +1999,12 @@ module aptos_token::token { ); let all_token_data = &borrow_global(signer::address_of(creator)).token_data; - assert!(table::contains(all_token_data, token_id.token_data_id), 1); + assert!(table::contains(all_token_data, token_id.token_data_id), 1); let props = &table::borrow(all_token_data, token_id.token_data_id).default_properties; assert!(property_map::read_u64(props, &string::utf8(b"attack")) == 1, 1); } - #[test(creator=@0xcafe)] + #[test(creator = @0xcafe)] fun test_mutate_token_uri( creator: &signer, ) acquires Collections, TokenStore { @@ -2025,7 +2022,6 @@ module aptos_token::token { vector[false, true, false, false, false], ); mutate_tokendata_uri(creator, token_id.token_data_id, string::utf8(b"new_url")); - assert!(get_tokendata_uri(signer::address_of(creator), token_id.token_data_id) == string::utf8(b"new_url"), 1); + assert!(get_tokendata_uri(signer::address_of(creator), token_id.token_data_id) == string::utf8(b"new_url"), 1); } - }