Skip to content

Commit 2f34392

Browse files
committed
Robustness checks around contract information
1 parent 0cdf581 commit 2f34392

File tree

1 file changed

+43
-23
lines changed
  • packages/common-ts/src/contracts

1 file changed

+43
-23
lines changed

packages/common-ts/src/contracts/index.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,81 +75,101 @@ export const connectContracts = async (
7575
): Promise<NetworkContracts> => {
7676
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7777
const deployedContracts = (DEPLOYED_CONTRACTS as any)[`${chainId}`]
78+
if (!deployedContracts) {
79+
throw new Error(`chainId: '${chainId}' has no deployed contracts`)
80+
}
81+
82+
const getContractAddress = (contractName: string) => {
83+
if (!deployedContracts[contractName]) {
84+
throw new Error(
85+
`Deployed contract '${contractName}' is undefined for chainId: '${chainId}'`,
86+
)
87+
}
88+
const address = deployedContracts[contractName].address
89+
if (!address) {
90+
throw new Error(
91+
`Deployed contract '${contractName}' address is undefined for chainId: '${chainId}'`,
92+
)
93+
}
94+
return address
95+
}
96+
7897
const GraphTokenFactory = GraphChain.isL1(chainId)
7998
? GraphToken__factory
8099
: L2GraphToken__factory
81100

82101
const graphTokenAddress = GraphChain.isL1(chainId)
83-
? deployedContracts.GraphToken.address
84-
: deployedContracts.L2GraphToken.address
102+
? getContractAddress('GraphToken')
103+
: getContractAddress('L2GraphToken')
104+
105+
const staking = GraphChain.isL1(chainId)
106+
? IL1Staking__factory.connect(getContractAddress('L1Staking'), providerOrSigner)
107+
: IL2Staking__factory.connect(getContractAddress('L2Staking'), providerOrSigner)
85108

86-
const staking = GraphChain.isL1(chainId)
87-
? IL1Staking__factory.connect(deployedContracts.L1Staking.address, providerOrSigner)
88-
: IL2Staking__factory.connect(deployedContracts.L2Staking.address, providerOrSigner)
109+
const gns = GraphChain.isL1(chainId)
110+
? GNS__factory.connect(getContractAddress('L1GNS'), providerOrSigner)
111+
: GNS__factory.connect(getContractAddress('L2GNS'), providerOrSigner)
89112

90113
const contracts: NetworkContracts = {
91-
curation: Curation__factory.connect(
92-
deployedContracts.Curation.address,
93-
providerOrSigner,
94-
),
114+
curation: Curation__factory.connect(getContractAddress('Curation'), providerOrSigner),
95115
disputeManager: DisputeManager__factory.connect(
96-
deployedContracts.DisputeManager.address,
116+
getContractAddress('DisputeManager'),
97117
providerOrSigner,
98118
),
99119
epochManager: EpochManager__factory.connect(
100-
deployedContracts.EpochManager.address,
120+
getContractAddress('EpochManager'),
101121
providerOrSigner,
102122
),
103-
gns: GNS__factory.connect(deployedContracts.GNS.address, providerOrSigner),
123+
gns,
104124
rewardsManager: RewardsManager__factory.connect(
105-
deployedContracts.RewardsManager.address,
125+
getContractAddress('RewardsManager'),
106126
providerOrSigner,
107127
),
108128
serviceRegistry: ServiceRegistry__factory.connect(
109-
deployedContracts.ServiceRegistry.address,
129+
getContractAddress('ServiceRegistry'),
110130
providerOrSigner,
111131
),
112-
staking: staking,
132+
staking,
113133
token: GraphTokenFactory.connect(graphTokenAddress, providerOrSigner),
114134
controller: Controller__factory.connect(
115-
deployedContracts.Controller.address,
135+
getContractAddress('Controller'),
116136
providerOrSigner,
117137
),
118138
allocationExchange: AllocationExchange__factory.connect(
119-
deployedContracts.AllocationExchange.address,
139+
getContractAddress('AllocationExchange'),
120140
providerOrSigner,
121141
),
122142
graphProxyAdmin: GraphProxyAdmin__factory.connect(
123-
deployedContracts.GraphProxyAdmin.address,
143+
getContractAddress('GraphProxyAdmin'),
124144
providerOrSigner,
125145
),
126146
subgraphNFT: SubgraphNFT__factory.connect(
127-
deployedContracts.SubgraphNFT.address,
147+
getContractAddress('SubgraphNFT'),
128148
providerOrSigner,
129149
),
130150
graphCurationToken: GraphCurationToken__factory.connect(
131-
deployedContracts.GraphCurationToken.address,
151+
getContractAddress('GraphCurationToken'),
132152
providerOrSigner,
133153
),
134154
}
135155

136156
if (GraphChain.isL1(chainId)) {
137157
if (deployedContracts.L1GraphTokenGateway) {
138158
contracts.l1GraphTokenGateway = L1GraphTokenGateway__factory.connect(
139-
deployedContracts.L1GraphTokenGateway.address,
159+
getContractAddress('L1GraphTokenGateway'),
140160
providerOrSigner,
141161
)
142162
}
143163
if (deployedContracts.BridgeEscrow) {
144164
contracts.bridgeEscrow = BridgeEscrow__factory.connect(
145-
deployedContracts.BridgeEscrow.address,
165+
getContractAddress('BridgeEscrow'),
146166
providerOrSigner,
147167
)
148168
}
149169
} else if (GraphChain.isL2(chainId)) {
150170
if (deployedContracts.L2GraphTokenGateway) {
151171
contracts.l2GraphTokenGateway = L2GraphTokenGateway__factory.connect(
152-
deployedContracts.L2GraphTokenGateway.address,
172+
getContractAddress('L2GraphTokenGateway'),
153173
providerOrSigner,
154174
)
155175
}

0 commit comments

Comments
 (0)