Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: assert events emitted from contract #216

Merged
merged 36 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6adf1b1
refactor: fungibles pallet
Daanvdplas Aug 16, 2024
fa448bb
fix: invalid imported crates & crate visibility
chungquantin Aug 19, 2024
84c2840
fix: revert primitive error imports
chungquantin Aug 19, 2024
83a65b8
style: fix tests asset_asset parameter
Daanvdplas Aug 19, 2024
2cdc71a
test: assert events emitted from contract
chungquantin Aug 19, 2024
6cda609
Merge branch 'daan/api' into chungquantin/test-api_events
chungquantin Aug 19, 2024
aa84719
Merge branch 'daan/api' into chungquantin/test-api_events
chungquantin Aug 19, 2024
684ca91
Merge branch 'daan/api' into chungquantin/test-api_events
chungquantin Aug 27, 2024
393e1c1
refactor: api integration tests
Daanvdplas Sep 11, 2024
9320d26
taplo fmt
Daanvdplas Sep 11, 2024
4383ae0
refactor: remove clone
Daanvdplas Sep 11, 2024
0f9b166
refactor: small refactors
Daanvdplas Sep 11, 2024
29f9e57
Merge branch 'daan/api' into chungquantin/test-api_events
chungquantin Sep 12, 2024
1d2ca0e
refactor: fungibles pallet
Daanvdplas Aug 16, 2024
1752cd1
fix: invalid imported crates & crate visibility
chungquantin Aug 19, 2024
6a47735
fix: revert primitive error imports
chungquantin Aug 19, 2024
e4bbe1b
test: assert events emitted from contract
chungquantin Aug 19, 2024
1157f22
fix: resolve conflict
chungquantin Sep 13, 2024
73df148
rebase daan/api
Daanvdplas Sep 13, 2024
0122ad9
refactor: example contract
Daanvdplas Sep 13, 2024
a34a2ba
refactor: use assets module
Daanvdplas Sep 13, 2024
9accd10
fix: update events
chungquantin Sep 13, 2024
1b41af8
refactor: remove imports
chungquantin Sep 13, 2024
576f39c
Merge branch 'daan/refactor-api' into chungquantin/test-api_events
chungquantin Sep 13, 2024
45bc9d6
fix: parameter type
chungquantin Sep 13, 2024
b431b07
feat: add event emitted tests for approved and transferred
chungquantin Sep 13, 2024
1307d52
fix: comments
chungquantin Sep 13, 2024
e233e60
fix: formatting
chungquantin Sep 13, 2024
79adb97
fix: compile error
chungquantin Sep 13, 2024
c8cd349
refactor: primitivces
chungquantin Sep 13, 2024
6f64e6c
fix: resolve comments
chungquantin Sep 13, 2024
c25a5bd
fix: resolve comments
chungquantin Sep 13, 2024
9f48bdf
Merge branch 'daan/api' into chungquantin/test-api_events
chungquantin Sep 13, 2024
11508ce
fix: resolve comments
chungquantin Sep 13, 2024
0d50f2c
refactor(pop-api/fungibles): events Approve -> Approval, Destroyed ->…
peterwht Sep 13, 2024
c6d390d
refactor: less cloning
peterwht Sep 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub mod pallet {
Ok(().into())
}

/// Decreases the allowance of a `spender` by `value` amount of tokens.
/// Decreases the allowance of a spender and asset.
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Parameters
/// - `asset` - The asset to have an allowance decreased.
Expand Down
13 changes: 11 additions & 2 deletions pop-api/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,38 @@ edition = "2021"

[dev-dependencies]
env_logger = "0.11.2"
scale = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
frame-support = { version = "29.0.0", default-features = false }
frame-system = { version = "29.0.0", default-features = false }
pallet-balances = { version = "29.0.2", default-features = false }
pallet-assets = { version = "30.0.0", default-features = false }
pallet-contracts = { version = "28.0.0", default-features = false }
pop-api = { path = "../../pop-api", default-features = false, features = [
"fungibles",
] }
pop-primitives = { path = "../../primitives", default-features = false }
pop-runtime-devnet = { path = "../../runtime/devnet", default-features = false }
sp-io = { version = "31.0.0", default-features = false }
sp-runtime = { version = "32.0.0", default-features = false }

# TODO
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
ink = { version = "5.0.0", default-features = false }

[features]
default = ["std"]
std = [
"frame-support/std",
"frame-system/std",
"ink/std",
"pallet-balances/std",
"pallet-assets/std",
"pallet-contracts/std",
"pop-api/std",
"pop-primitives/std",
"pop-runtime-devnet/std",
"scale/std",
"sp-io/std",
"sp-runtime/std",
]
]
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use ink::prelude::vec::Vec;
use pop_api::{
assets::fungibles::{self as api},
assets::fungibles::{self as api, events::Create},
primitives::AssetId,
StatusCode,
};
Expand All @@ -25,6 +24,7 @@ mod create_token_in_constructor {
// AccountId of the contract which will be set to the owner of the fungible token.
let owner = contract.env().account_id();
api::create(id, owner, min_balance)?;
contract.env().emit_event(Create { id, creator: owner, admin: owner });
Ok(contract)
}

Expand Down
38 changes: 30 additions & 8 deletions pop-api/integration-tests/contracts/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
///
use ink::prelude::vec::Vec;
use pop_api::{
assets::fungibles::{self as api},
assets::fungibles::{
self as api,
events::{Approval, ClearMetadata, Create, SetMetadata, StartDestroy, Transfer},
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
},
primitives::AssetId,
StatusCode,
};
Expand Down Expand Up @@ -61,7 +64,13 @@ mod fungibles {

#[ink(message)]
pub fn transfer(&self, id: AssetId, to: AccountId, value: Balance) -> Result<()> {
api::transfer(id, to, value)
let result = api::transfer(id, to, value);
self.env().emit_event(Transfer {
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
from: Some(self.env().account_id()),
to: Some(to),
value,
});
result
}

#[ink(message)]
Expand All @@ -74,12 +83,17 @@ mod fungibles {
// In the PSP-22 standard a `[u8]`, but the size needs to be known at compile time.
_data: Vec<u8>,
) -> Result<()> {
api::transfer_from(id, from, to, value)
let result = api::transfer_from(id, from, to, value);
self.env().emit_event(Transfer { from: Some(from), to: Some(to), value });
result
}

#[ink(message)]
pub fn approve(&self, id: AssetId, spender: AccountId, value: Balance) -> Result<()> {
api::approve(id, spender, value)
let result = api::approve(id, spender, value);
self.env()
.emit_event(Approval { owner: self.env().account_id(), spender, value });
result
}

#[ink(message)]
Expand Down Expand Up @@ -131,12 +145,16 @@ mod fungibles {

#[ink(message)]
pub fn create(&self, id: AssetId, admin: AccountId, min_balance: Balance) -> Result<()> {
api::create(id, admin, min_balance)
let result = api::create(id, admin, min_balance);
self.env().emit_event(Create { id, creator: admin, admin });
result
}

#[ink(message)]
pub fn start_destroy(&self, id: AssetId) -> Result<()> {
api::start_destroy(id)
let result = api::start_destroy(id);
self.env().emit_event(StartDestroy { id });
result
}

#[ink(message)]
Expand All @@ -147,12 +165,16 @@ mod fungibles {
symbol: Vec<u8>,
decimals: u8,
) -> Result<()> {
api::set_metadata(id, name, symbol, decimals)
let result = api::set_metadata(id, name.clone(), symbol.clone(), decimals);
self.env().emit_event(SetMetadata { id, name, symbol, decimals });
result
}

#[ink(message)]
pub fn clear_metadata(&self, id: AssetId) -> Result<()> {
api::clear_metadata(id)
let result = api::clear_metadata(id);
self.env().emit_event(ClearMetadata { id });
result
}

#[ink(message)]
Expand Down
37 changes: 35 additions & 2 deletions pop-api/integration-tests/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use super::*;
use pop_api::{
assets::fungibles::events::{
Approval, ClearMetadata, Create, SetMetadata, StartDestroy, Transfer,
},
primitives::account_id_from_slice,
};
use pop_primitives::error::{
ArithmeticError::*,
Error::{self, *},
Expand Down Expand Up @@ -186,6 +192,14 @@ fn approve_works() {
assert_eq!(0, Assets::allowance(asset, &addr, &BOB));
assert_ok!(approve(addr.clone(), asset, BOB, amount));
assert_eq!(Assets::allowance(asset, &addr, &BOB), amount);
// Successfully emit an event from approving.
let expected = Approval {
owner: account_id_from_slice(addr.clone().as_ref()),
spender: account_id_from_slice(BOB.as_ref()),
value: amount,
}
.encode();
assert_eq!(latest_contract_event(), expected.as_slice());
// Non-additive, sets new value.
assert_ok!(approve(addr.clone(), asset, BOB, amount / 2));
assert_eq!(Assets::allowance(asset, &addr, &BOB), amount / 2);
Expand Down Expand Up @@ -345,6 +359,10 @@ fn create_works() {
assert_eq!(create(addr.clone(), ASSET_ID, BOB, 0), Err(Module { index: 52, error: 7 }),);
// Create asset successfully.
assert_ok!(create(addr.clone(), ASSET_ID, BOB, 1));
// Successfully emit an event from creating a new asset class.
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
let admin = account_id_from_slice(BOB.as_ref());
let expected = Create { id: ASSET_ID, creator: admin, admin }.encode();
assert_eq!(latest_contract_event(), expected.as_slice());
// Asset ID is already taken.
assert_eq!(create(addr.clone(), ASSET_ID, BOB, 1), Err(Module { index: 52, error: 5 }),);
});
Expand All @@ -364,8 +382,14 @@ fn instantiate_and_create_fungible_works() {
Err(Module { index: 52, error: 5 })
);
// Successfully create an asset when instantiating the contract.
assert_ok!(instantiate_and_create_fungible(contract, ASSET_ID, 1));
let instantiator =
instantiate_and_create_fungible(contract, ASSET_ID, 1).expect("Should work");
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
assert!(Assets::asset_exists(ASSET_ID));
// Successfully emit an event from instantiating a new fungible.
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
let instantiator = account_id_from_slice(instantiator.as_ref());
let expected =
Create { id: ASSET_ID, creator: instantiator.clone(), admin: instantiator }.encode();
assert_eq!(latest_contract_event(), expected.as_slice());
});
}

Expand All @@ -383,6 +407,9 @@ fn start_destroy_works() {
assert_eq!(start_destroy(addr.clone(), asset), Err(Module { index: 52, error: 2 }),);
let asset = create_asset(addr.clone(), ASSET_ID, 1);
assert_ok!(start_destroy(addr.clone(), asset));
// Successfully emit an event from destroying an asset.
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
let expected = StartDestroy { id: ASSET_ID }.encode();
assert_eq!(latest_contract_event(), expected.as_slice());
});
}

Expand Down Expand Up @@ -423,7 +450,10 @@ fn set_metadata_works() {
Err(Module { index: 52, error: 9 }),
);
// Set metadata successfully.
assert_ok!(set_metadata(addr.clone(), ASSET_ID, name, symbol, decimals));
assert_ok!(set_metadata(addr.clone(), ASSET_ID, name.clone(), symbol.clone(), decimals));
// Successfully emit an event from set asset metadata.
let expected = SetMetadata { id: ASSET_ID, name, symbol, decimals }.encode();
assert_eq!(latest_contract_event(), expected.as_slice());
// Asset is not live, i.e. frozen or being destroyed.
start_destroy_asset(addr.clone(), asset);
assert_eq!(
Expand Down Expand Up @@ -458,6 +488,9 @@ fn clear_metadata_works() {
set_metadata_asset(addr.clone(), asset, name, symbol, decimals);
// Clear metadata successfully.
assert_ok!(clear_metadata(addr.clone(), ASSET_ID));
// Successfully emit an event from set asset metadata.
let expected = ClearMetadata { id: ASSET_ID }.encode();
assert_eq!(latest_contract_event(), expected.as_slice());
// Asset is not live, i.e. frozen or being destroyed.
start_destroy_asset(addr.clone(), asset);
assert_eq!(
Expand Down
31 changes: 25 additions & 6 deletions pop-api/integration-tests/src/fungibles/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub(super) fn instantiate_and_create_fungible(
contract: &str,
asset_id: AssetId,
min_balance: Balance,
) -> Result<(), Error> {
) -> Result<AccountId32, Error> {
let function = function_selector("new");
let input = [function, asset_id.encode(), min_balance.encode()].concat();
let (wasm_binary, _) =
Expand All @@ -329,11 +329,30 @@ pub(super) fn instantiate_and_create_fungible(
input,
vec![],
DEBUG_OUTPUT,
CollectEvents::Skip,
CollectEvents::UnsafeCollect,
)
.result
.expect("should work")
.result;
decoded::<Result<(), Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
.expect("should work");
let account_id = result.clone().account_id;
let result = decoded::<Result<(), Error>>(result.clone().result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result));
match result {
Ok(_) => Ok(account_id),
Err(error) => Err(error),
}
}

/// Get the latest event from pallet contracts.
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
pub(super) fn latest_contract_event() -> Vec<u8> {
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
let events = System::read_events_for_pallet::<pallet_contracts::Event<Runtime>>();
let contract_events = events
.iter()
.filter_map(|event| match event {
pallet_contracts::Event::<Runtime>::ContractEmitted { data, .. } => {
Some(data.as_slice())
},
_ => None,
})
.collect::<Vec<&[u8]>>();
contract_events.last().unwrap().to_vec()
}
6 changes: 6 additions & 0 deletions pop-api/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use ink::env::{DefaultEnvironment, Environment};
use ink::scale::Decode;
pub use pop_primitives::*;

pub(crate) type AccountId = <DefaultEnvironment as Environment>::AccountId;
pub(crate) type Balance = <DefaultEnvironment as Environment>::Balance;

/// Decode slice of bytes to environment associated type AccountId.
pub fn account_id_from_slice(s: &[u8; 32]) -> AccountId {
AccountId::decode(&mut &s[..]).expect("Should be decoded to AccountId")
}
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
Loading