Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added native token top-up and withdraw functionality in VRFv2PlusSubscriptionManager contract. #2123

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions public/samples/VRF/v2-5/SubscriptionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/V
*/

contract VRFv2PlusSubscriptionManager is VRFConsumerBaseV2Plus {

error InsufficientValue(uint256 required);
error NotEnoughBalance(uint256 available);

LinkTokenInterface LINKTOKEN;

// Sepolia coordinator. For other networks,
Expand Down Expand Up @@ -61,7 +65,7 @@ contract VRFv2PlusSubscriptionManager is VRFConsumerBaseV2Plus {
}

// Assumes the subscription is funded sufficiently.
function requestRandomWords() external onlyOwner {
function requestRandomWords(bool enableNativePayment) external onlyOwner {
// Will revert if subscription is not set and funded.
s_requestId = s_vrfCoordinator.requestRandomWords(
VRFV2PlusClient.RandomWordsRequest({
Expand All @@ -71,7 +75,7 @@ contract VRFv2PlusSubscriptionManager is VRFConsumerBaseV2Plus {
callbackGasLimit: callbackGasLimit,
numWords: numWords,
extraArgs: VRFV2PlusClient._argsToBytes(
VRFV2PlusClient.ExtraArgsV1({nativePayment: false})
VRFV2PlusClient.ExtraArgsV1({nativePayment: enableNativePayment})
)
})
);
Expand Down Expand Up @@ -101,6 +105,18 @@ contract VRFv2PlusSubscriptionManager is VRFConsumerBaseV2Plus {
);
}

// 1000000000000000000 = 1 ether / native token
function topUpSubscriptionWithNativeToken(uint256 amount) external payable onlyOwner
{
if (msg.value < amount) {
revert InsufficientValue(amount);
}

s_vrfCoordinator.fundSubscriptionWithNative{value: amount}(
s_subscriptionId
);
}

function addConsumer(address consumerAddress) external onlyOwner {
// Add a consumer contract to the subscription.
s_vrfCoordinator.addConsumer(s_subscriptionId, consumerAddress);
Expand All @@ -122,4 +138,13 @@ contract VRFv2PlusSubscriptionManager is VRFConsumerBaseV2Plus {
function withdraw(uint256 amount, address to) external onlyOwner {
LINKTOKEN.transfer(to, amount);
}

// Transfer this contract's native token balance to an address.
// 1000000000000000000 = 1 ether / native token
function withdrawNativeToken(uint256 amount, address payable to) external onlyOwner {
if (address(this).balance < amount) {
revert NotEnoughBalance(address(this).balance);
}
to.transfer(amount);
}
}
Loading