Skip to content

Commit

Permalink
Merge pull request #1 from clober-dex/feat/add-inverted-market-chart
Browse files Browse the repository at this point in the history
feat: add inverted market chart
  • Loading branch information
Dorvin authored Mar 26, 2024
2 parents d6f3ebb + 813b3fc commit b415b4a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
52 changes: 51 additions & 1 deletion src/book-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
createToken,
decodeBookIdFromOrderId,
encodeOrderId,
formatInvertedPrice,
formatPrice,
formatUnits,
rawToBase,
Expand Down Expand Up @@ -198,9 +199,14 @@ export function handleTake(event: Take): void {

// update chart
const baseTakenAmount = rawToBase(book, event.params.amount, price)
const quoteTakenAmount = rawToQuote(book, event.params.amount)
const baseToken = Token.load(book.base) as Token
const quoteToken = Token.load(book.quote) as Token
const formattedPrice = formatPrice(price)
const formattedPrice = formatPrice(
price,
baseToken.decimals,
quoteToken.decimals,
)
const formattedBaseTakenAmount = formatUnits(
baseTakenAmount,
baseToken.decimals.toI32() as u8,
Expand All @@ -212,6 +218,8 @@ export function handleTake(event: Take): void {
const timestampForAcc = (Math.floor(
(event.block.timestamp.toI64() as number) / intervalInNumber,
) * intervalInNumber) as i64

// natural chart log
const chartLogId = buildChartLogId(
baseToken,
quoteToken,
Expand Down Expand Up @@ -241,6 +249,48 @@ export function handleTake(event: Take): void {
chartLog.baseVolume = chartLog.baseVolume.plus(formattedBaseTakenAmount)
}
chartLog.save()

// inverted chart log
const formattedInvertedPrice = formatInvertedPrice(
price,
baseToken.decimals,
quoteToken.decimals,
)
const formattedQuoteTakenAmount = formatUnits(
quoteTakenAmount,
quoteToken.decimals.toI32() as u8,
)
const invertedChartLogId = buildChartLogId(
quoteToken,
baseToken,
intervalType,
timestampForAcc,
)
const invertedMarketCode = buildMarketCode(quoteToken, baseToken)
let invertedChartLog = ChartLog.load(invertedChartLogId)
if (invertedChartLog === null) {
invertedChartLog = new ChartLog(invertedChartLogId)
invertedChartLog.marketCode = invertedMarketCode
invertedChartLog.intervalType = intervalType
invertedChartLog.timestamp = BigInt.fromI64(timestampForAcc)
invertedChartLog.open = formattedInvertedPrice
invertedChartLog.high = formattedInvertedPrice
invertedChartLog.low = formattedInvertedPrice
invertedChartLog.close = formattedInvertedPrice
invertedChartLog.baseVolume = formattedQuoteTakenAmount
} else {
if (formattedInvertedPrice.gt(invertedChartLog.high)) {
invertedChartLog.high = formattedInvertedPrice
}
if (formattedInvertedPrice.lt(invertedChartLog.low)) {
invertedChartLog.low = formattedInvertedPrice
}
invertedChartLog.close = formattedInvertedPrice
invertedChartLog.baseVolume = invertedChartLog.baseVolume.plus(
formattedQuoteTakenAmount,
)
}
invertedChartLog.save()
}
}

Expand Down
47 changes: 36 additions & 11 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,21 @@ export function buildDepthId(bookId: string, tick: BigInt): string {
}

export function buildChartLogId(
token0: Token,
token1: Token,
base: Token,
quote: Token,
intervalType: string,
timestamp: i64,
): string {
const marketCode = buildMarketCode(token0, token1)
const marketCode = buildMarketCode(base, quote)
return marketCode
.concat('-')
.concat(intervalType)
.concat('-')
.concat(timestamp.toString())
}

export function buildMarketCode(token0: Token, token1: Token): string {
return token0.id < token1.id
? token0.id.concat('-').concat(token1.id)
: token1.id.concat('-').concat(token0.id)
export function buildMarketCode(base: Token, quote: Token): string {
return base.id.concat('/').concat(quote.id)
}

export function createToken(tokenAddress: Address): Token {
Expand All @@ -87,10 +85,37 @@ export function createToken(tokenAddress: Address): Token {
return token
}

export function formatPrice(price: BigInt): BigDecimal {
return BigDecimal.fromString(price.toString()).div(
pricePrecision.toBigDecimal(),
)
export function formatPrice(
price: BigInt,
baseDecimals: BigInt,
quoteDecimals: BigInt,
): BigDecimal {
return BigDecimal.fromString(price.toString())
.div(pricePrecision.toBigDecimal())
.times(
BigDecimal.fromString(
BigInt.fromI32(10)
.pow(baseDecimals.minus(quoteDecimals).toI32() as u8)
.toString(),
),
)
}

export function formatInvertedPrice(
price: BigInt,
baseDecimals: BigInt,
quoteDecimals: BigInt,
): BigDecimal {
return pricePrecision
.toBigDecimal()
.div(BigDecimal.fromString(price.toString()))
.times(
BigDecimal.fromString(
BigInt.fromI32(10)
.pow(quoteDecimals.minus(baseDecimals).toI32() as u8)
.toString(),
),
)
}

export function formatUnits(
Expand Down

0 comments on commit b415b4a

Please sign in to comment.