Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNeshi committed Sep 24, 2024
1 parent 89240e6 commit 282e9f9
Showing 1 changed file with 72 additions and 63 deletions.
135 changes: 72 additions & 63 deletions examples/safe-erc20/tests/safe-erc20.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![cfg(feature = "e2e")]

use std::assert_ne;

use alloy::primitives::uint;
use alloy_primitives::U256;
use e2e::{receipt, send, watch, Account, ReceiptExt, Revert};
Expand All @@ -14,35 +12,41 @@ mod mock;

#[e2e::test]
async fn safe_transfers(alice: Account, bob: Account) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract_alice = SafeErc20::new(contract_addr, &alice.wallet);
let alice_addr = alice.address();
let safe_erc20_mock_addr = alice.as_deployer().deploy().await?.address()?;
let safe_erc20_mock_alice =
SafeErc20::new(safe_erc20_mock_addr, &alice.wallet);
let bob_addr = bob.address();

let balance = uint!(10_U256);
let value = uint!(1_U256);

let erc20_address = erc20::deploy(&alice.wallet).await?;
let erc20_alice = ERC20Mock::new(erc20_address, &alice.wallet);
let erc20mock_address = erc20::deploy(&alice.wallet).await?;
let erc20_alice = ERC20Mock::new(erc20mock_address, &alice.wallet);

let _ = watch!(erc20_alice.mint(alice_addr, balance));
let _ = watch!(erc20_alice.mint(safe_erc20_mock_addr, balance));

let ERC20Mock::balanceOfReturn { _0: initial_alice_balance } =
erc20_alice.balanceOf(alice_addr).call().await?;
let ERC20Mock::balanceOfReturn { _0: initial_safe_erc20_mock_balance } =
erc20_alice.balanceOf(safe_erc20_mock_addr).call().await?;
let ERC20Mock::balanceOfReturn { _0: initial_bob_balance } =
erc20_alice.balanceOf(bob_addr).call().await?;
assert_eq!(initial_alice_balance, balance);
assert_eq!(initial_safe_erc20_mock_balance, balance);
assert_eq!(initial_bob_balance, U256::ZERO);

let _ =
receipt!(contract_alice.safeTransfer(erc20_address, bob_addr, value))?;
let _ = receipt!(safe_erc20_mock_alice.safeTransfer(
erc20mock_address,
bob_addr,
value
))?;

let ERC20Mock::balanceOfReturn { _0: alice_balance } =
erc20_alice.balanceOf(alice_addr).call().await?;
let ERC20Mock::balanceOfReturn { _0: safe_erc20_mock_balance } =
erc20_alice.balanceOf(safe_erc20_mock_addr).call().await?;
let ERC20Mock::balanceOfReturn { _0: bob_balance } =
erc20_alice.balanceOf(bob_addr).call().await?;

assert_eq!(initial_alice_balance - value, alice_balance);
assert_eq!(
initial_safe_erc20_mock_balance - value,
safe_erc20_mock_balance
);
assert_eq!(initial_bob_balance + value, bob_balance);

Ok(())
Expand All @@ -52,20 +56,25 @@ async fn safe_transfers(alice: Account, bob: Account) -> eyre::Result<()> {
async fn safe_transfer_rejects_with_eoa_as_token(
alice: Account,
bob: Account,
has_no_code: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract_alice = SafeErc20::new(contract_addr, &alice.wallet);
let alice_addr = alice.address();
let safe_erc20_mock_addr = alice.as_deployer().deploy().await?.address()?;
let safe_erc20_mock_alice =
SafeErc20::new(safe_erc20_mock_addr, &alice.wallet);
let bob_addr = bob.address();
let has_no_code_addr = has_no_code.address();

let value = uint!(1_U256);

let err = send!(contract_alice.safeTransfer(alice_addr, bob_addr, value))
.expect_err("should not be able to invoke 'transfer' on EOA");
let err = send!(safe_erc20_mock_alice.safeTransfer(
has_no_code_addr,
bob_addr,
value
))
.expect_err("should not be able to invoke 'transfer' on EOA");
assert!(err.reverted_with(SafeErc20::SafeErc20FailedOperation {
token: alice_addr
token: has_no_code_addr
}));
assert_ne!(alice_addr, bob_addr);

Ok(())
}
Expand All @@ -75,19 +84,21 @@ async fn safe_transfer_rejects_on_error(
alice: Account,
bob: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract_alice = SafeErc20::new(contract_addr, &alice.wallet);
let safe_erc20_mock_addr = alice.as_deployer().deploy().await?.address()?;
let safe_erc20_mock_alice =
SafeErc20::new(safe_erc20_mock_addr, &alice.wallet);
let bob_addr = bob.address();

let value = uint!(1_U256);

let erc20_address = erc20::deploy(&alice.wallet).await?;

let err =
send!(contract_alice.safeTransfer(erc20_address, bob_addr, value))
.expect_err(
"transfer should not succeed when insufficient balance",
);
let err = send!(safe_erc20_mock_alice.safeTransfer(
erc20_address,
bob_addr,
value
))
.expect_err("transfer should not succeed when insufficient balance");
assert!(err.reverted_with(SafeErc20::SafeErc20FailedOperation {
token: erc20_address
}));
Expand All @@ -96,48 +107,42 @@ async fn safe_transfer_rejects_on_error(
}

#[e2e::test]
async fn safe_transfers_from(
alice: Account,
bob: Account,
charlie: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract_alice = SafeErc20::new(contract_addr, &alice.wallet);
async fn safe_transfers_from(alice: Account, bob: Account) -> eyre::Result<()> {
let safe_erc20_mock_addr = alice.as_deployer().deploy().await?.address()?;
let safe_erc20_mock_alice =
SafeErc20::new(safe_erc20_mock_addr, &alice.wallet);
let alice_addr = alice.address();
let bob_addr = bob.address();
let charlie_addr = charlie.address();

let balance = uint!(10_U256);
let value = uint!(1_U256);

let erc20_address = erc20::deploy(&alice.wallet).await?;
let erc20_alice = ERC20Mock::new(erc20_address, &alice.wallet);

let _ = watch!(erc20_alice.mint(charlie_addr, balance));
let _ = watch!(erc20_alice.mint(alice_addr, balance));
let _ = watch!(erc20_alice.approve(safe_erc20_mock_addr, value));

let ERC20Mock::balanceOfReturn { _0: initial_charlie_balance } =
erc20_alice.balanceOf(charlie_addr).call().await?;
let ERC20Mock::balanceOfReturn { _0: initial_alice_balance } =
erc20_alice.balanceOf(alice_addr).call().await?;
let ERC20Mock::balanceOfReturn { _0: initial_bob_balance } =
erc20_alice.balanceOf(bob_addr).call().await?;
assert_eq!(initial_charlie_balance, balance);
assert_eq!(initial_alice_balance, balance);
assert_eq!(initial_bob_balance, U256::ZERO);

let erc20_charlie = ERC20Mock::new(erc20_address, &charlie.wallet);
let _ = watch!(erc20_charlie.approve(alice_addr, value));

let _ = receipt!(contract_alice.safeTransferFrom(
let _ = receipt!(safe_erc20_mock_alice.safeTransferFrom(
erc20_address,
charlie_addr,
alice_addr,
bob_addr,
value
))?;

let ERC20Mock::balanceOfReturn { _0: charlie_balance } =
erc20_alice.balanceOf(charlie_addr).call().await?;
let ERC20Mock::balanceOfReturn { _0: alice_balance } =
erc20_alice.balanceOf(alice_addr).call().await?;
let ERC20Mock::balanceOfReturn { _0: bob_balance } =
erc20_alice.balanceOf(bob_addr).call().await?;

assert_eq!(initial_charlie_balance - value, charlie_balance);
assert_eq!(initial_alice_balance - value, alice_balance);
assert_eq!(initial_bob_balance + value, bob_balance);

Ok(())
Expand All @@ -147,25 +152,26 @@ async fn safe_transfers_from(
async fn safe_transfer_from_rejects_with_eoa_as_token(
alice: Account,
bob: Account,
charlie: Account,
has_no_code: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract_alice = SafeErc20::new(contract_addr, &alice.wallet);
let safe_erc20_mock_addr = alice.as_deployer().deploy().await?.address()?;
let safe_erc20_mock_alice =
SafeErc20::new(safe_erc20_mock_addr, &alice.wallet);
let alice_addr = alice.address();
let bob_addr = bob.address();
let charlie_addr = charlie.address();
let has_no_code_addr = has_no_code.address();

let value = uint!(1_U256);

let err = send!(contract_alice.safeTransferFrom(
let err = send!(safe_erc20_mock_alice.safeTransferFrom(
has_no_code_addr,
alice_addr,
bob_addr,
charlie_addr,
value
))
.expect_err("should not be able to invoke 'transferFrom' on EOA");
assert!(err.reverted_with(SafeErc20::SafeErc20FailedOperation {
token: alice_addr
token: has_no_code_addr
}));

Ok(())
Expand All @@ -175,20 +181,23 @@ async fn safe_transfer_from_rejects_with_eoa_as_token(
async fn safe_transfer_from_rejects_on_error(
alice: Account,
bob: Account,
charlie: Account,
) -> eyre::Result<()> {
let contract_addr = alice.as_deployer().deploy().await?.address()?;
let contract_alice = SafeErc20::new(contract_addr, &alice.wallet);
let safe_erc20_mock_addr = alice.as_deployer().deploy().await?.address()?;
let safe_erc20_mock_alice =
SafeErc20::new(safe_erc20_mock_addr, &alice.wallet);
let alice_addr = alice.address();
let bob_addr = bob.address();
let charlie_addr = charlie.address();

let balance = uint!(10_U256);
let value = uint!(1_U256);

let erc20_address = erc20::deploy(&alice.wallet).await?;
let erc20_alice = ERC20Mock::new(erc20_address, &alice.wallet);
let _ = watch!(erc20_alice.mint(alice_addr, balance));

let err = send!(contract_alice.safeTransferFrom(
let err = send!(safe_erc20_mock_alice.safeTransferFrom(
erc20_address,
charlie_addr,
alice_addr,
bob_addr,
value
))
Expand Down

0 comments on commit 282e9f9

Please sign in to comment.