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/support multi denom payments #5

Merged
merged 6 commits into from
Aug 31, 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
3 changes: 2 additions & 1 deletion api/src/db/dbConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { chainDefinitions } from "@shared/chainDefinitions";

const csMap = {
mainnet: env.AkashDatabaseCS,
testnet: env.AkashTestnetDatabaseCS
testnet: env.AkashTestnetDatabaseCS,
sandbox: env.AkashSandboxDatabaseCS
};

if (!(env.Network in csMap)) {
Expand Down
22 changes: 18 additions & 4 deletions api/src/db/networkRevenueProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Day } from "@shared/dbSchemas/base";
import { AkashBlock as Block } from "@shared/dbSchemas/akash";
import { add } from "date-fns";
import { getTodayUTC } from "@src/shared/utils/date";
import { round, uaktToAKT } from "@src/shared/utils/math";
import { round, uaktToAKT, udenomToDenom } from "@src/shared/utils/math";

export const getWeb3IndexRevenue = async (debug?: boolean) => {
const dailyNetworkRevenues = await getDailyRevenue();
Expand All @@ -12,6 +12,7 @@ export const getWeb3IndexRevenue = async (debug?: boolean) => {
date: r.date.getTime() / 1000,
revenue: round(r.usd, 2),
revenueUAkt: r.uakt,
revenueUUsdc: r.uusdc,
aktPrice: r.aktPrice,
dateStr: r.date
}));
Expand Down Expand Up @@ -40,13 +41,22 @@ export const getWeb3IndexRevenue = async (debug?: boolean) => {
thirtyDaysAgoRevenueUAkt: number = 0,
sixtyDaysAgoRevenueUAkt: number = 0,
ninetyDaysAgoRevenueUAkt: number = 0;
let totalRevenueUUsdc: number = 0,
oneDayAgoRevenueUUsdc: number = 0,
twoDaysAgoRevenueUUsdc: number = 0,
oneWeekAgoRevenueUUsdc: number = 0,
twoWeeksAgoRevenueUUsdc: number = 0,
thirtyDaysAgoRevenueUUsdc: number = 0,
sixtyDaysAgoRevenueUUsdc: number = 0,
ninetyDaysAgoRevenueUUsdc: number = 0;

days.forEach((b) => {
const date = new Date(b.date * 1000);

if (date <= ninetyDaysAgo) {
ninetyDaysAgoRevenue += b.revenue;
ninetyDaysAgoRevenueUAkt += b.revenueUAkt;
ninetyDaysAgoRevenueUUsdc += b.revenueUUsdc;
}
if (date <= sixtyDaysAgo) {
sixtyDaysAgoRevenue += b.revenue;
Expand Down Expand Up @@ -121,7 +131,7 @@ async function getDailyRevenue() {
{
model: Block,
as: "lastBlockYet",
attributes: ["totalUAktSpent"],
attributes: ["totalUAktSpent", "totalUUsdcSpent"],
required: true
}
],
Expand All @@ -134,13 +144,15 @@ async function getDailyRevenue() {
let stats = result.map((day) => ({
date: day.date,
totalUAktSpent: (day.lastBlockYet as Block).totalUAktSpent,
totalUUsdcSpent: (day.lastBlockYet as Block).totalUUsdcSpent,
aktPrice: day.aktPrice // TODO handle no price
}));

let relativeStats = stats.reduce((arr, dataPoint, index) => {
let relativeStats: { date: Date; uakt: number; uusdc: number; aktPrice: number }[] = stats.reduce((arr, dataPoint, index) => {
arr[index] = {
date: dataPoint.date,
uakt: dataPoint.totalUAktSpent - (index > 0 ? stats[index - 1].totalUAktSpent : 0),
uusdc: dataPoint.totalUUsdcSpent - (index > 0 ? stats[index - 1].totalUUsdcSpent : 0),
aktPrice: dataPoint.aktPrice
};

Expand All @@ -151,7 +163,9 @@ async function getDailyRevenue() {
date: x.date,
uakt: x.uakt,
akt: uaktToAKT(x.uakt, 6),
usd: uaktToAKT(x.uakt, 6) * x.aktPrice,
uusdc: x.uusdc,
usdc: udenomToDenom(x.uusdc),
usd: uaktToAKT(x.uakt, 6) * x.aktPrice + udenomToDenom(x.uusdc),
aktPrice: x.aktPrice
}));
}
12 changes: 12 additions & 0 deletions api/src/db/statsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const getDashboardData = async () => {
dailyLeaseCount: latestBlockStats.totalLeaseCount - compareBlockStats.totalLeaseCount,
totalUAktSpent: latestBlockStats.totalUAktSpent,
dailyUAktSpent: latestBlockStats.totalUAktSpent - compareBlockStats.totalUAktSpent,
totalUUsdcSpent: latestBlockStats.totalUUsdcSpent,
dailyUUsdcSpent: latestBlockStats.totalUUsdcSpent - compareBlockStats.totalUUsdcSpent,
activeCPU: latestBlockStats.activeCPU,
activeGPU: latestBlockStats.activeGPU,
activeMemory: latestBlockStats.activeMemory,
Expand All @@ -52,6 +54,8 @@ export const getDashboardData = async () => {
dailyLeaseCount: compareBlockStats.totalLeaseCount - secondCompareBlockStats.totalLeaseCount,
totalUAktSpent: compareBlockStats.totalUAktSpent,
dailyUAktSpent: compareBlockStats.totalUAktSpent - secondCompareBlockStats.totalUAktSpent,
totalUUsdcSpent: compareBlockStats.totalUUsdcSpent,
dailyUUsdcSpent: compareBlockStats.totalUUsdcSpent - secondCompareBlockStats.totalUUsdcSpent,
activeCPU: compareBlockStats.activeCPU,
activeGPU: compareBlockStats.activeGPU,
activeMemory: compareBlockStats.activeMemory,
Expand Down Expand Up @@ -152,6 +156,14 @@ export const getProviderGraphData = async (dataName: ProviderStatsKey) => {
true
);

if (result.length < 2) {
return {
currentValue: 0,
compareValue: 0,
snapshots: []
};
}

const currentValue = result[result.length - 1] as ProviderStats;
const compareValue = result[result.length - 2] as ProviderStats;

Expand Down
1 change: 1 addition & 0 deletions api/src/shared/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const env = {
HealthchecksEnabled: process.env.HealthchecksEnabled,
AkashDatabaseCS: process.env.AkashDatabaseCS,
AkashTestnetDatabaseCS: process.env.AkashTestnetDatabaseCS,
AkashSandboxDatabaseCS: process.env.AkashSandboxDatabaseCS,
UserDatabaseCS: process.env.UserDatabaseCS,
Network: process.env.Network ?? "mainnet",
RestApiNodeUrl: process.env.RestApiNodeUrl,
Expand Down
4 changes: 4 additions & 0 deletions api/src/shared/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ export function round(amount: number, precision: number = 2) {

export function uaktToAKT(amount: number, precision = 2) {
return round(amount / 1_000_000, precision);
}

export function udenomToDenom(amount: number, precision = 2) {
return round(amount / 1_000_000, precision);
}
6 changes: 3 additions & 3 deletions deploy-web/src/components/deployment/DeploymentListRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ export const DeploymentListRow: React.FunctionComponent<Props> = ({ deployment,
title={
<>
<strong>
{udenomToDenom(isActive && hasActiveLeases ? realTimeLeft?.escrow : escrowBalance, 6)}&nbsp;{denomData.label}
{udenomToDenom(isActive && hasActiveLeases ? realTimeLeft?.escrow : escrowBalance, 6)}&nbsp;{denomData?.label}
</strong>
<Box display="flex">
{udenomToDenom(amountSpent, 2)} {denomData.label} spent
{udenomToDenom(amountSpent, 2)} {denomData?.label} spent
</Box>
<br />
The escrow account balance will be fully returned to your wallet balance when the deployment is closed.{" "}
Expand Down Expand Up @@ -266,7 +266,7 @@ export const DeploymentListRow: React.FunctionComponent<Props> = ({ deployment,
arrow
title={
<span>
{avgCost} {denomData.label} / month
{avgCost} {denomData?.label} / month
</span>
}
>
Expand Down
66 changes: 65 additions & 1 deletion deploy-web/src/types/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export interface RpcDeployment {
escrow_account: EscrowAccount;
}

interface DeploymentGroup {
// TODO Change after mainnet6 upgrade
type DeploymentGroup = DeploymentGroup_v2 | DeploymentGroup_v3;

interface DeploymentGroup_v2 {
group_id: {
owner: string;
dseq: string;
Expand Down Expand Up @@ -119,6 +122,67 @@ interface DeploymentGroup {
created_at: string;
}

interface DeploymentGroup_v3 {
group_id: {
owner: string;
dseq: string;
gseq: number;
};
state: string;
group_spec: {
name: string;
requirements: {
signed_by: {
all_of: string[];
any_of: string[];
};
attributes: Array<{
key: string;
value: string;
}>;
};
resources: Array<{
resource: {
cpu: {
units: {
val: string;
};
attributes: any[];
};
gpu: {
units: {
val: string;
};
attributes: any[];
};
memory: {
quantity: {
val: string;
};
attributes: any[];
};
storage: Array<{
name: string;
quantity: {
val: string;
};
attributes: any[];
}>;
endpoints: Array<{
kind: string;
sequence_number: number;
}>;
};
count: number;
price: {
denom: string;
amount: string;
};
}>;
};
created_at: string;
}

interface EscrowAccount {
id: {
scope: string;
Expand Down
6 changes: 3 additions & 3 deletions deploy-web/src/utils/deploymentData/v1beta3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ function validate(yamlJson) {
}
}

export function getManifest(yamlJson) {
const manifest = Manifest(yamlJson, "beta3");
export function getManifest(yamlJson, asString: boolean) {
const manifest = Manifest(yamlJson, "beta3", asString);

return manifest;
}

export async function getManifestVersion(yamlJson, asString = false) {
const version = await ManifestVersion(yamlJson, "beta2");
const version = await ManifestVersion(yamlJson, "beta3");

if (asString) {
return Buffer.from(version).toString("base64");
Expand Down
4 changes: 2 additions & 2 deletions deploy-web/src/utils/deploymentDetailUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { DeploymentDto, LeaseDto, RpcDeployment, RpcLease } from "@src/types/dep
import { coinToUAkt } from "./priceUtils";

export function deploymentResourceSum(deployment: RpcDeployment, resourceSelector) {
return deployment.groups.map(g => g.group_spec.resources.map(r => r.count * resourceSelector(r.resources)).reduce((a, b) => a + b)).reduce((a, b) => a + b);
return deployment.groups.map(g => g.group_spec.resources.map(r => r.count * resourceSelector(r.resources ?? r.resource)).reduce((a, b) => a + b)).reduce((a, b) => a + b);
}

export function deploymentGroupResourceSum(group, resourceSelector) {
if (!group || !group.group_spec || !group.group_spec) return 0;

return group.group_spec.resources.map(r => r.count * resourceSelector(r.resources)).reduce((a, b) => a + b);
return group.group_spec.resources.map(r => r.count * resourceSelector(r.resources ?? r.resource)).reduce((a, b) => a + b);
}

export function deploymentToDto(d: RpcDeployment): DeploymentDto {
Expand Down
4 changes: 2 additions & 2 deletions deploy-web/src/utils/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ export let networks = [
title: "Sandbox",
description: "Sandbox of the mainnet version.",
nodesUrl: sandboxNodes,
chainId: "sandbox",
chainId: "sandbox-01",
versionUrl: ApiUrlService.sandboxVersion(),
version: null, // Set asynchronously
enabled: true,
suggestKeplrChain: async () => {
await window.keplr.experimentalSuggestChain({
// Chain-id of the Craft chain.
chainId: "sandbox",
chainId: "sandbox-01",
// The name of the chain to be displayed to the user.
chainName: "Akash-Sandbox",
// RPC endpoint of the chain. In this case we are using blockapsis, as it's accepts connections from any host currently. No Cors limitations.
Expand Down
2 changes: 1 addition & 1 deletion deploy-web/src/utils/proto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function initProtoTypes() {
protoTypes = v1beta3;
break;
case sandboxId:
protoTypes = v1beta2;
protoTypes = v1beta3;
break;

default:
Expand Down
1 change: 1 addition & 0 deletions indexer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ AkashDatabaseCS|ex: `postgres://user:password@localhost:5432/cloudmos-akash`|Aka
ActiveChain|ex: `akash`|Chain code from [chainDefinitions.ts](../shared/chainDefinitions.ts)
KeepCache|`true` or `false`|Specify if the [block & block response cache](#block-cache-structure) should be kept on drive. Takes a lot of space, but allow rebuilding the database without redownloading every blocks.
Standby|`true` or `false`|If `true`, indexer will not start. Useful for stopping an indexer deployed on akash without needing to close the lease.
DataFolder|ex: `./data/`|Directory where block cache and node statuses should be saved. Defaults to `./data/`.

## Scheduled Tasks

Expand Down
14 changes: 7 additions & 7 deletions indexer/package-lock.json

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

2 changes: 1 addition & 1 deletion indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"homepage": "https://github.com/akash-network/cloudmos",
"dependencies": {
"@akashnetwork/akashjs": "0.4.5",
"@akashnetwork/akashjs": "0.4.11",
"@cosmjs/crypto": "^0.28.11",
"@cosmjs/encoding": "^0.28.11",
"@cosmjs/math": "^0.28.11",
Expand Down
6 changes: 5 additions & 1 deletion indexer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { bytesToHumanReadableSize } from "./shared/utils/files";
import { getCacheSize } from "./chain/dataStore";
import { env } from "./shared/utils/env";
import { nodeAccessor } from "./chain/nodeAccessor";
import { activeChain } from "@shared/chainDefinitions";
import { activeChain, chainDefinitions } from "@shared/chainDefinitions";
import { addressBalanceMonitor, deploymentBalanceMonitor } from "./monitors";
import { updateProvidersLocation } from "./providers/ipLocationProvider";
import { sleep } from "./shared/utils/delay";
Expand Down Expand Up @@ -124,6 +124,10 @@ async function initApp() {
}
}

if (!(process.env.ActiveChain in chainDefinitions)) {
throw new Error(`Unknown chain with code: ${process.env.ActiveChain}`);
}

await initDatabase();
await nodeAccessor.loadNodeStatus();

Expand Down
Loading