Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding pairs volume decimals. #57

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 46 additions & 45 deletions model.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ classDiagram
class User {
id: ID!
address: Bytes!
firstTradeTimestamp: BigInt
firstTradeTimestamp: Int!
ordersPlaced: [Order!]! @derivedFrom(field: "owner")
isSolver: Boolean!
numberOfTrades: Int!
Expand All @@ -23,20 +23,20 @@ User --o Order : ordersPlaced
class Token {
id: ID!
address: Bytes!
firstTradeTimestamp: BigInt
firstTradeTimestamp: Int!
name: String!
symbol: String!
decimals: Int!
totalVolume: BigInt
totalVolume: BigInt!
priceEth: BigDecimal
priceUsd: BigDecimal
allowedPools: [BigInt!]!
history: [TokenTradingEvent!]! @derivedFrom(field: "token")
hourlyTotals: [TokenHourlyTotal!]! @derivedFrom(field: "token")
dailyTotals: [TokenDailyTotal!]! @derivedFrom(field: "token")
numberOfTrades: Int!
totalVolumeUsd: BigDecimal
totalVolumeEth: BigDecimal
totalVolumeUsd: BigDecimal!
totalVolumeEth: BigDecimal!
}

Token --o TokenTradingEvent : History
Expand All @@ -46,9 +46,9 @@ Token --o TokenHourlyTotal : hourlyTotals
class Order {
id: ID!
owner: User
tradesTimestamp: BigInt
invalidateTimestamp: BigInt
presignTimestamp: BigInt
tradesTimestamp: Int
invalidateTimestamp: Int
presignTimestamp: Int
trades: [Trade!] @derivedFrom(field: "order")
isSigned: Boolean
isValid: Boolean
Expand All @@ -58,7 +58,7 @@ Order --o Trade : trades

class Trade {
id: ID!
timestamp: BigInt!
timestamp: Int!
gasPrice: BigInt!
feeAmount: BigInt!
feeAmountUsd: BigDecimal
Expand All @@ -84,11 +84,11 @@ Trade --o Settlement : settlement
class Settlement {
id: ID!
txHash: Bytes!
firstTradeTimestamp: BigInt!
firstTradeTimestamp: Int!
trades: [Trade!] @derivedFrom(field: "settlement")
solver: User
txCostUsd: BigDecimal!
aggregatedFeeAmountUsd: BigDecimal
aggregatedFeeAmountUsd: BigDecimal!
profitability: BigDecimal!
}

Expand All @@ -110,40 +110,33 @@ class Total {

class DailyTotal {
id: ID!
timestamp: BigInt!
totalTokens: BigInt!
timestamp: Int!
numberOfTrades: BigInt!
orders: BigInt!
settlements: BigInt!
volumeUsd: BigDecimal
volumeEth: BigDecimal
feesUsd: BigDecimal
feesEth: BigDecimal
tokens: [Token!]!
}

DailyTotal --o Token : tokens

class HourlyTotal {
id: ID!
timestamp: BigInt!
totalTokens: BigInt!
timestamp: Int!
numberOfTrades: BigInt!
orders: BigInt!
settlements: BigInt!
volumeUsd: BigDecimal
volumeEth: BigDecimal
feesUsd: BigDecimal
feesEth: BigDecimal
tokens: [Token!]!
}

HourlyTotal --o Token : tokens

class TokenDailyTotal {
id: ID!
token: Token!
timestamp: BigInt!
timestamp: Int!
totalVolume: BigInt!
totalVolumeUsd: BigDecimal!
totalVolumeEth: BigDecimal!
Expand All @@ -158,7 +151,7 @@ class TokenDailyTotal {
class TokenHourlyTotal {
id: ID!
token: Token!
timestamp: BigInt!
timestamp: Int!
totalVolume: BigInt!
totalVolumeUsd: BigDecimal!
totalVolumeEth: BigDecimal!
Expand All @@ -174,23 +167,27 @@ class TokenTradingEvent {
id: ID!
token: Token!
trade: Trade!
timestamp: BigInt!
amountEth: BigDecimal!
amountUsd: BigDecimal!
timestamp: Int!
amountEth: BigDecimal
amountUsd: BigDecimal
}

TokenTradingEvent --o Token : token
TokenTradingEvent --o Trade : trade

class Pair {
id: ID!
token0: Token!
token1: Token!
token0Price: BigDecimal
token1Price: BigDecimal
token0relativePrice: BigDecimal
token1relativePrice: BigDecimal
volumeToken0: BigInt
volumeToken1: BigInt
lastTradeTimestamp: Int!
token0Usd: BigDecimal
token1Usd: BigDecimal
token0PriceInToken1: BigDecimal
token1PriceInToken0: BigDecimal
volumeToken0: BigInt!
volumeToken1: BigInt!
volumeToken0Deciomals: BigDecimal!
volumeToken1Deciomals: BigDecimal!
volumeTradedEth: BigDecimal
volumeTradedUsd: BigDecimal
}
Expand All @@ -202,13 +199,15 @@ class PairDaily {
id: ID!
token0: Token!
token1: Token!
token0Price: BigDecimal
token1Price: BigDecimal
token0relativePrice: BigDecimal
token1relativePrice: BigDecimal
timestamp: BigInt
volumeToken0: BigInt
volumeToken1: BigInt
token0Usd: BigDecimal
token1Usd: BigDecimal
token0PriceInToken0: BigDecimal
token1PriceInToken1: BigDecimal
timestamp: Int!
volumeToken0: BigInt!
volumeToken1: BigInt!
volumeToken0Deciomals: BigDecimal!
volumeToken1Deciomals: BigDecimal!
volumeTradedEth: BigDecimal
volumeTradedUsd: BigDecimal
}
Expand All @@ -220,13 +219,15 @@ class PairHourly {
id: ID!
token0: Token!
token1: Token!
token0Price: BigDecimal
token1Price: BigDecimal
token0relativePrice: BigDecimal
token1relativePrice: BigDecimal
timestamp: BigInt
volumeToken0: BigInt
volumeToken1: BigInt
token0Usd: BigDecimal
token1Usd: BigDecimal
token0PriceInToken0: BigDecimal
token1PriceInToken1: BigDecimal
timestamp: Int!
volumeToken0: BigInt!
volumeToken1: BigInt!
volumeToken0Deciomals: BigDecimal!
volumeToken1Deciomals: BigDecimal!
volumeTradedEth: BigDecimal
volumeTradedUsd: BigDecimal
}
Expand Down
12 changes: 12 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ type Pair @entity {
volumeToken0: BigInt!
"Total volume of token 1 traded"
volumeToken1: BigInt!
"Total volume of token 0 expressed on token 0 decimals"
volumeToken0Decimals: BigDecimal!
"Total volume of token 1 expressed on token 1 decimals"
volumeToken1Decimals: BigDecimal!
"Total volume in Eth"
volumeTradedEth: BigDecimal
"Total volume in Usd"
Expand Down Expand Up @@ -373,6 +377,10 @@ type PairDaily @entity {
volumeToken0: BigInt!
"Total volume of token 1 traded"
volumeToken1: BigInt!
"Total volume of token 0 expressed on token 0 decimals"
volumeToken0Decimals: BigDecimal!
"Total volume of token 1 expressed on token 1 decimals"
volumeToken1Decimals: BigDecimal!
"Total volume in Eth"
volumeTradedEth: BigDecimal
"Total volume in Usd"
Expand Down Expand Up @@ -400,6 +408,10 @@ type PairHourly @entity {
volumeToken0: BigInt!
"Total volume of token 1 traded"
volumeToken1: BigInt!
"Total volume of token 0 expressed on token 0 decimals"
volumeToken0Decimals: BigDecimal!
"Total volume of token 1 expressed on token 1 decimals"
volumeToken1Decimals: BigDecimal!
"Total volume in Eth"
volumeTradedEth: BigDecimal
"Total volume in Usd"
Expand Down
31 changes: 27 additions & 4 deletions src/modules/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ export namespace pairs {
token: string
volume: BigInt
price: BigDecimal | null
priceInDecimals: BigDecimal
relativePrice: BigDecimal
constructor(
_token: string,
_volume: BigInt,
_price: BigDecimal | null,
_priceInDecimals: BigDecimal,
_relativePrice: BigDecimal
) {
this.token = _token
this.volume = _volume
this.price = _price
this.priceInDecimals = _priceInDecimals
this.relativePrice = _relativePrice
}
}
Expand All @@ -32,12 +35,14 @@ export namespace pairs {
let token0 = token0Props.token
let volumeToken0 = token0Props.volume
let priceToken0 = token0Props.price
let token0Decimals = token0Props.priceInDecimals
let token0RelativePrice = token0Props.relativePrice

let token1Props = canonicalMarket.get("token1")
let token1 = token1Props.token
let volumeToken1 = token1Props.volume
let priceToken1 = token1Props.price
let token1Decimals = token1Props.priceInDecimals
let token1RelativePrice = token1Props.relativePrice

let pairTotal = getOrCreatePair(token0, token1)
Expand All @@ -46,7 +51,7 @@ export namespace pairs {

totalsUpdate(timestamp, pairTotal, pairDailyTotal, pairHourlyTotal,
volumeToken0, volumeToken1, priceToken0, priceToken1,
token0RelativePrice, token1RelativePrice,
token0RelativePrice, token1RelativePrice, token0Decimals, token1Decimals,
sellAmountEth, sellAmountUsd)
}

Expand All @@ -70,8 +75,8 @@ export namespace pairs {
sellTokenExpressedOnBuyToken = sellAmountDecimals.div(buyAmountDecimals)
}

let buyTokenProps = new TokenProps(buyTokenId, buyAmount, buyTokenPriceUsd, buyTokenExpressedOnSellToken)
let sellTokenProps = new TokenProps(sellTokenId, sellAmount, sellTokenPriceUsd, sellTokenExpressedOnBuyToken)
let buyTokenProps = new TokenProps(buyTokenId, buyAmount, buyTokenPriceUsd, buyAmountDecimals, buyTokenExpressedOnSellToken)
let sellTokenProps = new TokenProps(sellTokenId, sellAmount, sellTokenPriceUsd, sellAmountDecimals, sellTokenExpressedOnBuyToken)

if (buyTokenNumber.lt(sellTokenNumber)) {
value.set("token0", buyTokenProps)
Expand All @@ -86,27 +91,35 @@ export namespace pairs {

function totalsUpdate(timestamp: i32, pair: Pair, pairDaily: PairDaily, pairHourly: PairHourly, volumeToken0: BigInt, volumeToken1: BigInt,
priceToken0: BigDecimal | null, priceToken1: BigDecimal | null, token0RelativePrice: BigDecimal, token1RelativePrice: BigDecimal,
sellAmountEth: BigDecimal | null, sellAmountUsd: BigDecimal | null): void {
token0PriceDecimals: BigDecimal, token1PriceDecimals: BigDecimal, sellAmountEth: BigDecimal | null, sellAmountUsd: BigDecimal | null): void {

let prevPairTotalVolume0 = pair.volumeToken0
let prevPairTotalVolume1 = pair.volumeToken1
let prevPairTotalVolume0Decimals = pair.volumeToken0Decimals
let prevPairTotalVolume1Decimals = pair.volumeToken1Decimals
let prevPairTotalEth = pair.volumeTradedEth
let prevPairTotalUsd = pair.volumeTradedUsd

let prevPairDailyVolume0 = pairDaily.volumeToken0
let prevPairDailyVolume1 = pairDaily.volumeToken1
let prevPairDailyVolume0Decimals = pairDaily.volumeToken0Decimals
let prevPairDailyVolume1Decimals = pairDaily.volumeToken1Decimals
let prevPairDailyEth = pairDaily.volumeTradedEth
let prevPairDailyUsd = pairDaily.volumeTradedUsd

let prevPairHourlyVolume0 = pairHourly.volumeToken0
let prevPairHourlyVolume1 = pairHourly.volumeToken1
let prevPairHourlyVolume0Decimals = pairHourly.volumeToken0Decimals
let prevPairHourlyVolume1Decimals = pairHourly.volumeToken1Decimals
let prevPairHourlyEth = pairHourly.volumeTradedEth
let prevPairHourlyUsd = pairHourly.volumeTradedUsd


// Updates volumes for a pair
pair.volumeToken0 = prevPairTotalVolume0.plus(volumeToken0)
pair.volumeToken1 = prevPairTotalVolume1.plus(volumeToken1)
pair.volumeToken0Decimals = prevPairTotalVolume0Decimals.plus(token0PriceDecimals)
pair.volumeToken1Decimals = prevPairTotalVolume1Decimals.plus(token1PriceDecimals)
if (prevPairTotalEth !== null && sellAmountEth) {
pair.volumeTradedEth = prevPairTotalEth.plus(sellAmountEth)
}
Expand All @@ -123,6 +136,8 @@ export namespace pairs {
// update volumes for a pair daily totals
pairDaily.volumeToken0 = prevPairDailyVolume0.plus(volumeToken0)
pairDaily.volumeToken1 = prevPairDailyVolume1.plus(volumeToken1)
pairDaily.volumeToken0Decimals = prevPairDailyVolume0Decimals.plus(token0PriceDecimals)
pairDaily.volumeToken1Decimals = prevPairDailyVolume1Decimals.plus(token1PriceDecimals)
pairDaily.volumeTradedEth = sellAmountEth && prevPairDailyEth ? prevPairDailyEth.plus(sellAmountEth) : prevPairDailyEth
pairDaily.volumeTradedUsd = sellAmountUsd && prevPairDailyUsd ? prevPairDailyUsd.plus(sellAmountUsd) : prevPairDailyUsd
pairDaily.token0Usd = priceToken0
Expand All @@ -134,6 +149,8 @@ export namespace pairs {
// update volumes for a pair hourly totals
pairHourly.volumeToken0 = prevPairHourlyVolume0.plus(volumeToken0)
pairHourly.volumeToken1 = prevPairHourlyVolume1.plus(volumeToken1)
pairHourly.volumeToken0Decimals = prevPairHourlyVolume0Decimals.plus(token0PriceDecimals)
pairHourly.volumeToken1Decimals = prevPairHourlyVolume1Decimals.plus(token1PriceDecimals)
pairHourly.volumeTradedEth = sellAmountEth && prevPairHourlyEth ? prevPairHourlyEth.plus(sellAmountEth) : prevPairHourlyEth
pairHourly.volumeTradedUsd = sellAmountUsd && prevPairHourlyUsd ? prevPairHourlyUsd.plus(sellAmountUsd) : prevPairHourlyUsd
pairHourly.token0Usd = priceToken0
Expand All @@ -154,6 +171,8 @@ export namespace pairs {
pairTotal.token1 = token1
pairTotal.volumeToken0 = ZERO_BI
pairTotal.volumeToken1 = ZERO_BI
pairTotal.volumeToken0Decimals = ZERO_BD
pairTotal.volumeToken1Decimals = ZERO_BD
pairTotal.volumeTradedEth = ZERO_BD
pairTotal.volumeTradedUsd = ZERO_BD
}
Expand All @@ -173,6 +192,8 @@ export namespace pairs {
pairDailyTotal.timestamp = timestamp
pairDailyTotal.volumeToken0 = ZERO_BI
pairDailyTotal.volumeToken1 = ZERO_BI
pairDailyTotal.volumeToken0Decimals = ZERO_BD
pairDailyTotal.volumeToken1Decimals = ZERO_BD
pairDailyTotal.volumeTradedEth = ZERO_BD
pairDailyTotal.volumeTradedUsd = ZERO_BD
}
Expand All @@ -193,6 +214,8 @@ export namespace pairs {
pairHourlyTotal.timestamp = timestamp
pairHourlyTotal.volumeToken0 = ZERO_BI
pairHourlyTotal.volumeToken1 = ZERO_BI
pairHourlyTotal.volumeToken0Decimals = ZERO_BD
pairHourlyTotal.volumeToken1Decimals = ZERO_BD
pairHourlyTotal.volumeTradedEth = ZERO_BD
pairHourlyTotal.volumeTradedUsd = ZERO_BD
}
Expand Down