Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for L2 contracts #95

Merged
merged 7 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/common-ts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2022-11-30
### Changed
- Update @graphprotocol/contracts to v2.1.0
- Add mainnet bridge contracts and L2 contracts

## [2.0.4-testnet] - 2022-11-09
### Changed
- Fix L1/L2 specific contracts not being loaded

## [2.0.3-testnet] - 2022-11-09
### Changed
- Don't error out if contract is not deployed

## [2.0.1-testnet] - 2022-11-09
### Changed
- Remove reservoir contracts

## [2.0.0-testnet] - 2022-11-09
### Changed
- Add support for L2 contracts
- Add GraphChain utils
- Update @graphprotocol/contracts to v2.0.0-testnet

## [Unreleased]
### Changed
- Include AllocationExchange in NetworkContracts
Expand Down
6 changes: 3 additions & 3 deletions packages/common-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@graphprotocol/common-ts",
"version": "1.8.6",
"version": "2.0.0",
"description": "Common TypeScript library for Graph Protocol components",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -16,15 +16,15 @@
"test:watch": "jest --watch --passWithNoTests --detectOpenHandles --verbose"
},
"dependencies": {
"@graphprotocol/contracts": "1.13.0",
"@graphprotocol/contracts": "2.1.0",
"@graphprotocol/pino-sentry-simple": "0.7.1",
"@urql/core": "2.4.4",
"@urql/exchange-execute": "1.2.2",
"body-parser": "1.19.1",
"bs58": "4.0.1",
"cors": "2.8.5",
"cross-fetch": "3.1.5",
"ethers": "5.6.2",
"ethers": "5.7.0",
"express": "4.17.3",
"graphql": "16.3.0",
"graphql-tag": "2.12.6",
Expand Down
46 changes: 46 additions & 0 deletions packages/common-ts/src/contracts/chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class MapWithGetKey<K> extends Map<K, K> {
getKey(value: K): K | undefined {
for (const [k, v] of this.entries()) {
if (v === value) {
return k
}
}
return
}
}

// List of supported L1 <> L2 chain mappings
const chainMap = new MapWithGetKey<number>([
[1, 42161], // Ethereum Mainnet - Arbitrum One
[4, 421611], // Ethereum Rinkeby - Arbitrum Rinkeby
[5, 421613], // Ethereum Goerli - Arbitrum Goerli
[1337, 412346], // Localhost - Arbitrum Localhost
])

export const l1Chains = Array.from(chainMap.keys())
export const l2Chains = Array.from(chainMap.values())
export const chains = [...l1Chains, ...l2Chains]

export const isL1 = (chainId: number): boolean => l1Chains.includes(chainId)
export const isL2 = (chainId: number): boolean => l2Chains.includes(chainId)
export const isSupported = (chainId: number | undefined): boolean =>
chainId !== undefined && chains.includes(chainId)

export const l1ToL2 = (chainId: number): number | undefined => chainMap.get(chainId)
export const l2ToL1 = (chainId: number): number | undefined => chainMap.getKey(chainId)
export const counterpart = (chainId: number): number | undefined => {
if (!isSupported(chainId)) return
return isL1(chainId) ? l1ToL2(chainId) : l2ToL1(chainId)
}

export default {
l1Chains,
l2Chains,
chains,
isL1,
isL2,
isSupported,
l1ToL2,
l2ToL1,
counterpart,
}
80 changes: 74 additions & 6 deletions packages/common-ts/src/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { providers, Signer } from 'ethers'
import graphChain from './chain'

// Contract addresses
import * as DEPLOYED_CONTRACTS from '@graphprotocol/contracts/addresses.json'
Expand All @@ -14,6 +15,13 @@ import { Staking } from '@graphprotocol/contracts/dist/types/Staking'
import { GraphToken } from '@graphprotocol/contracts/dist/types/GraphToken'
import { Controller } from '@graphprotocol/contracts/dist/types/Controller'
import { AllocationExchange } from '@graphprotocol/contracts/dist/types/AllocationExchange'
import { GraphProxyAdmin } from '@graphprotocol/contracts/dist/types/GraphProxyAdmin'
import { SubgraphNFT } from '@graphprotocol/contracts/dist/types/SubgraphNFT'
import { GraphCurationToken } from '@graphprotocol/contracts/dist/types/GraphCurationToken'
import { L1GraphTokenGateway } from '@graphprotocol/contracts/dist/types/L1GraphTokenGateway'
import { BridgeEscrow } from '@graphprotocol/contracts/dist/types/BridgeEscrow'
import { L2GraphToken } from '@graphprotocol/contracts/dist/types/L2GraphToken'
import { L2GraphTokenGateway } from '@graphprotocol/contracts/dist/types/L2GraphTokenGateway'

// Contract factories
import { Curation__factory } from '@graphprotocol/contracts/dist/types/factories/Curation__factory'
Expand All @@ -26,6 +34,15 @@ import { Staking__factory } from '@graphprotocol/contracts/dist/types/factories/
import { GraphToken__factory } from '@graphprotocol/contracts/dist/types/factories/GraphToken__factory'
import { Controller__factory } from '@graphprotocol/contracts/dist/types/factories/Controller__factory'
import { AllocationExchange__factory } from '@graphprotocol/contracts/dist/types/factories/AllocationExchange__factory'
import { GraphProxyAdmin__factory } from '@graphprotocol/contracts/dist/types/factories/GraphProxyAdmin__factory'
import { SubgraphNFT__factory } from '@graphprotocol/contracts/dist/types/factories/SubgraphNFT__factory'
import { GraphCurationToken__factory } from '@graphprotocol/contracts/dist/types/factories/GraphCurationToken__factory'
import { L1GraphTokenGateway__factory } from '@graphprotocol/contracts/dist/types/factories/L1GraphTokenGateway__factory'
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'

export const GraphChain = graphChain

export interface NetworkContracts {
curation: Curation
Expand All @@ -35,9 +52,19 @@ export interface NetworkContracts {
rewardsManager: RewardsManager
serviceRegistry: ServiceRegistry
staking: Staking
token: GraphToken
token: GraphToken | L2GraphToken
controller: Controller
allocationExchange: AllocationExchange
graphProxyAdmin: GraphProxyAdmin
subgraphNFT: SubgraphNFT
graphCurationToken: GraphCurationToken

// Only L1
l1GraphTokenGateway?: L1GraphTokenGateway
bridgeEscrow?: BridgeEscrow

// Only L2
l2GraphTokenGateway?: L2GraphTokenGateway
}

export const connectContracts = async (
Expand All @@ -46,7 +73,15 @@ export const connectContracts = async (
): Promise<NetworkContracts> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const deployedContracts = (DEPLOYED_CONTRACTS as any)[`${chainId}`]
return {
const GraphTokenFactory = GraphChain.isL1(chainId)
tmigone marked this conversation as resolved.
Show resolved Hide resolved
? GraphToken__factory
: L2GraphToken__factory

const graphTokenAddress = GraphChain.isL1(chainId)
? deployedContracts.GraphToken.address
: deployedContracts.L2GraphToken.address

const contracts: NetworkContracts = {
curation: Curation__factory.connect(
deployedContracts.Curation.address,
providerOrSigner,
Expand All @@ -72,10 +107,7 @@ export const connectContracts = async (
deployedContracts.Staking.address,
providerOrSigner,
),
token: GraphToken__factory.connect(
deployedContracts.GraphToken.address,
providerOrSigner,
),
token: GraphTokenFactory.connect(graphTokenAddress, providerOrSigner),
controller: Controller__factory.connect(
deployedContracts.Controller.address,
providerOrSigner,
Expand All @@ -84,5 +116,41 @@ export const connectContracts = async (
deployedContracts.AllocationExchange.address,
providerOrSigner,
),
graphProxyAdmin: GraphProxyAdmin__factory.connect(
deployedContracts.GraphProxyAdmin.address,
providerOrSigner,
),
subgraphNFT: SubgraphNFT__factory.connect(
deployedContracts.SubgraphNFT.address,
providerOrSigner,
),
graphCurationToken: GraphCurationToken__factory.connect(
deployedContracts.GraphCurationToken.address,
providerOrSigner,
),
}

if (GraphChain.isL1(chainId)) {
if (deployedContracts.L1GraphTokenGateway) {
contracts.l1GraphTokenGateway = L1GraphTokenGateway__factory.connect(
deployedContracts.L1GraphTokenGateway.address,
providerOrSigner,
)
}
if (deployedContracts.BridgeEscrow) {
contracts.bridgeEscrow = BridgeEscrow__factory.connect(
deployedContracts.BridgeEscrow.address,
providerOrSigner,
)
}
} else if (GraphChain.isL2(chainId)) {
if (deployedContracts.L2GraphTokenGateway) {
contracts.l2GraphTokenGateway = L2GraphTokenGateway__factory.connect(
deployedContracts.L2GraphTokenGateway.address,
providerOrSigner,
)
}
}

return contracts
}
Loading