From f3f6ddfc0c47e3b555981ff1f5303e805a07d54f Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 13 Dec 2023 13:37:07 +0100 Subject: [PATCH 1/5] check allowance --- azero/contracts/most/lib.rs | 2 +- azero/contracts/psp22-traits/lib.rs | 5 ++++- azero/contracts/tests/Cargo.lock | 24 ++++++++++++------------ azero/contracts/token/lib.rs | 14 +++++++++++++- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/azero/contracts/most/lib.rs b/azero/contracts/most/lib.rs index d3c6335d..8c3a2073 100755 --- a/azero/contracts/most/lib.rs +++ b/azero/contracts/most/lib.rs @@ -708,7 +708,7 @@ pub mod most { amount: u128, ) -> Result<(), PSP22Error> { let mut psp22: ink::contract_ref!(Burnable) = token.into(); - psp22.burn(from, amount) + psp22.burn_from(from, amount) } } diff --git a/azero/contracts/psp22-traits/lib.rs b/azero/contracts/psp22-traits/lib.rs index 2e2fa152..a71171d9 100755 --- a/azero/contracts/psp22-traits/lib.rs +++ b/azero/contracts/psp22-traits/lib.rs @@ -17,5 +17,8 @@ pub trait Mintable { #[ink::trait_definition] pub trait Burnable { #[ink(message)] - fn burn(&mut self, from: AccountId, amount: Balance) -> Result<(), PSP22Error>; + fn burn(&mut self, amount: Balance) -> Result<(), PSP22Error>; + + #[ink(message)] + fn burn_from(&mut self, from: AccountId, amount: Balance) -> Result<(), PSP22Error>; } diff --git a/azero/contracts/tests/Cargo.lock b/azero/contracts/tests/Cargo.lock index 6c2e987e..1ce67f26 100644 --- a/azero/contracts/tests/Cargo.lock +++ b/azero/contracts/tests/Cargo.lock @@ -2583,18 +2583,6 @@ dependencies = [ "regex-automata 0.1.10", ] -[[package]] -name = "most" -version = "1.0.0" -dependencies = [ - "ink", - "parity-scale-codec", - "psp22", - "psp22-traits", - "scale-info", - "shared", -] - [[package]] name = "memchr" version = "2.6.4" @@ -2678,6 +2666,18 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "most" +version = "1.0.0" +dependencies = [ + "ink", + "parity-scale-codec", + "psp22", + "psp22-traits", + "scale-info", + "shared", +] + [[package]] name = "no-std-net" version = "0.6.0" diff --git a/azero/contracts/token/lib.rs b/azero/contracts/token/lib.rs index 4dff7590..bac6a7bf 100755 --- a/azero/contracts/token/lib.rs +++ b/azero/contracts/token/lib.rs @@ -232,9 +232,21 @@ pub mod token { impl Burnable for Token { #[ink(message)] - fn burn(&mut self, from: AccountId, value: u128) -> Result<(), PSP22Error> { + fn burn(&mut self, value: u128) -> Result<(), PSP22Error> { + let events = self.data.burn(self.env().caller(), value)?; + self.emit_events(events); + Ok(()) + } + + #[ink(message)] + fn burn_from(&mut self, from: AccountId, value: u128) -> Result<(), PSP22Error> { self.ensure_minter_burner()?; + let caller = self.env().caller(); + if self.data.allowance(from, caller) < value { + return Err(PSP22Error::InsufficientAllowance); + } let events = self.data.burn(from, value)?; + self.data.decrease_allowance(from, caller, value)?; self.emit_events(events); Ok(()) } From fd7eee8b7fd1292f1445d2e2b968ff331f798194 Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 13 Dec 2023 14:02:50 +0100 Subject: [PATCH 2/5] call approval --- azero/contracts/tests/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/azero/contracts/tests/lib.rs b/azero/contracts/tests/lib.rs index b8e67055..1eb9edf3 100644 --- a/azero/contracts/tests/lib.rs +++ b/azero/contracts/tests/lib.rs @@ -124,6 +124,16 @@ mod e2e { .expect("balance_of should succeed"); let amount_to_send = 1000; + + _ = psp22_approve( + &mut client, + &alice(), + token_address, + most_address, + amount_to_send, + ) + .await; + most_send_request( &mut client, &alice(), @@ -198,6 +208,15 @@ mod e2e { .await .expect("should return base fee"); + _ = psp22_approve( + &mut client, + &alice(), + token_address, + most_address, + amount_to_send, + ) + .await; + let send_request_res = most_send_request( &mut client, &alice(), @@ -952,6 +971,23 @@ mod e2e { .return_value()) } + async fn psp22_approve( + client: &mut E2EClient, + caller: &Keypair, + token: AccountId, + spender: AccountId, + amount: u128, + ) -> CallResult<(), PSP22Error> { + call_message::( + client, + caller, + token, + |token| token.approve(spender, amount), + None, + ) + .await + } + async fn most_committee_rewards( client: &mut E2EClient, most_address: AccountId, From f38adaf404530ce6f8bc2217af39a4425fba59cb Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 13 Dec 2023 14:12:20 +0100 Subject: [PATCH 3/5] fix unit tests --- azero/contracts/token/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azero/contracts/token/lib.rs b/azero/contracts/token/lib.rs index bac6a7bf..33bdc96d 100755 --- a/azero/contracts/token/lib.rs +++ b/azero/contracts/token/lib.rs @@ -350,7 +350,7 @@ pub mod token { let alice_balance_before = token.balance_of(alice); set_caller::(charlie); - assert!(token.burn(alice, 100).is_ok()); + assert!(token.burn_from(alice, 100).is_ok()); assert_eq!(token.balance_of(alice), alice_balance_before - 100); } @@ -362,7 +362,7 @@ pub mod token { set_caller::(bob); assert_eq!( - token.burn(alice, 100), + token.burn_from(alice, 100), Err(PSP22Error::Custom(String::from( "Caller has to be the minter/burner." ))) From 0fe3ce58ba09b774e42cd4c5c48b0fb880ad8c56 Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 13 Dec 2023 14:16:26 +0100 Subject: [PATCH 4/5] fix unit --- azero/contracts/token/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/azero/contracts/token/lib.rs b/azero/contracts/token/lib.rs index 33bdc96d..82ba031a 100755 --- a/azero/contracts/token/lib.rs +++ b/azero/contracts/token/lib.rs @@ -345,10 +345,12 @@ pub mod token { fn minter_burner_can_burn() { let mut token = init_contract(INIT_SUPPLY_TEST); let alice = default_accounts::().alice; - let bob = default_accounts::().bob; let charlie = default_accounts::().charlie; let alice_balance_before = token.balance_of(alice); + set_caller::(alice); + assert!(token.approve(charlie, 100).is_ok()); + set_caller::(charlie); assert!(token.burn_from(alice, 100).is_ok()); assert_eq!(token.balance_of(alice), alice_balance_before - 100); From 58e6d44513ed56e65fc40c612cca1663c399f8c7 Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 14 Dec 2023 10:58:56 +0100 Subject: [PATCH 5/5] assert approval --- azero/contracts/tests/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/azero/contracts/tests/lib.rs b/azero/contracts/tests/lib.rs index 1eb9edf3..4d885fd1 100644 --- a/azero/contracts/tests/lib.rs +++ b/azero/contracts/tests/lib.rs @@ -125,14 +125,15 @@ mod e2e { let amount_to_send = 1000; - _ = psp22_approve( + psp22_approve( &mut client, &alice(), token_address, most_address, amount_to_send, ) - .await; + .await + .expect("approval should succeed"); most_send_request( &mut client, @@ -208,14 +209,15 @@ mod e2e { .await .expect("should return base fee"); - _ = psp22_approve( + psp22_approve( &mut client, &alice(), token_address, most_address, amount_to_send, ) - .await; + .await + .expect("approval should succeed"); let send_request_res = most_send_request( &mut client,