Skip to content

Commit

Permalink
feat: call request-vrf-direct with estimated fee price
Browse files Browse the repository at this point in the history
  • Loading branch information
bayram98 committed Jul 13, 2023
1 parent c163c0b commit 660ce99
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 11 additions & 3 deletions scripts/get-estimated-service-fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ import dotenv from 'dotenv'

dotenv.config()

async function main() {
export async function estimateServiceFee() {
const { vrfCoordinator: coordinatorAddress } = await hre.getNamedAccounts()
const coordinator = await ethers.getContractAt(CoordinatorBase__factory.abi, coordinatorAddress)

const reqCount = 1
const numSubmission = 1
const callbackGasLimit = 500_000
const estimatedGas = await coordinator.estimateFee(reqCount, numSubmission, callbackGasLimit)
const amountKlay = ethers.utils.formatUnits(estimatedGas, 'ether')
const estimatedServiceFee = await coordinator.estimateFee(
reqCount,
numSubmission,
callbackGasLimit
)
const amountKlay = ethers.utils.formatUnits(estimatedServiceFee, 'ether')

console.log(`Estimated Price for 1 Request is: ${amountKlay} Klay`)
return amountKlay
}
async function main() {
await estimateServiceFee()
}

main().catch((error) => {
Expand Down
5 changes: 4 additions & 1 deletion scripts/request-vrf-direct.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ethers, getNamedAccounts } from 'hardhat'
import { getKeyHash } from './utils'
import { estimateServiceFee } from './get-estimated-service-fee'

async function main() {
const vrfConsumer = await ethers.getContract('VRFConsumer')
Expand All @@ -9,9 +10,11 @@ async function main() {
const numWords = 1

const { deployer } = await getNamedAccounts()
const estimatedServiceFee = await estimateServiceFee()

const txReceipt = await (
await vrfConsumer.requestRandomWordsDirect(keyHash, callbackGasLimit, numWords, deployer, {
value: ethers.utils.parseEther('2.5')
value: ethers.utils.parseEther(estimatedServiceFee.toString())
})
).wait()

Expand Down

0 comments on commit 660ce99

Please sign in to comment.