Skip to content

Commit

Permalink
Merge branch 'main' into erc4906
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Jan 16, 2024
2 parents 10947c6 + 71405d6 commit f16c836
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 83 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* `Governor`: fix bug caused by duplicate write of immutable entity `ProposalSupport` ([#47](https://github.com/OpenZeppelin/openzeppelin-subgraphs/pull/47))
* `Governor`: rename `startBlock` to `voteStart` and `endBlock` to `voteEnd` ([#60](https://github.com/OpenZeppelin/openzeppelin-subgraphs/pull/60))
* `ERC721`: add support for `ERC4906` events ([#60](https://github.com/OpenZeppelin/openzeppelin-subgraphs/pull/60))
* `ERC20`: remove approval tracking as it cannot be consistently tracked during `transferFrom` calls ([#59](https://github.com/OpenZeppelin/openzeppelin-subgraphs/

Please note that this is a breaking release: some elements of the GraphQL API are renamed or removed.

Expand Down
11 changes: 0 additions & 11 deletions generated/all.schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ type Account @entity {
ERC1967ImplementationOfUpgraded: [ERC1967ImplementationUpgraded!]! @derivedFrom(field: "implementation")
asERC20: ERC20Contract
ERC20balances: [ERC20Balance!]! @derivedFrom(field: "account")
ERC20approvalsOwner: [ERC20Approval!]! @derivedFrom(field: "owner")
ERC20approvalsSpender: [ERC20Approval!]! @derivedFrom(field: "spender")
ERC20transferFromEvent: [ERC20Transfer!]! @derivedFrom(field: "from")
ERC20transferToEvent: [ERC20Transfer!]! @derivedFrom(field: "to")
asERC721: ERC721Contract
Expand Down Expand Up @@ -187,7 +185,6 @@ type ERC20Contract @entity(immutable: true) {
decimals: Int!
totalSupply: ERC20Balance!
balances: [ERC20Balance!]! @derivedFrom(field: "contract")
approvals: [ERC20Approval!]! @derivedFrom(field: "contract")
transfers: [ERC20Transfer!]! @derivedFrom(field: "contract")
}
type ERC20Balance @entity {
Expand All @@ -199,14 +196,6 @@ type ERC20Balance @entity {
transferFromEvent: [ERC20Transfer!]! @derivedFrom(field: "fromBalance")
transferToEvent: [ERC20Transfer!]! @derivedFrom(field: "toBalance")
}
type ERC20Approval @entity {
id: ID!
contract: ERC20Contract!
owner: Account!
spender: Account!
value: BigDecimal!
valueExact: BigInt!
}
type ERC20Transfer implements Event @entity(immutable: true) {
id: ID!
emitter: Account!
Expand Down
11 changes: 0 additions & 11 deletions generated/erc20.schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ type Account @entity {
id: Bytes!
asERC20: ERC20Contract
ERC20balances: [ERC20Balance!]! @derivedFrom(field: "account")
ERC20approvalsOwner: [ERC20Approval!]! @derivedFrom(field: "owner")
ERC20approvalsSpender: [ERC20Approval!]! @derivedFrom(field: "spender")
ERC20transferFromEvent: [ERC20Transfer!]! @derivedFrom(field: "from")
ERC20transferToEvent: [ERC20Transfer!]! @derivedFrom(field: "to")
events: [Event!]! @derivedFrom(field: "emitter")
Expand All @@ -16,7 +14,6 @@ type ERC20Contract @entity(immutable: true) {
decimals: Int!
totalSupply: ERC20Balance!
balances: [ERC20Balance!]! @derivedFrom(field: "contract")
approvals: [ERC20Approval!]! @derivedFrom(field: "contract")
transfers: [ERC20Transfer!]! @derivedFrom(field: "contract")
}
type ERC20Balance @entity {
Expand All @@ -28,14 +25,6 @@ type ERC20Balance @entity {
transferFromEvent: [ERC20Transfer!]! @derivedFrom(field: "fromBalance")
transferToEvent: [ERC20Transfer!]! @derivedFrom(field: "toBalance")
}
type ERC20Approval @entity {
id: ID!
contract: ERC20Contract!
owner: Account!
spender: Account!
value: BigDecimal!
valueExact: BigInt!
}
type ERC20Transfer implements Event @entity(immutable: true) {
id: ID!
emitter: Account!
Expand Down
12 changes: 0 additions & 12 deletions src/datasources/erc20.gql.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
{ "name": "id", "type": "Bytes!" },
{ "name": "asERC20", "type": "ERC20Contract" },
{ "name": "ERC20balances", "type": "ERC20Balance!", "derived": "account" },
{ "name": "ERC20approvalsOwner", "type": "ERC20Approval!", "derived": "owner" },
{ "name": "ERC20approvalsSpender", "type": "ERC20Approval!", "derived": "spender" },
{ "name": "ERC20transferFromEvent", "type": "ERC20Transfer!", "derived": "from" },
{ "name": "ERC20transferToEvent", "type": "ERC20Transfer!", "derived": "to" }
]
Expand All @@ -20,7 +18,6 @@
{ "name": "decimals", "type": "Int!" },
{ "name": "totalSupply", "type": "ERC20Balance!" },
{ "name": "balances", "type": "ERC20Balance!", "derived": "contract" },
{ "name": "approvals", "type": "ERC20Approval!", "derived": "contract" },
{ "name": "transfers", "type": "ERC20Transfer!", "derived": "contract" }
]
},{
Expand All @@ -33,15 +30,6 @@
{ "name": "transferFromEvent", "type": "ERC20Transfer!", "derived": "fromBalance" },
{ "name": "transferToEvent", "type": "ERC20Transfer!", "derived": "toBalance" }
]
},{
"name": "ERC20Approval",
"fields": [
{ "name": "contract", "type": "ERC20Contract!" },
{ "name": "owner", "type": "Account!" },
{ "name": "spender", "type": "Account!" },
{ "name": "value", "type": "BigDecimal!" },
{ "name": "valueExact", "type": "BigInt!" }
]
},{
"name": "ERC20Transfer",
"parent": "Event",
Expand Down
24 changes: 0 additions & 24 deletions src/datasources/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {

import {
Transfer as TransferEvent,
Approval as ApprovalEvent,
} from '../../generated/erc20/IERC20'

import {
Expand All @@ -24,7 +23,6 @@ import {
import {
fetchERC20,
fetchERC20Balance,
fetchERC20Approval,
} from '../fetch/erc20'

export function handleTransfer(event: TransferEvent): void {
Expand Down Expand Up @@ -70,25 +68,3 @@ export function handleTransfer(event: TransferEvent): void {
}
ev.save()
}

export function handleApproval(event: ApprovalEvent): void {
let contract = fetchERC20(event.address)

let owner = fetchAccount(event.params.owner)
let spender = fetchAccount(event.params.spender)
let approval = fetchERC20Approval(contract, owner, spender)
approval.valueExact = event.params.value
approval.value = decimals.toDecimals(event.params.value, contract.decimals)
approval.save()

// let ev = new ERC20ApprovalEvent(events.id(event))
// ev.emitter = contract.id
// ev.transaction = transactions.log(event).id
// ev.timestamp = event.block.timestamp
// ev.token = token.id
// ev.owner = owner.id
// ev.spender = spender.id
// ev.approval = approval.id
// ev.value = value.value
// ev.save()
}
2 changes: 0 additions & 2 deletions src/datasources/erc20.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
- name: IERC20
file: {root}/node_modules/@openzeppelin/contracts/build/contracts/IERC20Metadata.json
eventHandlers:
- event: Approval(indexed address,indexed address,uint256)
handler: handleApproval
- event: Transfer(indexed address,indexed address,uint256)
handler: handleTransfer
file: {file}
33 changes: 30 additions & 3 deletions src/datasources/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import {
Transfer as TransferEvent,
} from '../../generated/erc721/IERC721'

import {
ConsecutiveTransfer as ConsecutiveTransfer,
} from '../../generated/erc721-concecutive/IERC2309'

import {
MetadataUpdate as MetadataUpdateEvent,
BatchMetadataUpdate as BatchMetadataUpdateEvent,
} from '../../generated/erc721/IERC4906'
} from '../../generated/erc721-metadataupdate/IERC4906'

import {
events,
Expand All @@ -43,8 +47,6 @@ export function handleTransfer(event: TransferEvent): void {

token.owner = to.id
token.approval = fetchAccount(Address.zero()).id // implicit approval reset on transfer

contract.save()
token.save()

let ev = new ERC721Transfer(events.id(event))
Expand All @@ -59,6 +61,31 @@ export function handleTransfer(event: TransferEvent): void {
}
}

export function handleConsecutiveTransfer(event: ConsecutiveTransfer): void {
let contract = fetchERC721(event.address)
if (contract != null) {
let from = fetchAccount(event.params.fromAddress)
let to = fetchAccount(event.params.toAddress)

for (let tokenId = event.params.fromTokenId.toU64(); tokenId <= event.params.toTokenId.toU64(); ++tokenId) {
let token = fetchERC721Token(contract, BigInt.fromU64(tokenId))
token.owner = to.id
token.approval = fetchAccount(Address.zero()).id // implicit approval reset on transfer
token.save()

let ev = new ERC721Transfer(events.id(event).concat('-').concat(tokenId.toString()))
ev.emitter = contract.id
ev.transaction = transactions.log(event).id
ev.timestamp = event.block.timestamp
ev.contract = contract.id
ev.token = token.id
ev.from = from.id
ev.to = to.id
ev.save()
}
}
}

export function handleApproval(event: ApprovalEvent): void {
let contract = fetchERC721(event.address)
if (contract != null) {
Expand Down
42 changes: 39 additions & 3 deletions src/datasources/erc721.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
network: {chain}
source:
address: "{address}"
abi: IERC4906
abi: IERC721
startBlock: {startBlock}
mapping:
kind: ethereum/events
Expand All @@ -14,15 +14,51 @@
abis:
- name: IERC721
file: {root}/node_modules/@openzeppelin/contracts/build/contracts/IERC721Metadata.json
- name: IERC4906
file: {root}/node_modules/@openzeppelin/contracts/build/contracts/IERC4906.json
eventHandlers:
- event: Approval(indexed address,indexed address,indexed uint256)
handler: handleApproval
- event: ApprovalForAll(indexed address,indexed address,bool)
handler: handleApprovalForAll
- event: Transfer(indexed address,indexed address,indexed uint256)
handler: handleTransfer
file: {file}
- kind: ethereum/contract
name: {id}-concecutive
network: {chain}
source:
address: "{address}"
abi: IERC2309
startBlock: {startBlock}
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- ERC721Contract
abis:
- name: IERC2309
file: {root}/node_modules/@openzeppelin/contracts/build/contracts/IERC2309.json
eventHandlers:
- event: ConsecutiveTransfer(indexed uint256,uint256,indexed address,indexed address)
handler: handleConsecutiveTransfer
file: {file}
- kind: ethereum/contract
name: {id}-metadataupdate
network: {chain}
source:
address: "{address}"
abi: IERC4906
startBlock: {startBlock}
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- ERC721Contract
abis:
- name: IERC4906
file: {root}/node_modules/@openzeppelin/contracts/build/contracts/IERC4906.json
eventHandlers:
- event: MetadataUpdate(uint256)
handler: handleMetadataUpdate
- event: BatchMetadataUpdate(uint256,uint256)
Expand Down
17 changes: 0 additions & 17 deletions src/fetch/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Account,
ERC20Contract,
ERC20Balance,
ERC20Approval,
} from '../../generated/schema'

import {
Expand Down Expand Up @@ -62,19 +61,3 @@ export function fetchERC20Balance(contract: ERC20Contract, account: Account | nu

return balance as ERC20Balance
}

export function fetchERC20Approval(contract: ERC20Contract, owner: Account, spender: Account): ERC20Approval {
let id = contract.id.toHex().concat('/').concat(owner.id.toHex()).concat('/').concat(spender.id.toHex())
let approval = ERC20Approval.load(id)

if (approval == null) {
approval = new ERC20Approval(id)
approval.contract = contract.id
approval.owner = owner.id
approval.spender = spender.id
approval.value = constants.BIGDECIMAL_ZERO
approval.valueExact = constants.BIGINT_ZERO
}

return approval as ERC20Approval
}

0 comments on commit f16c836

Please sign in to comment.