Skip to content

Commit

Permalink
Remove amount arg in withdraw and withdraw all the withdrawable amount
Browse files Browse the repository at this point in the history
  • Loading branch information
kidambisrinivas committed Dec 18, 2023
1 parent 54a523e commit 94b1d53
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
10 changes: 6 additions & 4 deletions contracts/src/v0.8/vrf/dev/SubscriptionAPI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,14 @@ abstract contract SubscriptionAPI is ConfirmedOwner, IERC677Receiver, IVRFSubscr
* @param recipient where to send the funds
* @param amount amount to withdraw
*/
function withdraw(address recipient, uint96 amount) external nonReentrant onlyOwner {
function withdraw(address recipient) external nonReentrant onlyOwner {
if (address(LINK) == address(0)) {
revert LinkNotSet();
}
if (s_withdrawableTokens < amount) {
if (s_withdrawableTokens == 0) {
revert InsufficientBalance();
}
uint96 amount = s_withdrawableTokens;
s_withdrawableTokens -= amount;
s_totalBalance -= amount;
if (!LINK.transfer(recipient, amount)) {
Expand All @@ -227,11 +228,12 @@ abstract contract SubscriptionAPI is ConfirmedOwner, IERC677Receiver, IVRFSubscr
* @param recipient where to send the funds
* @param amount amount to withdraw
*/
function withdrawNative(address payable recipient, uint96 amount) external nonReentrant onlyOwner {
if (s_withdrawableNative < amount) {
function withdrawNative(address payable recipient) external nonReentrant onlyOwner {
if (s_withdrawableNative == 0) {
revert InsufficientBalance();
}
// Prevent re-entrancy by updating state before transfer.
uint96 amount = s_withdrawableNative;
s_withdrawableNative -= amount;
s_totalNativeBalance -= amount;
(bool sent, ) = recipient.call{value: amount}("");
Expand Down
14 changes: 7 additions & 7 deletions contracts/test/v0.8/foundry/vrf/VRFV2PlusSubscriptionAPI.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ contract VRFV2PlusSubscriptionAPITest is BaseTest {
function testWithdrawNoLink() public {
// CASE: no link token set
vm.expectRevert(SubscriptionAPI.LinkNotSet.selector);
s_subscriptionAPI.withdraw(OWNER, 1 ether);
s_subscriptionAPI.withdraw(OWNER);
}

function testWithdrawInsufficientBalance() public {
Expand All @@ -330,7 +330,7 @@ contract VRFV2PlusSubscriptionAPITest is BaseTest {

// call withdraw
vm.expectRevert(SubscriptionAPI.InsufficientBalance.selector);
s_subscriptionAPI.withdraw(OWNER, 1 ether);
s_subscriptionAPI.withdraw(OWNER);
}

function testWithdrawSufficientBalanceLinkSet() public {
Expand All @@ -355,7 +355,7 @@ contract VRFV2PlusSubscriptionAPITest is BaseTest {
// call Withdraw from owner address
uint256 ownerBalance = linkToken.balanceOf(OWNER);
changePrank(OWNER);
s_subscriptionAPI.withdraw(OWNER, 1 ether);
s_subscriptionAPI.withdraw(OWNER);
// assert link balance of owner
assertEq(linkToken.balanceOf(OWNER) - ownerBalance, 1 ether, "owner link balance incorrect");
// assert state of subscription api
Expand All @@ -371,21 +371,21 @@ contract VRFV2PlusSubscriptionAPITest is BaseTest {
// call WithdrawNative
changePrank(OWNER);
vm.expectRevert(SubscriptionAPI.InsufficientBalance.selector);
s_subscriptionAPI.withdrawNative(payable(OWNER), 1 ether);
s_subscriptionAPI.withdrawNative(payable(OWNER));
}

function testWithdrawLinkInvalidOwner() public {
address invalidAddress = makeAddr("invalidAddress");
changePrank(invalidAddress);
vm.expectRevert("Only callable by owner");
s_subscriptionAPI.withdraw(payable(OWNER), 1 ether);
s_subscriptionAPI.withdraw(payable(OWNER));
}

function testWithdrawNativeInvalidOwner() public {
address invalidAddress = makeAddr("invalidAddress");
changePrank(invalidAddress);
vm.expectRevert("Only callable by owner");
s_subscriptionAPI.withdrawNative(payable(OWNER), 1 ether);
s_subscriptionAPI.withdrawNative(payable(OWNER));
}

function testWithdrawNativeSufficientBalance() public {
Expand All @@ -405,7 +405,7 @@ contract VRFV2PlusSubscriptionAPITest is BaseTest {

// call WithdrawNative from owner address
changePrank(OWNER);
s_subscriptionAPI.withdrawNative(payable(OWNER), 1 ether);
s_subscriptionAPI.withdrawNative(payable(OWNER));
// assert native balance
assertEq(address(OWNER).balance, 1 ether, "owner native balance incorrect");
// assert state of subscription api
Expand Down

0 comments on commit 94b1d53

Please sign in to comment.