Skip to content

Commit

Permalink
Update VRFV2PlusWrapper billing parameters to mirror VRFCoordinatorV2…
Browse files Browse the repository at this point in the history
…_5 (#12407)

* Update VRFV2PlusWrapper billing parameters to mirror VRFCoordinatorV2_5

* Fix integration tests

* Fix Go scripts

* Fix calculate request price native using link premium percentage instead of native

* Update Go solidity wrappers

* vrfv2pluswrapper: optimize gas (#12428)

* Fix storage slot comment
  • Loading branch information
leeyikjiun authored and kidambisrinivas committed Mar 27, 2024
1 parent d7b9e9d commit 0911184
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 170 deletions.
231 changes: 135 additions & 96 deletions contracts/src/v0.8/vrf/dev/VRFV2PlusWrapper.sol

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions contracts/src/v0.8/vrf/dev/interfaces/IVRFV2PlusWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ interface IVRFV2PlusWrapper {
event ConfigSet(
uint32 wrapperGasOverhead,
uint32 coordinatorGasOverhead,
uint8 wrapperPremiumPercentage,
uint8 wrapperNativePremiumPercentage,
uint8 wrapperLinkPremiumPercentage,
bytes32 keyHash,
uint8 maxNumWords,
uint32 stalenessSeconds,
int256 fallbackWeiPerUnitLink,
uint32 fulfillmentFlatFeeLinkPPM,
uint32 fulfillmentFlatFeeNativePPM
uint32 fulfillmentFlatFeeNativePPM,
uint32 fulfillmentFlatFeeLinkDiscountPPM
);
event FallbackWeiPerUnitLinkUsed(uint256 requestId, int256 fallbackWeiPerUnitLink);
event Withdrawn(address indexed to, uint256 amount);
Expand Down
93 changes: 84 additions & 9 deletions contracts/test/v0.8/foundry/vrf/VRFV2PlusWrapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ contract VRFV2PlusWrapperTest is BaseTest {

function setConfigWrapper() internal {
vm.expectEmit(false, false, false, true, address(s_wrapper));
emit ConfigSet(wrapperGasOverhead, coordinatorGasOverhead, 0, vrfKeyHash, 10, 1, 50000000000000000, 0, 0);
emit ConfigSet(wrapperGasOverhead, coordinatorGasOverhead, 0, 0, vrfKeyHash, 10, 1, 50000000000000000, 0, 0);
s_wrapper.setConfig(
wrapperGasOverhead, // wrapper gas overhead
coordinatorGasOverhead, // coordinator gas overhead
0, // premium percentage
0, // native premium percentage,
0, // link premium percentage
vrfKeyHash, // keyHash
10, // max number of words,
1, // stalenessSeconds
50000000000000000, // fallbackWeiPerUnitLink
0, // fulfillmentFlatFeeLinkPPM
0 // fulfillmentFlatFeeNativePPM
0, // fulfillmentFlatFeeNativePPM
0 // fulfillmentFlatFeeLinkDiscountPPM
);
(
,
Expand All @@ -85,13 +86,15 @@ contract VRFV2PlusWrapperTest is BaseTest {
,
uint32 _wrapperGasOverhead,
uint32 _coordinatorGasOverhead,
uint8 _wrapperPremiumPercentage,
uint8 _wrapperNativePremiumPercentage,
uint8 _wrapperLinkPremiumPercentage,
bytes32 _keyHash,
uint8 _maxNumWords
) = s_wrapper.getConfig();
assertEq(_wrapperGasOverhead, wrapperGasOverhead);
assertEq(_coordinatorGasOverhead, coordinatorGasOverhead);
assertEq(0, _wrapperPremiumPercentage);
assertEq(0, _wrapperNativePremiumPercentage);
assertEq(0, _wrapperLinkPremiumPercentage);
assertEq(vrfKeyHash, _keyHash);
assertEq(10, _maxNumWords);
}
Expand All @@ -114,13 +117,14 @@ contract VRFV2PlusWrapperTest is BaseTest {
event ConfigSet(
uint32 wrapperGasOverhead,
uint32 coordinatorGasOverhead,
uint8 wrapperPremiumPercentage,
uint8 wrapperNativePremiumPercentage,
uint8 wrapperLinkPremiumPercentage,
bytes32 keyHash,
uint8 maxNumWords,
uint32 stalenessSeconds,
int256 fallbackWeiPerUnitLink,
uint32 fulfillmentFlatFeeLinkPPM,
uint32 fulfillmentFlatFeeNativePPM
uint32 fulfillmentFlatFeeNativePPM,
uint32 fulfillmentFlatFeeLinkDiscountPPM
);
event FallbackWeiPerUnitLinkUsed(uint256 requestId, int256 fallbackWeiPerUnitLink);
event Withdrawn(address indexed to, uint256 amount);
Expand Down Expand Up @@ -232,6 +236,77 @@ contract VRFV2PlusWrapperTest is BaseTest {
assertEq(address(s_wrapper).balance, 0);
}

function testSetConfig_FulfillmentFlatFee_LinkDiscountTooHigh() public {
// Test that setting link discount flat fee higher than native flat fee reverts
vm.expectRevert(abi.encodeWithSelector(VRFV2PlusWrapper.LinkDiscountTooHigh.selector, uint32(501), uint32(500)));
s_wrapper.setConfig(
wrapperGasOverhead, // wrapper gas overhead
coordinatorGasOverhead, // coordinator gas overhead
0, // native premium percentage,
0, // link premium percentage
vrfKeyHash, // keyHash
10, // max number of words,
1, // stalenessSeconds
50000000000000000, // fallbackWeiPerUnitLink
500, // fulfillmentFlatFeeNativePPM
501 // fulfillmentFlatFeeLinkDiscountPPM
);
}

function testSetConfig_FulfillmentFlatFee_LinkDiscountEqualsNative() public {
// Test that setting link discount flat fee equal to native flat fee does not revert
s_wrapper.setConfig(
wrapperGasOverhead, // wrapper gas overhead
coordinatorGasOverhead, // coordinator gas overhead
0, // native premium percentage,
0, // link premium percentage
vrfKeyHash, // keyHash
10, // max number of words,
1, // stalenessSeconds
50000000000000000, // fallbackWeiPerUnitLink
450, // fulfillmentFlatFeeNativePPM
450 // fulfillmentFlatFeeLinkDiscountPPM
);
}

function testSetConfig_NativePremiumPercentage_InvalidPremiumPercentage() public {
// Test that setting native premium percentage higher than 155 will revert
vm.expectRevert(
abi.encodeWithSelector(VRFCoordinatorV2_5.InvalidPremiumPercentage.selector, uint8(156), uint8(155))
);
s_wrapper.setConfig(
wrapperGasOverhead, // wrapper gas overhead
coordinatorGasOverhead, // coordinator gas overhead
156, // native premium percentage,
0, // link premium percentage
vrfKeyHash, // keyHash
10, // max number of words,
1, // stalenessSeconds
50000000000000000, // fallbackWeiPerUnitLink
0, // fulfillmentFlatFeeNativePPM
0 // fulfillmentFlatFeeLinkDiscountPPM
);
}

function testSetConfig_LinkPremiumPercentage_InvalidPremiumPercentage() public {
// Test that setting LINK premium percentage higher than 155 will revert
vm.expectRevert(
abi.encodeWithSelector(VRFCoordinatorV2_5.InvalidPremiumPercentage.selector, uint8(202), uint8(155))
);
s_wrapper.setConfig(
wrapperGasOverhead, // wrapper gas overhead
coordinatorGasOverhead, // coordinator gas overhead
15, // native premium percentage,
202, // link premium percentage
vrfKeyHash, // keyHash
10, // max number of words,
1, // stalenessSeconds
50000000000000000, // fallbackWeiPerUnitLink
0, // fulfillmentFlatFeeNativePPM
0 // fulfillmentFlatFeeLinkDiscountPPM
);
}

function testRequestAndFulfillRandomWordsLINKWrapper() public {
// Fund subscription.
s_linkToken.transferAndCall(address(s_testCoordinator), 10 ether, abi.encode(s_wrapper.SUBSCRIPTION_ID()));
Expand Down
13 changes: 8 additions & 5 deletions contracts/test/v0.8/foundry/vrf/VRFV2PlusWrapper_Migration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ contract VRFV2PlusWrapper_MigrationTest is BaseTest {
s_wrapper.setConfig(
wrapperGasOverhead, // wrapper gas overhead
coordinatorGasOverhead, // coordinator gas overhead
0, // premium percentage
0, // native premium percentage,
0, // link premium percentage
vrfKeyHash, // keyHash
10, // max number of words,
1, // stalenessSeconds
50000000000000000, // fallbackWeiPerUnitLink
0, // fulfillmentFlatFeeLinkPPM
0 // fulfillmentFlatFeeNativePPM
0, // fulfillmentFlatFeeNativePPM
0 // fulfillmentFlatFeeLinkDiscountPPM
);
(
,
Expand All @@ -104,13 +105,15 @@ contract VRFV2PlusWrapper_MigrationTest is BaseTest {
,
uint32 _wrapperGasOverhead,
uint32 _coordinatorGasOverhead,
uint8 _wrapperPremiumPercentage,
uint8 _wrapperNativePremiumPercentage,
uint8 _wrapperLinkPremiumPercentage,
bytes32 _keyHash,
uint8 _maxNumWords
) = s_wrapper.getConfig();
assertEq(_wrapperGasOverhead, wrapperGasOverhead);
assertEq(_coordinatorGasOverhead, coordinatorGasOverhead);
assertEq(0, _wrapperPremiumPercentage);
assertEq(0, _wrapperNativePremiumPercentage);
assertEq(0, _wrapperLinkPremiumPercentage);
assertEq(vrfKeyHash, _keyHash);
assertEq(10, _maxNumWords);
}
Expand Down
Loading

0 comments on commit 0911184

Please sign in to comment.