diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index 448a1792..3642b95f 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -101,6 +101,8 @@ pub mod pallet { pub enum Event { /// Event emitted when allowance by `owner` to `spender` changes. Approval { + /// The ID of the asset. + id: AssetIdOf, /// Account providing allowance. owner: AccountIdOf, /// Allowance beneficiary. @@ -110,6 +112,8 @@ pub mod pallet { }, /// Event emitted when transfer of tokens occurs. Transfer { + /// The ID of the asset. + id: AssetIdOf, /// Transfer sender. `None` in case of minting new tokens. from: Option>, /// Transfer recipient. `None` in case of burning tokens. @@ -138,12 +142,12 @@ pub mod pallet { ) -> DispatchResult { AssetsOf::::transfer_keep_alive( origin.clone(), - id.into(), + id.clone().into(), T::Lookup::unlookup(to.clone()), value, )?; let from = ensure_signed(origin)?; - Self::deposit_event(Event::Transfer { from: Some(from), to: Some(to), value }); + Self::deposit_event(Event::Transfer { id, from: Some(from), to: Some(to), value }); Ok(()) } @@ -166,12 +170,12 @@ pub mod pallet { ) -> DispatchResult { AssetsOf::::transfer_approved( origin, - id.into(), + id.clone().into(), T::Lookup::unlookup(from.clone()), T::Lookup::unlookup(to.clone()), value, )?; - Self::deposit_event(Event::Transfer { from: Some(from), to: Some(to), value }); + Self::deposit_event(Event::Transfer { id, from: Some(from), to: Some(to), value }); Ok(()) } @@ -201,7 +205,7 @@ pub mod pallet { Greater => { AssetsOf::::approve_transfer( origin, - id.into(), + id.clone().into(), T::Lookup::unlookup(spender.clone()), value.saturating_sub(current_allowance), ) @@ -211,23 +215,23 @@ pub mod pallet { // If the new value is less than the current allowance, cancel the approval and // set the new value. Less => { - let id: AssetIdParameterOf = id.into(); + let id_param: AssetIdParameterOf = id.clone().into(); let spender_source = T::Lookup::unlookup(spender.clone()); AssetsOf::::cancel_approval( origin.clone(), - id.clone(), + id_param.clone(), spender_source.clone(), ) .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; if value.is_zero() { Self::weight_approve(0, 1) } else { - AssetsOf::::approve_transfer(origin, id, spender_source, value)?; + AssetsOf::::approve_transfer(origin, id_param, spender_source, value)?; Self::weight_approve(1, 1) } }, }; - Self::deposit_event(Event::Approval { owner, spender, value }); + Self::deposit_event(Event::Approval { id, owner, spender, value }); Ok(Some(return_weight).into()) } @@ -254,8 +258,8 @@ pub mod pallet { value, ) .map_err(|e| e.with_weight(AssetsWeightInfoOf::::approve_transfer()))?; - let value = AssetsOf::::allowance(id, &owner, &spender); - Self::deposit_event(Event::Approval { owner, spender, value }); + let value = AssetsOf::::allowance(id.clone(), &owner, &spender); + Self::deposit_event(Event::Approval { id, owner, spender, value }); Ok(().into()) } @@ -277,22 +281,26 @@ pub mod pallet { .map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?; let current_allowance = AssetsOf::::allowance(id.clone(), &owner, &spender); let spender_source = T::Lookup::unlookup(spender.clone()); - let id: AssetIdParameterOf = id.into(); + let id_param: AssetIdParameterOf = id.clone().into(); if value.is_zero() { return Ok(Some(Self::weight_approve(0, 0)).into()); } // Cancel the approval and set the new value if `new_allowance` is more than zero. - AssetsOf::::cancel_approval(origin.clone(), id.clone(), spender_source.clone()) - .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; + AssetsOf::::cancel_approval( + origin.clone(), + id_param.clone(), + spender_source.clone(), + ) + .map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?; let new_allowance = current_allowance.saturating_sub(value); let weight = if new_allowance.is_zero() { Self::weight_approve(0, 1) } else { - AssetsOf::::approve_transfer(origin, id, spender_source, new_allowance)?; + AssetsOf::::approve_transfer(origin, id_param, spender_source, new_allowance)?; Self::weight_approve(1, 1) }; - Self::deposit_event(Event::Approval { owner, spender, value: new_allowance }); + Self::deposit_event(Event::Approval { id, owner, spender, value: new_allowance }); Ok(Some(weight).into()) } @@ -368,8 +376,13 @@ pub mod pallet { account: AccountIdOf, value: BalanceOf, ) -> DispatchResult { - AssetsOf::::mint(origin, id.into(), T::Lookup::unlookup(account.clone()), value)?; - Self::deposit_event(Event::Transfer { from: None, to: Some(account), value }); + AssetsOf::::mint( + origin, + id.clone().into(), + T::Lookup::unlookup(account.clone()), + value, + )?; + Self::deposit_event(Event::Transfer { id, from: None, to: Some(account), value }); Ok(()) } @@ -387,8 +400,13 @@ pub mod pallet { account: AccountIdOf, value: BalanceOf, ) -> DispatchResult { - AssetsOf::::burn(origin, id.into(), T::Lookup::unlookup(account.clone()), value)?; - Self::deposit_event(Event::Transfer { from: Some(account), to: None, value }); + AssetsOf::::burn( + origin, + id.clone().into(), + T::Lookup::unlookup(account.clone()), + value, + )?; + Self::deposit_event(Event::Transfer { id, from: Some(account), to: None, value }); Ok(()) } } diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index a30aaa2f..a77d7e60 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -24,7 +24,7 @@ fn transfer_works() { assert_ok!(Fungibles::transfer(signed(ALICE), id, BOB, value)); let balance_after_transfer = Assets::balance(id, &BOB); assert_eq!(balance_after_transfer, balance_before_transfer + value); - System::assert_last_event(Event::Transfer { from, to, value }.into()); + System::assert_last_event(Event::Transfer { id, from, to, value }.into()); }); } @@ -47,7 +47,7 @@ fn transfer_from_works() { // Check that BOB receives the `value` and ALICE `amount` is spent successfully by CHARLIE. assert_eq!(bob_balance_after_transfer, bob_balance_before_transfer + value); assert_eq!(alice_balance_after_transfer, alice_balance_before_transfer - value); - System::assert_last_event(Event::Transfer { from, to, value }.into()); + System::assert_last_event(Event::Transfer { id, from, to, value }.into()); }); } @@ -64,23 +64,23 @@ fn approve_works() { assert_eq!(0, Assets::allowance(id, &ALICE, &BOB)); assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value)); assert_eq!(Assets::allowance(id, &ALICE, &BOB), value); - System::assert_last_event(Event::Approval { owner, spender, value }.into()); + System::assert_last_event(Event::Approval { id, owner, spender, value }.into()); // Approves an value to spend that is lower than the current allowance. assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value / 2)); assert_eq!(Assets::allowance(id, &ALICE, &BOB), value / 2); - System::assert_last_event(Event::Approval { owner, spender, value: value / 2 }.into()); + System::assert_last_event(Event::Approval { id, owner, spender, value: value / 2 }.into()); // Approves an value to spend that is higher than the current allowance. assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value * 2)); assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2); - System::assert_last_event(Event::Approval { owner, spender, value: value * 2 }.into()); + System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into()); // Approves an value to spend that is equal to the current allowance. assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value * 2)); assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2); - System::assert_last_event(Event::Approval { owner, spender, value: value * 2 }.into()); + System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into()); // Sets allowance to zero. assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, 0)); assert_eq!(Assets::allowance(id, &ALICE, &BOB), 0); - System::assert_last_event(Event::Approval { owner, spender, value: 0 }.into()); + System::assert_last_event(Event::Approval { id, owner, spender, value: 0 }.into()); }); } @@ -96,11 +96,11 @@ fn increase_allowance_works() { assert_eq!(0, Assets::allowance(id, &ALICE, &BOB)); assert_ok!(Fungibles::increase_allowance(signed(ALICE), id, BOB, value)); assert_eq!(Assets::allowance(id, &ALICE, &BOB), value); - System::assert_last_event(Event::Approval { owner, spender, value }.into()); + System::assert_last_event(Event::Approval { id, owner, spender, value }.into()); // Additive. assert_ok!(Fungibles::increase_allowance(signed(ALICE), id, BOB, value)); assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2); - System::assert_last_event(Event::Approval { owner, spender, value: value * 2 }.into()); + System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into()); }); } @@ -120,11 +120,11 @@ fn decrease_allowance_works() { // Decrease allowance successfully. assert_ok!(Fungibles::decrease_allowance(signed(ALICE), id, BOB, value / 2)); assert_eq!(Assets::allowance(id, &ALICE, &BOB), value / 2); - System::assert_last_event(Event::Approval { owner, spender, value: value / 2 }.into()); + System::assert_last_event(Event::Approval { id, owner, spender, value: value / 2 }.into()); // Saturating if current allowance is decreased more than the owner balance. assert_ok!(Fungibles::decrease_allowance(signed(ALICE), id, BOB, value)); assert_eq!(Assets::allowance(id, &ALICE, &BOB), 0); - System::assert_last_event(Event::Approval { owner, spender, value: 0 }.into()); + System::assert_last_event(Event::Approval { id, owner, spender, value: 0 }.into()); }); } @@ -197,7 +197,7 @@ fn mint_works() { assert_ok!(Fungibles::mint(signed(ALICE), id, BOB, value)); let balance_after_mint = Assets::balance(id, &BOB); assert_eq!(balance_after_mint, balance_before_mint + value); - System::assert_last_event(Event::Transfer { from, to, value }.into()); + System::assert_last_event(Event::Transfer { id, from, to, value }.into()); }); } @@ -214,7 +214,7 @@ fn burn_works() { assert_ok!(Fungibles::burn(signed(ALICE), id, BOB, value)); let balance_after_burn = Assets::balance(id, &BOB); assert_eq!(balance_after_burn, balance_before_burn - value); - System::assert_last_event(Event::Transfer { from, to, value }.into()); + System::assert_last_event(Event::Transfer { id, from, to, value }.into()); }); }