Skip to content

Commit

Permalink
Merge pull request #119 from helium/mbthiery/db-cleanup
Browse files Browse the repository at this point in the history
Rename max_supply to supply_limit
  • Loading branch information
mbthiery committed Jan 8, 2024
2 parents b24a867 + 40ac1af commit 9a9d92b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
17 changes: 17 additions & 0 deletions migrations/20240103152443_rename_max_supply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Knex } from "knex"

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable("max_supply_records", (table) => {
table.renameColumn("max_supply", "supply_limit")
})

return knex.schema.renameTable("max_supply_records", "supply_limit_records")
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable("supply_limit_records", (table) => {
table.renameColumn("supply_limit", "max_supply")
})

return knex.schema.renameTable("supply_limit_records", "max_supply_records")
}
8 changes: 4 additions & 4 deletions src/app/api/stats/supply/[token]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getRemainingEmissions,
} from "@/app/stats/utils/remainingEmissions"
import { db } from "@/knex/db"
import { MaxSupply } from "@/knex/maxSupply"
import { SupplyLimit } from "@/knex/supplyLimit"
import { HNT_MINT, IOT_MINT, MOBILE_MINT, toNumber } from "@helium/spl-utils"
import { NextRequest, NextResponse } from "next/server"

Expand Down Expand Up @@ -65,9 +65,9 @@ export async function GET(
remainingEmissions = Math.ceil(MAX_DAILY_NET_EMISSIONS)

// using existing supply limit logic to avoid repeating edge case logic
const maxSupplyDb = new MaxSupply(db)
const supplyLimit = (await maxSupplyDb.getLatest({ withBurn: false }))
?.max_supply!
const supplyLimitDb = new SupplyLimit(db)
const supplyLimit = (await supplyLimitDb.getLatest({ withBurn: false }))
?.supply_limit!
supply = supplyLimit
} else {
remainingEmissions += Math.ceil(getRemainingEmissions(new Date(), token))
Expand Down
14 changes: 7 additions & 7 deletions src/app/stats/components/HntInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StatItem } from "@/app/stats/components/StatItem"
import { StatsList } from "@/app/stats/components/StatsList"
import { ONE_DAY_MS, epochFromDate, fetcher } from "@/app/stats/utils"
import { db } from "@/knex/db"
import { MaxSupply } from "@/knex/maxSupply"
import { SupplyLimit } from "@/knex/supplyLimit"
import { BN } from "@coral-xyz/anchor"
import { currentEpoch } from "@helium/helium-sub-daos-sdk"
import {
Expand Down Expand Up @@ -70,7 +70,7 @@ export const HntInfo = async () => {
const remainingHntEmissions = Math.round(
getRemainingEmissions(new Date(), "hnt")
)
const maxSupply =
const supplyLimit =
hntMint.info?.info.supply! +
BigInt(remainingHntEmissions) * BigInt(100000000) +
BigInt(
Expand All @@ -80,15 +80,15 @@ export const HntInfo = async () => {
) *
BigInt(10000)

const maxSupplyDb = new MaxSupply(db)
const maxSupplyRecord = await maxSupplyDb.latestOrInsert({
const supplyLimitDb = new SupplyLimit(db)
const supplyLimitRecord = await supplyLimitDb.latestOrInsert({
recorded_at: isSameDay
? new Date(hntBurned.execution_started_at)
: new Date(),
hnt_burned:
BigInt(Math.round(parseFloat(todayHntBurn) * 10000)) * BigInt(10000),
supply: hntMint.info?.info.supply!,
max_supply: maxSupply,
supply_limit: supplyLimit,
})

return (
Expand Down Expand Up @@ -130,15 +130,15 @@ export const HntInfo = async () => {
<StatItem
label="Supply Limit"
value={`~${humanReadableBigint(
maxSupplyRecord.max_supply,
supplyLimitRecord.supply_limit,
hntMint?.info?.info.decimals || 8,
0
)}`}
tooltip={{
description:
"Maximum supply of HNT derived by summing current supply, remaining emissions, and today's burned HNT (which are re-emitted via net emissions). This is an upper limit that will not be reached and does not consider future HNT burn. Accurate within 1643 HNT.",
cadence: `Daily (last run ${format(
maxSupplyRecord.recorded_at,
supplyLimitRecord.recorded_at,
DATE_FORMAT
)})`,
id: "HNT Supply Limit",
Expand Down
18 changes: 9 additions & 9 deletions src/knex/maxSupply.ts → src/knex/supplyLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { epochFromDate } from "@/app/stats/utils"
import { isEqual } from "date-fns"
import { Knex } from "knex"

type MaxSupplyRecord = {
type SupplyLimitRecord = {
recorded_at: Date
hnt_burned: bigint
supply: bigint
max_supply: bigint
supply_limit: bigint
}

export class MaxSupply {
export class SupplyLimit {
knex: Knex

constructor(knex: Knex) {
Expand All @@ -20,8 +20,8 @@ export class MaxSupply {
withBurn,
}: {
withBurn: boolean
}): Promise<MaxSupplyRecord | undefined> {
const query = this.knex("max_supply_records")
}): Promise<SupplyLimitRecord | undefined> {
const query = this.knex("supply_limit_records")
.select("*")
.orderBy("recorded_at", "desc")
.first()
Expand All @@ -35,15 +35,15 @@ export class MaxSupply {
recorded_at: latest.recorded_at,
hnt_burned: BigInt(latest.hnt_burned),
supply: BigInt(latest.supply),
max_supply: BigInt(latest.max_supply),
supply_limit: BigInt(latest.supply_limit),
}
}

private async addRecord(record: MaxSupplyRecord) {
return this.knex("max_supply_records").insert(record)
private async addRecord(record: SupplyLimitRecord) {
return this.knex("supply_limit_records").insert(record)
}

async latestOrInsert(record: MaxSupplyRecord) {
async latestOrInsert(record: SupplyLimitRecord) {
// only want to display latest without burn but want to track both. The with burn value helps as heuristic for when HNT was disbursed.
const [latest, latestBurn] = await Promise.all([
this.getLatest({ withBurn: false }),
Expand Down

0 comments on commit 9a9d92b

Please sign in to comment.