diff --git a/config/ganache.json b/config/ganache.json index 53cc588..e4dbad3 100644 --- a/config/ganache.json +++ b/config/ganache.json @@ -1,5 +1,4 @@ { "network": "mainnet", - "address": "0x9561C133DD8580860B6b7E504bC5Aa500f0f06a7", - "indexCalls": false + "address": "0x9561C133DD8580860B6b7E504bC5Aa500f0f06a7" } diff --git a/config/mainnet.json b/config/mainnet.json index 0169905..188c107 100644 --- a/config/mainnet.json +++ b/config/mainnet.json @@ -1,6 +1,5 @@ { "network": "mainnet", "address": "0x6F400810b62df8E13fded51bE75fF5393eaa841F", - "startBlock": 9340147, - "indexCalls": true + "startBlock": 9340147 } diff --git a/config/rinkeby.json b/config/rinkeby.json index 3dcbc93..785b201 100644 --- a/config/rinkeby.json +++ b/config/rinkeby.json @@ -1,6 +1,5 @@ { "network": "rinkeby", "address": "0xC576eA7bd102F7E476368a5E98FA455d1Ea34dE2", - "startBlock": 5844678, - "indexCalls": false + "startBlock": 5844678 } diff --git a/config/xdai.json b/config/xdai.json index 201b0c1..d03c31e 100644 --- a/config/xdai.json +++ b/config/xdai.json @@ -1,6 +1,5 @@ { "network": "xdai", "address": "0x25B06305CC4ec6AfCF3E7c0b673da1EF8ae26313", - "startBlock": 11948310, - "indexCalls": false + "startBlock": 11948310 } diff --git a/src/mappings/index.ts b/src/mappings/index.ts index 026ab1b..aacc742 100644 --- a/src/mappings/index.ts +++ b/src/mappings/index.ts @@ -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' diff --git a/src/mappings/orders.ts b/src/mappings/orders.ts index 2e8f54c..8eba674 100644 --- a/src/mappings/orders.ts +++ b/src/mappings/orders.ts @@ -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 { @@ -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) diff --git a/src/mappings/tokens.ts b/src/mappings/tokens.ts index 4e40add..8797a19 100644 --- a/src/mappings/tokens.ts +++ b/src/mappings/tokens.ts @@ -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) @@ -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! } diff --git a/subgraph.yaml.mustache b/subgraph.yaml.mustache index 0398ef7..ebc7bb1 100644 --- a/subgraph.yaml.mustache +++ b/subgraph.yaml.mustache @@ -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