Skip to content

Commit

Permalink
tokenstandard + symbol (#111)
Browse files Browse the repository at this point in the history
* tokenstandard + symbol

* explicit token type check
  • Loading branch information
psparacino authored Feb 2, 2024
1 parent 276dfd1 commit f667689
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions carbonmark/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ type Category @entity {
id: String!
}

enum TokenStandard {
ERC20
ERC1155
}

type Listing @entity {
id: ID!
totalAmountToSell: BigInt!
Expand All @@ -62,6 +67,7 @@ type Listing @entity {
activities: [Activity!] @derivedFrom(field: "listing")
createdAt: BigInt
updatedAt: BigInt
tokenStandard: TokenStandard!
}

type Activity @entity {
Expand Down
22 changes: 17 additions & 5 deletions carbonmark/src/Carbonmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
import { ZERO_BI } from '../../lib/utils/Decimals'
import { ZERO_ADDRESS } from '../../lib/utils/Constants'
import { ERC20 } from '../generated/Carbonmark/ERC20'
import { ERC1155 } from '../generated/Carbonmark/ERC1155'
import { Bytes, log } from '@graphprotocol/graph-ts'

export function handleListingCreated(event: ListingCreated): void {
// Ensure the user entity exists
Expand All @@ -18,8 +20,21 @@ export function handleListingCreated(event: ListingCreated): void {

let listing = loadOrCreateListing(event.params.id.toHexString())

let tokenContract = ERC20.bind(event.params.token)
let tokenSymbol = tokenContract.try_symbol()
let ERC20TokenContract = ERC20.bind(event.params.token)
let ERC1155TokenContract = ERC1155.bind(event.params.token)

let tokenSymbol = ERC20TokenContract.try_symbol()
let interfaceID = ERC1155TokenContract.try_supportsInterface(Bytes.fromHexString('0xd9b67a26'))

if (!tokenSymbol.reverted) {
listing.tokenStandard = 'ERC20'
listing.tokenSymbol = tokenSymbol.value
} else if (!interfaceID.reverted) {
listing.tokenStandard = 'ERC1155'
listing.tokenSymbol = project.id
} else {
log.error('Token does not implement ERC20 or ERC1155', [])
}

listing.totalAmountToSell = event.params.amount
listing.leftToSell = event.params.amount
Expand All @@ -34,9 +49,6 @@ export function handleListingCreated(event: ListingCreated): void {
listing.seller = event.params.account
listing.createdAt = event.block.timestamp
listing.updatedAt = event.block.timestamp
if (!tokenSymbol.reverted) {
listing.tokenSymbol = tokenSymbol.value
}

listing.save()

Expand Down
1 change: 1 addition & 0 deletions carbonmark/src/Entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function loadOrCreateListing(id: string): Listing {
listing.createdAt = ZERO_BI
listing.updatedAt = ZERO_BI
listing.tokenSymbol = ''
listing.tokenStandard = ''
listing.save()
}
return listing
Expand Down
2 changes: 2 additions & 0 deletions carbonmark/subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dataSources:
file: ../lib/abis/Carbonmark.json
- name: ERC20
file: ../lib/abis/ERC20.json
- name: ERC1155
file: ../lib/abis/ERC1155.json
eventHandlers:
- event: ListingCreated(bytes32,address,address,uint256,uint256,uint256,uint256,uint256)
handler: handleListingCreated
Expand Down

0 comments on commit f667689

Please sign in to comment.