Direct Funding Method
This guide explains how to generate random numbers using the Direct funding method. This method doesn’t require a subscription and is optimal for one-off requests for randomness. This method also works best for applications where your end-users must pay the fees for VRF because the cost of the request is determined at request time.
Unlike the subscription method, the Direct funding method does not require you to create subscriptions and pre-fund them. Instead, you must directly fund consuming contracts with WKCS tokens before they request randomness.
For KCC VRF v2 to fulfill your requests, you must have a sufficient amount of WKCS in your consuming contract. Gas cost calculation includes the following variables:
- Gas price: The current gas price, which fluctuates depending on network conditions.
- Callback gas: The amount of gas used for the callback request that returns your requested random values.
- Verification gas: The amount of gas used to verify randomness on-chain.
- Wrapper overhead gas: The amount of gas used by the VRF Wrapper contract.
The gas price depends on current network conditions. The callback gas depends on your callback function and the number of random values in your request. You define the limits that you are willing to spend for the request with the following variable:
- Callback gas limit: Specifies the maximum amount of gas you are willing to spend on the callback request. Define this limit by specifying the
callbackGasLimitvalue in your request
The VRF wrapper calls the coordinator to process the request using the following steps:
- 1.The consuming contract must inherit VRFV2WrapperConsumerBase and implement the
fulfillRandomWordsfunction, which is the callback VRF function. Submit your VRF request by calling the
requestRandomnessfunction in the VRFV2WrapperConsumerBase contract. Include the following parameters in your request:
requestConfirmations: The number of block confirmations the VRF service will wait to respond. The minimum and maximum confirmations for your network can be found here.
callbackGasLimit: The maximum amount of gas to pay for completing the callback VRF function.
numWords: The number of random numbers to request. You can find the maximum number of random values per request for your network in the Supported networks page.
- 2.The consuming contract calls the VRFV2Wrapper
calculateRequestPricefunction to estimate the total transaction cost to fulfill randomness. If
paymentEnabledis true, then the consuming contract calls the WKCSToken
transferFromfunction to pay the wrapper with the calculated request price. Finally, VRFV2Wrapper will call VRF Coordinator
requestRandomWordsfunction to request randomness. The final gas cost to fulfill randomness is estimated based on how much gas is expected for the verification and callback. The total gas cost in wei uses the following formula:(Gas price * (Verification gas + Callback gas limit + Wrapper gas Overhead)) = total gas costA WKCS premium is then added to the total gas cost. The premium is divided in two parts:
((total gas cost * Wrapper premium) + Coordinator premium) = total request cost
- Wrapper premium: The premium percentage. You can find the percentage for your network in the Supported networks page.
- Coordinator premium: A flat fee. This premium is defined in the
fulfillmentFlatFeePPMparameter in millionths of WKCS. You can find the flat fee of the coordinator for your network in the Supported networks page.
- 3.The VRF coordinator emits an event.
- 4.The event is picked up by the VRF service and waits for the specified number of block confirmations to respond back to the VRF coordinator with the random values and a proof (
- 5.The VRF coordinator verifies the proof on-chain. Then, it calls back the wrapper contract
- 6.Finally, the VRF Wrapper calls back your consuming contract.
- Each wrapper has a
maxNumWordsparameter that limits the maximum number of random values you can receive in each request.
NOTE ON MAXIMUM GAS LIMITThe maximum allowed
callbackGasLimitvalue for your requests is defined in the Coordinator contract supported networks page. Because the VRF v2 Wrapper adds an overhead, your
callbackGasLimitmust not exceed
maxGasLimit - wrapperGasOverhead.