Skip to content

Commit

Permalink
Add native payment to RandomWordsFulfilled event
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyikjiun committed Feb 19, 2024
1 parent 3fd52ff commit 2180952
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
26 changes: 11 additions & 15 deletions contracts/src/v0.8/vrf/dev/VRFCoordinatorV2_5.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ contract VRFCoordinatorV2_5 is VRF, SubscriptionAPI, IVRFCoordinatorV2Plus {
uint256 outputSeed,
uint256 indexed subId,
uint96 payment,
bool nativePayment,
bool success,
bool onlyPremium
);
Expand Down Expand Up @@ -457,28 +458,23 @@ contract VRFCoordinatorV2_5 is VRF, SubscriptionAPI, IVRFCoordinatorV2Plus {
}
}

uint256 requestId = output.requestId;
delete s_requestCommitments[requestId];
bool success = _deliverRandomness(requestId, rc, randomWords);
delete s_requestCommitments[output.requestId];
bool success = _deliverRandomness(output.requestId, rc, randomWords);

// Increment the req count for the subscription.
uint256 subId = rc.subId;
++s_subscriptions[subId].reqCount;
++s_subscriptions[rc.subId].reqCount;

// stack too deep error
{
bool nativePayment = uint8(rc.extraArgs[rc.extraArgs.length - 1]) == 1;
bool nativePayment = uint8(rc.extraArgs[rc.extraArgs.length - 1]) == 1;

// We want to charge users exactly for how much gas they use in their callback.
// The gasAfterPaymentCalculation is meant to cover these additional operations where we
// decrement the subscription balance and increment the oracles withdrawable balance.
payment = _calculatePaymentAmount(startGas, gasPrice, nativePayment, onlyPremium);
// We want to charge users exactly for how much gas they use in their callback.
// The gasAfterPaymentCalculation is meant to cover these additional operations where we
// decrement the subscription balance and increment the oracles withdrawable balance.
payment = _calculatePaymentAmount(startGas, gasPrice, nativePayment, onlyPremium);

_chargePayment(payment, nativePayment, subId);
}
_chargePayment(payment, nativePayment, rc.subId);

// Include payment in the event for tracking costs.
emit RandomWordsFulfilled(requestId, randomness, subId, payment, success, onlyPremium);
emit RandomWordsFulfilled(output.requestId, randomness, rc.subId, payment, nativePayment, success, onlyPremium);

return payment;
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/test/v0.8/foundry/vrf/VRFV2Plus.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ contract VRFV2Plus is BaseTest {
VmSafe.Log[] memory entries = vm.getRecordedLogs();
assertEq(entries[0].topics[1], bytes32(uint256(requestId)));
assertEq(entries[0].topics[2], bytes32(uint256(subId)));
(uint256 loggedOutputSeed, , bool loggedSuccess) = abi.decode(entries[0].data, (uint256, uint256, bool));
(uint256 loggedOutputSeed, , , bool loggedSuccess) = abi.decode(entries[0].data, (uint256, uint256, bool, bool));
assertEq(loggedOutputSeed, outputSeed);
assertEq(loggedSuccess, true);

Expand Down Expand Up @@ -374,7 +374,7 @@ contract VRFV2Plus is BaseTest {
VmSafe.Log[] memory entries = vm.getRecordedLogs();
assertEq(entries[0].topics[1], bytes32(uint256(requestId)));
assertEq(entries[0].topics[2], bytes32(uint256(subId)));
(uint256 loggedOutputSeed, , bool loggedSuccess) = abi.decode(entries[0].data, (uint256, uint256, bool));
(uint256 loggedOutputSeed, , , bool loggedSuccess) = abi.decode(entries[0].data, (uint256, uint256, bool, bool));
assertEq(loggedOutputSeed, outputSeed);
assertEq(loggedSuccess, true);

Expand Down Expand Up @@ -624,7 +624,7 @@ contract VRFV2Plus is BaseTest {
VmSafe.Log[] memory entries = vm.getRecordedLogs();
assertEq(entries[0].topics[1], bytes32(uint256(requestId)));
assertEq(entries[0].topics[2], bytes32(uint256(subId)));
(uint256 loggedOutputSeed, , bool loggedSuccess) = abi.decode(entries[0].data, (uint256, uint256, bool));
(uint256 loggedOutputSeed, , , bool loggedSuccess) = abi.decode(entries[0].data, (uint256, uint256, bool, bool));
assertEq(loggedOutputSeed, outputSeed);
assertEq(loggedSuccess, true);

Expand Down Expand Up @@ -671,7 +671,7 @@ contract VRFV2Plus is BaseTest {
VmSafe.Log[] memory entries = vm.getRecordedLogs();
assertEq(entries[0].topics[1], bytes32(uint256(requestId)));
assertEq(entries[0].topics[2], bytes32(uint256(subId)));
(uint256 loggedOutputSeed, , bool loggedSuccess) = abi.decode(entries[0].data, (uint256, uint256, bool));
(uint256 loggedOutputSeed, , , bool loggedSuccess) = abi.decode(entries[0].data, (uint256, uint256, bool, bool));
assertEq(loggedOutputSeed, outputSeed);
assertEq(loggedSuccess, true);

Expand Down
Loading

0 comments on commit 2180952

Please sign in to comment.