Skip to content

Commit

Permalink
ON-509: tracking erc1155
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lima committed Oct 26, 2023
1 parent 9c22442 commit 4de49ab
Show file tree
Hide file tree
Showing 25 changed files with 691 additions and 172 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const config = require('@oriumnetwork/orium-commons/lint/eslint-config')
config.rules['@typescript-eslint/ban-types'] = ['warn', { types: { BigInt: false }, extendDefaults: true }]
config.rules['@typescript-eslint-no-non-null-assertion'] = ['off']
module.exports = config
325 changes: 325 additions & 0 deletions abis/ERC1155.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
[
{
"inputs": [
{
"internalType": "string",
"name": "uri_",
"type": "string"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "operator",
"type": "address"
},
{
"indexed": false,
"internalType": "bool",
"name": "approved",
"type": "bool"
}
],
"name": "ApprovalForAll",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "operator",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256[]",
"name": "ids",
"type": "uint256[]"
},
{
"indexed": false,
"internalType": "uint256[]",
"name": "values",
"type": "uint256[]"
}
],
"name": "TransferBatch",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "operator",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "TransferSingle",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "string",
"name": "value",
"type": "string"
},
{
"indexed": true,
"internalType": "uint256",
"name": "id",
"type": "uint256"
}
],
"name": "URI",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
},
{
"internalType": "uint256",
"name": "id",
"type": "uint256"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "accounts",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "ids",
"type": "uint256[]"
}
],
"name": "balanceOfBatch",
"outputs": [
{
"internalType": "uint256[]",
"name": "",
"type": "uint256[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
},
{
"internalType": "address",
"name": "operator",
"type": "address"
}
],
"name": "isApprovedForAll",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "from",
"type": "address"
},
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256[]",
"name": "ids",
"type": "uint256[]"
},
{
"internalType": "uint256[]",
"name": "amounts",
"type": "uint256[]"
},
{
"internalType": "bytes",
"name": "data",
"type": "bytes"
}
],
"name": "safeBatchTransferFrom",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "from",
"type": "address"
},
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "data",
"type": "bytes"
}
],
"name": "safeTransferFrom",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "operator",
"type": "address"
},
{
"internalType": "bool",
"name": "approved",
"type": "bool"
}
],
"name": "setApprovalForAll",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes4",
"name": "interfaceId",
"type": "bytes4"
}
],
"name": "supportsInterface",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "uri",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
}
]
7 changes: 7 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
enum NftType {
ERC721
ERC1155
}

type Nft @entity {
id: ID! # tokenId + tokenAddress
tokenAddress: String!
tokenId: BigInt!
owner: Account!
type: NftType!
amount: BigInt
roles: [Role!] @derivedFrom(field: "nft")
}

Expand Down
2 changes: 2 additions & 0 deletions src/erc1155/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { handleTransferBatch } from './transfer-batch-handler'
export { handleTransferSingle } from './transfer-single-handler'
41 changes: 41 additions & 0 deletions src/erc1155/transfer-batch-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { log } from '@graphprotocol/graph-ts'
import { TransferBatch } from '../../generated/ERC1155/ERC1155'
import { upsertERC1155Nft } from '../../utils'

/**
@dev This handler is called when a token is transferred.
@param event TransferBatch The event emitted by the contract.
Example:
emit TransferBatch(operator, from, to, ids, amounts);
*/
export function handleTransferBatch(event: TransferBatch): void {
const tokenAddress = event.address.toHex()
const tokenIds = event.params.ids
const fromAddress = event.params.to.toHex()
const toAddress = event.params.to.toHex()
const amounts = event.params.values

if (tokenIds.length != amounts.length) {
log.error('[erc1155][handleTransferBatch] tokenIds length {} does not match amounts length {}, tx {}', [
tokenIds.length.toString(),
amounts.length.toString(),
event.transaction.hash.toHex(),
])
}

for (let i = 0; i < tokenIds.length; i++) {
const tokenId = tokenIds[i]
const amount = amounts[i]

const nft = upsertERC1155Nft(tokenAddress, tokenId, amount, fromAddress, toAddress)

log.warning('[erc1155][handleTransferBatch] NFT {} amount {} transferred from {} to {} tx {}', [
nft.id,
amount.toString(),
fromAddress,
toAddress,
event.transaction.hash.toHex(),
])
}
}
Loading

0 comments on commit 4de49ab

Please sign in to comment.