Skip to content

Commit

Permalink
implement deployment deposit params query on chain
Browse files Browse the repository at this point in the history
  • Loading branch information
baktun14 committed Mar 1, 2024
1 parent ca4df49 commit 4d964f4
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
15 changes: 11 additions & 4 deletions deploy-web/src/hooks/useWalletBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { udenomToDenom } from "@src/utils/mathHelpers";
import { uaktToAKT } from "@src/utils/priceUtils";
import { useEffect, useState } from "react";
import { useUsdcDenom } from "./useDenom";
import { useDepositParams } from "@src/queries/useSettings";

export const useTotalWalletBalance = () => {
const { isLoaded, price } = usePricing();
Expand All @@ -24,6 +25,7 @@ export const useTotalWalletBalance = () => {
};

type DenomData = {
min: number;
label: string;
balance: number;
inputMax: number;
Expand All @@ -34,20 +36,26 @@ export const useDenomData = (denom: string) => {
const { walletBalances } = useWallet();
const [depositData, setDepositData] = useState<DenomData>(null);
const usdcIbcDenom = useUsdcDenom();
const { data: depositParams } = useDepositParams();

useEffect(() => {
if (isLoaded && walletBalances) {
let depositData: DenomData = null;
if (isLoaded && walletBalances && depositParams) {
let depositData: DenomData = null,
params = null;
switch (denom) {
case uAktDenom:
params = depositParams.find(p => p.denom === uAktDenom);
depositData = {
min: parseInt(params?.amount || 0),
label: "AKT",
balance: uaktToAKT(walletBalances.uakt, 6),
inputMax: uaktToAKT(Math.max(walletBalances.uakt - txFeeBuffer, 0), 6)
};
break;
case usdcIbcDenom:
params = depositParams.find(p => p.denom === usdcIbcDenom);
depositData = {
min: parseInt(params?.amount || 0),
label: "USDC",
balance: udenomToDenom(walletBalances.usdc, 6),
inputMax: udenomToDenom(Math.max(walletBalances.usdc - txFeeBuffer, 0), 6)
Expand All @@ -59,8 +67,7 @@ export const useDenomData = (denom: string) => {

setDepositData(depositData);
}
}, [denom, isLoaded, price, walletBalances, usdcIbcDenom]);
}, [denom, isLoaded, price, walletBalances, usdcIbcDenom, depositParams]);

return depositData;
};

1 change: 1 addition & 0 deletions deploy-web/src/queries/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ export class QueryKeys {
static getBalancesKey = (address: string) => ["BALANCES", address];
static getTemplatesKey = () => ["TEMPLATES"];
static getProviderAttributesSchema = () => ["PROVIDER_ATTRIBUTES_SCHEMA"];
static getDepositParamsKey = () => ["DEPOSIT_PARAMS"];
}
21 changes: 20 additions & 1 deletion deploy-web/src/queries/useSettings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { useSettings } from "@src/context/SettingsProvider";
import { useCustomUser } from "@src/hooks/useCustomUser";
import { UserSettings } from "@src/types/user";
import { ApiUrlService } from "@src/utils/apiUtils";
import axios, { AxiosResponse } from "axios";
import { useSnackbar } from "notistack";
import { useMutation } from "react-query";
import { useMutation, useQuery } from "react-query";
import { QueryKeys } from "./queryKeys";
import { DepositParams, RpcDeposiParams } from "@src/types/deployment";

export function useSaveSettings() {
const { enqueueSnackbar } = useSnackbar();
Expand All @@ -19,3 +23,18 @@ export function useSaveSettings() {
}
});
}

async function getDepositParams(apiEndpoint: string) {
const depositParamsQuery = await axios.get(ApiUrlService.depositParams(apiEndpoint));
const depositParams = depositParamsQuery.data as RpcDeposiParams;
const params = JSON.parse(depositParams.params.value) as DepositParams[];

console.log(depositParams, params);

return params;
}

export function useDepositParams(options = {}) {
const { settings } = useSettings();
return useQuery(QueryKeys.getDepositParamsKey(), () => getDepositParams(settings.apiEndpoint), options);
}
22 changes: 18 additions & 4 deletions deploy-web/src/types/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,26 @@ interface DeploymentResource_V2 {
units: {
val: string;
};
attributes: {key: string, value: string}[];
attributes: { key: string; value: string }[];
};
gpu: {
units: {
val: string;
};
attributes: {key: string, value: string}[];
attributes: { key: string; value: string }[];
};
memory: {
quantity: {
val: string;
};
attributes: {key: string, value: string}[];
attributes: { key: string; value: string }[];
};
storage: Array<{
name: string;
quantity: {
val: string;
};
attributes: {key: string, value: string}[];
attributes: { key: string; value: string }[];
}>;
endpoints: Array<{
kind: string;
Expand Down Expand Up @@ -319,3 +319,17 @@ export interface BidDto {
count: number;
}>;
}

export interface RpcDeposiParams {
params: {
subspace: string;
key: string;
// Array of { denom: string, amount: string }
value: string;
};
}

export interface DepositParams {
denom: string;
amount: string;
}
2 changes: 1 addition & 1 deletion deploy-web/src/types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export type Network = {
rpcEndpoint?: string;
version: string;
enabled: boolean;
};
};
3 changes: 3 additions & 0 deletions deploy-web/src/utils/apiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import axios from "axios";
import { appendSearchParams } from "./urlUtils";

export class ApiUrlService {
static depositParams(apiEndpoint: string) {
return `${apiEndpoint}/cosmos/params/v1beta1/params?subspace=deployment&key=MinDeposits`;
}
static deploymentList(apiEndpoint: string, address: string) {
return `${apiEndpoint}/akash/deployment/${networkVersion}/deployments/list?filters.owner=${address}`;
}
Expand Down
2 changes: 1 addition & 1 deletion deploy-web/src/utils/deploymentDetailUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeploymentDto, LeaseDto, RpcDeployment, RpcLease } from "@src/types/deployment";
import { DeploymentDto, DepositParams, LeaseDto, RpcDeployment, RpcDeposiParams, RpcLease } from "@src/types/deployment";
import { coinToUDenom } from "./priceUtils";

export function deploymentResourceSum(deployment: RpcDeployment, resourceSelector) {
Expand Down

0 comments on commit 4d964f4

Please sign in to comment.