Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixFan1992 committed Mar 26, 2024
2 parents 036a256 + b3086d0 commit 4c81fb2
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 118 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-coats-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

fix withdraw LINK bug in auto 2.3
5 changes: 5 additions & 0 deletions .changeset/wicked-gorillas-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

VRFV2PlusWrapper config refactor
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ core/scripts/gateway @smartcontractkit/functions
/contracts/src/v0.8/l2ep @chris-de-leon-cll
/contracts/src/v0.8/llo-feeds @smartcontractkit/mercury-team
# TODO: mocks folder, folder should be removed and files moved to the correct folders
/contracts/src/v0.8/operatorforwarder @smartcontractkit/foundations
/contracts/src/v0.8/operatorforwarder @RensR
/contracts/src/v0.8/shared @RensR
# TODO: tests folder, folder should be removed and files moved to the correct folders
# TODO: transmission folder, owner should be found
Expand Down
5 changes: 5 additions & 0 deletions contracts/.changeset/eight-peas-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chainlink/contracts": minor
---

VRFV2PlusWrapper config refactor
5 changes: 5 additions & 0 deletions contracts/.changeset/famous-feet-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chainlink/contracts": patch
---

fix withdraw LINK bug in auto 2.3

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions contracts/src/v0.8/automation/dev/test/AutomationRegistry2_3.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ contract AddFunds is SetUp {

// it fails when the billing token is not native, but trying to pay with native
function test_RevertsWhen_NativePaymentDoesntMatchBillingToken() external {
vm.expectRevert(abi.encodeWithSelector(Registry.InvalidBillingToken.selector));
vm.expectRevert(abi.encodeWithSelector(Registry.InvalidToken.selector));
registry.addFunds{value: 1}(linkUpkeepID, 0);
}

Expand Down Expand Up @@ -251,6 +251,14 @@ contract Withdraw is SetUp {
registry.withdrawERC20Fees(address(usdToken), FINANCE_ADMIN, 1);
}

function test_WithdrawERC20Fees_RevertsWhenAttemptingToWithdrawLINK() public {
_mintLink(address(registry), 1e10);
vm.startPrank(FINANCE_ADMIN);
vm.expectRevert(Registry.InvalidToken.selector);
registry.withdrawERC20Fees(address(linkToken), FINANCE_ADMIN, 1); // should revert
registry.withdrawLink(FINANCE_ADMIN, 1); // but using link withdraw functions succeeds
}

function testWithdrawERC20FeeSuccess() public {
// deposit excess USDToken to the registry (this goes to the "finance withdrawable" pool be default)
uint256 startReserveAmount = registry.getReserveAmount(address(usdToken));
Expand Down Expand Up @@ -551,7 +559,7 @@ contract SetConfig is SetUp {
);
}

function testSetConfigRevertDueToInvalidBillingToken() public {
function testSetConfigRevertDueToInvalidToken() public {
address[] memory billingTokens = new address[](1);
billingTokens[0] = address(linkToken);

Expand All @@ -567,7 +575,7 @@ contract SetConfig is SetUp {
// deploy registry with OFF_CHAIN payout mode
registry = deployRegistry(AutoBase.PayoutMode.OFF_CHAIN);

vm.expectRevert(abi.encodeWithSelector(Registry.InvalidBillingToken.selector));
vm.expectRevert(abi.encodeWithSelector(Registry.InvalidToken.selector));
registry.setConfigTypeSafe(
SIGNERS,
TRANSMITTERS,
Expand Down Expand Up @@ -1161,7 +1169,7 @@ contract RegisterUpkeep is SetUp {
}

function test_RevertsWhen_TheBillingTokenIsNotConfigured() public {
vm.expectRevert(Registry.InvalidBillingToken.selector);
vm.expectRevert(Registry.InvalidToken.selector);
registry.registerUpkeep(
address(TARGET1),
config.maxPerformGas,
Expand Down Expand Up @@ -1240,7 +1248,7 @@ contract OnTokenTransfer is SetUp {

function test_RevertsWhen_TheUpkeepDoesNotUseLINKAsItsBillingToken() public {
vm.startPrank(address(linkToken));
vm.expectRevert(Registry.InvalidBillingToken.selector);
vm.expectRevert(Registry.InvalidToken.selector);
registry.onTokenTransfer(UPKEEP_ADMIN, 100, abi.encode(usdUpkeepID));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ contract AutomationRegistry2_3 is AutomationRegistryBase2_3, OCR2Abstract, Chain
if (data.length != 32) revert InvalidDataLength();
uint256 id = abi.decode(data, (uint256));
if (s_upkeep[id].maxValidBlocknumber != UINT32_MAX) revert UpkeepCancelled();
if (address(s_upkeep[id].billingToken) != address(i_link)) revert InvalidBillingToken();
if (address(s_upkeep[id].billingToken) != address(i_link)) revert InvalidToken();
s_upkeep[id].balance = s_upkeep[id].balance + uint96(amount);
s_reserveAmounts[IERC20(address(i_link))] = s_reserveAmounts[IERC20(address(i_link))] + amount;
emit FundsAdded(id, sender, uint96(amount));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {
error IncorrectNumberOfSigners();
error IndexOutOfRange();
error InsufficientBalance(uint256 available, uint256 requested);
error InvalidBillingToken();
error InvalidDataLength();
error InvalidFeed();
error InvalidTrigger();
error InvalidPayee();
error InvalidRecipient();
error InvalidReport();
error InvalidSigner();
error InvalidToken();
error InvalidTransmitter();
error InvalidTriggerType();
error MigrationNotPermitted();
Expand Down Expand Up @@ -545,7 +545,7 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {
if (upkeep.performGas < PERFORM_GAS_MIN || upkeep.performGas > s_storage.maxPerformGas)
revert GasLimitOutsideRange();
if (address(s_upkeep[id].forwarder) != address(0)) revert UpkeepAlreadyExists();
if (address(s_billingConfigs[upkeep.billingToken].priceFeed) == address(0)) revert InvalidBillingToken();
if (address(s_billingConfigs[upkeep.billingToken].priceFeed) == address(0)) revert InvalidToken();
s_upkeep[id] = upkeep;
s_upkeepAdmin[id] = admin;
s_checkData[id] = checkData;
Expand Down Expand Up @@ -1073,7 +1073,7 @@ abstract contract AutomationRegistryBase2_3 is ConfirmedOwner {

// if LINK is a billing option, payout mode must be ON_CHAIN
if (address(token) == address(i_link) && mode == PayoutMode.OFF_CHAIN) {
revert InvalidBillingToken();
revert InvalidToken();
}
if (address(token) == ZERO_ADDRESS || address(config.priceFeed) == ZERO_ADDRESS) {
revert ZeroAddressNotAllowed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ contract AutomationRegistryLogicB2_3 is AutomationRegistryBase2_3, Chainable {

if (msg.value != 0) {
if (upkeep.billingToken != IERC20(i_wrappedNativeToken)) {
revert InvalidBillingToken();
revert InvalidToken();
}
amount = SafeCast.toUint96(msg.value);
}
Expand Down Expand Up @@ -205,13 +205,18 @@ contract AutomationRegistryLogicB2_3 is AutomationRegistryBase2_3, Chainable {
}

/**
* @notice LINK available to withdraw by the finance team
* @notice returns the size of the LINK liquidity pool
# @dev LINK max supply < 2^96, so casting to int256 is safe
*/
function linkAvailableForPayment() public view returns (int256) {
return int256(i_link.balanceOf(address(this))) - int256(s_reserveAmounts[IERC20(address(i_link))]);
}

/**
* @notice withdraws excess LINK from the liquidity pool
* @param to the address to send the fees to
* @param amount the amount to withdraw
*/
function withdrawLink(address to, uint256 amount) external {
_onlyFinanceAdminAllowed();
if (to == ZERO_ADDRESS) revert InvalidRecipient();
Expand All @@ -230,9 +235,16 @@ contract AutomationRegistryLogicB2_3 is AutomationRegistryBase2_3, Chainable {
emit FeesWithdrawn(address(i_link), to, amount);
}

/**
* @notice withdraws non-LINK fees earned by the contract
* @param asset the asset to withdraw
* @param to the address to send the fees to
* @param amount the amount to withdraw
*/
function withdrawERC20Fees(IERC20 asset, address to, uint256 amount) external {
_onlyFinanceAdminAllowed();
if (to == ZERO_ADDRESS) revert InvalidRecipient();
if (address(asset) == address(i_link)) revert InvalidToken();
uint256 available = asset.balanceOf(address(this)) - s_reserveAmounts[asset];
if (amount > available) revert InsufficientBalance(available, amount);

Expand Down
51 changes: 27 additions & 24 deletions contracts/src/v0.8/vrf/dev/VRFV2PlusWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@ contract VRFV2PlusWrapper is ConfirmedOwner, TypeAndVersionInterface, VRFConsume
// charges for link payment.
uint32 private s_fulfillmentFlatFeeLinkDiscountPPM;

// s_wrapperNativePremiumPercentage is the premium ratio in percentage for native payment. For example, a value of 0
// indicates no premium. A value of 15 indicates a 15 percent premium.
uint8 private s_wrapperNativePremiumPercentage;
// s_coordinatorNativePremiumPercentage is the coordinator's premium ratio in percentage for native payment.
// For example, a value of 0 indicates no premium. A value of 15 indicates a 15 percent premium.
// Wrapper has no premium. This premium is for VRFCoordinator.
uint8 private s_coordinatorNativePremiumPercentage;

// s_wrapperLinkPremiumPercentage is the premium ratio in percentage for link payment. For example, a value of 0
// indicates no premium. A value of 15 indicates a 15 percent premium.
uint8 private s_wrapperLinkPremiumPercentage;
// s_coordinatorLinkPremiumPercentage is the premium ratio in percentage for link payment. For example, a
// value of 0 indicates no premium. A value of 15 indicates a 15 percent premium.
// Wrapper has no premium. This premium is for VRFCoordinator.
uint8 private s_coordinatorLinkPremiumPercentage;

// 10 bytes left
/* Storage Slot 6: END */
Expand Down Expand Up @@ -200,9 +202,9 @@ contract VRFV2PlusWrapper is ConfirmedOwner, TypeAndVersionInterface, VRFConsume
* @param _coordinatorGasOverhead reflects the gas overhead of the coordinator's
* fulfillRandomWords function.
*
* @param _wrapperNativePremiumPercentage is the premium ratio in percentage for wrapper requests paid in native.
* @param _coordinatorNativePremiumPercentage is the coordinator's premium ratio in percentage for requests paid in native.
*
* @param _wrapperLinkPremiumPercentage is the premium ratio in percentage for wrapper requests paid in link.
* @param _coordinatorLinkPremiumPercentage is the coordinator's premium ratio in percentage for requests paid in link.
*
* @param _keyHash to use for requesting randomness.
* @param _maxNumWords is the max number of words that can be requested in a single wrapped VRF request
Expand All @@ -221,8 +223,8 @@ contract VRFV2PlusWrapper is ConfirmedOwner, TypeAndVersionInterface, VRFConsume
function setConfig(
uint32 _wrapperGasOverhead,
uint32 _coordinatorGasOverhead,
uint8 _wrapperNativePremiumPercentage,
uint8 _wrapperLinkPremiumPercentage,
uint8 _coordinatorNativePremiumPercentage,
uint8 _coordinatorLinkPremiumPercentage,
bytes32 _keyHash,
uint8 _maxNumWords,
uint32 _stalenessSeconds,
Expand All @@ -233,17 +235,17 @@ contract VRFV2PlusWrapper is ConfirmedOwner, TypeAndVersionInterface, VRFConsume
if (_fulfillmentFlatFeeLinkDiscountPPM > _fulfillmentFlatFeeNativePPM) {
revert LinkDiscountTooHigh(_fulfillmentFlatFeeLinkDiscountPPM, _fulfillmentFlatFeeNativePPM);
}
if (_wrapperNativePremiumPercentage > PREMIUM_PERCENTAGE_MAX) {
revert InvalidPremiumPercentage(_wrapperNativePremiumPercentage, PREMIUM_PERCENTAGE_MAX);
if (_coordinatorNativePremiumPercentage > PREMIUM_PERCENTAGE_MAX) {
revert InvalidPremiumPercentage(_coordinatorNativePremiumPercentage, PREMIUM_PERCENTAGE_MAX);
}
if (_wrapperLinkPremiumPercentage > PREMIUM_PERCENTAGE_MAX) {
revert InvalidPremiumPercentage(_wrapperLinkPremiumPercentage, PREMIUM_PERCENTAGE_MAX);
if (_coordinatorLinkPremiumPercentage > PREMIUM_PERCENTAGE_MAX) {
revert InvalidPremiumPercentage(_coordinatorLinkPremiumPercentage, PREMIUM_PERCENTAGE_MAX);
}

s_wrapperGasOverhead = _wrapperGasOverhead;
s_coordinatorGasOverhead = _coordinatorGasOverhead;
s_wrapperNativePremiumPercentage = _wrapperNativePremiumPercentage;
s_wrapperLinkPremiumPercentage = _wrapperLinkPremiumPercentage;
s_coordinatorNativePremiumPercentage = _coordinatorNativePremiumPercentage;
s_coordinatorLinkPremiumPercentage = _coordinatorLinkPremiumPercentage;
s_keyHash = _keyHash;
s_maxNumWords = _maxNumWords;
s_configured = true;
Expand All @@ -257,8 +259,8 @@ contract VRFV2PlusWrapper is ConfirmedOwner, TypeAndVersionInterface, VRFConsume
emit ConfigSet(
_wrapperGasOverhead,
_coordinatorGasOverhead,
_wrapperNativePremiumPercentage,
_wrapperLinkPremiumPercentage,
_coordinatorNativePremiumPercentage,
_coordinatorLinkPremiumPercentage,
_keyHash,
_maxNumWords,
_stalenessSeconds,
Expand Down Expand Up @@ -324,8 +326,8 @@ contract VRFV2PlusWrapper is ConfirmedOwner, TypeAndVersionInterface, VRFConsume
s_fulfillmentFlatFeeLinkDiscountPPM,
s_wrapperGasOverhead,
s_coordinatorGasOverhead,
s_wrapperNativePremiumPercentage,
s_wrapperLinkPremiumPercentage,
s_coordinatorNativePremiumPercentage,
s_coordinatorLinkPremiumPercentage,
s_keyHash,
s_maxNumWords
);
Expand Down Expand Up @@ -390,8 +392,8 @@ contract VRFV2PlusWrapper is ConfirmedOwner, TypeAndVersionInterface, VRFConsume

// coordinatorCostWithPremiumAndFlatFeeWei is the coordinator cost with the percentage premium and flat fee applied
// coordinator cost * premium multiplier + flat fee
uint256 coordinatorCostWithPremiumAndFlatFeeWei = ((coordinatorCostWei * (s_wrapperNativePremiumPercentage + 100)) /
100) + (1e12 * uint256(s_fulfillmentFlatFeeNativePPM));
uint256 coordinatorCostWithPremiumAndFlatFeeWei = ((coordinatorCostWei *
(s_coordinatorNativePremiumPercentage + 100)) / 100) + (1e12 * uint256(s_fulfillmentFlatFeeNativePPM));

return wrapperCostWei + coordinatorCostWithPremiumAndFlatFeeWei;
}
Expand All @@ -413,8 +415,9 @@ contract VRFV2PlusWrapper is ConfirmedOwner, TypeAndVersionInterface, VRFConsume

// coordinatorCostWithPremiumAndFlatFeeWei is the coordinator cost with the percentage premium and flat fee applied
// coordinator cost * premium multiplier + flat fee
uint256 coordinatorCostWithPremiumAndFlatFeeWei = ((coordinatorCostWei * (s_wrapperLinkPremiumPercentage + 100)) /
100) + (1e12 * uint256(s_fulfillmentFlatFeeNativePPM - s_fulfillmentFlatFeeLinkDiscountPPM));
uint256 coordinatorCostWithPremiumAndFlatFeeWei = ((coordinatorCostWei *
(s_coordinatorLinkPremiumPercentage + 100)) / 100) +
(1e12 * uint256(s_fulfillmentFlatFeeNativePPM - s_fulfillmentFlatFeeLinkDiscountPPM));

// requestPrice is denominated in juels (link)
// (1e18 juels/link) * wei / (wei/link) = juels
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/v0.8/vrf/dev/interfaces/IVRFV2PlusWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ interface IVRFV2PlusWrapper {
event ConfigSet(
uint32 wrapperGasOverhead,
uint32 coordinatorGasOverhead,
uint8 wrapperNativePremiumPercentage,
uint8 wrapperLinkPremiumPercentage,
uint8 coordinatorNativePremiumPercentage,
uint8 coordinatorLinkPremiumPercentage,
bytes32 keyHash,
uint8 maxNumWords,
uint32 stalenessSeconds,
Expand Down
Loading

0 comments on commit 4c81fb2

Please sign in to comment.