Skip to content

Commit

Permalink
explicitly select address book
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Nov 14, 2023
1 parent 711b1ba commit 8c97d85
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/common-ts/src/contracts/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Signer } from 'ethers'
import { connectContracts } from '.'
import * as DEPLOYED_CONTRACTS from '@graphprotocol/contracts/addresses.json'

jest.mock('ethers')

Expand All @@ -8,7 +9,7 @@ const mockSigner = jest.fn() as unknown as Signer
describe('Contracts', () => {
// Test for each supported protocol network
test.each([1, 5, 42161, 421613])('Connect contracts [chainId: %p]', chainId => {
const contracts = connectContracts(mockSigner, chainId)
const contracts = connectContracts(mockSigner, chainId, DEPLOYED_CONTRACTS)
expect(contracts).toBeDefined()
})
})
14 changes: 4 additions & 10 deletions packages/common-ts/src/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { providers, Signer } from 'ethers'
import graphChain from './chain'

// Contract addresses
import * as DEPLOYED_CONTRACTS from '@graphprotocol/contracts/addresses.json'

// Contract ABIs
import { Curation } from '@graphprotocol/contracts/dist/types/Curation'
import { DisputeManager } from '@graphprotocol/contracts/dist/types/DisputeManager'
Expand Down Expand Up @@ -45,7 +42,6 @@ import { L1GraphTokenGateway__factory } from '@graphprotocol/contracts/dist/type
import { BridgeEscrow__factory } from '@graphprotocol/contracts/dist/types/factories/BridgeEscrow__factory'
import { L2GraphToken__factory } from '@graphprotocol/contracts/dist/types/factories/L2GraphToken__factory'
import { L2GraphTokenGateway__factory } from '@graphprotocol/contracts/dist/types/factories/L2GraphTokenGateway__factory'
import { readFileSync } from 'fs'

export const GraphChain = graphChain

Expand All @@ -72,16 +68,14 @@ export interface NetworkContracts {
l2GraphTokenGateway?: L2GraphTokenGateway
}

export type AddressBook = { [key: string]: { [key: string]: { address: string } } }

export const connectContracts = async (
providerOrSigner: providers.Provider | Signer,
chainId: number,
addressBook?: string,
addressBook: AddressBook,
): Promise<NetworkContracts> => {
const deployedContracts = addressBook
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
JSON.parse(readFileSync(addressBook).toString())[`${chainId}`]
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
(DEPLOYED_CONTRACTS as any)[`${chainId}`]
const deployedContracts = addressBook[`${chainId}`]
if (!deployedContracts) {
throw new Error(`chainId: '${chainId}' has no deployed contracts`)
}
Expand Down

0 comments on commit 8c97d85

Please sign in to comment.