From f2cce10ed264ac1d4165f6a75cac90bc4a147019 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 17 Nov 2020 17:43:21 +0200 Subject: [PATCH] resourceID parametr add to example pallet calls, test update (#71) * resourceID parametr add to example pallet calls, test update * cargo fmt --- example-pallet/src/lib.rs | 6 +++--- example-pallet/src/tests.rs | 30 ++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/example-pallet/src/lib.rs b/example-pallet/src/lib.rs index 286509b..c320a32 100644 --- a/example-pallet/src/lib.rs +++ b/example-pallet/src/lib.rs @@ -103,7 +103,7 @@ decl_module! { /// Executes a simple currency transfer using the bridge account as the source #[weight = 195_000_000] - pub fn transfer(origin, to: T::AccountId, amount: BalanceOf) -> DispatchResult { + pub fn transfer(origin, to: T::AccountId, amount: BalanceOf, r_id: ResourceId) -> DispatchResult { let source = T::BridgeOrigin::ensure_origin(origin)?; ::Currency::transfer(&source, &to, amount.into(), AllowDeath)?; Ok(()) @@ -111,7 +111,7 @@ decl_module! { /// This can be called by the bridge to demonstrate an arbitrary call from a proposal. #[weight = 195_000_000] - pub fn remark(origin, hash: T::Hash) -> DispatchResult { + pub fn remark(origin, hash: T::Hash, r_id: ResourceId) -> DispatchResult { T::BridgeOrigin::ensure_origin(origin)?; Self::deposit_event(RawEvent::Remark(hash)); Ok(()) @@ -119,7 +119,7 @@ decl_module! { /// Allows the bridge to issue new erc721 tokens #[weight = 195_000_000] - pub fn mint_erc721(origin, recipient: T::AccountId, id: U256, metadata: Vec) -> DispatchResult { + pub fn mint_erc721(origin, recipient: T::AccountId, id: U256, metadata: Vec, r_id: ResourceId) -> DispatchResult { T::BridgeOrigin::ensure_origin(origin)?; >::mint_token(recipient, id, metadata)?; Ok(()) diff --git a/example-pallet/src/tests.rs b/example-pallet/src/tests.rs index b4461d3..7aa34a0 100644 --- a/example-pallet/src/tests.rs +++ b/example-pallet/src/tests.rs @@ -16,11 +16,13 @@ use sp_core::{blake2_256, H256}; const TEST_THRESHOLD: u32 = 2; fn make_remark_proposal(hash: H256) -> Call { - Call::Example(crate::Call::remark(hash)) + let resource_id = HashId::get(); + Call::Example(crate::Call::remark(hash, resource_id)) } fn make_transfer_proposal(to: u64, amount: u64) -> Call { - Call::Example(crate::Call::transfer(to, amount.into())) + let resource_id = HashId::get(); + Call::Example(crate::Call::transfer(to, amount.into(), resource_id)) } #[test] @@ -173,16 +175,20 @@ fn execute_remark() { fn execute_remark_bad_origin() { new_test_ext().execute_with(|| { let hash: H256 = "ABC".using_encoded(blake2_256).into(); - - assert_ok!(Example::remark(Origin::signed(Bridge::account_id()), hash)); + let resource_id = HashId::get(); + assert_ok!(Example::remark( + Origin::signed(Bridge::account_id()), + hash, + resource_id + )); // Don't allow any signed origin except from bridge addr assert_noop!( - Example::remark(Origin::signed(RELAYER_A), hash), + Example::remark(Origin::signed(RELAYER_A), hash, resource_id), DispatchError::BadOrigin ); // Don't allow root calls assert_noop!( - Example::remark(Origin::root(), hash), + Example::remark(Origin::root(), hash, resource_id), DispatchError::BadOrigin ); }) @@ -193,12 +199,14 @@ fn transfer() { new_test_ext().execute_with(|| { // Check inital state let bridge_id: u64 = Bridge::account_id(); + let resource_id = HashId::get(); assert_eq!(Balances::free_balance(&bridge_id), ENDOWED_BALANCE); // Transfer and check result assert_ok!(Example::transfer( Origin::signed(Bridge::account_id()), RELAYER_A, - 10 + 10, + resource_id, )); assert_eq!(Balances::free_balance(&bridge_id), ENDOWED_BALANCE - 10); assert_eq!(Balances::free_balance(RELAYER_A), ENDOWED_BALANCE + 10); @@ -218,7 +226,7 @@ fn mint_erc721() { let recipient = RELAYER_A; let metadata = vec![1, 1, 1, 1]; let bridge_id: u64 = Bridge::account_id(); - + let resource_id = HashId::get(); // Token doesn't yet exist assert_eq!(Erc721::tokens(token_id), None); // Mint @@ -226,7 +234,8 @@ fn mint_erc721() { Origin::signed(bridge_id), recipient, token_id, - metadata.clone() + metadata.clone(), + resource_id, )); // Ensure token exists assert_eq!( @@ -242,7 +251,8 @@ fn mint_erc721() { Origin::signed(bridge_id), recipient, token_id, - metadata.clone() + metadata.clone(), + resource_id, ), erc721::Error::::TokenAlreadyExists );