From dc34270e55b6cd7a3d4c6b2c0ff77b48e10a99a3 Mon Sep 17 00:00:00 2001 From: Filip Bielejec Date: Thu, 14 Dec 2023 11:12:08 +0100 Subject: [PATCH] check allowance (#42) * check allowance * call approval * fix unit tests * fix unit * assert approval --- azero/contracts/most/lib.rs | 2 +- azero/contracts/psp22-traits/lib.rs | 5 +++- azero/contracts/tests/Cargo.lock | 24 +++++++++--------- azero/contracts/tests/lib.rs | 38 +++++++++++++++++++++++++++++ azero/contracts/token/lib.rs | 22 ++++++++++++++--- 5 files changed, 73 insertions(+), 18 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/tests/lib.rs b/azero/contracts/tests/lib.rs index b8e67055..4d885fd1 100644 --- a/azero/contracts/tests/lib.rs +++ b/azero/contracts/tests/lib.rs @@ -124,6 +124,17 @@ 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 + .expect("approval should succeed"); + most_send_request( &mut client, &alice(), @@ -198,6 +209,16 @@ mod e2e { .await .expect("should return base fee"); + psp22_approve( + &mut client, + &alice(), + token_address, + most_address, + amount_to_send, + ) + .await + .expect("approval should succeed"); + let send_request_res = most_send_request( &mut client, &alice(), @@ -952,6 +973,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, diff --git a/azero/contracts/token/lib.rs b/azero/contracts/token/lib.rs index 4dff7590..82ba031a 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(()) } @@ -333,12 +345,14 @@ 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(alice, 100).is_ok()); + assert!(token.burn_from(alice, 100).is_ok()); assert_eq!(token.balance_of(alice), alice_balance_before - 100); } @@ -350,7 +364,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." )))