Skip to content

Commit

Permalink
Add native payment to RandomWordsFulfilled event (#12085)
Browse files Browse the repository at this point in the history
* Add native payment to RandomWordsFulfilled event

* Minor change

---------

Co-authored-by: Sri Kidambi <[email protected]>
  • Loading branch information
leeyikjiun and kidambisrinivas authored Feb 19, 2024
1 parent 1b5ab9a commit 6039298
Show file tree
Hide file tree
Showing 5 changed files with 31 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ vrf_consumer_v2_upgradeable_example: ../../contracts/solc/v0.8.6/VRFConsumerV2Up
vrf_coordinator_mock: ../../contracts/solc/v0.8.6/VRFCoordinatorMock/VRFCoordinatorMock.abi ../../contracts/solc/v0.8.6/VRFCoordinatorMock/VRFCoordinatorMock.bin 5c495cf8df1f46d8736b9150cdf174cce358cb8352f60f0d5bb9581e23920501
vrf_coordinator_test_v2: ../../contracts/solc/v0.8.6/VRFCoordinatorTestV2/VRFCoordinatorTestV2.abi ../../contracts/solc/v0.8.6/VRFCoordinatorTestV2/VRFCoordinatorTestV2.bin eaefd785c38bac67fb11a7fc2737ab2da68c988ca170e7db8ff235c80893e01c
vrf_coordinator_v2: ../../contracts/solc/v0.8.6/VRFCoordinatorV2/VRFCoordinatorV2.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2/VRFCoordinatorV2.bin 295f35ce282060317dfd01f45959f5a2b05ba26913e422fbd4fb6bf90b107006
vrf_coordinator_v2_5: ../../contracts/solc/v0.8.6/VRFCoordinatorV2_5/VRFCoordinatorV2_5.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2_5/VRFCoordinatorV2_5.bin aa5875f42461b4f128483ee0fd8b1f1b72a395ee857e6153197e92bcb21d149f
vrf_coordinator_v2_5: ../../contracts/solc/v0.8.6/VRFCoordinatorV2_5/VRFCoordinatorV2_5.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2_5/VRFCoordinatorV2_5.bin 3230631b7920236588ccd4ad8795e52ede6222cac7a70fa477c7240da0955a5e
vrf_coordinator_v2_plus_v2_example: ../../contracts/solc/v0.8.6/VRFCoordinatorV2Plus_V2Example/VRFCoordinatorV2Plus_V2Example.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2Plus_V2Example/VRFCoordinatorV2Plus_V2Example.bin 4a5b86701983b1b65f0a8dfa116b3f6d75f8f706fa274004b57bdf5992e4cec3
vrf_coordinator_v2plus_interface: ../../contracts/solc/v0.8.6/IVRFCoordinatorV2PlusInternal/IVRFCoordinatorV2PlusInternal.abi ../../contracts/solc/v0.8.6/IVRFCoordinatorV2PlusInternal/IVRFCoordinatorV2PlusInternal.bin 86b8e23aab28c5b98e3d2384dc4f702b093e382dc985c88101278e6e4bf6f7b8
vrf_external_sub_owner_example: ../../contracts/solc/v0.8.6/VRFExternalSubOwnerExample/VRFExternalSubOwnerExample.abi ../../contracts/solc/v0.8.6/VRFExternalSubOwnerExample/VRFExternalSubOwnerExample.bin 14f888eb313930b50233a6f01ea31eba0206b7f41a41f6311670da8bb8a26963
Expand Down
4 changes: 4 additions & 0 deletions core/services/vrf/v2/coordinator_v2x_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,10 @@ func (rwf *v2_5RandomWordsFulfilled) Raw() types.Log {
return rwf.event.Raw
}

func (rwf *v2_5RandomWordsFulfilled) NativePayment() bool {
return rwf.event.NativePayment
}

var (
_ SubscriptionCreatedIterator = (*v2SubscriptionCreatedIterator)(nil)
_ SubscriptionCreatedIterator = (*v2_5SubscriptionCreatedIterator)(nil)
Expand Down

0 comments on commit 6039298

Please sign in to comment.