Skip to content

Commit

Permalink
check allowance (#42)
Browse files Browse the repository at this point in the history
* check allowance

* call approval

* fix unit tests

* fix unit

* assert approval
  • Loading branch information
fbielejec authored Dec 14, 2023
1 parent 72666c2 commit dc34270
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 18 deletions.
2 changes: 1 addition & 1 deletion azero/contracts/most/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
5 changes: 4 additions & 1 deletion azero/contracts/psp22-traits/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}
24 changes: 12 additions & 12 deletions azero/contracts/tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions azero/contracts/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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::<TokenRef, _, _, _, _>(
client,
caller,
token,
|token| token.approve(spender, amount),
None,
)
.await
}

async fn most_committee_rewards(
client: &mut E2EClient,
most_address: AccountId,
Expand Down
22 changes: 18 additions & 4 deletions azero/contracts/token/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -333,12 +345,14 @@ pub mod token {
fn minter_burner_can_burn() {
let mut token = init_contract(INIT_SUPPLY_TEST);
let alice = default_accounts::<E>().alice;
let bob = default_accounts::<E>().bob;
let charlie = default_accounts::<E>().charlie;
let alice_balance_before = token.balance_of(alice);

set_caller::<E>(alice);
assert!(token.approve(charlie, 100).is_ok());

set_caller::<E>(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);
}

Expand All @@ -350,7 +364,7 @@ pub mod token {

set_caller::<E>(bob);
assert_eq!(
token.burn(alice, 100),
token.burn_from(alice, 100),
Err(PSP22Error::Custom(String::from(
"Caller has to be the minter/burner."
)))
Expand Down

0 comments on commit dc34270

Please sign in to comment.