Skip to content

Commit

Permalink
use token listing event
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Rodrigues Lordello authored and nlordell committed Sep 29, 2020
1 parent ea5b6fe commit dc72b4b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 71 deletions.
3 changes: 1 addition & 2 deletions config/ganache.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"network": "mainnet",
"address": "0x9561C133DD8580860B6b7E504bC5Aa500f0f06a7",
"indexCalls": false
"address": "0x9561C133DD8580860B6b7E504bC5Aa500f0f06a7"
}
3 changes: 1 addition & 2 deletions config/mainnet.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"network": "mainnet",
"address": "0x6F400810b62df8E13fded51bE75fF5393eaa841F",
"startBlock": 9340147,
"indexCalls": true
"startBlock": 9340147
}
3 changes: 1 addition & 2 deletions config/rinkeby.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"network": "rinkeby",
"address": "0xC576eA7bd102F7E476368a5E98FA455d1Ea34dE2",
"startBlock": 5844678,
"indexCalls": false
"startBlock": 5844678
}
3 changes: 1 addition & 2 deletions config/xdai.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"network": "xdai",
"address": "0x25B06305CC4ec6AfCF3E7c0b673da1EF8ae26313",
"startBlock": 11948310,
"indexCalls": false
"startBlock": 11948310
}
2 changes: 1 addition & 1 deletion src/mappings/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { onAddToken } from './tokens'
export { onTokenListing } from './tokens'
export { onOrderPlacement, onOrderCancellation, onOrderDeletion } from './orders'
export { onTrade, onTradeReversion } from './trades'
export { onDeposit } from './deposits'
Expand Down
9 changes: 5 additions & 4 deletions src/mappings/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '../../generated/BatchExchange/BatchExchange'
import { Order, Token, Trade, User } from '../../generated/schema'
import { toOrderId, batchIdToEpoch, getBatchId } from '../utils'
import { createTokenIfNotCreated } from './tokens'
import { getTokenById } from './tokens'
import { createUserIfNotCreated } from './users'

export function onOrderPlacement(event: OrderPlacementEvent): void {
Expand All @@ -15,9 +15,10 @@ export function onOrderPlacement(event: OrderPlacementEvent): void {
// Crete user if doesn't exist
let owner = createUserIfNotCreated(params.owner, event)

// Crete tokens if they don't exist
let sellToken = createTokenIfNotCreated(params.sellToken, event)
let buyToken = createTokenIfNotCreated(params.buyToken, event)
// Get existing token entities - the `BatchExchange` contract requires token
// IDs to be valid to place orders.
let sellToken = getTokenById(params.sellToken)
let buyToken = getTokenById(params.buyToken)

// Create order
_createOrder(event, owner, sellToken, buyToken)
Expand Down
71 changes: 19 additions & 52 deletions src/mappings/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,21 @@
import { log, Address, BigInt, Bytes, ethereum } from '@graphprotocol/graph-ts'
import { log, BigInt } from '@graphprotocol/graph-ts'
import { Token } from '../../generated/schema'
import { AddTokenCall, BatchExchange } from '../../generated/BatchExchange/BatchExchange'
import { TokenListing as TokenListingEvent } from '../../generated/BatchExchange/BatchExchange'
import { Erc20 } from '../../generated/BatchExchange/Erc20'
import { epochToBatchId } from '../utils'

export function onAddToken(call: AddTokenCall): void {
let address = call.inputs.token
let timestamp = call.block.timestamp
let txHash = call.transaction.hash
export function onTokenListing(event: TokenListingEvent): void {
let params = event.params

// Get token id
let batchExchange = BatchExchange.bind(call.to)
let tokenId = batchExchange.tokenAddressToIdMap(address)
log.info('[onAddToken] Add token {} with address', [BigInt.fromI32(tokenId).toString(), address.toHex()])

// Create token
let id = BigInt.fromI32(tokenId).toString()
_createToken(id, address, timestamp, txHash)

// It's possible, that there was already some deposits
// TODO: Update balances (once balances are implemented)
}

export function createTokenIfNotCreated(tokenId: u32, event: ethereum.Event): Token {
let id = BigInt.fromI32(tokenId).toString()
let token = Token.load(id)
log.info('[createTokenIfNotCreated] Make sure token {} is created', [id])

if (token == null) {
let batchExchange = BatchExchange.bind(event.address)
let address = batchExchange.tokenIdToAddressMap(tokenId)

let timestamp = event.block.timestamp
let txHash = event.transaction.hash

// Create token if not created
token = _createToken(id, address, timestamp, txHash)
}

return token!
}

export function getTokenById(tokenId: i32): Token {
let id = BigInt.fromI32(tokenId).toString()
let tokenOpt = Token.load(id)
if (!tokenOpt) {
throw new Error("Order doesn't exist: " + id)
}
return tokenOpt!
}

export function _createToken(id: string, address: Address, timestamp: BigInt, txHash: Bytes): Token {
log.info('[createToken] Create Token {} with address {}', [id, address.toHex()])
let id = BigInt.fromI32(params.id).toString()
let address = params.token
log.info('[onTokenListing] Add token {} with address {}', [id, address.toHex()])

// Create token
let token = new Token(id)
token.address = address
token.fromBatchId = epochToBatchId(timestamp)
token.fromBatchId = epochToBatchId(event.block.timestamp)

// Add symbol (optional)
let erc20 = Erc20.bind(address)
Expand Down Expand Up @@ -87,11 +46,19 @@ export function _createToken(id: string, address: Address, timestamp: BigInt, tx
}

// Audit dates
token.createEpoch = timestamp
token.createEpoch = event.block.timestamp

// Transaction
token.txHash = txHash
token.txHash = event.transaction.hash

token.save()
return token
}

export function getTokenById(tokenId: i32): Token {
let id = BigInt.fromI32(tokenId).toString()
let tokenOpt = Token.load(id)
if (!tokenOpt) {
throw new Error("Token doesn't exist: " + id)
}
return tokenOpt!
}
10 changes: 4 additions & 6 deletions subgraph.yaml.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ dataSources:
file: ./abis/BatchExchange.json
- name: Erc20
file: ./abis/Erc20.json
{{#indexCalls}}
callHandlers:
# Tokens
- function: addToken(address)
handler: onAddToken
{{/indexCalls}}
eventHandlers:
# Tokens
- event: TokenListing(address,uint16)
handler: onTokenListing

# Deposits
- event: Deposit(indexed address,indexed address,uint256,uint32)
handler: onDeposit
Expand Down

0 comments on commit dc72b4b

Please sign in to comment.