1
+ /**
2
+ * Test fixtures and setup utilities
3
+ * Contains deployment functions, shared constants, and test utilities
4
+ */
5
+
1
6
import type { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers'
2
7
import * as fs from 'fs'
3
8
4
9
const { ethers, upgrades } = require ( 'hardhat' )
5
- const { SHARED_CONSTANTS } = require ( './sharedFixtures' )
6
- const { OPERATOR_ROLE } = SHARED_CONSTANTS
10
+
11
+ // Shared test constants
12
+ export const SHARED_CONSTANTS = {
13
+ PPM : 1_000_000 ,
14
+
15
+ // Pre-calculated role constants to avoid repeated async calls
16
+ GOVERNOR_ROLE : ethers . keccak256 ( ethers . toUtf8Bytes ( 'GOVERNOR_ROLE' ) ) ,
17
+ OPERATOR_ROLE : ethers . keccak256 ( ethers . toUtf8Bytes ( 'OPERATOR_ROLE' ) ) ,
18
+ PAUSE_ROLE : ethers . keccak256 ( ethers . toUtf8Bytes ( 'PAUSE_ROLE' ) ) ,
19
+ ORACLE_ROLE : ethers . keccak256 ( ethers . toUtf8Bytes ( 'ORACLE_ROLE' ) ) ,
20
+ } as const
21
+
22
+ // Interface IDs
23
+ export const INTERFACE_IDS = {
24
+ IERC165 : '0x01ffc9a7' ,
25
+ } as const
7
26
8
27
// Types
9
28
export interface TestAccounts {
@@ -15,6 +34,24 @@ export interface TestAccounts {
15
34
indexer2 : HardhatEthersSigner
16
35
}
17
36
37
+ export interface SharedContracts {
38
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ graphToken : any
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ rewardsEligibilityOracle : any
42
+ }
43
+
44
+ export interface SharedAddresses {
45
+ graphToken : string
46
+ rewardsEligibilityOracle : string
47
+ }
48
+
49
+ export interface SharedFixtures {
50
+ accounts : TestAccounts
51
+ contracts : SharedContracts
52
+ addresses : SharedAddresses
53
+ }
54
+
18
55
/**
19
56
* Get standard test accounts
20
57
*/
@@ -60,12 +97,12 @@ export async function deployTestGraphToken(): Promise<any> {
60
97
* Deploy the RewardsEligibilityOracle contract with proxy using OpenZeppelin's upgrades library
61
98
* @param graphToken The Graph Token contract address
62
99
* @param governor The governor signer
63
- * @param validityPeriod The validity period in seconds (default: 7 days)
100
+ * @param validityPeriod The validity period in seconds (default: 14 days)
64
101
*/
65
102
export async function deployRewardsEligibilityOracle (
66
103
graphToken : string ,
67
104
governor : HardhatEthersSigner ,
68
- validityPeriod : number = 7 * 24 * 60 * 60 , // 7 days in seconds
105
+ validityPeriod : number = 14 * 24 * 60 * 60 , // 14 days in seconds (contract default)
69
106
// eslint-disable-next-line @typescript-eslint/no-explicit-any
70
107
) : Promise < any > {
71
108
// Deploy implementation and proxy using OpenZeppelin's upgrades library
@@ -84,14 +121,65 @@ export async function deployRewardsEligibilityOracle(
84
121
// Get the contract instance
85
122
const rewardsEligibilityOracle = rewardsEligibilityOracleContract
86
123
87
- // Set the validity period if it's different from the default
88
- if ( validityPeriod !== 7 * 24 * 60 * 60 ) {
89
- // First grant operator role to governor so they can set the validity period
90
- await rewardsEligibilityOracle . connect ( governor ) . grantOperatorRole ( governor . address )
91
- await rewardsEligibilityOracle . connect ( governor ) . setValidityPeriod ( validityPeriod )
124
+ // Set the eligibility period if it's different from the default (14 days)
125
+ if ( validityPeriod !== 14 * 24 * 60 * 60 ) {
126
+ // First grant operator role to governor so they can set the eligibility period
127
+ await rewardsEligibilityOracle . connect ( governor ) . grantRole ( SHARED_CONSTANTS . OPERATOR_ROLE , governor . address )
128
+ await rewardsEligibilityOracle . connect ( governor ) . setEligibilityPeriod ( validityPeriod )
92
129
// Now revoke the operator role from governor to ensure tests start with clean state
93
- await rewardsEligibilityOracle . connect ( governor ) . revokeRole ( OPERATOR_ROLE , governor . address )
130
+ await rewardsEligibilityOracle . connect ( governor ) . revokeRole ( SHARED_CONSTANTS . OPERATOR_ROLE , governor . address )
94
131
}
95
132
96
133
return rewardsEligibilityOracle
97
134
}
135
+
136
+ /**
137
+ * Shared contract deployment and setup
138
+ */
139
+ export async function deploySharedContracts ( ) : Promise < SharedFixtures > {
140
+ const accounts = await getTestAccounts ( )
141
+
142
+ // Deploy base contracts
143
+ const graphToken = await deployTestGraphToken ( )
144
+ const graphTokenAddress = await graphToken . getAddress ( )
145
+
146
+ const rewardsEligibilityOracle = await deployRewardsEligibilityOracle ( graphTokenAddress , accounts . governor )
147
+
148
+ // Cache addresses
149
+ const addresses : SharedAddresses = {
150
+ graphToken : graphTokenAddress ,
151
+ rewardsEligibilityOracle : await rewardsEligibilityOracle . getAddress ( ) ,
152
+ }
153
+
154
+ // Create helper
155
+ return {
156
+ accounts,
157
+ contracts : {
158
+ graphToken,
159
+ rewardsEligibilityOracle,
160
+ } ,
161
+ addresses,
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Reset contract state to initial conditions
167
+ * Optimized to avoid redeployment while ensuring clean state
168
+ */
169
+ export async function resetContractState ( contracts : SharedContracts , accounts : TestAccounts ) : Promise < void > {
170
+ const { rewardsEligibilityOracle } = contracts
171
+
172
+ // Reset RewardsEligibilityOracle state
173
+ try {
174
+ if ( await rewardsEligibilityOracle . paused ( ) ) {
175
+ await rewardsEligibilityOracle . connect ( accounts . governor ) . unpause ( )
176
+ }
177
+
178
+ // Reset eligibility validation to default (disabled)
179
+ if ( await rewardsEligibilityOracle . getEligibilityValidation ( ) ) {
180
+ await rewardsEligibilityOracle . connect ( accounts . governor ) . setEligibilityValidation ( false )
181
+ }
182
+ } catch ( error ) {
183
+ console . warn ( 'RewardsEligibilityOracle state reset failed:' , error instanceof Error ? error . message : String ( error ) )
184
+ }
185
+ }
0 commit comments