11import { HardhatRuntimeEnvironment } from "hardhat/types" ;
22import { BigNumber } from "ethers" ;
33import { DeployFunction } from "hardhat-deploy/types" ;
4- import { SortitionModule , RandomizerRNG , VRFSubscriptionManagerV2Mock } from "../typechain-types" ;
4+ import {
5+ SortitionModule ,
6+ RandomizerRNG ,
7+ VRFSubscriptionManagerV2Mock ,
8+ VRFSubscriptionManagerV2 ,
9+ } from "../typechain-types" ;
510
611enum HomeChains {
712 ARBITRUM_ONE = 42161 ,
@@ -14,22 +19,26 @@ const pnkByChain = new Map<HomeChains, string>([
1419 [ HomeChains . ARBITRUM_GOERLI , "0x4DEeeFD054434bf6721eF39Aa18EfB3fd0D12610" ] ,
1520] ) ;
1621
22+ // https://randomizer.ai/docs#addresses
1723const randomizerByChain = new Map < HomeChains , string > ( [
1824 [ HomeChains . ARBITRUM_ONE , "0x00" ] ,
1925 [ HomeChains . ARBITRUM_GOERLI , "0x57F7a8aA8291A04B325F3f0d2c4d03353d3Ef25f" ] ,
2026] ) ;
2127
28+ // https://docs.chain.link/resources/link-token-contracts?parent=vrf#arbitrum
2229const linkByChain = new Map < HomeChains , string > ( [
2330 [ HomeChains . ARBITRUM_ONE , "0xf97f4df75117a78c1A5a0DBb814Af92458539FB4" ] ,
2431 [ HomeChains . ARBITRUM_GOERLI , "0xd14838A68E8AFBAdE5efb411d5871ea0011AFd28" ] ,
2532] ) ;
2633
34+ // https://docs.chain.link/vrf/v2/subscription/supported-networks#arbitrum-mainnet
2735const keyHashByChain = new Map < HomeChains , string > ( [
2836 [ HomeChains . ARBITRUM_ONE , "0x72d2b016bb5b62912afea355ebf33b91319f828738b111b723b78696b9847b63" ] , // 30 gwei key Hash
2937 [ HomeChains . ARBITRUM_GOERLI , "0x83d1b6e3388bed3d76426974512bb0d270e9542a765cd667242ea26c0cc0b730" ] ,
30- [ HomeChains . HARDHAT , "0x83d1b6e3388bed3d76426974512bb0d270e9542a765cd667242ea26c0cc0b730 " ] ,
38+ [ HomeChains . HARDHAT , "0x0000000000000000000000000000000000000000000000000000000000000000 " ] ,
3139] ) ;
3240
41+ // https://docs.chain.link/vrf/v2/subscription/supported-networks#arbitrum-mainnet
3342const vrfCoordinatorByChain = new Map < HomeChains , string > ( [
3443 [ HomeChains . ARBITRUM_ONE , "0x41034678D6C633D8a95c75e1138A360a28bA15d1" ] ,
3544 [ HomeChains . ARBITRUM_GOERLI , "0x6D80646bEAdd07cE68cab36c27c626790bBcf17f" ] ,
@@ -88,13 +97,14 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
8897 const sortitionModule = ( await hre . ethers . getContract ( "SortitionModule" ) ) as SortitionModule ;
8998 await sortitionModule . changeRandomNumberGenerator ( rng . address , RNG_LOOKAHEAD ) ;
9099
91- const link = linkByChain . get ( Number ( await getChainId ( ) ) ) ?? AddressZero ;
100+ const link = linkByChain . get ( Number ( await getChainId ( ) ) ) ?? AddressZero ; // LINK not needed on hardhat local node
92101 const keyHash = keyHashByChain . get ( Number ( await getChainId ( ) ) ) ?? AddressZero ;
93- const requestConfirmations = 3 ;
94- const callbackGasLimit = 100000 ;
102+ const requestConfirmations = 3 ; // Paramater to be fixed, range [1 ; 200] on Arbitrum
103+ const callbackGasLimit = 100000 ; // Parameter to be fixed, 50000 on RandomizerRNG but no external call to sortitionModule.passPhase() in the callback
95104 const numWords = 1 ;
96105 const vrfCoordinator = vrfCoordinatorByChain . get ( Number ( await getChainId ( ) ) ) ?? AddressZero ;
97- const vrfSubscriptionManagerDeploy = vrfCoordinator
106+ // Deploy the VRF Subscription Manager contract on Arbitrum, a mock contract on Hardhat node or nothing on other networks.
107+ const vrfSubscriptionManager = vrfCoordinator
98108 ? chainId === HomeChains . HARDHAT
99109 ? await deploy ( "VRFSubscriptionManagerV2Mock" , {
100110 from : deployer ,
@@ -108,31 +118,51 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
108118 } )
109119 : AddressZero ;
110120
111- if ( vrfSubscriptionManagerDeploy ) {
121+ // Execute the setup transactions for using VRF and deploy the Consumer contract on Hardhat node
122+ // The Sortition Module rng source is not changed to the VRF Consumer.
123+ if ( vrfSubscriptionManager ) {
112124 if ( chainId === HomeChains . HARDHAT ) {
113- const vrfSubscriptionManager = ( await hre . ethers . getContract (
125+ const vrfSubscriptionManagerContract = ( await hre . ethers . getContract (
114126 "VRFSubscriptionManagerV2Mock"
115127 ) ) as VRFSubscriptionManagerV2Mock ;
116- await vrfSubscriptionManager . createNewSubscription ( ) ;
117- await vrfSubscriptionManager . topUpSubscription ( BigNumber . from ( 10 ) . pow ( 20 ) ) ; // 100 LINK
118- const subId = await vrfSubscriptionManager . subscriptionId ( ) ;
128+ await vrfSubscriptionManagerContract . topUpSubscription ( BigNumber . from ( 10 ) . pow ( 20 ) ) ; // 100 LINK
129+ const subscriptionId = await vrfSubscriptionManagerContract . subscriptionId ( ) ;
119130 const vrfConsumer = await deploy ( "VRFConsumerV2" , {
120131 from : deployer ,
121132 args : [
122133 deployer ,
123134 vrfCoordinator ,
124135 sortitionModule . address ,
125136 keyHash ,
126- subId ,
137+ subscriptionId ,
127138 requestConfirmations ,
128139 callbackGasLimit ,
129140 numWords ,
130141 ] ,
131142 log : true ,
132143 } ) ;
133- await vrfSubscriptionManager . addConsumer ( vrfConsumer . address ) ;
134- await sortitionModule . changeRandomNumberGenerator ( vrfConsumer . address , RNG_LOOKAHEAD ) ;
144+ await vrfSubscriptionManagerContract . addConsumer ( vrfConsumer . address ) ;
135145 }
146+ } else {
147+ const vrfSubscriptionManagerContract = ( await hre . ethers . getContract (
148+ "VRFSubscriptionManagerV2"
149+ ) ) as VRFSubscriptionManagerV2 ;
150+ const subscriptionId = await vrfSubscriptionManagerContract . subscriptionId ( ) ;
151+ const vrfConsumer = await deploy ( "VRFConsumerV2" , {
152+ from : deployer ,
153+ args : [
154+ deployer ,
155+ vrfCoordinator ,
156+ sortitionModule . address ,
157+ keyHash ,
158+ subscriptionId ,
159+ requestConfirmations ,
160+ callbackGasLimit ,
161+ numWords ,
162+ ] ,
163+ log : true ,
164+ } ) ;
165+ await vrfSubscriptionManagerContract . addConsumer ( vrfConsumer . address ) ;
136166 }
137167} ;
138168
0 commit comments