From b31db975a836feea1080c0dba0e374aa29ddb322 Mon Sep 17 00:00:00 2001 From: Tin Chung <56880684+chungquantin@users.noreply.github.com> Date: Fri, 27 Sep 2024 18:22:57 +0700 Subject: [PATCH 01/12] feat(pallet): follow psp22 standard for `decrease_allowance` and `burn` (#310) --- Cargo.toml | 6 +++- pallets/api/src/fungibles/mod.rs | 12 +++++-- pallets/api/src/fungibles/tests.rs | 35 +++++++++++-------- pop-api/examples/.gitignore | 0 pop-api/examples/balance-transfer/Cargo.toml | 0 pop-api/examples/balance-transfer/lib.rs | 0 pop-api/examples/fungibles/Cargo.toml | 0 pop-api/examples/fungibles/lib.rs | 0 pop-api/examples/nfts/Cargo.toml | 0 pop-api/examples/nfts/lib.rs | 0 pop-api/examples/place-spot-order/Cargo.toml | 0 pop-api/examples/place-spot-order/lib.rs | 0 .../examples/read-runtime-state/Cargo.toml | 0 pop-api/examples/read-runtime-state/lib.rs | 0 .../create_token_in_constructor/Cargo.toml | 0 .../create_token_in_constructor/lib.rs | 0 .../contracts/fungibles/Cargo.toml | 0 .../contracts/fungibles/lib.rs | 0 .../integration-tests/src/fungibles/mod.rs | 5 --- 19 files changed, 36 insertions(+), 22 deletions(-) mode change 100755 => 100644 pop-api/examples/.gitignore mode change 100755 => 100644 pop-api/examples/balance-transfer/Cargo.toml mode change 100755 => 100644 pop-api/examples/balance-transfer/lib.rs mode change 100755 => 100644 pop-api/examples/fungibles/Cargo.toml mode change 100755 => 100644 pop-api/examples/fungibles/lib.rs mode change 100755 => 100644 pop-api/examples/nfts/Cargo.toml mode change 100755 => 100644 pop-api/examples/nfts/lib.rs mode change 100755 => 100644 pop-api/examples/place-spot-order/Cargo.toml mode change 100755 => 100644 pop-api/examples/place-spot-order/lib.rs mode change 100755 => 100644 pop-api/examples/read-runtime-state/Cargo.toml mode change 100755 => 100644 pop-api/examples/read-runtime-state/lib.rs mode change 100755 => 100644 pop-api/integration-tests/contracts/create_token_in_constructor/Cargo.toml mode change 100755 => 100644 pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs mode change 100755 => 100644 pop-api/integration-tests/contracts/fungibles/Cargo.toml mode change 100755 => 100644 pop-api/integration-tests/contracts/fungibles/lib.rs diff --git a/Cargo.toml b/Cargo.toml index c6e15c69..c8ab144a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,11 @@ license = "Unlicense" repository = "https://github.com/r0gue-io/pop-node/" [workspace] -exclude = [ "extension/contract", "pop-api", "tests/contracts" ] +exclude = [ + "extension/contract", + "pop-api", + "tests/contracts", +] members = [ "integration-tests", "node", diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index 4c840628..07fdbb43 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -17,6 +17,7 @@ type AccountIdOf = ::AccountId; type TokenIdOf = as Inspect<::AccountId>>::AssetId; type TokenIdParameterOf = >>::AssetIdParameter; type AssetsOf = pallet_assets::Pallet>; +type AssetsErrorOf = pallet_assets::Error>; type AssetsInstanceOf = ::AssetsInstance; type AssetsWeightInfoOf = >>::WeightInfo; type BalanceOf = as Inspect<::AccountId>>::Balance; @@ -33,7 +34,7 @@ pub mod pallet { }; use frame_system::pallet_prelude::*; use sp_runtime::{ - traits::{StaticLookup, Zero}, + traits::{CheckedSub, StaticLookup, Zero}, Saturating, }; use sp_std::vec::Vec; @@ -338,13 +339,16 @@ pub mod pallet { let token_param: TokenIdParameterOf = token.clone().into(); // Cancel the approval and approve `new_allowance` if difference is more than zero. + let new_allowance = match current_allowance.checked_sub(&value) { + Some(allowance) => allowance, + None => return Err(AssetsErrorOf::::Unapproved.into()), + }; AssetsOf::::cancel_approval( origin.clone(), token_param.clone(), spender_source.clone(), ) .map_err(|e| e.with_weight(WeightOf::::approve(0, 1)))?; - let new_allowance = current_allowance.saturating_sub(value); let weight = if new_allowance.is_zero() { WeightOf::::approve(0, 1) } else { @@ -467,6 +471,10 @@ pub mod pallet { account: AccountIdOf, value: BalanceOf, ) -> DispatchResult { + let current_balance = AssetsOf::::balance(token.clone(), &account); + if current_balance < value { + return Err(AssetsErrorOf::::BalanceLow.into()); + } AssetsOf::::burn( origin, token.clone().into(), diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index 2e430ca8..6eeea428 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -272,20 +272,27 @@ fn decrease_allowance_works() { BadOrigin.with_weight(WeightInfo::approve(0, 0)) ); } + assets::create_mint_and_approve(owner, token, owner, value, spender, value); + assert_eq!(Assets::allowance(token, &owner, &spender), value); // Check error works for `Assets::cancel_approval()`. No error test for `approve_transfer` // because it is not possible. + assert_ok!(Assets::freeze_asset(signed(owner), token)); assert_noop!( Fungibles::decrease_allowance(signed(owner), token, spender, value / 2), - AssetsError::Unknown.with_weight(WeightInfo::approve(0, 1)) + AssetsError::AssetNotLive.with_weight(WeightInfo::approve(0, 1)) ); - assets::create_mint_and_approve(owner, token, owner, value, spender, value); - assert_eq!(Assets::allowance(token, &owner, &spender), value); + assert_ok!(Assets::thaw_asset(signed(owner), token)); // Owner balance is not changed if decreased by zero. assert_eq!( Fungibles::decrease_allowance(signed(owner), token, spender, 0), Ok(Some(WeightInfo::approve(0, 0)).into()) ); assert_eq!(Assets::allowance(token, &owner, &spender), value); + // "Unapproved" error is returned if the current allowance is less than `value`. + assert_noop!( + Fungibles::decrease_allowance(signed(owner), token, spender, value * 2), + AssetsError::Unapproved + ); // Decrease allowance successfully. assert_eq!( Fungibles::decrease_allowance(signed(owner), token, spender, value / 2), @@ -295,13 +302,6 @@ fn decrease_allowance_works() { System::assert_last_event( Event::Approval { token, owner, spender, value: value / 2 }.into(), ); - // Saturating if current allowance is decreased more than the owner balance. - assert_eq!( - Fungibles::decrease_allowance(signed(owner), token, spender, value), - Ok(Some(WeightInfo::approve(0, 1)).into()) - ); - assert_eq!(Assets::allowance(token, &owner, &spender), 0); - System::assert_last_event(Event::Approval { token, owner, spender, value: 0 }.into()); }); } @@ -416,10 +416,17 @@ fn burn_works() { let from = BOB; let total_supply = value * 2; - // Check error works for `Assets::burn()`. - assert_noop!(Fungibles::burn(signed(owner), token, from, value), AssetsError::Unknown); assets::create_and_mint_to(owner, token, from, total_supply); assert_eq!(Assets::total_supply(TOKEN), total_supply); + // Check error works for `Assets::burn()`. + assert_ok!(Assets::freeze_asset(signed(owner), token)); + assert_noop!(Fungibles::burn(signed(owner), token, from, value), AssetsError::AssetNotLive); + assert_ok!(Assets::thaw_asset(signed(owner), token)); + // "BalanceLow" error is returned if the balance is less than `value`. + assert_noop!( + Fungibles::burn(signed(owner), token, from, total_supply * 2), + AssetsError::BalanceLow + ); let balance_before_burn = Assets::balance(token, &from); assert_ok!(Fungibles::burn(signed(owner), token, from, value)); assert_eq!(Assets::total_supply(TOKEN), total_supply - value); @@ -677,8 +684,8 @@ mod read_weights { } mod ensure_codec_indexes { - use super::{Encode, RuntimeCall, *}; - use crate::{fungibles, fungibles::Call::*, mock::RuntimeCall::Fungibles}; + use super::{Encode, *}; + use crate::{fungibles, mock::RuntimeCall::Fungibles}; #[test] fn ensure_read_variant_indexes() { diff --git a/pop-api/examples/.gitignore b/pop-api/examples/.gitignore old mode 100755 new mode 100644 diff --git a/pop-api/examples/balance-transfer/Cargo.toml b/pop-api/examples/balance-transfer/Cargo.toml old mode 100755 new mode 100644 diff --git a/pop-api/examples/balance-transfer/lib.rs b/pop-api/examples/balance-transfer/lib.rs old mode 100755 new mode 100644 diff --git a/pop-api/examples/fungibles/Cargo.toml b/pop-api/examples/fungibles/Cargo.toml old mode 100755 new mode 100644 diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs old mode 100755 new mode 100644 diff --git a/pop-api/examples/nfts/Cargo.toml b/pop-api/examples/nfts/Cargo.toml old mode 100755 new mode 100644 diff --git a/pop-api/examples/nfts/lib.rs b/pop-api/examples/nfts/lib.rs old mode 100755 new mode 100644 diff --git a/pop-api/examples/place-spot-order/Cargo.toml b/pop-api/examples/place-spot-order/Cargo.toml old mode 100755 new mode 100644 diff --git a/pop-api/examples/place-spot-order/lib.rs b/pop-api/examples/place-spot-order/lib.rs old mode 100755 new mode 100644 diff --git a/pop-api/examples/read-runtime-state/Cargo.toml b/pop-api/examples/read-runtime-state/Cargo.toml old mode 100755 new mode 100644 diff --git a/pop-api/examples/read-runtime-state/lib.rs b/pop-api/examples/read-runtime-state/lib.rs old mode 100755 new mode 100644 diff --git a/pop-api/integration-tests/contracts/create_token_in_constructor/Cargo.toml b/pop-api/integration-tests/contracts/create_token_in_constructor/Cargo.toml old mode 100755 new mode 100644 diff --git a/pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs b/pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs old mode 100755 new mode 100644 diff --git a/pop-api/integration-tests/contracts/fungibles/Cargo.toml b/pop-api/integration-tests/contracts/fungibles/Cargo.toml old mode 100755 new mode 100644 diff --git a/pop-api/integration-tests/contracts/fungibles/lib.rs b/pop-api/integration-tests/contracts/fungibles/lib.rs old mode 100755 new mode 100644 diff --git a/pop-api/integration-tests/src/fungibles/mod.rs b/pop-api/integration-tests/src/fungibles/mod.rs index d2e06285..4b064786 100644 --- a/pop-api/integration-tests/src/fungibles/mod.rs +++ b/pop-api/integration-tests/src/fungibles/mod.rs @@ -253,11 +253,6 @@ fn decrease_allowance_works() { let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); let amount: Balance = 100 * UNIT; - // Token does not exist. - assert_eq!( - decrease_allowance(&addr, 0, &BOB, amount), - Err(Module { index: 52, error: [3, 0] }), - ); // Create token and mint `amount` to contract address, then approve Bob to spend `amount`. let token = assets::create_mint_and_approve(&addr, 0, &addr, amount, &BOB, amount); // Token is not live, i.e. frozen or being destroyed. From 437d71bb4b0f3780a9cecfc5ba300564ba812122 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Fri, 27 Sep 2024 18:41:42 +0700 Subject: [PATCH 02/12] refactor: convert to `ok_or` --- pallets/api/src/fungibles/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index 07fdbb43..a4b228b7 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -339,10 +339,8 @@ pub mod pallet { let token_param: TokenIdParameterOf = token.clone().into(); // Cancel the approval and approve `new_allowance` if difference is more than zero. - let new_allowance = match current_allowance.checked_sub(&value) { - Some(allowance) => allowance, - None => return Err(AssetsErrorOf::::Unapproved.into()), - }; + let new_allowance = + current_allowance.checked_sub(&value).ok_or(AssetsErrorOf::::Unapproved)?; AssetsOf::::cancel_approval( origin.clone(), token_param.clone(), From bf474c66b789ec01cfc3aeecf0a27d7ce8652817 Mon Sep 17 00:00:00 2001 From: Tin Chung <56880684+chungquantin@users.noreply.github.com> Date: Sat, 28 Sep 2024 13:33:03 +0700 Subject: [PATCH 03/12] fix: apply suggestions from code review Co-authored-by: Daan van der Plas <93204684+Daanvdplas@users.noreply.github.com> --- pallets/api/src/fungibles/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index 6eeea428..2c925a12 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -288,7 +288,7 @@ fn decrease_allowance_works() { Ok(Some(WeightInfo::approve(0, 0)).into()) ); assert_eq!(Assets::allowance(token, &owner, &spender), value); - // "Unapproved" error is returned if the current allowance is less than `value`. + // "Unapproved" error is returned if the current allowance is less than amount to decrease with. assert_noop!( Fungibles::decrease_allowance(signed(owner), token, spender, value * 2), AssetsError::Unapproved @@ -422,7 +422,7 @@ fn burn_works() { assert_ok!(Assets::freeze_asset(signed(owner), token)); assert_noop!(Fungibles::burn(signed(owner), token, from, value), AssetsError::AssetNotLive); assert_ok!(Assets::thaw_asset(signed(owner), token)); - // "BalanceLow" error is returned if the balance is less than `value`. + // "BalanceLow" error is returned if the balance is less than amount to burn. assert_noop!( Fungibles::burn(signed(owner), token, from, total_supply * 2), AssetsError::BalanceLow From 7fd90b586acdc22b589c4681c17e327af087e8b0 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Mon, 30 Sep 2024 15:20:17 +0700 Subject: [PATCH 04/12] feat: integration-tests --- pop-api/integration-tests/src/fungibles/mod.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pop-api/integration-tests/src/fungibles/mod.rs b/pop-api/integration-tests/src/fungibles/mod.rs index 4b064786..99202327 100644 --- a/pop-api/integration-tests/src/fungibles/mod.rs +++ b/pop-api/integration-tests/src/fungibles/mod.rs @@ -253,6 +253,11 @@ fn decrease_allowance_works() { let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); let amount: Balance = 100 * UNIT; + // Token does not exist. + assert_eq!( + decrease_allowance(&addr, 0, &BOB, amount), + Err(Module { index: 52, error: [10, 0] }), + ); // Create token and mint `amount` to contract address, then approve Bob to spend `amount`. let token = assets::create_mint_and_approve(&addr, 0, &addr, amount, &BOB, amount); // Token is not live, i.e. frozen or being destroyed. @@ -262,16 +267,21 @@ fn decrease_allowance_works() { Err(Module { index: 52, error: [16, 0] }), ); assets::thaw(&addr, token); + // "Unapproved" error is returned if the current allowance is less than `value`. + assert_eq!( + decrease_allowance(&addr, token, &BOB, amount * 2), + Err(Module { index: 52, error: [10, 0] }), + ); // Successfully decrease allowance. let allowance_before = Assets::allowance(token, &addr, &BOB); - assert_ok!(decrease_allowance(&addr, 0, &BOB, amount / 2 - 1 * UNIT)); + assert_ok!(decrease_allowance(&addr, token, &BOB, amount / 2 - 1 * UNIT)); let allowance_after = Assets::allowance(token, &addr, &BOB); assert_eq!(allowance_before - allowance_after, amount / 2 - 1 * UNIT); // Token is not live, i.e. frozen or being destroyed. assets::start_destroy(&addr, token); assert_eq!( decrease_allowance(&addr, token, &BOB, amount), - Err(Module { index: 52, error: [16, 0] }), + Err(Module { index: 52, error: [10, 0] }), ); }); } From 3f7a0fb8abfb3e432029944e16d6f3d919c65710 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Mon, 30 Sep 2024 15:22:11 +0700 Subject: [PATCH 05/12] fix: formatting --- pallets/api/src/fungibles/tests.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index 2c925a12..35d10bdc 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -288,7 +288,8 @@ fn decrease_allowance_works() { Ok(Some(WeightInfo::approve(0, 0)).into()) ); assert_eq!(Assets::allowance(token, &owner, &spender), value); - // "Unapproved" error is returned if the current allowance is less than amount to decrease with. + // "Unapproved" error is returned if the current allowance is less than amount to decrease + // with. assert_noop!( Fungibles::decrease_allowance(signed(owner), token, spender, value * 2), AssetsError::Unapproved From eb11d7d3bfddb0fb9b186dc1151d0a20369eee56 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Mon, 30 Sep 2024 20:03:54 +0700 Subject: [PATCH 06/12] fix: burn integration tests --- pop-api/integration-tests/src/fungibles/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pop-api/integration-tests/src/fungibles/mod.rs b/pop-api/integration-tests/src/fungibles/mod.rs index 99202327..272670e7 100644 --- a/pop-api/integration-tests/src/fungibles/mod.rs +++ b/pop-api/integration-tests/src/fungibles/mod.rs @@ -535,14 +535,14 @@ fn burn_works() { let amount: Balance = 100 * UNIT; // Token does not exist. - assert_eq!(burn(&addr, 1, &BOB, amount), Err(Module { index: 52, error: [3, 0] })); + assert_eq!(burn(&addr, 1, &BOB, 0), Err(Module { index: 52, error: [3, 0] })); let token = assets::create(&ALICE, 1, 1); - // Bob has no tokens and therefore doesn't exist. - assert_eq!(burn(&addr, token, &BOB, 1), Err(Module { index: 52, error: [1, 0] })); + // Bob has no tokens. + assert_eq!(burn(&addr, token, &BOB, amount), Err(Module { index: 52, error: [0, 0] })); // Burning can only be done by the manager. assets::mint(&ALICE, token, &BOB, amount); assert_eq!(burn(&addr, token, &BOB, 1), Err(Module { index: 52, error: [2, 0] })); - let token = assets::create_and_mint_to(&addr, 2, &BOB, amount); + let token = assets::create_and_mint_to(&addr, 2, &BOB, amount * 2); // Token is not live, i.e. frozen or being destroyed. assets::freeze(&addr, token); assert_eq!(burn(&addr, token, &BOB, amount), Err(Module { index: 52, error: [16, 0] })); From 53fac61a49019e8a770a234fefd9e96ee84cc6c4 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Tue, 1 Oct 2024 15:50:04 +0700 Subject: [PATCH 07/12] chore: update weights --- pallets/api/src/fungibles/mod.rs | 2 +- pallets/api/src/fungibles/weights.rs | 54 ++++++++++++++-------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index a4b228b7..d5196899 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -462,7 +462,7 @@ pub mod pallet { /// - `account` - The account from which the tokens will be destroyed. /// - `value` - The number of tokens to destroy. #[pallet::call_index(20)] - #[pallet::weight(AssetsWeightInfoOf::::burn())] + #[pallet::weight(::WeightInfo::balance_of() + AssetsWeightInfoOf::::burn())] pub fn burn( origin: OriginFor, token: TokenIdOf, diff --git a/pallets/api/src/fungibles/weights.rs b/pallets/api/src/fungibles/weights.rs index 28c5a6b5..74c7caab 100644 --- a/pallets/api/src/fungibles/weights.rs +++ b/pallets/api/src/fungibles/weights.rs @@ -2,7 +2,7 @@ //! Autogenerated weights for `fungibles` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 40.0.0 -//! DATE: 2024-09-13, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2024-10-01, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `R0GUE`, CPU: `` //! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024` @@ -57,12 +57,12 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `413 + c * (102 ±0)` // Estimated: `3675` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(1_473_469, 3675) - // Standard Error: 89_329 - .saturating_add(Weight::from_parts(19_606_122, 0).saturating_mul(a.into())) - // Standard Error: 89_329 - .saturating_add(Weight::from_parts(30_920_408, 0).saturating_mul(c.into())) + // Minimum execution time: 25_000_000 picoseconds. + Weight::from_parts(1_056_122, 3675) + // Standard Error: 81_557 + .saturating_add(Weight::from_parts(24_943_877, 0).saturating_mul(a.into())) + // Standard Error: 81_557 + .saturating_add(Weight::from_parts(38_450_000, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(T::DbWeight::get().writes(2_u64)) @@ -74,7 +74,7 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3675` - // Minimum execution time: 1_000_000 picoseconds. + // Minimum execution time: 2_000_000 picoseconds. Weight::from_parts(2_000_000, 3675) .saturating_add(T::DbWeight::get().reads(1_u64)) } @@ -84,7 +84,7 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3599` - // Minimum execution time: 2_000_000 picoseconds. + // Minimum execution time: 3_000_000 picoseconds. Weight::from_parts(3_000_000, 3599) .saturating_add(T::DbWeight::get().reads(1_u64)) } @@ -94,8 +94,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3613` - // Minimum execution time: 4_000_000 picoseconds. - Weight::from_parts(4_000_000, 3613) + // Minimum execution time: 5_000_000 picoseconds. + Weight::from_parts(6_000_000, 3613) .saturating_add(T::DbWeight::get().reads(1_u64)) } /// Storage: `Assets::Metadata` (r:1 w:0) @@ -104,7 +104,7 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3605` - // Minimum execution time: 1_000_000 picoseconds. + // Minimum execution time: 2_000_000 picoseconds. Weight::from_parts(2_000_000, 3605) .saturating_add(T::DbWeight::get().reads(1_u64)) } @@ -124,7 +124,7 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3605` - // Minimum execution time: 1_000_000 picoseconds. + // Minimum execution time: 2_000_000 picoseconds. Weight::from_parts(2_000_000, 3605) .saturating_add(T::DbWeight::get().reads(1_u64)) } @@ -134,7 +134,7 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3675` - // Minimum execution time: 1_000_000 picoseconds. + // Minimum execution time: 2_000_000 picoseconds. Weight::from_parts(2_000_000, 3675) .saturating_add(T::DbWeight::get().reads(1_u64)) } @@ -154,12 +154,12 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `413 + c * (102 ±0)` // Estimated: `3675` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(1_473_469, 3675) - // Standard Error: 89_329 - .saturating_add(Weight::from_parts(19_606_122, 0).saturating_mul(a.into())) - // Standard Error: 89_329 - .saturating_add(Weight::from_parts(30_920_408, 0).saturating_mul(c.into())) + // Minimum execution time: 25_000_000 picoseconds. + Weight::from_parts(1_056_122, 3675) + // Standard Error: 81_557 + .saturating_add(Weight::from_parts(24_943_877, 0).saturating_mul(a.into())) + // Standard Error: 81_557 + .saturating_add(Weight::from_parts(38_450_000, 0).saturating_mul(c.into())) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(RocksDbWeight::get().writes(2_u64)) @@ -171,7 +171,7 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3675` - // Minimum execution time: 1_000_000 picoseconds. + // Minimum execution time: 2_000_000 picoseconds. Weight::from_parts(2_000_000, 3675) .saturating_add(RocksDbWeight::get().reads(1_u64)) } @@ -181,7 +181,7 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3599` - // Minimum execution time: 2_000_000 picoseconds. + // Minimum execution time: 3_000_000 picoseconds. Weight::from_parts(3_000_000, 3599) .saturating_add(RocksDbWeight::get().reads(1_u64)) } @@ -191,8 +191,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3613` - // Minimum execution time: 4_000_000 picoseconds. - Weight::from_parts(4_000_000, 3613) + // Minimum execution time: 5_000_000 picoseconds. + Weight::from_parts(6_000_000, 3613) .saturating_add(RocksDbWeight::get().reads(1_u64)) } /// Storage: `Assets::Metadata` (r:1 w:0) @@ -201,7 +201,7 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3605` - // Minimum execution time: 1_000_000 picoseconds. + // Minimum execution time: 2_000_000 picoseconds. Weight::from_parts(2_000_000, 3605) .saturating_add(RocksDbWeight::get().reads(1_u64)) } @@ -221,7 +221,7 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3605` - // Minimum execution time: 1_000_000 picoseconds. + // Minimum execution time: 2_000_000 picoseconds. Weight::from_parts(2_000_000, 3605) .saturating_add(RocksDbWeight::get().reads(1_u64)) } @@ -231,7 +231,7 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `6` // Estimated: `3675` - // Minimum execution time: 1_000_000 picoseconds. + // Minimum execution time: 2_000_000 picoseconds. Weight::from_parts(2_000_000, 3675) .saturating_add(RocksDbWeight::get().reads(1_u64)) } From 4c20399622f65ebb0f4c76197f01eb4bb755a669 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Tue, 1 Oct 2024 16:27:04 +0700 Subject: [PATCH 08/12] fix: resolve comments --- pop-api/integration-tests/src/fungibles/mod.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/pop-api/integration-tests/src/fungibles/mod.rs b/pop-api/integration-tests/src/fungibles/mod.rs index 272670e7..5f7ed04b 100644 --- a/pop-api/integration-tests/src/fungibles/mod.rs +++ b/pop-api/integration-tests/src/fungibles/mod.rs @@ -253,11 +253,6 @@ fn decrease_allowance_works() { let addr = instantiate(CONTRACT, INIT_VALUE, vec![]); let amount: Balance = 100 * UNIT; - // Token does not exist. - assert_eq!( - decrease_allowance(&addr, 0, &BOB, amount), - Err(Module { index: 52, error: [10, 0] }), - ); // Create token and mint `amount` to contract address, then approve Bob to spend `amount`. let token = assets::create_mint_and_approve(&addr, 0, &addr, amount, &BOB, amount); // Token is not live, i.e. frozen or being destroyed. @@ -280,8 +275,8 @@ fn decrease_allowance_works() { // Token is not live, i.e. frozen or being destroyed. assets::start_destroy(&addr, token); assert_eq!( - decrease_allowance(&addr, token, &BOB, amount), - Err(Module { index: 52, error: [10, 0] }), + decrease_allowance(&addr, token, &BOB, 1 * UNIT), + Err(Module { index: 52, error: [16, 0] }), ); }); } From 2d2be28498307ca232099990fb3c8de9ddd63e64 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Tue, 1 Oct 2024 16:39:46 +0700 Subject: [PATCH 09/12] fix: return weight on burn error --- pallets/api/src/fungibles/mod.rs | 17 ++++++++++------- pallets/api/src/fungibles/tests.rs | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index d5196899..b60249bb 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -468,10 +468,11 @@ pub mod pallet { token: TokenIdOf, account: AccountIdOf, value: BalanceOf, - ) -> DispatchResult { + ) -> DispatchResultWithPostInfo { let current_balance = AssetsOf::::balance(token.clone(), &account); if current_balance < value { - return Err(AssetsErrorOf::::BalanceLow.into()); + return Err(AssetsErrorOf::::BalanceLow + .with_weight(::WeightInfo::balance_of())); } AssetsOf::::burn( origin, @@ -480,7 +481,7 @@ pub mod pallet { value, )?; Self::deposit_event(Event::Transfer { token, from: Some(account), to: None, value }); - Ok(()) + Ok(().into()) } } @@ -516,10 +517,12 @@ pub mod pallet { use Read::*; match request { TotalSupply(token) => ReadResult::TotalSupply(AssetsOf::::total_supply(token)), - BalanceOf { token, owner } => - ReadResult::BalanceOf(AssetsOf::::balance(token, owner)), - Allowance { token, owner, spender } => - ReadResult::Allowance(AssetsOf::::allowance(token, &owner, &spender)), + BalanceOf { token, owner } => { + ReadResult::BalanceOf(AssetsOf::::balance(token, owner)) + }, + Allowance { token, owner, spender } => { + ReadResult::Allowance(AssetsOf::::allowance(token, &owner, &spender)) + }, TokenName(token) => ReadResult::TokenName( as MetadataInspect< AccountIdOf, >>::name(token)), diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index 35d10bdc..559f4831 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -426,7 +426,7 @@ fn burn_works() { // "BalanceLow" error is returned if the balance is less than amount to burn. assert_noop!( Fungibles::burn(signed(owner), token, from, total_supply * 2), - AssetsError::BalanceLow + AssetsError::BalanceLow.with_weight(WeightInfo::balance_of()) ); let balance_before_burn = Assets::balance(token, &from); assert_ok!(Fungibles::burn(signed(owner), token, from, value)); From 2e7c3ecf1874003618f6b0d5cac52f1cb6a381d7 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Tue, 1 Oct 2024 16:40:23 +0700 Subject: [PATCH 10/12] fix: formatting --- pallets/api/src/fungibles/mod.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index b60249bb..4ba587cf 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -517,12 +517,10 @@ pub mod pallet { use Read::*; match request { TotalSupply(token) => ReadResult::TotalSupply(AssetsOf::::total_supply(token)), - BalanceOf { token, owner } => { - ReadResult::BalanceOf(AssetsOf::::balance(token, owner)) - }, - Allowance { token, owner, spender } => { - ReadResult::Allowance(AssetsOf::::allowance(token, &owner, &spender)) - }, + BalanceOf { token, owner } => + ReadResult::BalanceOf(AssetsOf::::balance(token, owner)), + Allowance { token, owner, spender } => + ReadResult::Allowance(AssetsOf::::allowance(token, &owner, &spender)), TokenName(token) => ReadResult::TokenName( as MetadataInspect< AccountIdOf, >>::name(token)), From 64ac34541f5b014ee26cedfce2968fa0c4829bcd Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Tue, 1 Oct 2024 16:46:21 +0700 Subject: [PATCH 11/12] chore: update weights --- pallets/api/src/fungibles/weights.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pallets/api/src/fungibles/weights.rs b/pallets/api/src/fungibles/weights.rs index 74c7caab..cdaf2872 100644 --- a/pallets/api/src/fungibles/weights.rs +++ b/pallets/api/src/fungibles/weights.rs @@ -57,12 +57,12 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `413 + c * (102 ±0)` // Estimated: `3675` - // Minimum execution time: 25_000_000 picoseconds. - Weight::from_parts(1_056_122, 3675) - // Standard Error: 81_557 - .saturating_add(Weight::from_parts(24_943_877, 0).saturating_mul(a.into())) - // Standard Error: 81_557 - .saturating_add(Weight::from_parts(38_450_000, 0).saturating_mul(c.into())) + // Minimum execution time: 26_000_000 picoseconds. + Weight::from_parts(2_877_551, 3675) + // Standard Error: 100_168 + .saturating_add(Weight::from_parts(24_846_938, 0).saturating_mul(a.into())) + // Standard Error: 100_168 + .saturating_add(Weight::from_parts(38_375_510, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(T::DbWeight::get().writes(2_u64)) @@ -95,7 +95,7 @@ impl WeightInfo for SubstrateWeight { // Measured: `6` // Estimated: `3613` // Minimum execution time: 5_000_000 picoseconds. - Weight::from_parts(6_000_000, 3613) + Weight::from_parts(5_000_000, 3613) .saturating_add(T::DbWeight::get().reads(1_u64)) } /// Storage: `Assets::Metadata` (r:1 w:0) @@ -154,12 +154,12 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `413 + c * (102 ±0)` // Estimated: `3675` - // Minimum execution time: 25_000_000 picoseconds. - Weight::from_parts(1_056_122, 3675) - // Standard Error: 81_557 - .saturating_add(Weight::from_parts(24_943_877, 0).saturating_mul(a.into())) - // Standard Error: 81_557 - .saturating_add(Weight::from_parts(38_450_000, 0).saturating_mul(c.into())) + // Minimum execution time: 26_000_000 picoseconds. + Weight::from_parts(2_877_551, 3675) + // Standard Error: 100_168 + .saturating_add(Weight::from_parts(24_846_938, 0).saturating_mul(a.into())) + // Standard Error: 100_168 + .saturating_add(Weight::from_parts(38_375_510, 0).saturating_mul(c.into())) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(RocksDbWeight::get().writes(2_u64)) @@ -192,7 +192,7 @@ impl WeightInfo for () { // Measured: `6` // Estimated: `3613` // Minimum execution time: 5_000_000 picoseconds. - Weight::from_parts(6_000_000, 3613) + Weight::from_parts(5_000_000, 3613) .saturating_add(RocksDbWeight::get().reads(1_u64)) } /// Storage: `Assets::Metadata` (r:1 w:0) From 93f048cc827516913f898a06db9911a3ae04f9e8 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:44:32 +0700 Subject: [PATCH 12/12] fix: file permission --- Cargo.toml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c8ab144a..c6e15c69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,11 +15,7 @@ license = "Unlicense" repository = "https://github.com/r0gue-io/pop-node/" [workspace] -exclude = [ - "extension/contract", - "pop-api", - "tests/contracts", -] +exclude = [ "extension/contract", "pop-api", "tests/contracts" ] members = [ "integration-tests", "node",