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

Features/improve api usdc support #16

Merged
merged 12 commits into from
Sep 13, 2023
Merged
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
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
**/npm-debug.log
**/.git
**/.env
**/.env.*
**/.next
*.md
build.ps1
13 changes: 13 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@types/node": "^16.6.0",
"@types/node-fetch": "^2.6.2",
"@types/pg": "^8.6.5",
"@types/semver": "^7.5.1",
"@types/uuid": "^8.3.1",
"alias-hq": "^5.1.6",
"nodemon": "^2.0.7",
Expand Down
25 changes: 20 additions & 5 deletions api/src/db/networkRevenueProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,41 @@ export const getWeb3IndexRevenue = async (debug?: boolean) => {
if (date <= sixtyDaysAgo) {
sixtyDaysAgoRevenue += b.revenue;
sixtyDaysAgoRevenueUAkt += b.revenueUAkt;
sixtyDaysAgoRevenueUUsdc += b.revenueUUsdc;
}
if (date <= thirtyDaysAgo) {
thirtyDaysAgoRevenue += b.revenue;
thirtyDaysAgoRevenueUAkt += b.revenueUAkt;
thirtyDaysAgoRevenueUUsdc += b.revenueUUsdc;
}
if (date <= twoWeeksAgo) {
twoWeeksAgoRevenue += b.revenue;
twoWeeksAgoRevenueUAkt += b.revenueUAkt;
twoWeeksAgoRevenueUUsdc += b.revenueUUsdc;
}
if (date <= oneWeekAgo) {
oneWeekAgoRevenue += b.revenue;
oneWeekAgoRevenueUAkt += b.revenueUAkt;
oneWeekAgoRevenueUUsdc += b.revenueUUsdc;
}
if (date <= twoDaysAgo) {
twoDaysAgoRevenue += b.revenue;
twoDaysAgoRevenueUAkt += b.revenueUAkt;
twoDaysAgoRevenueUUsdc += b.revenueUUsdc;
}
if (date <= oneDayAgo) {
oneDayAgoRevenue += b.revenue;
oneDayAgoRevenueUAkt += b.revenueUAkt;
oneDayAgoRevenueUUsdc += b.revenueUUsdc;
}

totalRevenue += b.revenue;
totalRevenueUAkt += b.revenueUAkt;
totalRevenueUUsdc += b.revenueUUsdc;
}, 0);

if (!debug) {
days = days.map(({ dateStr, revenueUAkt, aktPrice, ...others }) => others) as any;
days = days.map(({ dateStr, revenueUAkt, revenueUUsdc, aktPrice, ...others }) => others) as any;
}

let revenueStats = {
Expand All @@ -112,7 +119,15 @@ export const getWeb3IndexRevenue = async (debug?: boolean) => {
twoWeeksAgAkt: uaktToAKT(twoWeeksAgoRevenueUAkt, 6),
thirtyDaysAgoAkt: uaktToAKT(thirtyDaysAgoRevenueUAkt, 6),
sixtyDaysAgoAkt: uaktToAKT(sixtyDaysAgoRevenueUAkt, 6),
ninetyDaysAgoAkt: uaktToAKT(ninetyDaysAgoRevenueUAkt, 6)
ninetyDaysAgoAkt: uaktToAKT(ninetyDaysAgoRevenueUAkt, 6),
nowUsdc: udenomToDenom(totalRevenueUUsdc, 6),
oneDayAgoUsdc: udenomToDenom(oneDayAgoRevenueUUsdc, 6),
twoDaysAgoUsdc: udenomToDenom(twoDaysAgoRevenueUUsdc, 6),
oneWeekAgoUsdc: udenomToDenom(oneWeekAgoRevenueUUsdc, 6),
twoWeeksAgUsdc: udenomToDenom(twoWeeksAgoRevenueUUsdc, 6),
thirtyDaysAgoUsdc: udenomToDenom(thirtyDaysAgoRevenueUUsdc, 6),
sixtyDaysAgoUsdc: udenomToDenom(sixtyDaysAgoRevenueUUsdc, 6),
ninetyDaysAgoUsdc: udenomToDenom(ninetyDaysAgoRevenueUUsdc, 6)
} as any;
}

Expand All @@ -124,7 +139,7 @@ export const getWeb3IndexRevenue = async (debug?: boolean) => {
return responseObj;
};

async function getDailyRevenue() {
export async function getDailyRevenue() {
const result = await Day.findAll({
attributes: ["date", "aktPrice"],
include: [
Expand Down Expand Up @@ -164,8 +179,8 @@ async function getDailyRevenue() {
uakt: x.uakt,
akt: uaktToAKT(x.uakt, 6),
uusdc: x.uusdc,
usdc: udenomToDenom(x.uusdc),
usd: uaktToAKT(x.uakt, 6) * x.aktPrice + udenomToDenom(x.uusdc),
usdc: udenomToDenom(x.uusdc, 6),
usd: uaktToAKT(x.uakt, 6) * x.aktPrice + udenomToDenom(x.uusdc, 6),
aktPrice: x.aktPrice
}));
}
35 changes: 28 additions & 7 deletions api/src/db/statsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ import { chainDb } from "./dbConnection";
import { ProviderActiveLeasesStats, ProviderStats, ProviderStatsKey } from "@src/types/graph";
import { cacheKeys, cacheResponse } from "@src/caching/helpers";

type GraphData = {
currentValue: number;
compareValue: number;
snapshots: { date: Date; value: number }[];
};

export const getDashboardData = async () => {
const latestBlockStats = await Block.findOne({
where: {
isProcessed: true
isProcessed: true,
totalUUsdSpent: { [Op.not]: null }
},
order: [["height", "DESC"]]
});
Expand Down Expand Up @@ -41,6 +48,8 @@ export const getDashboardData = async () => {
dailyUAktSpent: latestBlockStats.totalUAktSpent - compareBlockStats.totalUAktSpent,
totalUUsdcSpent: latestBlockStats.totalUUsdcSpent,
dailyUUsdcSpent: latestBlockStats.totalUUsdcSpent - compareBlockStats.totalUUsdcSpent,
totalUUsdSpent: latestBlockStats.totalUUsdSpent,
dailyUUsdSpent: latestBlockStats.totalUUsdSpent - compareBlockStats.totalUUsdSpent,
activeCPU: latestBlockStats.activeCPU,
activeGPU: latestBlockStats.activeGPU,
activeMemory: latestBlockStats.activeMemory,
Expand All @@ -56,6 +65,8 @@ export const getDashboardData = async () => {
dailyUAktSpent: compareBlockStats.totalUAktSpent - secondCompareBlockStats.totalUAktSpent,
totalUUsdcSpent: compareBlockStats.totalUUsdcSpent,
dailyUUsdcSpent: compareBlockStats.totalUUsdcSpent - secondCompareBlockStats.totalUUsdcSpent,
totalUUsdSpent: compareBlockStats.totalUUsdSpent,
dailyUUsdSpent: compareBlockStats.totalUUsdSpent - secondCompareBlockStats.totalUUsdSpent,
activeCPU: compareBlockStats.activeCPU,
activeGPU: compareBlockStats.activeGPU,
activeMemory: compareBlockStats.activeMemory,
Expand All @@ -64,27 +75,37 @@ export const getDashboardData = async () => {
};
};

export const getGraphData = async (dataName: string) => {
export async function getGraphData(dataName: string): Promise<GraphData> {
console.log("getGraphData: " + dataName);

let attributes = [dataName];
let isRelative = false;
let getter = (block: Block) => block[dataName];
let getter = (block: Block) => block[dataName] as number;

switch (dataName) {
case "dailyUAktSpent":
attributes = ["totalUAktSpent"];
getter = (block: Block) => block["totalUAktSpent"];
getter = (block: Block) => block.totalUAktSpent;
isRelative = true;
break;
case "dailyUUsdcSpent":
attributes = ["totalUUsdcSpent"];
getter = (block: Block) => block.totalUUsdcSpent;
isRelative = true;
break;
case "dailyUUsdSpent":
attributes = ["totalUUsdSpent"];
getter = (block: Block) => block.totalUUsdSpent;
isRelative = true;
break;
case "dailyLeaseCount":
attributes = ["totalLeaseCount"];
getter = (block: Block) => block["totalLeaseCount"];
getter = (block: Block) => block.totalLeaseCount;
isRelative = true;
break;
case "activeStorage":
attributes = ["activeEphemeralStorage", "activePersistentStorage"];
getter = (block: Block) => block["activeEphemeralStorage"] + block["activePersistentStorage"];
getter = (block: Block) => block.activeEphemeralStorage + block.activePersistentStorage;
break;
}

Expand Down Expand Up @@ -126,7 +147,7 @@ export const getGraphData = async (dataName: string) => {
compareValue: dashboardData.compare[dataName],
snapshots: stats
};
};
}

export const getProviderGraphData = async (dataName: ProviderStatsKey) => {
console.log("getProviderGraphData: " + dataName);
Expand Down
3 changes: 1 addition & 2 deletions api/src/providers/providerStatusProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { ProviderSnapshot } from "@shared/dbSchemas/akash/providerSnapshot";
import { toUTC } from "@src/shared/utils/date";
import { add } from "date-fns";
import { Op } from "sequelize";

const semver = require("semver");
import semver from "semver";

export async function getNetworkCapacity() {
const providers = await Provider.findAll({
Expand Down
4 changes: 4 additions & 0 deletions api/src/routers/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,12 @@ apiRouter.get(
const dataName = req.params.dataName;
const authorizedDataNames = [
"dailyUAktSpent",
"dailyUUsdcSpent",
"dailyUUsdSpent",
"dailyLeaseCount",
"totalUAktSpent",
"totalUUsdcSpent",
"totalUUsdSpent",
"activeLeaseCount",
"totalLeaseCount",
"activeCPU",
Expand Down
2 changes: 1 addition & 1 deletion api/src/shared/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function endOfDay(date: Date) {
return currentDate;
}

export function toUTC(date) {
export function toUTC(date: Date) {
const now_utc = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());

return new Date(now_utc);
Expand Down
2 changes: 1 addition & 1 deletion api/src/shared/utils/delay.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function sleep(ms) {
export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
29 changes: 14 additions & 15 deletions api/src/shared/utils/files.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
export const bytesToHumanReadableSize = function (bytes: number) {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];

export const bytesToHumanReadableSize = function (bytes) {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];

if (bytes == 0) {
return "n/a";
}

const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString());

if (i == 0) {
return bytes + " " + sizes[i];
}

return (bytes / Math.pow(1024, i)).toFixed(1) + " " + sizes[i];
};
if (bytes == 0) {
return "n/a";
}

const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString());

if (i == 0) {
return bytes + " " + sizes[i];
}

return (bytes / Math.pow(1024, i)).toFixed(1) + " " + sizes[i];
};
4 changes: 2 additions & 2 deletions api/src/shared/utils/math.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function pickRandomElement(arr: any[]) {
export function pickRandomElement<T>(arr: T[]) {
return arr[Math.floor(Math.random() * arr.length)];
}

Expand All @@ -12,4 +12,4 @@ export function uaktToAKT(amount: number, precision = 2) {

export function udenomToDenom(amount: number, precision = 2) {
return round(amount / 1_000_000, precision);
}
}
2 changes: 1 addition & 1 deletion api/src/shared/utils/protobuf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const akashTypes: ReadonlyArray<[string, GeneratedType]> = [...Object.values(v1b
]);
const missingTypes: ReadonlyArray<[string, GeneratedType]> = [["/cosmos.slashing.v1beta1.MsgUnjail", MsgUnjail]];

export function decodeMsg(type: string, msg) {
export function decodeMsg(type: string, msg: Uint8Array) {
const myRegistry = new Registry([...defaultRegistryTypes, ...akashTypes, ...missingTypes]);

const msgType = myRegistry.lookupType(type);
Expand Down
21 changes: 0 additions & 21 deletions api/src/shared/utils/types.ts

This file was deleted.

Loading