Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
resourceID parametr add to example pallet calls, test update (#71)
Browse files Browse the repository at this point in the history
* resourceID parametr add to example pallet calls, test update

* cargo fmt
  • Loading branch information
P1sar authored Nov 17, 2020
1 parent d0755a4 commit f2cce10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions example-pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,23 @@ 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<T>) -> DispatchResult {
pub fn transfer(origin, to: T::AccountId, amount: BalanceOf<T>, r_id: ResourceId) -> DispatchResult {
let source = T::BridgeOrigin::ensure_origin(origin)?;
<T as Trait>::Currency::transfer(&source, &to, amount.into(), AllowDeath)?;
Ok(())
}

/// 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(())
}

/// Allows the bridge to issue new erc721 tokens
#[weight = 195_000_000]
pub fn mint_erc721(origin, recipient: T::AccountId, id: U256, metadata: Vec<u8>) -> DispatchResult {
pub fn mint_erc721(origin, recipient: T::AccountId, id: U256, metadata: Vec<u8>, r_id: ResourceId) -> DispatchResult {
T::BridgeOrigin::ensure_origin(origin)?;
<erc721::Module<T>>::mint_token(recipient, id, metadata)?;
Ok(())
Expand Down
30 changes: 20 additions & 10 deletions example-pallet/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
);
})
Expand All @@ -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);
Expand All @@ -218,15 +226,16 @@ 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
assert_ok!(Example::mint_erc721(
Origin::signed(bridge_id),
recipient,
token_id,
metadata.clone()
metadata.clone(),
resource_id,
));
// Ensure token exists
assert_eq!(
Expand All @@ -242,7 +251,8 @@ fn mint_erc721() {
Origin::signed(bridge_id),
recipient,
token_id,
metadata.clone()
metadata.clone(),
resource_id,
),
erc721::Error::<Test>::TokenAlreadyExists
);
Expand Down

0 comments on commit f2cce10

Please sign in to comment.