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 1 commit
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
9 changes: 5 additions & 4 deletions pop-api/integration-tests/build.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use contract_build::{
execute, BuildArtifacts, BuildMode, BuildResult, ExecuteArgs, ManifestPath, OutputType,
Verbosity,
};
use std::{
fs,
path::{Path, PathBuf},
process,
};

use contract_build::{
execute, BuildArtifacts, BuildMode, BuildResult, ExecuteArgs, ManifestPath, OutputType,
Verbosity,
};

fn main() {
let contracts_dir = PathBuf::from("./contracts/");
let contract_dirs = match get_subcontract_directories(&contracts_dir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ mod create_token_in_constructor {

#[ink(storage)]
pub struct Fungible {
token: TokenId,
id: TokenId,
}

impl Fungible {
#[ink(constructor, payable)]
pub fn new(token: TokenId, min_balance: Balance) -> Result<Self> {
let contract = Self { token };
pub fn new(id: TokenId, min_balance: Balance) -> Result<Self> {
let contract = Self { id };
// AccountId of the contract which will be set to the owner of the fungible token.
let owner = contract.env().account_id();
api::create(token, owner, min_balance)?;
contract.env().emit_event(Created { token, creator: owner, admin: owner });
api::create(id, owner, min_balance)?;
contract.env().emit_event(Created { id, creator: owner, admin: owner });
Ok(contract)
}

#[ink(message)]
pub fn token_exists(&self) -> Result<bool> {
api::token_exists(self.token)
api::token_exists(self.id)
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
6 changes: 3 additions & 3 deletions pop-api/integration-tests/contracts/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ mod fungibles {
#[ink(message)]
pub fn create(
&mut self,
token: TokenId,
id: TokenId,
admin: AccountId,
min_balance: Balance,
) -> Result<()> {
api::create(token, admin, min_balance)?;
self.env().emit_event(Created { token, creator: admin, admin });
api::create(id, admin, min_balance)?;
self.env().emit_event(Created { id, creator: admin, admin });
Ok(())
}

Expand Down
13 changes: 6 additions & 7 deletions pop-api/integration-tests/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::*;
use pop_api::{
fungibles::events::{Approved, Created, Destroyed, MetadataCleared, MetadataSet, Transferred},
primitives::account_id_from_slice,
use pop_api::fungibles::events::{
Approved, Created, Destroyed, MetadataCleared, MetadataSet, Transferred,
};
use pop_primitives::{ArithmeticError::*, Error, Error::*, TokenError::*, TokenId};
use utils::*;

use super::*;

mod utils;

const TOKEN_ID: TokenId = 1;
Expand Down Expand Up @@ -341,7 +341,7 @@ fn create_works() {
assert_eq!(Assets::owner(TOKEN_ID), Some(addr.clone()));
// Successfully emit event.
let admin = account_id_from_slice(BOB.as_ref());
let expected = Created { token: TOKEN_ID, creator: admin, admin }.encode();
let expected = Created { id: TOKEN_ID, creator: admin, admin }.encode();
assert_eq!(last_contract_event(), expected.as_slice());
// Token ID is already taken.
assert_eq!(create(&addr, TOKEN_ID, &BOB, 1), Err(Module { index: 52, error: [5, 0] }),);
Expand Down Expand Up @@ -369,8 +369,7 @@ fn instantiate_and_create_fungible_works() {
// Successfully emit event.
let instantiator = account_id_from_slice(instantiator.unwrap().as_ref());
let expected =
Created { token: TOKEN_ID, creator: instantiator.clone(), admin: instantiator }
.encode();
Created { id: TOKEN_ID, creator: instantiator.clone(), admin: instantiator }.encode();
assert_eq!(last_contract_event(), expected.as_slice());
});
}
Expand Down
12 changes: 9 additions & 3 deletions pop-api/integration-tests/src/fungibles/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use pop_api::primitives::AccountId;

use super::*;

type AssetId = TokenId;
Expand Down Expand Up @@ -361,11 +363,15 @@ pub(super) fn last_contract_event() -> Vec<u8> {
let contract_events = events
.iter()
.filter_map(|event| match event {
pallet_contracts::Event::<Runtime>::ContractEmitted { data, .. } => {
Some(data.as_slice())
},
pallet_contracts::Event::<Runtime>::ContractEmitted { data, .. } =>
Some(data.as_slice()),
_ => None,
})
.collect::<Vec<&[u8]>>();
contract_events.last().unwrap().to_vec()
}

/// Decode slice of bytes to environment associated type AccountId.
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
pub fn account_id_from_slice(s: &[u8; 32]) -> AccountId {
AccountId::decode(&mut &s[..]).expect("Should be decoded to AccountId")
}
9 changes: 2 additions & 7 deletions pop-api/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use ink::env::{DefaultEnvironment, Environment};
use ink::scale::Decode;
pub use pop_primitives::*;

pub(crate) type AccountId = <DefaultEnvironment as Environment>::AccountId;
// Public due to integration tests crate.
pub 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")
}
2 changes: 1 addition & 1 deletion pop-api/src/v0/fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub mod events {
pub struct Created {
/// The token identifier.
#[ink(topic)]
pub token: TokenId,
pub id: TokenId,
/// The creator of the token.
#[ink(topic)]
pub creator: AccountId,
Expand Down
Loading