Skip to content

Commit

Permalink
Merge pull request #12 from graphprotocol/juanmardefago/subgraph-tran…
Browse files Browse the repository at this point in the history
…sfer

feat: add support for Subgraph Transfer event
  • Loading branch information
juanmardefago authored Feb 26, 2024
2 parents 0d61365 + 0bfbfda commit 965a863
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
31 changes: 31 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type CounterEntity @entity {
subgraphL2TransferFinalizedEventCount: BigInt!
curatorBalanceReceivedEventCount: BigInt!
curatorBalanceReturnedToBeneficiaryEventCount: BigInt!
subgraphTransferredEventCount: BigInt!
#interface counters
eventCount: BigInt!
subgraphEventCount: BigInt!
Expand Down Expand Up @@ -595,6 +596,36 @@ type SubgraphL2TransferFinalizedEvent implements SubgraphEvent & Event @entity(i
tx_cumulativeGasUsed: BigInt!
}

type SubgraphTransferredEvent implements SubgraphEvent & GraphAccountEvent & Event
@entity(immutable: true) {
id: ID!

timestamp: BigInt!

blockNumber: BigInt!

tx_hash: Bytes!

typename: String!

subgraph: Subgraph!

from: GraphAccount!

to: GraphAccount!

"User that triggered the Event"
accounts: [GraphAccount!]!

tx_gasLimit: BigInt!

tx_gasPrice: BigInt!

tx_gasUsed: BigInt!

tx_cumulativeGasUsed: BigInt!
}

# Profile events (includes Indexer, Curator and Delegator events)

interface GraphAccountEvent {
Expand Down
47 changes: 47 additions & 0 deletions src/mappings/gns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
SubgraphVersionUpdated,
LegacySubgraphClaimed,
GNS,
Transfer,
} from '../types/GNS/GNS'

import {
Expand All @@ -41,6 +42,7 @@ import {
ParameterUpdatedEvent,
SetDefaultNameEvent,
SubgraphVersionMetadataUpdatedEvent,
SubgraphTransferredEvent,
} from '../types/schema'

import {
Expand Down Expand Up @@ -885,3 +887,48 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi
export function handleLegacySubgraphClaimed(event: LegacySubgraphClaimed): void {
let eventId = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString())
}

// - event: Transfer(indexed address,indexed address,indexed uint256)
// handler: handleTransfer

export function handleTransfer(event: Transfer): void {
let eventId = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString())
let newOwner = createOrLoadGraphAccount(event.params.to.toHexString())
let oldOwner = createOrLoadGraphAccount(event.params.from.toHexString())
let subgraphID = convertBigIntSubgraphIDToBase58(event.params.tokenId)
let accounts = new Array<String>()
accounts.push(oldOwner.id)
accounts.push(newOwner.id)

// Update subgraph v2
let subgraph = createOrLoadSubgraph(
subgraphID,
event.transaction.from
)
subgraph.owner = newOwner.id
subgraph.save()

//todo
let otherEventEntity = new SubgraphTransferredEvent(eventId)
otherEventEntity.timestamp = event.block.timestamp
otherEventEntity.tx_gasLimit = event.transaction.gasLimit
otherEventEntity.tx_gasPrice = event.transaction.gasPrice
otherEventEntity.tx_gasUsed = event.receipt!.gasUsed
otherEventEntity.tx_cumulativeGasUsed = event.receipt!.cumulativeGasUsed
otherEventEntity.blockNumber = event.block.number
otherEventEntity.tx_hash = event.transaction.hash
otherEventEntity.typename = 'SubgraphTransferredEvent'
otherEventEntity.subgraph = subgraph.id
otherEventEntity.from = oldOwner.id
otherEventEntity.to = newOwner.id
otherEventEntity.accounts = accounts
otherEventEntity.save()

let counter = getCounter()
counter.subgraphEventCount = counter.subgraphEventCount.plus(BIGINT_ONE)
counter.graphAccountEventCount = counter.graphAccountEventCount.plus(BIGINT_ONE)
counter.eventCount = counter.eventCount.plus(BIGINT_ONE)
counter.subgraphTransferredEventCount = counter.subgraphTransferredEventCount.plus(BIGINT_ONE)
counter.save()
}

1 change: 1 addition & 0 deletions src/mappings/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function getCounter(): CounterEntity {
counter.subgraphL2TransferFinalizedEventCount = BIGINT_ZERO
counter.curatorBalanceReceivedEventCount = BIGINT_ZERO
counter.curatorBalanceReturnedToBeneficiaryEventCount = BIGINT_ZERO
counter.subgraphTransferredEventCount = BIGINT_ZERO
// Interface counters
counter.eventCount = BIGINT_ZERO
counter.subgraphEventCount = BIGINT_ZERO
Expand Down
27 changes: 27 additions & 0 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@ dataSources:
handler: handleCuratorBalanceReturnedToBeneficiary
receipt: true
{{/isL1}}
- kind: ethereum/contract
name: SubgraphNFT
network: {{network}}
source:
address: "{{subgraphNFT}}"
abi: SubgraphNFT
startBlock: {{blockNumber}}
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
file: ./src/mappings/gns.ts
entities:
- Subgraph
- SubgraphVersion
- SubgraphDeployment
- GraphAccount
- NameSignal
abis:
- name: SubgraphNFT
file: ./node_modules/@graphprotocol/contracts/dist/abis/SubgraphNFT.json
- name: EpochManager
file: ./node_modules/@graphprotocol/contracts/dist/abis/EpochManager.json
eventHandlers:
- event: Transfer(indexed address,indexed address,indexed uint256)
handler: handleTransfer
receipt: true
- kind: ethereum/contract
name: ServiceRegistry
network: {{network}}
Expand Down

0 comments on commit 965a863

Please sign in to comment.