Skip to content

Commit

Permalink
Features/improve usdc support (#13)
Browse files Browse the repository at this point in the history
* Add typing in templateReposProvider

* Add api sandbox support

* Improve deployment detail api endpoint

* Run provider syncing for sandbox

* Update indexer ibc denom for usdc

* Cleanup

* Add sdl denom validation

* Remove console.log
  • Loading branch information
Redm4x authored Sep 7, 2023
1 parent bcc850c commit 9ebdf66
Show file tree
Hide file tree
Showing 20 changed files with 294 additions and 62 deletions.
22 changes: 12 additions & 10 deletions api/package-lock.json

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

35 changes: 20 additions & 15 deletions api/src/providers/apiNodeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ import { Op } from "sequelize";
import { Provider, ProviderAttribute } from "@shared/dbSchemas/akash";
import { cacheKeys, cacheResponse } from "@src/caching/helpers";
import { env } from "@src/shared/utils/env";
import { RestDeploymentInfoResponse, RestLeaseListResponse } from "@src/types/rest";

const defaultNodeUrl = env.Network === "testnet" ? "https://api.testnet-02.aksh.pw:443" : "https://rest.cosmos.directory/akash";
const apiNodeUrl = env.RestApiNodeUrl ?? defaultNodeUrl;
const betaTypeVersion = env.Network === "testnet" ? "v1beta3" : "v1beta3";
const defaultNodeUrlMapping = {
mainnet: "https://rest.cosmos.directory/akash",
sandbox: "https://api.sandbox-01.aksh.pw",
testnet: "https://api.testnet-02.aksh.pw"
};

const apiNodeUrl = env.RestApiNodeUrl ?? defaultNodeUrlMapping[env.Network] ?? defaultNodeUrlMapping.mainnet;
const betaTypeVersion = "v1beta3";

export async function getChainStats() {
const result: { communityPool: number; inflation: number; communityTax: number; bondedTokens: number; totalSupply: number } = await cacheResponse(
Expand Down Expand Up @@ -323,13 +329,17 @@ export async function getDeployment(owner: string, dseq: string) {
return null;
}

const deploymentData = await deploymentResponse.json();
const deploymentData = (await deploymentResponse.json()) as RestDeploymentInfoResponse;

if (deploymentData.message?.toLowerCase().includes("deployment not found")) {
return null;
if ("code" in deploymentData) {
if (deploymentData.message?.toLowerCase().includes("deployment not found")) {
return null;
} else {
throw new Error(deploymentData.message);
}
}

const leasesData = await leasesResponse.json();
const leasesData = (await leasesResponse.json()) as RestLeaseListResponse;

const providerAddresses = leasesData.leases.map((x) => x.lease.lease_id.provider);
const providers = await Provider.findAll({
Expand All @@ -342,12 +352,10 @@ export async function getDeployment(owner: string, dseq: string) {
});

const marketData = await cacheResponse(60 * 5, cacheKeys.getMarketData, getMarketData);
const aktPrice = marketData?.price;
const deploymentDenom = deploymentData.escrow_account.balance.denom;

const leases = leasesData.leases.map((x) => {
const provider = providers.find((p) => p.owner === x.lease.lease_id.provider);
const monthlyUAKT = Math.round(parseFloat(x.lease.price.amount) * averageBlockCountInAMonth);
const group = deploymentData.groups.find((g) => g.group_id.gseq === x.lease.lease_id.gseq);

return {
Expand All @@ -363,10 +371,7 @@ export async function getDeployment(owner: string, dseq: string) {
}))
},
status: x.lease.state,
denom: deploymentDenom,
monthlyCostAKT: round(monthlyUAKT / 1_000_000, 2),
// TODO Improve: Add USDC into calculation
monthlyCostUSD: deploymentDenom === "uakt" ? (aktPrice ? round((monthlyUAKT / 1_000_000) * aktPrice, 2) : round(monthlyUAKT / 1_000_000, 2)) : null,
monthlyCostUDenom: Math.round(parseFloat(x.lease.price.amount) * averageBlockCountInAMonth),
cpuUnits: group.group_spec.resources.map((r) => parseInt(r.resource.cpu.units.val) * r.count).reduce((a, b) => a + b, 0),
gpuUnits: group.group_spec.resources.map((r) => parseInt(r.resource.gpu?.units?.val) * r.count || 0).reduce((a, b) => a + b, 0),
memoryQuantity: group.group_spec.resources.map((r) => parseInt(r.resource.memory.quantity.val) * r.count).reduce((a, b) => a + b, 0),
Expand All @@ -380,9 +385,9 @@ export async function getDeployment(owner: string, dseq: string) {
owner: deploymentData.deployment.deployment_id.owner,
dseq: deploymentData.deployment.deployment_id.dseq,
balance: parseFloat(deploymentData.escrow_account.balance.amount),
denom: deploymentDenom,
status: deploymentData.deployment.state,
totalMonthlyCostAKT: leases.map((x) => x.monthlyCostAKT).reduce((a, b) => a + b, 0),
totalMonthlyCostUSD: leases.map((x) => x.monthlyCostUSD).reduce((a, b) => a + b, 0),
totalMonthlyCostUDenom: leases.map((x) => x.monthlyCostUDenom).reduce((a, b) => a + b, 0),
leases: leases,
events: relatedMessages || [],
other: deploymentData
Expand Down
12 changes: 9 additions & 3 deletions api/src/providers/templateReposProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import * as fs from "fs";
import { Octokit } from "@octokit/rest";
import { getLogoFromPath } from "./templateReposLogos";
import { dataFolderPath } from "@src/shared/constants";
import { GithubChainRegistryAssetListResponse } from "@src/types";
import { GithubDirectoryItem } from "@src/types/github";

let generatingTasks = {};
let lastServedData = null;
Expand Down Expand Up @@ -139,7 +141,7 @@ async function fetchOmnibusTemplates(octokit: Octokit, repoVersion: string) {

githubRequestsRemaining = response.headers["x-ratelimit-remaining"];

if (!(response.data instanceof Array)) throw "Counld not fetch list of files from akash-network/cosmos-omnibus";
if (!Array.isArray(response.data)) throw "Could not fetch list of files from akash-network/cosmos-omnibus";

const folders = response.data.filter((f) => f.type === "dir" && !f.name.startsWith(".") && !f.name.startsWith("_"));
const templates = folders.map((x) => ({
Expand All @@ -159,7 +161,7 @@ async function fetchOmnibusTemplates(octokit: Octokit, repoVersion: string) {

if (assetListResponse.status !== 200) throw "Could not fetch assetlist.json";

const assetList = await assetListResponse.json();
const assetList = (await assetListResponse.json()) as GithubChainRegistryAssetListResponse;
if (assetList.assets.length === 0) {
throw "No asset found";
}
Expand Down Expand Up @@ -336,6 +338,8 @@ export async function fetchLinuxServerTemplatesInfo(octokit: Octokit, categories
}
});

if (!Array.isArray(response.data)) throw "Response data is not an array";

githubRequestsRemaining = response.headers["x-ratelimit-remaining"];

const readme = await findFileContentAsync("README.md", response.data);
Expand Down Expand Up @@ -418,6 +422,8 @@ export async function fetchTemplatesInfo(octokit: Octokit, categories: Category[
}
});

if (!Array.isArray(response.data)) throw "Response data is not an array";

githubRequestsRemaining = response.headers["x-ratelimit-remaining"];

const readme = await findFileContentAsync("README.md", response.data);
Expand Down Expand Up @@ -464,7 +470,7 @@ export async function fetchTemplatesInfo(octokit: Octokit, categories: Category[
}

// Find a github file by name and dowload it
async function findFileContentAsync(filename, fileList) {
async function findFileContentAsync(filename: string | string[], fileList: GithubDirectoryItem[]) {
const filenames = typeof filename === "string" ? [filename] : filename;
const fileDef = fileList.find((f) => filenames.some((x) => x.toLowerCase() === f.name.toLowerCase()));

Expand Down
20 changes: 20 additions & 0 deletions api/src/types/chainRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export type GithubChainRegistryAssetListResponse = {
$schema: string;
chain_name: string;
assets: {
description: string;
denom_units: {
denom: string;
exponent: number;
}[];
base: string;
name: string;
display: string;
symbol: string;
logo_URIs: {
png: string;
svg: string;
};
coingecko_id: string;
}[];
};
17 changes: 17 additions & 0 deletions api/src/types/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export type GithubDirectoryItem = {
type: string;
size: number;
name: string;
path: string;
content?: string;
sha: string;
url: string;
git_url: string;
html_url: string;
download_url: string;
_links: {
git: string;
html: string;
self: string;
};
};
4 changes: 3 additions & 1 deletion api/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./template"
export * from "./template";
export * from "./graph";
export * from "./chainRegistry";
103 changes: 103 additions & 0 deletions api/src/types/rest/deploymentInfoResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
export type RestDeploymentInfoResponse =
| {
code: number;
message: string;
details: string[];
}
| {
deployment: {
deployment_id: {
owner: string;
dseq: string;
};
state: string;
version: string;
created_at: string;
};
groups: {
group_id: {
owner: string;
dseq: string;
gseq: number;
};
state: string;
group_spec: {
name: string;
requirements: {
signed_by: {
all_of: string[];
any_of: string[];
};
attributes: { key: string; value: string }[];
};
resources: {
resource: {
id: number;
cpu: {
units: {
val: string;
};
attributes: { key: string; value: string }[];
};
memory: {
quantity: {
val: string;
};
attributes: { key: string; value: string }[];
};
storage: [
{
name: string;
quantity: {
val: string;
};
attributes: { key: string; value: string }[];
},
{
name: string;
quantity: {
val: string;
};
attributes: { key: string; value: string }[];
}
];
gpu: {
units: {
val: string;
};
attributes: { key: string; value: string }[];
};
endpoints: { kind: string; sequence_number: number }[];
};
count: number;
price: {
denom: string;
amount: string;
};
}[];
};
created_at: string;
}[];
escrow_account: {
id: {
scope: string;
xid: string;
};
owner: string;
state: string;
balance: {
denom: string;
amount: string;
};
transferred: {
denom: string;
amount: string;
};
settled_at: string;
depositor: string;
funds: {
denom: string;
amount: string;
};
};
};
2 changes: 2 additions & 0 deletions api/src/types/rest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./leaseListResponse";
export * from "./deploymentInfoResponse";
45 changes: 45 additions & 0 deletions api/src/types/rest/leaseListResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export type RestLeaseListResponse = {
leases: {
lease: {
lease_id: {
owner: string;
dseq: string;
gseq: number;
oseq: number;
provider: string;
};
state: string;
price: {
denom: string;
amount: string;
};
created_at: string;
closed_on: string;
};
escrow_payment: {
account_id: {
scope: string;
xid: string;
};
payment_id: string;
owner: string;
state: string;
rate: {
denom: string;
amount: string;
};
balance: {
denom: string;
amount: string;
};
withdrawn: {
denom: string;
amount: string;
};
};
}[];
pagination: {
next_key: string;
total: string;
};
};
2 changes: 1 addition & 1 deletion deploy-web/src/hooks/useWalletBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const useDenomData = (denom: string) => {
break;
case usdcIbcDenom:
depositData = {
label: "UDSC",
label: "USDC",
balance: udenomToDenom(walletBalances.usdc, 6),
inputMax: udenomToDenom(walletBalances.usdc - txFeeBuffer, 6)
};
Expand Down
Loading

0 comments on commit 9ebdf66

Please sign in to comment.