Skip to content

Commit

Permalink
feat: initial refactor of graphNetwork entity to hold L1 Block
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmardefago committed Jan 13, 2023
1 parent 218b704 commit 2a2da6d
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 106 deletions.
2 changes: 2 additions & 0 deletions config/addresses.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class Addresses {
l1GraphTokenGateway: string
l2GraphTokenGateway: string
ethereumDIDRegistry: string
isL1: boolean
}

// AS compiler does not like const
Expand All @@ -42,4 +43,5 @@ export let addresses: Addresses = {
l1GraphTokenGateway: '{{l1GraphTokenGateway}}',
l2GraphTokenGateway: '{{l2GraphTokenGateway}}',
ethereumDIDRegistry: '{{ethereumDIDRegistry}}',
isL1: {{isL1}},
}
2 changes: 1 addition & 1 deletion config/arbitrumGoerliAddressScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export let addresses: Addresses = {
l1GraphTokenGateway: '',
l2GraphTokenGateway: '{{arbgor.L2GraphTokenGateway.address}}',
ethereumDIDRegistry: '{{arbgor.IEthereumDIDRegistry.address}}',
isL1: false,
}

const main = (): void => {
Expand All @@ -37,7 +38,6 @@ const main = (): void => {
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}`)
Expand Down
2 changes: 1 addition & 1 deletion config/goerliAddressScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export let addresses: Addresses = {
l1GraphTokenGateway: '{{goerli.L1GraphTokenGateway.address}}',
l2GraphTokenGateway: '',
ethereumDIDRegistry: '{{goerli.IEthereumDIDRegistry.address}}',
isL1: true,
}

const main = (): void => {
Expand All @@ -38,7 +39,6 @@ const main = (): void => {
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
2 changes: 1 addition & 1 deletion config/mainnetAddressScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export let addresses: Addresses = {
l1GraphTokenGateway: '{{mainnet.L1GraphTokenGateway.address}}',
l2GraphTokenGateway: '',
ethereumDIDRegistry: '{{mainnet.IEthereumDIDRegistry.address}}',
isL1: true,
}

const main = (): void => {
Expand All @@ -38,7 +39,6 @@ const main = (): void => {
output.bridgeBlockNumber = '16083315' // 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
2 changes: 1 addition & 1 deletion config/mainnetArbitrumAddressScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export let addresses: Addresses = {
l1GraphTokenGateway: '',
l2GraphTokenGateway: '{{arbitrum.L2GraphTokenGateway.address}}',
ethereumDIDRegistry: '{{arbitrum.IEthereumDIDRegistry.address}}',
isL1: false,
}

const main = (): void => {
Expand All @@ -41,7 +42,6 @@ const main = (): void => {
if(output.ethereumDIDRegistry == '') {
output.ethereumDIDRegistry = '0xdCa7EF03e98e0DC2B855bE647C39ABe984fcF21B' // since the package doens't have it yet
}
output.isL1 = false
fs.writeFileSync(__dirname + '/generatedAddresses.json', JSON.stringify(output, null, 2))
} catch (e) {
console.log(`Error saving artifacts: ${e.message}`)
Expand Down
42 changes: 0 additions & 42 deletions config/rinkebyAddressScript.ts

This file was deleted.

2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ type GraphNetwork @entity {
totalGRTDepositedConfirmed: BigInt!
"Total amount of GRT withdrawn from the L2 gateway. Note that the actual amount claimed in L1 might be lower due to outbound transactions not finalized."
totalGRTWithdrawn: BigInt!
"Block number for L1. Only implemented for L2 deployments to properly reflect the L1 block used for timings"
currentL1BlockNumber: BigInt
}

"""
Expand Down
13 changes: 7 additions & 6 deletions src/mappings/curation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
batchUpdateSubgraphSignalledTokens,
} from './helpers'
import { zeroBD } from './utils'
import { addresses } from '../../config/addresses'

/**
* @dev handleStaked
Expand Down Expand Up @@ -52,7 +53,7 @@ export function handleSignalled(event: Signalled): void {
let signal = createOrLoadSignal(
id,
subgraphDeploymentID,
event.block.number.toI32(),
(addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32(),
event.block.timestamp.toI32(),
)

Expand All @@ -66,7 +67,7 @@ export function handleSignalled(event: Signalled): void {

signal.signal = signal.signal.plus(event.params.signal)
signal.lastUpdatedAt = event.block.timestamp.toI32()
signal.lastUpdatedAtBlock = event.block.number.toI32()
signal.lastUpdatedAtBlock = (addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32()
signal.averageCostBasis = signal.averageCostBasis.plus(event.params.tokens.toBigDecimal())

let gnsSignalNewAmount = signal.signal.toBigDecimal()
Expand Down Expand Up @@ -110,7 +111,7 @@ export function handleSignalled(event: Signalled): void {
batchUpdateSubgraphSignalledTokens(deployment as SubgraphDeployment)

// Update epoch
let epoch = createOrLoadEpoch(event.block.number)
let epoch = createOrLoadEpoch((addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!))
epoch.signalledTokens = epoch.signalledTokens.plus(
event.params.tokens.minus(event.params.curationTax),
)
Expand Down Expand Up @@ -146,7 +147,7 @@ export function handleSignalled(event: Signalled): void {
let signalTransaction = new SignalTransaction(
event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()),
)
signalTransaction.blockNumber = event.block.number.toI32()
signalTransaction.blockNumber = (addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32()
signalTransaction.timestamp = event.block.timestamp.toI32()
signalTransaction.signer = event.params.curator.toHexString()
signalTransaction.type = 'MintSignal'
Expand Down Expand Up @@ -179,7 +180,7 @@ export function handleBurned(event: Burned): void {
// Note - if you immediately deposited and then withdrew, you would lose 5%, and you were
// realize this loss by seeing unsignaled tokens being 95 and signalled 100
signal.lastUpdatedAt = event.block.timestamp.toI32()
signal.lastUpdatedAtBlock = event.block.number.toI32()
signal.lastUpdatedAtBlock = (addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32()
signal.unsignalledTokens = signal.unsignalledTokens.plus(event.params.tokens)
signal.signal = signal.signal.minus(event.params.signal)

Expand Down Expand Up @@ -269,7 +270,7 @@ export function handleBurned(event: Burned): void {
let signalTransaction = new SignalTransaction(
event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()),
)
signalTransaction.blockNumber = event.block.number.toI32()
signalTransaction.blockNumber = (addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32()
signalTransaction.timestamp = event.block.timestamp.toI32()
signalTransaction.signer = event.params.curator.toHexString()
signalTransaction.type = 'BurnSignal'
Expand Down
3 changes: 2 additions & 1 deletion src/mappings/disputeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DisputeManager,
} from '../types/DisputeManager/DisputeManager'
import { DisputeManagerStitched } from '../types/DisputeManager/DisputeManagerStitched'
import { createOrLoadGraphNetwork } from './helpers'

// This handles Single query and Conflicting disputes
export function handleQueryDisputeCreated(event: QueryDisputeCreated): void {
Expand Down Expand Up @@ -150,7 +151,7 @@ export function handleDisputeLinked(event: DisputeLinked): void {
*/
export function handleParameterUpdated(event: ParameterUpdated): void {
let parameter = event.params.param
let graphNetwork = GraphNetwork.load('1')!
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
let disputeManager = DisputeManagerStitched.bind(event.address as Address)

if (parameter == 'arbitrator') {
Expand Down
13 changes: 7 additions & 6 deletions src/mappings/epochManager.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { GraphNetwork } from '../types/schema'
import { EpochRun, EpochLengthUpdate } from '../types/EpochManager/EpochManager'
import { createOrLoadEpoch, createEpoch } from './helpers'
import { createOrLoadEpoch, createEpoch, createOrLoadGraphNetwork } from './helpers'
import { addresses } from '../../config/addresses'

/**
* @dev handleEpochRun
* - updates the last run epoch
*/
export function handleEpochRun(event: EpochRun): void {
let graphNetwork = GraphNetwork.load('1')!
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
graphNetwork.lastRunEpoch = event.params.epoch.toI32()
graphNetwork.save()
}
Expand All @@ -17,26 +18,26 @@ export function handleEpochRun(event: EpochRun): void {
* - updates the length and the last block and epoch it happened
*/
export function handleEpochLengthUpdate(event: EpochLengthUpdate): void {
let graphNetwork = GraphNetwork.load('1')!
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)

// This event is emitted on EpochManagers constructor, so it has some special logic to handle
// initialization here
if (graphNetwork.epochLength == 0) {
// Will only ever be 0 on initialization in the contracts
graphNetwork.epochLength = event.params.epochLength.toI32()
graphNetwork.lastLengthUpdateBlock = event.block.number.toI32()
graphNetwork.lastLengthUpdateBlock = (addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32()
graphNetwork.currentEpoch = 0
graphNetwork.epochCount = 1
graphNetwork.lastLengthUpdateEpoch = graphNetwork.currentEpoch
graphNetwork.save()

createEpoch(event.block.number.toI32(), graphNetwork.epochLength, graphNetwork.currentEpoch)
createEpoch((addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32(), graphNetwork.epochLength, graphNetwork.currentEpoch)
// return here so it doesn't run the normal handler
return
}

// This returns a new epoch, or current epoch, with the old epoch length
let epoch = createOrLoadEpoch(event.block.number)
let epoch = createOrLoadEpoch((addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!))

// Check that the endBlock for the current epoch match what it should based on the
// changed epoch length
Expand Down
28 changes: 17 additions & 11 deletions src/mappings/gns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ import {
duplicateOrUpdateSubgraphWithNewID,
duplicateOrUpdateSubgraphVersionWithNewID,
duplicateOrUpdateNameSignalWithNewID,
createOrLoadGraphNetwork
} from './helpers'
import { fetchSubgraphMetadata, fetchSubgraphVersionMetadata } from './metadataHelpers'
import { addresses } from '../../config/addresses'

export function handleSetDefaultName(event: SetDefaultName): void {
let graphAccount = createOrLoadGraphAccount(event.params.graphAccount, event.block.timestamp)
Expand Down Expand Up @@ -303,7 +305,7 @@ export function handleSubgraphDeprecated(event: SubgraphDeprecated): void {
let subgraphDuplicate = duplicateOrUpdateSubgraphWithNewID(subgraph, oldID, 1)
subgraphDuplicate.save()

let graphNetwork = GraphNetwork.load('1')!
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
graphNetwork.activeSubgraphCount = graphNetwork.activeSubgraphCount - 1
graphNetwork.save()

Expand Down Expand Up @@ -333,6 +335,7 @@ export function handleNameSignalEnabled(event: NameSignalEnabled): void {
}

export function handleNSignalMinted(event: NSignalMinted): void {
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
let curatorID = event.params.nameCurator.toHexString()
let oldID = joinID([
event.params.graphAccount.toHexString(),
Expand Down Expand Up @@ -433,7 +436,7 @@ export function handleNSignalMinted(event: NSignalMinted): void {
curator.activeCombinedSignalCount = curator.activeCombinedSignalCount + 1

if (curator.activeCombinedSignalCount == 1) {
let graphNetwork = GraphNetwork.load('1')!
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
graphNetwork.activeCuratorCount = graphNetwork.activeCuratorCount + 1
graphNetwork.save()
}
Expand All @@ -444,7 +447,7 @@ export function handleNSignalMinted(event: NSignalMinted): void {
let nSignalTransaction = new NameSignalTransaction(
event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()),
)
nSignalTransaction.blockNumber = event.block.number.toI32()
nSignalTransaction.blockNumber = (addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32()
nSignalTransaction.timestamp = event.block.timestamp.toI32()
nSignalTransaction.signer = event.params.nameCurator.toHexString()
nSignalTransaction.type = 'MintNSignal'
Expand All @@ -456,6 +459,7 @@ export function handleNSignalMinted(event: NSignalMinted): void {
}

export function handleNSignalBurned(event: NSignalBurned): void {
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
let curatorID = event.params.nameCurator.toHexString()
let oldID = joinID([
event.params.graphAccount.toHexString(),
Expand Down Expand Up @@ -554,7 +558,7 @@ export function handleNSignalBurned(event: NSignalBurned): void {
curator.activeCombinedSignalCount = curator.activeCombinedSignalCount - 1

if (curator.activeCombinedSignalCount == 0) {
let graphNetwork = GraphNetwork.load('1')!
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
graphNetwork.activeCuratorCount = graphNetwork.activeCuratorCount - 1
graphNetwork.save()
}
Expand All @@ -566,7 +570,7 @@ export function handleNSignalBurned(event: NSignalBurned): void {
let nSignalTransaction = new NameSignalTransaction(
event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()),
)
nSignalTransaction.blockNumber = event.block.number.toI32()
nSignalTransaction.blockNumber = (addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32()
nSignalTransaction.timestamp = event.block.timestamp.toI32()
nSignalTransaction.signer = event.params.nameCurator.toHexString()
nSignalTransaction.type = 'BurnNSignal'
Expand Down Expand Up @@ -726,7 +730,7 @@ export function handleGRTWithdrawn(event: GRTWithdrawn): void {
*/
export function handleParameterUpdated(event: ParameterUpdated): void {
let parameter = event.params.param
let graphNetwork = GraphNetwork.load('1')!
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
let gns = GNS.bind(event.address)

if (parameter == 'ownerTaxPercentage') {
Expand Down Expand Up @@ -804,7 +808,7 @@ export function handleSubgraphDeprecatedV2(event: SubgraphDeprecated1): void {
subgraphDuplicate.save()
}

let graphNetwork = GraphNetwork.load('1')!
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
graphNetwork.activeSubgraphCount = graphNetwork.activeSubgraphCount - 1
graphNetwork.save()

Expand Down Expand Up @@ -857,6 +861,7 @@ export function handleSubgraphMetadataUpdatedV2(event: SubgraphMetadataUpdated1)
// handler: handleNSignalMintedV2

export function handleNSignalMintedV2(event: SignalMinted): void {
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
let curatorID = event.params.curator.toHexString()
let bigIntID = event.params.subgraphID
let subgraphID = convertBigIntSubgraphIDToBase58(bigIntID)
Expand Down Expand Up @@ -959,7 +964,7 @@ export function handleNSignalMintedV2(event: SignalMinted): void {
curator.activeCombinedSignalCount = curator.activeCombinedSignalCount + 1

if (curator.activeCombinedSignalCount == 1) {
let graphNetwork = GraphNetwork.load('1')!
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
graphNetwork.activeCuratorCount = graphNetwork.activeCuratorCount + 1
graphNetwork.save()
}
Expand All @@ -970,7 +975,7 @@ export function handleNSignalMintedV2(event: SignalMinted): void {
let nSignalTransaction = new NameSignalTransaction(
event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()),
)
nSignalTransaction.blockNumber = event.block.number.toI32()
nSignalTransaction.blockNumber = (addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32()
nSignalTransaction.timestamp = event.block.timestamp.toI32()
nSignalTransaction.signer = event.params.curator.toHexString()
nSignalTransaction.type = 'MintNSignal'
Expand All @@ -985,6 +990,7 @@ export function handleNSignalMintedV2(event: SignalMinted): void {
// handler: handleNSignalBurnedV2

export function handleNSignalBurnedV2(event: SignalBurned): void {
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
let bigIntID = event.params.subgraphID
let subgraphID = convertBigIntSubgraphIDToBase58(bigIntID)
let subgraph = Subgraph.load(subgraphID)!
Expand Down Expand Up @@ -1084,7 +1090,7 @@ export function handleNSignalBurnedV2(event: SignalBurned): void {
curator.activeCombinedSignalCount = curator.activeCombinedSignalCount - 1

if (curator.activeCombinedSignalCount == 0) {
let graphNetwork = GraphNetwork.load('1')!
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
graphNetwork.activeCuratorCount = graphNetwork.activeCuratorCount - 1
graphNetwork.save()
}
Expand All @@ -1096,7 +1102,7 @@ export function handleNSignalBurnedV2(event: SignalBurned): void {
let nSignalTransaction = new NameSignalTransaction(
event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString()),
)
nSignalTransaction.blockNumber = event.block.number.toI32()
nSignalTransaction.blockNumber = (addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!).toI32()
nSignalTransaction.timestamp = event.block.timestamp.toI32()
nSignalTransaction.signer = event.params.curator.toHexString()
nSignalTransaction.type = 'BurnNSignal'
Expand Down
Loading

0 comments on commit 2a2da6d

Please sign in to comment.