diff --git a/schema.graphql b/schema.graphql index 6f0ad35..e71a72d 100644 --- a/schema.graphql +++ b/schema.graphql @@ -4,7 +4,7 @@ type User @entity { "Owner's address" address: Bytes! "First trade block timestamp" - firstTradeTimestamp: Int + firstTradeTimestamp: Int! "List of orders placed by this user" ordersPlaced: [Order!]! @derivedFrom(field: "owner") "Determine if user has solved a settlement" @@ -27,7 +27,7 @@ type Token @entity { "Token address" address: Bytes! "First token trade block timestamp" - firstTradeTimestamp: Int + firstTradeTimestamp: Int! "Token name fetched by ERC20 contract call" name: String! "Token symbol fetched by contract call" diff --git a/src/mapping.ts b/src/mapping.ts index 6a45d86..cb2c2c2 100644 --- a/src/mapping.ts +++ b/src/mapping.ts @@ -5,10 +5,11 @@ import { Settlement, Trade } from "../generated/GPV2Settlement/GPV2Settlement" +import { WETH_ADDRESS } from "./utils/pricing" import { tokens, trades, orders, users } from "./modules" import { getPrices } from "./utils/getPrices" -import { MINUS_ONE_BD, ZERO_BI } from "./utils/constants" -import { BigDecimal, BigInt, dataSource, log } from "@graphprotocol/graph-ts" +import { MINUS_ONE_BD, ZERO_BD } from "./utils/constants" +import { Address, BigDecimal, BigInt, dataSource, log } from "@graphprotocol/graph-ts" import { convertTokenToDecimal } from "./utils" import { UniswapToken } from "../generated/schema" @@ -63,9 +64,18 @@ export function handleTrade(event: Trade): void { sellToken.totalVolume = tokenCurrentSellAmount.plus(sellAmount) buyToken.totalVolume = tokenCurrentBuyAmount.plus(buyAmount) + let buyTokenAddressHexString = buyTokenAddress.toHexString() + let isBuyTokenTheNativeOne = buyTokenAddressHexString == "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + if (network == 'xdai') { let sellTokenPrices = getPrices(sellTokenAddress) - let buyTokenPrices = getPrices(buyTokenAddress) + let buyTokenPrices = new Map() + if (isBuyTokenTheNativeOne) { + let XDAI_ADDRESS = Address.fromString("0xe91d153e0b41518a2ce8dd3d7944fa863463a97d") + buyTokenPrices = getPrices(XDAI_ADDRESS) + } else { + buyTokenPrices = getPrices(buyTokenAddress) + } if (sellTokenPrices.get("usd") != MINUS_ONE_BD && sellTokenPrices.get("eth") != MINUS_ONE_BD) { sellToken.priceUsd = sellTokenPrices.get("usd") @@ -77,8 +87,11 @@ export function handleTrade(event: Trade): void { buyToken.priceEth = buyTokenPrices.get("eth") } } else { + if (isBuyTokenTheNativeOne){ + buyTokenAddressHexString = WETH_ADDRESS + } let sellUniToken = UniswapToken.load(sellTokenAddress.toHexString()) - let buyUniToken = UniswapToken.load(buyTokenAddress.toHexString()) + let buyUniToken = UniswapToken.load(buyTokenAddressHexString) if (sellUniToken) { sellToken.priceUsd = sellUniToken.priceUsd ? sellUniToken.priceUsd : null sellToken.priceEth = sellUniToken.priceEth ? sellUniToken.priceEth : null @@ -136,5 +149,4 @@ export function handleTrade(event: Trade): void { sellToken.save() buyToken.save() order.save() - } diff --git a/src/modules/users.ts b/src/modules/users.ts index fe04fa9..c379acc 100644 --- a/src/modules/users.ts +++ b/src/modules/users.ts @@ -16,6 +16,7 @@ export namespace users { user.solvedAmountUsd = ZERO_BD user.tradedAmountEth = ZERO_BD user.tradedAmountUsd = ZERO_BD + user.firstTradeTimestamp = 0 } return user as User diff --git a/src/utils/pricing.ts b/src/utils/pricing.ts index 3d6204d..d25845c 100644 --- a/src/utils/pricing.ts +++ b/src/utils/pricing.ts @@ -4,7 +4,7 @@ import { Bundle, UniswapPool, UniswapToken } from './../../generated/schema' import { BigDecimal, BigInt } from '@graphprotocol/graph-ts' import { exponentToBigDecimal, safeDiv } from '../utils/index' -const WETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' +export const WETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' const USDC_WETH_03_POOL = '0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8' // token where amounts should contribute to tracked volume and liquidity