Skip to content

Commit bce23cd

Browse files
committed
feat: add support for L2 contracts
Signed-off-by: Tomás Migone <[email protected]>
1 parent 5a29c87 commit bce23cd

File tree

5 files changed

+520
-366
lines changed

5 files changed

+520
-366
lines changed

packages/common-ts/CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [2.0.4-testnet-scratch1] - 2022-08-30
8+
### Changed
9+
- Update to use @graphprotocol/contracts@2.0.4-testnet-scratch1
10+
11+
## [2.0.3-testnet-scratch1] - 2022-08-30
12+
### Fixed
13+
- Fixed a bug where L2 graph token would initialize with L1 address.
14+
15+
## [2.0.2-testnet-scratch1] - 2022-08-30
16+
### Fixed
17+
- Don't require L1Reservoir to be deployed (not deployed in scratch1)
18+
19+
## [2.0.1-testnet-scratch1] - 2022-08-29
20+
### Changed
21+
- Add support for L2 contracts
22+
- Add GraphChain utils
23+
- Update @graphprotocol/contracts to v2.0.2-testnet-scratch1
24+
725
## [Unreleased]
826
### Changed
927
- Include AllocationExchange in NetworkContracts

packages/common-ts/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@graphprotocol/common-ts",
3-
"version": "1.8.6",
3+
"version": "2.0.4-testnet-scratch1",
44
"description": "Common TypeScript library for Graph Protocol components",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -16,15 +16,15 @@
1616
"test:watch": "jest --watch --passWithNoTests --detectOpenHandles --verbose"
1717
},
1818
"dependencies": {
19-
"@graphprotocol/contracts": "1.13.0",
19+
"@graphprotocol/contracts": "^2.0.4-testnet-scratch1",
2020
"@graphprotocol/pino-sentry-simple": "0.7.1",
2121
"@urql/core": "2.4.4",
2222
"@urql/exchange-execute": "1.2.2",
2323
"body-parser": "1.19.1",
2424
"bs58": "4.0.1",
2525
"cors": "2.8.5",
2626
"cross-fetch": "3.1.5",
27-
"ethers": "5.6.2",
27+
"ethers": "5.7.0",
2828
"express": "4.17.3",
2929
"graphql": "16.3.0",
3030
"graphql-tag": "2.12.6",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class MapWithGetKey<K> extends Map<K, K> {
2+
getKey(value: K): K | undefined {
3+
for (const [k, v] of this.entries()) {
4+
if (v === value) {
5+
return k
6+
}
7+
}
8+
return
9+
}
10+
}
11+
12+
// List of supported L1 <> L2 chain mappings
13+
const chainMap = new MapWithGetKey<number>([
14+
[1, 42161], // Ethereum Mainnet - Arbitrum One
15+
[4, 421611], // Ethereum Rinkeby - Arbitrum Rinkeby
16+
[5, 421613], // Ethereum Goerli - Arbitrum Goerli
17+
[1337, 412346], // Localhost - Arbitrum Localhost
18+
])
19+
20+
export const l1Chains = Array.from(chainMap.keys())
21+
export const l2Chains = Array.from(chainMap.values())
22+
export const chains = [...l1Chains, ...l2Chains]
23+
24+
export const isL1 = (chainId: number): boolean => l1Chains.includes(chainId)
25+
export const isL2 = (chainId: number): boolean => l2Chains.includes(chainId)
26+
export const isSupported = (chainId: number | undefined): boolean =>
27+
chainId !== undefined && chains.includes(chainId)
28+
29+
export const l1ToL2 = (chainId: number): number | undefined => chainMap.get(chainId)
30+
export const l2ToL1 = (chainId: number): number | undefined => chainMap.getKey(chainId)
31+
export const counterpart = (chainId: number): number | undefined => {
32+
if (!isSupported(chainId)) return
33+
return isL1(chainId) ? l1ToL2(chainId) : l2ToL1(chainId)
34+
}
35+
36+
export default {
37+
l1Chains,
38+
l2Chains,
39+
chains,
40+
isL1,
41+
isL2,
42+
isSupported,
43+
l1ToL2,
44+
l2ToL1,
45+
counterpart,
46+
}

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

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { providers, Signer } from 'ethers'
2+
import graphChain from './chain'
23

34
// Contract addresses
45
import * as DEPLOYED_CONTRACTS from '@graphprotocol/contracts/addresses.json'
@@ -14,6 +15,15 @@ import { Staking } from '@graphprotocol/contracts/dist/types/Staking'
1415
import { GraphToken } from '@graphprotocol/contracts/dist/types/GraphToken'
1516
import { Controller } from '@graphprotocol/contracts/dist/types/Controller'
1617
import { AllocationExchange } from '@graphprotocol/contracts/dist/types/AllocationExchange'
18+
import { GraphProxyAdmin } from '@graphprotocol/contracts/dist/types/GraphProxyAdmin'
19+
import { SubgraphNFT } from '@graphprotocol/contracts/dist/types/SubgraphNFT'
20+
import { GraphCurationToken } from '@graphprotocol/contracts/dist/types/GraphCurationToken'
21+
import { L1GraphTokenGateway } from '@graphprotocol/contracts/dist/types/L1GraphTokenGateway'
22+
import { L1Reservoir } from '@graphprotocol/contracts/dist/types/L1Reservoir'
23+
import { BridgeEscrow } from '@graphprotocol/contracts/dist/types/BridgeEscrow'
24+
import { L2GraphToken } from '@graphprotocol/contracts/dist/types/L2GraphToken'
25+
import { L2GraphTokenGateway } from '@graphprotocol/contracts/dist/types/L2GraphTokenGateway'
26+
import { L2Reservoir } from '@graphprotocol/contracts/dist/types/L2Reservoir'
1727

1828
// Contract factories
1929
import { Curation__factory } from '@graphprotocol/contracts/dist/types/factories/Curation__factory'
@@ -26,6 +36,17 @@ import { Staking__factory } from '@graphprotocol/contracts/dist/types/factories/
2636
import { GraphToken__factory } from '@graphprotocol/contracts/dist/types/factories/GraphToken__factory'
2737
import { Controller__factory } from '@graphprotocol/contracts/dist/types/factories/Controller__factory'
2838
import { AllocationExchange__factory } from '@graphprotocol/contracts/dist/types/factories/AllocationExchange__factory'
39+
import { GraphProxyAdmin__factory } from '@graphprotocol/contracts/dist/types/factories/GraphProxyAdmin__factory'
40+
import { SubgraphNFT__factory } from '@graphprotocol/contracts/dist/types/factories/SubgraphNFT__factory'
41+
import { GraphCurationToken__factory } from '@graphprotocol/contracts/dist/types/factories/GraphCurationToken__factory'
42+
import { L1GraphTokenGateway__factory } from '@graphprotocol/contracts/dist/types/factories/L1GraphTokenGateway__factory'
43+
import { L1Reservoir__factory } from '@graphprotocol/contracts/dist/types/factories/L1Reservoir__factory'
44+
import { BridgeEscrow__factory } from '@graphprotocol/contracts/dist/types/factories/BridgeEscrow__factory'
45+
import { L2GraphToken__factory } from '@graphprotocol/contracts/dist/types/factories/L2GraphToken__factory'
46+
import { L2GraphTokenGateway__factory } from '@graphprotocol/contracts/dist/types/factories/L2GraphTokenGateway__factory'
47+
import { L2Reservoir__factory } from '@graphprotocol/contracts/dist/types/factories/L2Reservoir__factory'
48+
49+
export const GraphChain = graphChain
2950

3051
export interface NetworkContracts {
3152
curation: Curation
@@ -35,9 +56,21 @@ export interface NetworkContracts {
3556
rewardsManager: RewardsManager
3657
serviceRegistry: ServiceRegistry
3758
staking: Staking
38-
token: GraphToken
59+
token: GraphToken | L2GraphToken
3960
controller: Controller
4061
allocationExchange: AllocationExchange
62+
graphProxyAdmin: GraphProxyAdmin
63+
subgraphNFT: SubgraphNFT
64+
graphCurationToken: GraphCurationToken
65+
66+
// Only L1
67+
l1GraphTokenGateway?: L1GraphTokenGateway
68+
l1Reservoir?: L1Reservoir
69+
bridgeEscrow?: BridgeEscrow
70+
71+
// Only L2
72+
l2GraphTokenGateway?: L2GraphTokenGateway
73+
l2Reservoir?: L2Reservoir
4174
}
4275

4376
export const connectContracts = async (
@@ -46,7 +79,16 @@ export const connectContracts = async (
4679
): Promise<NetworkContracts> => {
4780
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4881
const deployedContracts = (DEPLOYED_CONTRACTS as any)[`${chainId}`]
49-
return {
82+
83+
const GraphTokenFactory = GraphChain.isL1(chainId)
84+
? GraphToken__factory
85+
: L2GraphToken__factory
86+
87+
const graphTokenAddress = GraphChain.isL1(chainId)
88+
? deployedContracts.GraphToken.address
89+
: deployedContracts.L2GraphToken.address
90+
91+
const contracts: NetworkContracts = {
5092
curation: Curation__factory.connect(
5193
deployedContracts.Curation.address,
5294
providerOrSigner,
@@ -72,8 +114,8 @@ export const connectContracts = async (
72114
deployedContracts.Staking.address,
73115
providerOrSigner,
74116
),
75-
token: GraphToken__factory.connect(
76-
deployedContracts.GraphToken.address,
117+
token: GraphTokenFactory.connect(
118+
graphTokenAddress,
77119
providerOrSigner,
78120
),
79121
controller: Controller__factory.connect(
@@ -84,5 +126,46 @@ export const connectContracts = async (
84126
deployedContracts.AllocationExchange.address,
85127
providerOrSigner,
86128
),
129+
graphProxyAdmin: GraphProxyAdmin__factory.connect(
130+
deployedContracts.GraphProxyAdmin.address,
131+
providerOrSigner,
132+
),
133+
subgraphNFT: SubgraphNFT__factory.connect(
134+
deployedContracts.SubgraphNFT.address,
135+
providerOrSigner,
136+
),
137+
graphCurationToken: GraphCurationToken__factory.connect(
138+
deployedContracts.GraphCurationToken.address,
139+
providerOrSigner,
140+
),
87141
}
142+
143+
if (GraphChain.isL1(chainId)) {
144+
contracts.l1GraphTokenGateway = L1GraphTokenGateway__factory.connect(
145+
deployedContracts.L1GraphTokenGateway.address,
146+
providerOrSigner,
147+
)
148+
contracts.bridgeEscrow = BridgeEscrow__factory.connect(
149+
deployedContracts.BridgeEscrow.address,
150+
providerOrSigner,
151+
)
152+
// L1Reservoir is not deployed on scratch1
153+
if (deployedContracts.L1Reservoir) {
154+
contracts.l1Reservoir = L1Reservoir__factory.connect(
155+
deployedContracts.L1Reservoir.address,
156+
providerOrSigner,
157+
)
158+
}
159+
} else if (GraphChain.isL2(chainId)) {
160+
contracts.l2GraphTokenGateway = L2GraphTokenGateway__factory.connect(
161+
deployedContracts.L2GraphTokenGateway.address,
162+
providerOrSigner,
163+
)
164+
contracts.l2Reservoir = L2Reservoir__factory.connect(
165+
deployedContracts.L2Reservoir.address,
166+
providerOrSigner,
167+
)
168+
}
169+
170+
return contracts
88171
}

0 commit comments

Comments
 (0)