Skip to content

Commit

Permalink
add fulfill-vrf task
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincharm committed Apr 16, 2024
1 parent af6318e commit 118faa6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"hardhat:bid-random": "hardhat --network localhost bid-random",
"hardhat:settle-auction": "hardhat --network localhost settle-auction",
"hardhat:settle-raffle": "hardhat --network localhost settle-raffle",
"hardhat:settle": "pnpm node:increase-time && pnpm hardhat:settle-auction && pnpm hardhat:settle-raffle",
"hardhat:fulfill-vrf": "hardhat --network localhost fulfill-vrf",
"hardhat:settle": "pnpm node:increase-time && pnpm hardhat:settle-auction && pnpm hardhat:settle-raffle && pnpm hardhat:fulfill-vrf",
"rinkeby:generate-dotenv": "hardhat generate-dotenv",
"rinkeby:transfer-ether": "hardhat --network rinkeby transfer-ether",
"rinkeby:init-bids": "hardhat --network rinkeby init-bids",
Expand Down
5 changes: 3 additions & 2 deletions packages/contracts/scripts/node/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { bidAsSigner } from 'scripts/utils/bid'
import * as hre from 'hardhat'
import { parseEther, parseUnits } from 'ethers/lib/utils'
import { Contract, Wallet, utils } from 'ethers'
import { utils } from 'ethers'
import { deployAuctionRaffle } from '../deployAuctionRaffle'
import {
AuctionRaffle,
Expand All @@ -25,7 +25,7 @@ async function run() {
const deployer = signers[0]

// Mock VRF
const { vrfRequesterParams } = await setupMockVrf(deployer)
const { vrfCoordinator, vrfRequesterParams } = await setupMockVrf(deployer)

const now = Math.floor(new Date().valueOf() / SECOND)
await hre.network.provider.send('evm_setNextBlockTimestamp', [now])
Expand Down Expand Up @@ -53,6 +53,7 @@ async function run() {
},
vrfConfig: vrfRequesterParams,
})
await vrfCoordinator.addConsumer(vrfRequesterParams.subId, auctionRaffle.address)
console.log('Contracts deployed\n')

await bid(auctionRaffle, signers.slice(0, 20), deployer)
Expand Down
21 changes: 14 additions & 7 deletions packages/contracts/scripts/tasks/auctionRaffle/hardhatTasks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { task, types } from 'hardhat/config'
import { connectToAuctionRaffle } from 'scripts/utils/auctionRaffle'
import { BigNumber, BigNumberish, constants, Contract, utils } from 'ethers'
import { BigNumberish, constants, Contract, utils } from 'ethers'
import { parseEther } from 'ethers/lib/utils'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { randomBigNumbers } from 'scripts/utils/random'
import { randomBN } from 'scripts/utils/random'
import { generateRandomAccounts } from 'scripts/utils/generateRandomAccounts'
import { fundAccounts } from 'scripts/utils/fundAccounts'
import { bidAsSigner } from 'scripts/utils/bid'
import { initialRequiredScore, minBidIncrement, reservePrice } from 'scripts/node/config'
import { connectToScoreAttestationVerifier } from 'scripts/utils/scoreAttestationVerifier'
import { connectToMockVrfCoordinator } from 'scripts/utils/mockVrfCoordinator'

const mockVrfCoordinatorAddress = '0xcf7ed3acca5a467e9e704c703e8d87f634fb0fc9'
const scoreAttestationVerifierAddress = '0x0165878a594ca255338adfa4d48449f69242eb8f'
const auctionRaffleAddress = '0xa513e6e4b8f2a923d98304ec87f64353c4d5c853'

Expand All @@ -20,7 +22,6 @@ task('bid', 'Places bid for given account with provided amount')
.setAction(async ({ account, amount }: { account: string; amount: string }, hre) => {
const signer = await hre.ethers.getSigner(account)
const auctionRaffle = await connectToAuctionRaffle(hre, auctionRaffleAddress)
const auctionRaffleAsSigner = auctionRaffle.connect(signer)
const [attestor] = await hre.ethers.getSigners()
const scoreAttestationVerifier = await connectToScoreAttestationVerifier(hre, scoreAttestationVerifierAddress)

Expand Down Expand Up @@ -68,13 +69,19 @@ task('settle-auction', 'Settles auction').setAction(async (taskArgs, hre) => {
task('settle-raffle', 'Settles raffle').setAction(async (taskArgs, hre) => {
const auctionRaffle = await auctionRaffleAsOwner(hre)

const raffleWinnersCount = await auctionRaffle.raffleWinnersCount()
const randomNumbersCount = BigNumber.from(raffleWinnersCount).div(8).toNumber()

await auctionRaffle.settleRaffle(randomBigNumbers(randomNumbersCount))
await auctionRaffle.settleRaffle()
console.log('Raffle settled!')
})

task('fulfill-vrf', 'Fulfill VRF request').setAction(async (taskArgs, hre) => {
const auctionRaffle = await auctionRaffleAsOwner(hre)
const requestId = await auctionRaffle.requestId()
const mockVrfCoordinator = await connectToMockVrfCoordinator(hre, mockVrfCoordinatorAddress)
const randomWords = [randomBN()]
await mockVrfCoordinator.fulfillRandomWords(requestId, auctionRaffle.address, randomWords)
console.log(`Fulfilled VRF request with: ${randomWords}`)
})

function logBid(address: string, bidAmount: BigNumberish) {
console.log(`Account ${address} bid ${formatEther(bidAmount)}`)
}
Expand Down
9 changes: 9 additions & 0 deletions packages/contracts/scripts/utils/mockVrfCoordinator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { HardhatRuntimeEnvironment } from 'hardhat/types'

export const mockVrfCoordinatorArtifactName =
'contracts/mocks/VRFCoordinatorV2MockWithERC677.sol:VRFCoordinatorV2MockWithERC677'

export async function connectToMockVrfCoordinator(hre: HardhatRuntimeEnvironment, mockVrfCoordinator: string) {
const mockVrfCoordinatorFactory = await hre.ethers.getContractFactory(mockVrfCoordinatorArtifactName)
return mockVrfCoordinatorFactory.attach(mockVrfCoordinator)
}

0 comments on commit 118faa6

Please sign in to comment.