Skip to content

Commit

Permalink
feat: add initial bridge support
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Migone <tomas@edgeandnode.com>
  • Loading branch information
tmigone authored and juanmardefago committed Dec 26, 2022
1 parent c0cf4e9 commit 9d0067c
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 363 deletions.
6 changes: 6 additions & 0 deletions config/addresses.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ export class Addresses {
ens: string
ensPublicResolver: string
blockNumber: string
bridgeBlockNumber: string
network: string
tokenLockManager: string
subgraphNFT: string
l1GraphTokenGateway?: string
l2GraphTokenGateway?: string
}

// AS compiler does not like const
Expand All @@ -31,7 +34,10 @@ export let addresses: Addresses = {
ens: '{{ens}}',
ensPublicResolver: '{{ensPublicResolver}}',
blockNumber: '{{blockNumber}}',
bridgeBlockNumber: '{{bridgeBlockNumber}}',
network: '{{network}}',
tokenLockManager: '{{tokenLockManager}}',
subgraphNFT: '{{subgraphNFT}}',
l1GraphTokenGateway: '{{l1GraphTokenGateway}}',
l2GraphTokenGateway: '{{l2GraphTokenGateway}}',
}
45 changes: 45 additions & 0 deletions config/arbitrumGoerliAddressScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as fs from 'fs'
import * as mustache from 'mustache'
import * as networkAddresses from '@graphprotocol/contracts/addresses.json'
import { Addresses } from './addresses.template'

// mustache doesn't like numbered object keys
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let renameAddresses: any = networkAddresses
renameAddresses['arbgor'] = networkAddresses['421613']

export let addresses: Addresses = {
controller: '{{arbgor.Controller.address}}',
graphToken: '{{arbgor.GraphToken.address}}',
epochManager: '{{arbgor.EpochManager.address}}',
disputeManager: '{{arbgor.DisputeManager.address}}',
staking: '{{arbgor.Staking.address}}',
curation: '{{arbgor.Curation.address}}',
rewardsManager: '{{arbgor.RewardsManager.address}}',
serviceRegistry: '{{arbgor.ServiceRegistry.address}}',
gns: '{{arbgor.GNS.address}}',
ens: '{{arbgor.IENS.address}}',
ensPublicResolver: '{{arbgor.IPublicResolver.address}}',
blockNumber: '',
bridgeBlockNumber: '',
network: '',
tokenLockManager: '',
subgraphNFT: '{{arbgor.SubgraphNFT.address}}',
l2GraphTokenGateway: '{{arbgor.L2GraphTokenGateway.address}}',
}

const main = (): void => {
try {
let output = JSON.parse(mustache.render(JSON.stringify(addresses), renameAddresses))
output.blockNumber = '1023264' // Protocol deployment
output.bridgeBlockNumber = '1023272' // Bridge deployment block
output.network = 'arbitrum-goerli'
output.useTokenLockManager = false
output.isL1 = false
fs.writeFileSync(__dirname + '/generatedAddresses.json', JSON.stringify(output, null, 2))
} catch (e) {
console.log(`Error saving artifacts: ${e.message}`)
}
}

main()
4 changes: 4 additions & 0 deletions config/goerliAddressScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ export let addresses: Addresses = {
ens: '{{goerli.IENS.address}}',
ensPublicResolver: '{{goerli.IPublicResolver.address}}',
blockNumber: '',
bridgeBlockNumber: '',
network: '',
tokenLockManager: '',
subgraphNFT: '{{goerli.SubgraphNFT.address}}',
l1GraphTokenGateway: '{{goerli.L1GraphTokenGateway.address}}',
}

const main = (): void => {
try {
let output = JSON.parse(mustache.render(JSON.stringify(addresses), renameAddresses))
output.blockNumber = '7210000' // Hardcoded from before first contract deploy of the latest phase
output.bridgeBlockNumber = '7891183' // Bridge deployment block
output.network = 'goerli'
output.tokenLockManager = '0x9a7a54e86560f4304d8862Ea00F45D1090c59ac8' // we don't have one, this is rinkebys'
output.useTokenLockManager = true
output.isL1 = true
fs.writeFileSync(__dirname + '/generatedAddresses.json', JSON.stringify(output, null, 2))
} catch (e) {
console.log(`Error saving artifacts: ${e.message}`)
Expand Down
4 changes: 4 additions & 0 deletions config/mainnetAddressScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ export let addresses: Addresses = {
ens: '{{mainnet.IENS.address}}',
ensPublicResolver: '{{mainnet.IPublicResolver.address}}',
blockNumber: '',
bridgeBlockNumber: '',
network: '',
tokenLockManager: '',
subgraphNFT: '{{mainnet.SubgraphNFT.address}}',
l1GraphTokenGateway: '{{goerli.L1GraphTokenGateway.address}}',
}

const main = (): void => {
try {
let output = JSON.parse(mustache.render(JSON.stringify(addresses), renameAddresses))
output.blockNumber = '11440000' // Hardcoded a few thousand blocks before 1st contract deployed
output.network = 'mainnet'
output.bridgeBlockNumber = '' // TBD
output.tokenLockManager = '0xFCf78AC094288D7200cfdB367A8CD07108dFa128'
output.useTokenLockManager = true
output.isL1 = true
fs.writeFileSync(__dirname + '/generatedAddresses.json', JSON.stringify(output, null, 2))
} catch (e) {
console.log(`Error saving artifacts: ${e.message}`)
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,27 @@
"deploy-rinkeby-no-ipfs": "yarn && yarn prep:no-ipfs && yarn prepare:rinkeby && graph deploy graphprotocol/graph-network-testnet --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy-goerli": "yarn && yarn prep:ipfs && yarn prepare:goerli && graph deploy graphprotocol/graph-network-goerli --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy-goerli-no-ipfs": "yarn && yarn prep:no-ipfs && yarn prepare:goerli && graph deploy graphprotocol/graph-network-goerli --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy-arbitrum-goerli": "yarn && yarn prep:ipfs && yarn prepare:arbitrum-goerli && graph deploy graphprotocol/graph-network-arbitrum-goerli --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy-arbitrum-goerli-no-ipfs": "yarn && yarn prep:no-ipfs && yarn prepare:arbitrum-goerli && graph deploy graphprotocol/graph-network-arbitrum-goerli --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy-testing": "yarn && yarn prep:<ipfs/no-ipfs> && yarn prepare:<mainnet/rinkeby/goerli> && graph deploy <subgraph name> --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"prep:addresses:rinkeby": "ts-node config/rinkebyAddressScript.ts && mustache ./config/generatedAddresses.json ./config/addresses.template.ts > ./config/addresses.ts",
"prepare:rinkeby": "yarn prep:addresses:rinkeby && mustache ./config/generatedAddresses.json subgraph.template.yaml > subgraph.yaml && graph codegen --output-dir src/types/",
"prep:addresses:goerli": "ts-node config/goerliAddressScript.ts && mustache ./config/generatedAddresses.json ./config/addresses.template.ts > ./config/addresses.ts",
"prepare:goerli": "yarn prep:addresses:goerli && mustache ./config/generatedAddresses.json subgraph.template.yaml > subgraph.yaml && graph codegen --output-dir src/types/",
"prepare:mainnet": "yarn prep:addresses:mainnet && mustache ./config/generatedAddresses.json subgraph.template.yaml > subgraph.yaml && graph codegen --output-dir src/types/",
"prep:addresses:mainnet": "ts-node config/mainnetAddressScript.ts && mustache ./config/generatedAddresses.json ./config/addresses.template.ts > ./config/addresses.ts",
"prep:addresses:arbitrum-goerli": "ts-node config/arbitrumGoerliAddressScript.ts && mustache ./config/generatedAddresses.json ./config/addresses.template.ts > ./config/addresses.ts",
"prepare:arbitrum-goerli": "yarn prep:addresses:arbitrum-goerli && mustache ./config/generatedAddresses.json subgraph.template.yaml > subgraph.yaml && graph codegen --output-dir src/types/",
"prep:ipfs": "mustache ./config/ipfs.json ./src/mappings/metadataHelpers.template.ts > ./src/mappings/metadataHelpers.ts",
"prep:no-ipfs": "mustache ./config/no-ipfs.json ./src/mappings/metadataHelpers.template.ts > ./src/mappings/metadataHelpers.ts",
"prepare:mainnet": "yarn prep:addresses:mainnet && mustache ./config/generatedAddresses.json subgraph.template.yaml > subgraph.yaml && graph codegen --output-dir src/types/",
"lint": "yarn eslint .",
"lint-fix": "eslint . --fix",
"prettier": "prettier '**/*.ts'",
"prettier-write": "prettier --write '**/*.ts'",
"test": "graph test -v 0.2.0"
},
"devDependencies": {
"@graphprotocol/contracts": "1.14.0",
"@graphprotocol/contracts": "^2.0.0-testnet",
"@graphprotocol/graph-cli": "^0.25.1",
"@graphprotocol/graph-ts": "^0.24.1",
"@types/node": "^14.0.13",
Expand Down
6 changes: 6 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ type GraphNetwork @entity {
minimumDisputeDeposit: BigInt!
"Reward to Fisherman on successful disputes. In parts per million"
fishermanRewardPercentage: Int!

# Bridge totals
"Total amount of GRT deposited to the L1 gateway. Only available on L1 networks."
totalGRTDeposited: BigInt!
"Total amount of GRT withdrawn from the L2 gateway. Only available on L2 networks."
totalGRTWithdrawn: BigInt!
}

"""
Expand Down
8 changes: 8 additions & 0 deletions src/mappings/l1Gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DepositInitiated } from '../types/L1GraphTokenGateway/L1GraphTokenGateway'
import { GraphNetwork } from '../types/schema'

export function handleDepositInitiated(event: DepositInitiated): void {
let graphNetwork = GraphNetwork.load('1')!
graphNetwork.totalGRTDeposited = graphNetwork.totalGRTDeposited.plus(event.params.amount)
graphNetwork.save()
}
8 changes: 8 additions & 0 deletions src/mappings/l2Gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { WithdrawalInitiated } from '../types/L1GraphTokenGateway/L2GraphTokenGateway'
import { GraphNetwork } from '../types/schema'

export function handleWithdrawalInitiated(event: WithdrawalInitiated): void {
let graphNetwork = GraphNetwork.load('1')!
graphNetwork.totalGRTWithdrawn = graphNetwork.totalGRTWithdrawn.plus(event.params.amount)
graphNetwork.save()
}
44 changes: 44 additions & 0 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,50 @@ dataSources:
- event: TokenDestinationAllowed(indexed address,bool)
handler: handleTokenDestinationAllowed
{{/useTokenLockManager}}
{{#isL1}}
- kind: ethereum/contract
name: L1GraphTokenGateway
network: {{network}}
source:
address: "{{l1GraphTokenGateway}}"
abi: L1GraphTokenGateway
startBlock: {{bridgeBlockNumber}}
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
file: ./src/mappings/l1Gateway.ts
entities:
- GraphNetwork
abis:
- name: L1GraphTokenGateway
file: ./node_modules/@graphprotocol/contracts/dist/abis/L1GraphTokenGateway.json
eventHandlers:
- event: DepositInitiated(address,indexed address,indexed address,indexed uint256,uint256)
handler: handleDepositInitiated
{{/isL1}}
{{^isL1}}
- kind: ethereum/contract
name: L2GraphTokenGateway
network: {{network}}
source:
address: "{{l2GraphTokenGateway}}"
abi: L2GraphTokenGateway
startBlock: {{bridgeBlockNumber}}
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
file: ./src/mappings/l2Gateway.ts
entities:
- GraphNetwork
abis:
- name: L2GraphTokenGateway
file: ./node_modules/@graphprotocol/contracts/dist/abis/L2GraphTokenGateway.json
eventHandlers:
- event: WithdrawalInitiated(address,indexed address,indexed address,indexed uint256,uint256,uint256)
handler: handleWithdrawalInitiated
{{/isL1}}
templates:
- kind: ethereum/contract
name: GraphTokenLockWallet
Expand Down
Loading

0 comments on commit 9d0067c

Please # to comment.