Skip to content

Commit

Permalink
test(erc20): tests for Burnable extension (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
bidzyyys committed Apr 4, 2024
1 parent dc04ea3 commit af75199
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 5 deletions.
110 changes: 109 additions & 1 deletion contracts/src/erc20/extensions/burnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_))));
}
}
10 changes: 6 additions & 4 deletions contracts/src/erc20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit af75199

Please sign in to comment.