diff --git a/schema.graphql b/schema.graphql index a3fd3c2..040837d 100644 --- a/schema.graphql +++ b/schema.graphql @@ -156,3 +156,19 @@ type TransactionCandleStick @entity { volumeInFV: BigInt! lendingMarket: LendingMarket! } + +type TransactionStatsContainer @entity { + id: ID! + stats: [TransactionStats!]! @derivedFrom(field: "container") +} + +# TODO: add for APR +# TOOD: add for vol +type TransactionStats @entity { + id: ID! + currency: Bytes! + maturity: BigInt! + priceChange: BigInt + percentageChange: BigDecimal + container: TransactionStatsContainer! +} diff --git a/src/helper/initializer.ts b/src/helper/initializer.ts index 0a56a2c..6d136b6 100644 --- a/src/helper/initializer.ts +++ b/src/helper/initializer.ts @@ -14,6 +14,8 @@ import { Protocol, Transaction, TransactionCandleStick, + TransactionStats, + TransactionStatsContainer, Transfer, User, } from '../../generated/schema'; @@ -276,6 +278,66 @@ export const initTransfer = ( user.save(); }; +export function updateTransactionStats( + currency: Bytes, + maturity: BigInt, + currentTimestamp: BigInt +): void { + const currentCandleStickId = getTransactionCandleStickEntityId( + currency, + maturity, + BigInt.fromI32(86400), // 1 day interval + currentTimestamp.div(BigInt.fromI32(86400)) + ); + const currentCandleStick = + TransactionCandleStick.load(currentCandleStickId); + + if (currentCandleStick) { + const previousTimestamp = currentTimestamp.minus(BigInt.fromI32(86400)); + const previousCandleStickId = getTransactionCandleStickEntityId( + currency, + maturity, + BigInt.fromI32(86400), // 1 day interval + previousTimestamp.div(BigInt.fromI32(86400)) + ); + const previousCandleStick = TransactionCandleStick.load( + previousCandleStickId + ); + + if (previousCandleStick) { + const currentPrice = currentCandleStick.close; + const previousPrice = previousCandleStick.close; + + const priceChange = currentPrice.minus(previousPrice); + const percentageChange = priceChange + .times(BigInt.fromI32(10000)) + .div(previousPrice); + + const containerId = 'CONTAINER'; + let container = TransactionStatsContainer.load(containerId); + + if (!container) { + container = new TransactionStatsContainer(containerId); + } + + const statsId = currency.toHexString() + '-' + maturity.toString(); + let stats = TransactionStats.load(statsId); + + if (!stats) { + stats = new TransactionStats(statsId); + stats.currency = currency; + stats.maturity = maturity; + stats.container = container.id; + stats.save(); + } + + stats.priceChange = priceChange; + stats.percentageChange = percentageChange.toBigDecimal(); + stats.save(); + } + } +} + export const initOrUpdateTransactionCandleStick = ( currency: Bytes, maturity: BigInt, diff --git a/src/mappings/lending-market.ts b/src/mappings/lending-market.ts index 9f7b018..8a8648e 100644 --- a/src/mappings/lending-market.ts +++ b/src/mappings/lending-market.ts @@ -14,6 +14,7 @@ import { initOrUpdateTransactionCandleStick, initOrder, initTransaction, + updateTransactionStats, } from '../helper/initializer'; import { getOrderEntityId } from '../utils/id-generation'; @@ -213,6 +214,12 @@ export function handlePositionUnwound(event: PositionUnwound): void { BigInt.fromI32(intervals[i]) ); } + // TODO: check if this needs to be added in other handlers + updateTransactionStats( + event.params.ccy, + event.params.maturity, + event.block.timestamp + ); } }