Skip to content

Commit

Permalink
(test): Amend Functions onTokenTransferTest to be able to fuzz with f…
Browse files Browse the repository at this point in the history
…ull LINK supply (#11234)
  • Loading branch information
justinkaseman authored Nov 8, 2023
1 parent 5d4961b commit b8caeda
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
10 changes: 5 additions & 5 deletions contracts/gas-snapshots/functions.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ FunctionsSubscriptions_GetSubscriptionsInRange:test_GetSubscriptionsInRange_Reve
FunctionsSubscriptions_GetSubscriptionsInRange:test_GetSubscriptionsInRange_RevertIfStartIsAfterEnd() (gas: 13459)
FunctionsSubscriptions_GetSubscriptionsInRange:test_GetSubscriptionsInRange_Success() (gas: 59592)
FunctionsSubscriptions_GetTotalBalance:test_GetTotalBalance_Success() (gas: 15010)
FunctionsSubscriptions_OnTokenTransfer:test_OnTokenTransfer_RevertIfCallerIsNoCalldata(uint96) (runs: 256, μ: 28446, ~: 28446)
FunctionsSubscriptions_OnTokenTransfer:test_OnTokenTransfer_RevertIfCallerIsNoSubscription(uint96) (runs: 256, μ: 30958, ~: 30958)
FunctionsSubscriptions_OnTokenTransfer:test_OnTokenTransfer_RevertIfCallerIsNotLink(uint96) (runs: 256, μ: 14293, ~: 14293)
FunctionsSubscriptions_OnTokenTransfer:test_OnTokenTransfer_RevertIfPaused(uint96) (runs: 256, μ: 35938, ~: 35938)
FunctionsSubscriptions_OnTokenTransfer:test_OnTokenTransfer_Success(uint96) (runs: 256, μ: 59686, ~: 59686)
FunctionsSubscriptions_OnTokenTransfer:test_OnTokenTransfer_RevertIfCallerIsNoCalldata(uint96) (runs: 256, μ: 43685, ~: 45548)
FunctionsSubscriptions_OnTokenTransfer:test_OnTokenTransfer_RevertIfCallerIsNoSubscription(uint96) (runs: 256, μ: 46197, ~: 48060)
FunctionsSubscriptions_OnTokenTransfer:test_OnTokenTransfer_RevertIfCallerIsNotLink(uint96) (runs: 256, μ: 14295, ~: 14295)
FunctionsSubscriptions_OnTokenTransfer:test_OnTokenTransfer_RevertIfPaused(uint96) (runs: 256, μ: 51089, ~: 53040)
FunctionsSubscriptions_OnTokenTransfer:test_OnTokenTransfer_Success(uint96) (runs: 256, μ: 86057, ~: 89604)
FunctionsSubscriptions_OracleWithdraw:test_OracleWithdraw_RevertIfAmountMoreThanBalance() (gas: 20745)
FunctionsSubscriptions_OracleWithdraw:test_OracleWithdraw_RevertIfBalanceInvariant() (gas: 189)
FunctionsSubscriptions_OracleWithdraw:test_OracleWithdraw_RevertIfNoAmount() (gas: 15638)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,22 @@ contract FunctionsSubscriptions_OwnerWithdraw is FunctionsFulfillmentSetup {
}

/// @notice #onTokenTransfer
contract FunctionsSubscriptions_OnTokenTransfer is FunctionsSubscriptionSetup {
contract FunctionsSubscriptions_OnTokenTransfer is FunctionsClientSetup {
uint64 s_subscriptionId;

function setUp() public virtual override {
FunctionsClientSetup.setUp();

// Create subscription, but do not fund it
s_subscriptionId = s_functionsRouter.createSubscription();
s_functionsRouter.addConsumer(s_subscriptionId, address(s_functionsClient));
}

function test_OnTokenTransfer_RevertIfPaused(uint96 fundingAmount) public {
// Funding amount must be less than LINK total supply
vm.assume(fundingAmount < 1_000_000_000 * 1e18);
vm.assume(fundingAmount > 0);
uint256 totalSupplyJuels = 1_000_000_000 * 1e18;
vm.assume(fundingAmount <= totalSupplyJuels);
vm.assume(fundingAmount >= 0);

s_functionsRouter.pause();
vm.expectRevert("Pausable: paused");
Expand All @@ -322,44 +333,45 @@ contract FunctionsSubscriptions_OnTokenTransfer is FunctionsSubscriptionSetup {

function test_OnTokenTransfer_RevertIfCallerIsNotLink(uint96 fundingAmount) public {
// Funding amount must be less than LINK total supply
vm.assume(fundingAmount < 1_000_000_000 * 1e18);
vm.assume(fundingAmount > 0);
uint256 totalSupplyJuels = 1_000_000_000 * 1e18;
vm.assume(fundingAmount <= totalSupplyJuels);
vm.assume(fundingAmount >= 0);

vm.expectRevert(FunctionsSubscriptions.OnlyCallableFromLink.selector);
s_functionsRouter.onTokenTransfer(address(s_functionsRouter), fundingAmount, abi.encode(s_subscriptionId));
}

function test_OnTokenTransfer_RevertIfCallerIsNoCalldata(uint96 fundingAmount) public {
// Funding amount must be less than LINK total supply
vm.assume(fundingAmount < 1_000_000_000 * 1e18);
vm.assume(fundingAmount > 0);
uint256 totalSupplyJuels = 1_000_000_000 * 1e18;
vm.assume(fundingAmount <= totalSupplyJuels);
vm.assume(fundingAmount >= 0);

vm.expectRevert(FunctionsSubscriptions.InvalidCalldata.selector);
s_linkToken.transferAndCall(address(s_functionsRouter), fundingAmount, new bytes(0));
}

function test_OnTokenTransfer_RevertIfCallerIsNoSubscription(uint96 fundingAmount) public {
// Funding amount must be less than LINK total supply
vm.assume(fundingAmount < 1_000_000_000 * 1e18);
vm.assume(fundingAmount > 0);
uint256 totalSupplyJuels = 1_000_000_000 * 1e18;
vm.assume(fundingAmount <= totalSupplyJuels);
vm.assume(fundingAmount >= 0);

vm.expectRevert(FunctionsSubscriptions.InvalidSubscription.selector);
uint64 invalidSubscriptionId = 123456789;
s_linkToken.transferAndCall(address(s_functionsRouter), fundingAmount, abi.encode(invalidSubscriptionId));
}

function test_OnTokenTransfer_Success(uint96 fundingAmount) public {
uint96 subscriptionBalanceBefore = s_functionsRouter.getSubscription(s_subscriptionId).balance;

// Funding amount must be less than LINK total supply
uint96 TOTAL_LINK = 1_000_000_000 * 1e18;
uint256 totalSupplyJuels = 1_000_000_000 * 1e18;
// Some of the total supply is already in the subscription account
vm.assume(fundingAmount < TOTAL_LINK - subscriptionBalanceBefore);
vm.assume(fundingAmount > 0);
vm.assume(fundingAmount <= totalSupplyJuels);
vm.assume(fundingAmount >= 0);

s_linkToken.transferAndCall(address(s_functionsRouter), fundingAmount, abi.encode(s_subscriptionId));
uint96 subscriptionBalanceAfter = s_functionsRouter.getSubscription(s_subscriptionId).balance;
assertEq(subscriptionBalanceBefore + fundingAmount, subscriptionBalanceAfter);
assertEq(fundingAmount, subscriptionBalanceAfter);
}
}

Expand Down

0 comments on commit b8caeda

Please sign in to comment.