Skip to content

Commit

Permalink
test: use deal(token, address, amount) instead of depending on whale …
Browse files Browse the repository at this point in the history
…balance
  • Loading branch information
0xChin committed Nov 19, 2024
1 parent 8100918 commit e5a4aa3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
21 changes: 11 additions & 10 deletions test/integration/Grateful.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ contract IntegrationGreeter is IntegrationBase {

// Tests for Standard Payments
function test_Payment() public {
_approveAndPay(_payer, _merchant, _AMOUNT_USDC, _NOT_YIELDING_FUNDS);
_approveAndPay(_user, _merchant, _AMOUNT_USDC, _NOT_YIELDING_FUNDS);

assertEq(
_usdc.balanceOf(_merchant), _grateful.applyFee(_merchant, _AMOUNT_USDC), "Merchant balance mismatch after payment"
Expand All @@ -22,7 +22,7 @@ contract IntegrationGreeter is IntegrationBase {
// Capture owner's initial balance before payment
uint256 ownerInitialBalance = _usdc.balanceOf(_owner);

_approveAndPay(_payer, _merchant, _AMOUNT_USDC, _YIELDING_FUNDS);
_approveAndPay(_user, _merchant, _AMOUNT_USDC, _YIELDING_FUNDS);

// Advance time to accrue yield
vm.warp(block.timestamp + 1 days);
Expand Down Expand Up @@ -57,7 +57,7 @@ contract IntegrationGreeter is IntegrationBase {

// Tests for One-Time Payments
function test_OneTimePayment() public {
_setupAndExecuteOneTimePayment(_payer, _merchant, _AMOUNT_USDC, _PAYMENT_SALT, _NOT_YIELDING_FUNDS);
_setupAndExecuteOneTimePayment(_user, _merchant, _AMOUNT_USDC, _PAYMENT_SALT, _NOT_YIELDING_FUNDS);

// Merchant receives the payment
assertEq(
Expand All @@ -72,7 +72,7 @@ contract IntegrationGreeter is IntegrationBase {
uint256 ownerInitialBalance = _usdc.balanceOf(_owner);

// Setup one-time payment with yielding funds
_setupAndExecuteOneTimePayment(_payer, _merchant, _AMOUNT_USDC, _PAYMENT_SALT, _YIELDING_FUNDS);
_setupAndExecuteOneTimePayment(_user, _merchant, _AMOUNT_USDC, _PAYMENT_SALT, _YIELDING_FUNDS);

// Advance time to accrue yield
vm.warp(block.timestamp + 1 days);
Expand Down Expand Up @@ -106,13 +106,14 @@ contract IntegrationGreeter is IntegrationBase {
}

function test_OverpaidOneTimePayment() public {
uint256 paymentId = _grateful.calculateId(_payer, _merchant, address(_usdc), _AMOUNT_USDC);
uint256 paymentId = _grateful.calculateId(_user, _merchant, address(_usdc), _AMOUNT_USDC);
address precomputed = address(
_grateful.computeOneTimeAddress(_merchant, _tokens, _AMOUNT_USDC, _PAYMENT_SALT, paymentId, _NOT_YIELDING_FUNDS)
);

// Payer sends double the amount
vm.prank(_payer);
deal(address(_usdc), _user, _AMOUNT_USDC * 2);
vm.prank(_user);
_usdc.transfer(precomputed, _AMOUNT_USDC * 2);

vm.prank(_gratefulAutomation);
Expand All @@ -133,12 +134,12 @@ contract IntegrationGreeter is IntegrationBase {
);

// Rescue funds
uint256 prevPayerBalance = _usdc.balanceOf(_payer);
uint256 prevPayerBalance = _usdc.balanceOf(_user);
vm.prank(_owner);
_oneTime.rescueFunds(_usdc, _payer, _AMOUNT_USDC);
_oneTime.rescueFunds(_usdc, _user, _AMOUNT_USDC);

// Verify payer's balance after rescuing funds
assertEq(_usdc.balanceOf(_payer), prevPayerBalance + _AMOUNT_USDC, "Payer balance mismatch after rescuing funds");
assertEq(_usdc.balanceOf(_user), prevPayerBalance + _AMOUNT_USDC, "Payer balance mismatch after rescuing funds");
}

function test_PaymentWithCustomFee() public {
Expand All @@ -163,7 +164,7 @@ contract IntegrationGreeter is IntegrationBase {
vm.warp(block.timestamp + 1);

// Process payment
_approveAndPay(_payer, _merchant, _AMOUNT_USDC, _NOT_YIELDING_FUNDS);
_approveAndPay(_user, _merchant, _AMOUNT_USDC, _NOT_YIELDING_FUNDS);

// Calculate expected amounts
uint256 feeAmount = (_AMOUNT_USDC * customFees[i]) / 10_000;
Expand Down
3 changes: 2 additions & 1 deletion test/integration/IntegrationBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ contract IntegrationBase is Test {
address internal _merchant = makeAddr("merchant");
address internal _owner = makeAddr("owner");
address internal _gratefulAutomation = makeAddr("gratefulAutomation");
address internal _payer = 0x555d73f2002A457211d690313f942B065eAD1FFF;

// Tokens
IERC20 internal _usdc = IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
Expand Down Expand Up @@ -111,6 +110,7 @@ contract IntegrationBase is Test {

function _approveAndPay(address payer, address merchant, uint256 amount, bool yieldFunds) internal {
uint256 paymentId = _grateful.calculateId(payer, merchant, address(_usdc), amount);
deal(address(_usdc), payer, amount);
vm.startPrank(payer);
_usdc.approve(address(_grateful), amount);
_grateful.pay(merchant, address(_usdc), amount, paymentId, yieldFunds);
Expand Down Expand Up @@ -143,6 +143,7 @@ contract IntegrationBase is Test {
uint256 salt,
bool yieldFunds
) internal returns (uint256 paymentId, address precomputed) {
deal(address(_usdc), payer, amount);
paymentId = _grateful.calculateId(payer, merchant, address(_usdc), amount);
precomputed = address(_grateful.computeOneTimeAddress(merchant, _tokens, amount, salt, paymentId, yieldFunds));
vm.prank(payer);
Expand Down

0 comments on commit e5a4aa3

Please sign in to comment.