Skip to content

Commit 34a6f86

Browse files
committed
feat: add test for withdrawing native tokens and ERC20 tokens in Subscription contract
1 parent 5d6a8a6 commit 34a6f86

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/no-fork/SubscriptionTest.t.sol

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ contract SubscriptionTest is Test {
1919
address user;
2020
uint256 userPrivateKey;
2121

22+
uint256 constant AMOUNT = 1e18;
2223
uint256 constant AMOUNT_CCIPBNM = 1e18;
24+
uint256 private constant SUBSCRIPTION_FEE = 1e16; // 0.01 ether
25+
2326
IRouterClient destinationRouter;
2427
address linkAddress;
2528

@@ -154,6 +157,43 @@ contract SubscriptionTest is Test {
154157
);
155158
}
156159

160+
function testWithdrawNativeTokenAndTokenSuccess() public {
161+
vm.deal(user, AMOUNT);
162+
deal(address(CCIPBnM), user, AMOUNT_CCIPBNM);
163+
164+
// 1. withdraw native token
165+
vm.prank(user);
166+
subscription.paySubscriptionFeeForPrimaryChain{value: AMOUNT}(
167+
address(0)
168+
);
169+
assertEq(address(subscription).balance, AMOUNT);
170+
assertEq(user.balance, 0);
171+
vm.prank(user);
172+
subscription.withdraw();
173+
assertEq(address(subscription).balance, 0);
174+
assertEq(user.balance, AMOUNT);
175+
176+
// 2. withdraw token
177+
vm.startPrank(user);
178+
CCIPBnM.approve(address(subscription), AMOUNT_CCIPBNM);
179+
subscription.paySubscriptionFeeForPrimaryChain(address(CCIPBnM));
180+
vm.stopPrank();
181+
assertEq(
182+
IERC20(address(CCIPBnM)).balanceOf(address(subscription)),
183+
SUBSCRIPTION_FEE
184+
);
185+
assertEq(
186+
IERC20(address(CCIPBnM)).balanceOf(user),
187+
AMOUNT_CCIPBNM - SUBSCRIPTION_FEE
188+
);
189+
190+
vm.prank(user);
191+
subscription.withdrawToken(address(CCIPBnM));
192+
193+
assertEq(IERC20(address(CCIPBnM)).balanceOf(address(subscription)), 0);
194+
assertEq(IERC20(address(CCIPBnM)).balanceOf(user), AMOUNT_CCIPBNM);
195+
}
196+
157197
/*//////////////////////////////////////////////////////////////
158198
HELPER FUNCTIONS
159199
//////////////////////////////////////////////////////////////*/

0 commit comments

Comments
 (0)