From af75199a568d7658afa73a9a06d3a36080667653 Mon Sep 17 00:00:00 2001 From: Daniel Bigos Date: Thu, 4 Apr 2024 16:47:02 +0200 Subject: [PATCH] test(erc20): tests for Burnable extension (#27) --- contracts/src/erc20/extensions/burnable.rs | 110 ++++++++++++++++++++- contracts/src/erc20/mod.rs | 10 +- 2 files changed, 115 insertions(+), 5 deletions(-) diff --git a/contracts/src/erc20/extensions/burnable.rs b/contracts/src/erc20/extensions/burnable.rs index 5df468026..34916a542 100644 --- a/contracts/src/erc20/extensions/burnable.rs +++ b/contracts/src/erc20/extensions/burnable.rs @@ -61,4 +61,112 @@ impl ERC20Burnable { } #[cfg(test)] -mod tests {} +mod tests { + use alloy_primitives::{address, Address, U256}; + use stylus_sdk::msg; + + use crate::erc20::{Error, ERC20}; + + use super::ERC20Burnable; + + impl Default for ERC20Burnable { + fn default() -> Self { + Self { erc20: ERC20::default() } + } + } + + #[grip::test] + fn burns(contract: ERC20Burnable) { + let zero = U256::ZERO; + let one = U256::from(1); + + assert_eq!(zero, contract.erc20.total_supply()); + + // Mint some tokens for msg::sender(). + let sender = msg::sender(); + + let two = U256::from(2); + contract.erc20._update(Address::ZERO, sender, two).unwrap(); + assert_eq!(two, contract.erc20.balance_of(sender)); + assert_eq!(two, contract.erc20.total_supply()); + + contract.burn(one).unwrap(); + + assert_eq!(one, contract.erc20.balance_of(sender)); + assert_eq!(one, contract.erc20.total_supply()); + } + + #[grip::test] + fn burns_errors_when_insufficient_balance(contract: ERC20Burnable) { + let one = U256::from(1); + let sender = msg::sender(); + + assert_eq!(U256::ZERO, contract.erc20.balance_of(sender)); + + let result = contract.burn(one); + + assert!(matches!(result, Err(Error::InsufficientBalance(_)))); + } + + #[grip::test] + fn burn_from(contract: ERC20Burnable) { + let alice = address!("A11CEacF9aa32246d767FCCD72e02d6bCbcC375d"); + let sender = msg::sender(); + + // Alice approves `msg::sender`. + let one = U256::from(1); + contract.erc20._allowances.setter(alice).setter(sender).set(one); + + // Mint some tokens for Alice. + let two = U256::from(2); + contract.erc20._update(Address::ZERO, alice, two).unwrap(); + assert_eq!(two, contract.erc20.balance_of(alice)); + assert_eq!(two, contract.erc20.total_supply()); + + contract.burn_from(alice, one).unwrap(); + + assert_eq!(one, contract.erc20.balance_of(alice)); + assert_eq!(one, contract.erc20.total_supply()); + assert_eq!(U256::ZERO, contract.erc20.allowance(alice, sender)); + } + + #[grip::test] + fn burns_from_errors_when_insufficient_balance(contract: ERC20Burnable) { + let alice = address!("A11CEacF9aa32246d767FCCD72e02d6bCbcC375d"); + + // Alice approves `msg::sender`. + let one = U256::from(1); + contract.erc20._allowances.setter(alice).setter(msg::sender()).set(one); + assert_eq!(U256::ZERO, contract.erc20.balance_of(alice)); + + let one = U256::from(1); + let result = contract.burn_from(alice, one); + assert!(matches!(result, Err(Error::InsufficientBalance(_)))); + } + + #[grip::test] + fn burns_from_errors_when_invalid_sender(contract: ERC20Burnable) { + let one = U256::from(1); + contract + .erc20 + ._allowances + .setter(Address::ZERO) + .setter(msg::sender()) + .set(one); + let result = contract.burn_from(Address::ZERO, one); + assert!(matches!(result, Err(Error::InvalidSender(_)))); + } + + #[grip::test] + fn burns_from_errors_when_insufficient_allowance(contract: ERC20Burnable) { + let alice = address!("A11CEacF9aa32246d767FCCD72e02d6bCbcC375d"); + + // Mint some tokens for Alice. + let one = U256::from(1); + contract.erc20._update(Address::ZERO, alice, one).unwrap(); + assert_eq!(one, contract.erc20.balance_of(alice)); + + let result = contract.burn_from(alice, one); + assert!(matches!(result, Err(Error::InsufficientAllowance(_)))); + } +} diff --git a/contracts/src/erc20/mod.rs b/contracts/src/erc20/mod.rs index 59c62ce0f..4c76c0816 100644 --- a/contracts/src/erc20/mod.rs +++ b/contracts/src/erc20/mod.rs @@ -482,7 +482,7 @@ mod tests { // Mint some tokens for Alice. let two = U256::from(2); - contract._balances.setter(alice).set(two); + contract._update(Address::ZERO, alice, two).unwrap(); assert_eq!(two, contract.balance_of(alice)); contract.transfer_from(alice, bob, one).unwrap(); @@ -495,20 +495,22 @@ mod tests { fn transfers_from(contract: ERC20) { let alice = address!("A11CEacF9aa32246d767FCCD72e02d6bCbcC375d"); let bob = address!("B0B0cB49ec2e96DF5F5fFB081acaE66A2cBBc2e2"); + let sender = msg::sender(); // Alice approves `msg::sender`. let one = U256::from(1); - contract._allowances.setter(alice).setter(msg::sender()).set(one); + contract._allowances.setter(alice).setter(sender).set(one); // Mint some tokens for Alice. let two = U256::from(2); - contract._balances.setter(alice).set(two); + contract._update(Address::ZERO, alice, two).unwrap(); assert_eq!(two, contract.balance_of(alice)); contract.transfer_from(alice, bob, one).unwrap(); assert_eq!(one, contract.balance_of(alice)); assert_eq!(one, contract.balance_of(bob)); + assert_eq!(U256::ZERO, contract.allowance(alice, sender)); } #[grip::test] @@ -555,7 +557,7 @@ mod tests { // Mint some tokens for Alice. let one = U256::from(1); - contract._balances.setter(alice).set(one); + contract._update(Address::ZERO, alice, one).unwrap(); assert_eq!(one, contract.balance_of(alice)); let result = contract.transfer_from(alice, bob, one);