Skip to content
This repository has been archived by the owner on Mar 20, 2020. It is now read-only.

Commit

Permalink
Generic Asset Module Events (#16)
Browse files Browse the repository at this point in the history
* Raise events when asset permissions are updated or total asset issuance is changed.

* Add unit tests to assert that appropriate events are raised when asset permissions are updated or total asset issuance is changed.

* Execute `cargo fmt`.
  • Loading branch information
charles-salmon authored and cowboy-bebug committed Oct 16, 2019
1 parent fc6274c commit c0967fc
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 9 deletions.
13 changes: 11 additions & 2 deletions generic-asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,10 @@ decl_module! {
let permissions: PermissionVersions<T::AccountId> = new_permission.into();

if Self::check_permission(&asset_id, &origin, &PermissionType::Update) {
<Permissions<T>>::insert(asset_id, permissions);
<Permissions<T>>::insert(asset_id, &permissions);

Self::deposit_event(RawEvent::PermissionUpdated(asset_id, permissions.into()));

Ok(())
} else {
return Err("Origin does not have enough permission to update permissions.");
Expand All @@ -348,6 +351,8 @@ decl_module! {
<TotalIssuance<T>>::insert(asset_id, new_total_issuance);
Self::set_free_balance(&asset_id, &to, value);

Self::deposit_event(RawEvent::Minted(asset_id, to, amount));

Ok(())
} else {
return Err("The origin does not have permission to mint an asset, Permission error.");
Expand All @@ -367,9 +372,10 @@ decl_module! {
let value = original_free_balance.checked_sub(&amount).ok_or_else(|| "free_balance got underflow after burning")?;

<TotalIssuance<T>>::insert(asset_id, new_total_issuance);

Self::set_free_balance(&asset_id, &to, value);

Self::deposit_event(RawEvent::Burned(asset_id, to, amount));

Ok(())
} else {
return Err("The origin does not have permission to burn an asset, Permission error.");
Expand Down Expand Up @@ -453,8 +459,11 @@ decl_event!(
Created(AssetId, AccountId, AssetOptions),
/// Asset transfer succeeded (asset_id, from, to, amount).
Transferred(AssetId, AccountId, AccountId, Balance),
/// Asset permissions updated (asset_id, new_permissions).
PermissionUpdated(AssetId, PermissionLatest<AccountId>),
/// Asset minted (asset_id, to, amount).
Minted(AssetId, AccountId, Balance),
/// Asset burned (asset_id, to, amount).
Burned(AssetId, AccountId, Balance),
}
);
Expand Down
24 changes: 18 additions & 6 deletions generic-asset/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
#![cfg(test)]

use parity_codec::{Encode, Decode};
use serde::{Serialize, Deserialize};
use parity_codec::{Decode, Encode};
use primitives::{
testing::{Digest, DigestItem, Header},
traits::{BlakeTwo256, IdentityLookup, Verify, Lazy},
traits::{BlakeTwo256, IdentityLookup, Lazy, Verify},
BuildStorage,
};
use serde::{Deserialize, Serialize};
use substrate_primitives::{Blake2Hasher, H256};
use support::impl_outer_origin;
use support::{impl_outer_event, impl_outer_origin};

use super::*;

Expand Down Expand Up @@ -59,19 +59,31 @@ impl system::Trait for Test {
type AccountId = u64;
type Lookup = IdentityLookup<u64>;
type Header = Header;
type Event = ();
type Event = TestEvent;
type Log = DigestItem;
type Signature = Signature;
}

impl Trait for Test {
type Balance = u64;
type AssetId = u32;
type Event = ();
type Event = TestEvent;
}

mod generic_asset {
pub use crate::Event;
}

impl_outer_event! {
pub enum TestEvent for Test {
generic_asset<T>,
}
}

pub type GenericAsset = Module<Test>;

pub type System = system::Module<Test>;

pub struct ExtBuilder {
asset_id: u32,
next_asset_id: u32,
Expand Down
122 changes: 121 additions & 1 deletion generic-asset/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#![cfg(test)]

use super::*;
use crate::mock::{new_test_ext, ExtBuilder, GenericAsset, Origin, Test};
use crate::mock::{new_test_ext, ExtBuilder, GenericAsset, Origin, System, Test, TestEvent};
use runtime_io::with_externalities;
use support::{assert_noop, assert_ok};

Expand Down Expand Up @@ -1224,3 +1224,123 @@ fn create_should_reserve_stake_asset() {
},
);
}

#[test]
fn update_permission_should_raise_event() {
// Arrange
let staking_asset_id = 16000;
let asset_id = 1000;
let origin = 1;
let initial_balance = 1000;
let permissions = PermissionLatest {
update: Owner::Address(origin),
mint: Owner::Address(origin),
burn: Owner::Address(origin),
};

with_externalities(
&mut ExtBuilder::default()
.next_asset_id(asset_id)
.free_balance((staking_asset_id, origin, initial_balance))
.build(),
|| {
assert_ok!(GenericAsset::create(
Origin::signed(origin),
AssetOptions {
initial_issuance: 0,
permissions: permissions.clone(),
}
));

// Act
assert_ok!(GenericAsset::update_permission(
Origin::signed(origin),
asset_id,
permissions.clone()
));

// Assert
assert!(System::events().iter().any(|record| record.event
== TestEvent::generic_asset(RawEvent::PermissionUpdated(asset_id, permissions.clone()))));
},
);
}

#[test]
fn mint_should_raise_event() {
// Arrange
let staking_asset_id = 16000;
let asset_id = 1000;
let origin = 1;
let initial_balance = 1000;
let permissions = PermissionLatest {
update: Owner::Address(origin),
mint: Owner::Address(origin),
burn: Owner::Address(origin),
};
let to = 2;
let amount = 100;

with_externalities(
&mut ExtBuilder::default()
.next_asset_id(asset_id)
.free_balance((staking_asset_id, origin, initial_balance))
.build(),
|| {
assert_ok!(GenericAsset::create(
Origin::signed(origin),
AssetOptions {
initial_issuance: 0,
permissions: permissions.clone(),
}
));

// Act
assert_ok!(GenericAsset::mint(Origin::signed(origin), asset_id, to, amount));

// Assert
assert!(System::events()
.iter()
.any(|record| record.event == TestEvent::generic_asset(RawEvent::Minted(asset_id, to, amount))));
},
);
}

#[test]
fn burn_should_raise_event() {
// Arrange
let staking_asset_id = 16000;
let asset_id = 1000;
let origin = 1;
let initial_balance = 1000;
let permissions = PermissionLatest {
update: Owner::Address(origin),
mint: Owner::Address(origin),
burn: Owner::Address(origin),
};
let amount = 100;

with_externalities(
&mut ExtBuilder::default()
.next_asset_id(asset_id)
.free_balance((staking_asset_id, origin, initial_balance))
.build(),
|| {
assert_ok!(GenericAsset::create(
Origin::signed(origin),
AssetOptions {
initial_issuance: amount,
permissions: permissions.clone(),
}
));

// Act
assert_ok!(GenericAsset::burn(Origin::signed(origin), asset_id, origin, amount));

// Assert
assert!(System::events()
.iter()
.any(|record| record.event == TestEvent::generic_asset(RawEvent::Burned(asset_id, origin, amount))));
},
);
}

0 comments on commit c0967fc

Please sign in to comment.