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 29 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 pop-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version = "0.0.0"
[dependencies]
ink = { version = "5.0.0", default-features = false }
pop-primitives = { path = "../primitives", default-features = false }
sp-io = { version = "31.0.0", default-features = false, features = [
sp-io = { version = "37.0.0", default-features = false, features = [
"disable_allocator",
"disable_oom",
"disable_panic_handler",
Expand Down
137 changes: 30 additions & 107 deletions pop-api/examples/fungibles/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

/// Local Fungibles:
/// 1. PSP-22 Interface
/// 2. PSP-22 Metadata Interface
/// 3. Asset Management
///
use ink::prelude::vec::Vec;
use pop_api::{
assets::fungibles::{self as api},
primitives::AssetId,
fungibles::{self as api},
primitives::TokenId,
StatusCode,
};

Expand All @@ -25,161 +20,89 @@ mod fungibles {
impl Fungibles {
#[ink(constructor, payable)]
pub fn new() -> Self {
ink::env::debug_println!("PopApiFungiblesExample::new");
Default::default()
}

/// 1. PSP-22 Interface:
/// - total_supply
/// - balance_of
/// - allowance
/// - transfer
/// - transfer_from
/// - approve
/// - increase_allowance
/// - decrease_allowance

#[ink(message)]
pub fn total_supply(&self, id: AssetId) -> Result<Balance> {
api::total_supply(id)
pub fn total_supply(&self, token: TokenId) -> Result<Balance> {
api::total_supply(token)
}

#[ink(message)]
pub fn balance_of(&self, id: AssetId, owner: AccountId) -> Result<Balance> {
api::balance_of(id, owner)
pub fn balance_of(&self, token: TokenId, owner: AccountId) -> Result<Balance> {
api::balance_of(token, owner)
}

#[ink(message)]
pub fn allowance(
&self,
id: AssetId,
token: TokenId,
owner: AccountId,
spender: AccountId,
) -> Result<Balance> {
api::allowance(id, owner, spender)
api::allowance(token, owner, spender)
}

#[ink(message)]
pub fn transfer(&mut self, id: AssetId, to: AccountId, value: Balance) -> Result<()> {
api::transfer(id, to, value)
pub fn transfer(&mut self, token: TokenId, to: AccountId, value: Balance) -> Result<()> {
api::transfer(token, to, value)
}

#[ink(message)]
pub fn transfer_from(
&mut self,
id: AssetId,
token: TokenId,
from: AccountId,
to: AccountId,
value: Balance,
// 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)
api::transfer_from(token, from, to, value)
}

#[ink(message)]
pub fn approve(&mut self, id: AssetId, spender: AccountId, value: Balance) -> Result<()> {
api::approve(id, spender, value)
pub fn approve(
&mut self,
token: TokenId,
spender: AccountId,
value: Balance,
) -> Result<()> {
api::approve(token, spender, value)
}

#[ink(message)]
pub fn increase_allowance(
&mut self,
id: AssetId,
token: TokenId,
spender: AccountId,
value: Balance,
) -> Result<()> {
api::increase_allowance(id, spender, value)
api::increase_allowance(token, spender, value)
}

#[ink(message)]
pub fn decrease_allowance(
&mut self,
id: AssetId,
token: TokenId,
spender: AccountId,
value: Balance,
) -> Result<()> {
api::decrease_allowance(id, spender, value)
api::decrease_allowance(token, spender, value)
}

/// 2. PSP-22 Metadata Interface:
/// - token_name
/// - token_symbol
/// - token_decimals

#[ink(message)]
pub fn token_name(&self, id: AssetId) -> Result<Vec<u8>> {
api::token_name(id)
pub fn token_name(&self, token: TokenId) -> Result<Vec<u8>> {
api::token_name(token)
}

#[ink(message)]
pub fn token_symbol(&self, id: AssetId) -> Result<Vec<u8>> {
api::token_symbol(id)
pub fn token_symbol(&self, token: TokenId) -> Result<Vec<u8>> {
api::token_symbol(token)
}

#[ink(message)]
pub fn token_decimals(&self, id: AssetId) -> Result<u8> {
api::token_decimals(id)
}

// 3. Asset Management:
// - create
// - start_destroy
// - destroy_accounts
// - destroy_approvals
// - finish_destroy
// - set_metadata
// - clear_metadata

// #[ink(message)]
// pub fn create(&self, id: AssetId, admin: AccountId, min_balance: Balance) -> Result<()> {
// ink::env::debug_println!(
// "PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}",
// id,
// admin,
// min_balance,
// );
// let result = api::create(id, admin, min_balance);
// ink::env::debug_println!("Result: {:?}", result);
// result.map_err(|e| e.into())
// result
// }

// #[ink(message)]
// pub fn set_metadata(
// &self,
// id: AssetId,
// name: Vec<u8>,
// symbol: Vec<u8>,
// decimals: u8,
// ) -> Result<()> {
// ink::env::debug_println!(
// "PopApiFungiblesExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}",
// id,
// name,
// symbol,
// decimals,
// );
// let result = api::set_metadata(id, name, symbol, decimals);
// ink::env::debug_println!("Result: {:?}", result);
// // result.map_err(|e| e.into())
// result
// }
//
// #[ink(message)]
// pub fn asset_exists(&self, id: AssetId) -> Result<bool> {
// // api::asset_exists(id).map_err(|e| e.into())
// api::asset_exists(id)
// }
}

#[cfg(test)]
mod tests {
use super::*;

#[ink::test]
fn default_works() {
PopApiFungiblesExample::new();
pub fn token_decimals(&self, token: TokenId) -> Result<u8> {
api::token_decimals(token)
}
}
}
}
5 changes: 4 additions & 1 deletion pop-api/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ log = "0.4.22"
pallet-assets = { version = "37.0.0", default-features = false }
pallet-balances = { version = "37.0.0", default-features = false }
pallet-contracts = { version = "35.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 }
scale = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = [
Expand All @@ -24,7 +27,6 @@ scale = { package = "parity-scale-codec", version = "3.6.12", default-features =
sp-io = { version = "37.0.0", default-features = false }
sp-runtime = { version = "=38.0.0", default-features = false }


[features]
default = [ "std" ]
std = [
Expand All @@ -33,6 +35,7 @@ std = [
"pallet-assets/std",
"pallet-balances/std",
"pallet-contracts/std",
"pop-api/std",
"pop-primitives/std",
"pop-runtime-devnet/std",
"scale/std",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[package]
authors = [ "[your_name] <[your_email]>" ]
edition = "2021"
name = "create_token_in_constructor"
version = "0.1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use pop_api::{
fungibles::{self as api},
fungibles::{self as api, events::Created},
primitives::TokenId,
StatusCode,
};
Expand All @@ -14,22 +14,23 @@ mod create_token_in_constructor {

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

impl Fungible {
#[ink(constructor, payable)]
pub fn new(id: TokenId, min_balance: Balance) -> Result<Self> {
let contract = Self { id };
pub fn new(token: TokenId, min_balance: Balance) -> Result<Self> {
let contract = Self { token };
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
// 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)?;
api::create(token, owner, min_balance)?;
contract.env().emit_event(Created { token, creator: owner, admin: owner });
Ok(contract)
}

#[ink(message)]
pub fn token_exists(&self) -> Result<bool> {
api::token_exists(self.id)
api::token_exists(self.token)
}
}
}
1 change: 0 additions & 1 deletion pop-api/integration-tests/contracts/fungibles/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[package]
authors = [ "[your_name] <[your_email]>" ]
edition = "2021"
name = "fungibles"
version = "0.1.0"
Expand Down
49 changes: 38 additions & 11 deletions pop-api/integration-tests/contracts/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
/// 4. PSP-22 Mintable & Burnable
use ink::prelude::vec::Vec;
use pop_api::{
fungibles::{self as api},
fungibles::{
self as api,
events::{Approved, Created, Destroyed, MetadataCleared, MetadataSet, Transferred},
},
primitives::TokenId,
StatusCode,
};
Expand Down Expand Up @@ -60,7 +63,13 @@ mod fungibles {

#[ink(message)]
pub fn transfer(&mut self, token: TokenId, to: AccountId, value: Balance) -> Result<()> {
api::transfer(token, to, value)
api::transfer(token, to, value)?;
self.env().emit_event(Transferred {
from: Some(self.env().account_id()),
to: Some(to),
value,
});
Ok(())
}

#[ink(message)]
Expand All @@ -73,12 +82,22 @@ 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(token, from, to, value)
api::transfer_from(token, from, to, value)?;
self.env().emit_event(Transferred { from: Some(from), to: Some(to), value });
Ok(())
}

#[ink(message)]
pub fn approve(&mut self, token: TokenId, spender: AccountId, value: Balance) -> Result<()> {
api::approve(token, spender, value)
pub fn approve(
&mut self,
token: TokenId,
spender: AccountId,
value: Balance,
) -> Result<()> {
api::approve(token, spender, value)?;
self.env()
.emit_event(Approved { owner: self.env().account_id(), spender, value });
Ok(())
}

#[ink(message)]
Expand Down Expand Up @@ -131,16 +150,20 @@ mod fungibles {
#[ink(message)]
pub fn create(
&mut self,
id: TokenId,
token: TokenId,
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
admin: AccountId,
min_balance: Balance,
) -> Result<()> {
api::create(id, admin, min_balance)
api::create(token, admin, min_balance)?;
self.env().emit_event(Created { token, creator: admin, admin });
Ok(())
}

#[ink(message)]
pub fn start_destroy(&mut self, token: TokenId) -> Result<()> {
api::start_destroy(token)
api::start_destroy(token)?;
self.env().emit_event(Destroyed { token });
Ok(())
}

#[ink(message)]
Expand All @@ -151,12 +174,16 @@ mod fungibles {
symbol: Vec<u8>,
decimals: u8,
) -> Result<()> {
api::set_metadata(token, name, symbol, decimals)
api::set_metadata(token, name.clone(), symbol.clone(), decimals)?;
self.env().emit_event(MetadataSet { token, name, symbol, decimals });
Ok(())
}

#[ink(message)]
pub fn clear_metadata(&self, token: TokenId) -> Result<()> {
api::clear_metadata(token)
pub fn clear_metadata(&mut self, token: TokenId) -> Result<()> {
api::clear_metadata(token)?;
self.env().emit_event(MetadataCleared { token });
Ok(())
}

#[ink(message)]
Expand Down
Loading
Loading