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

[balance] Fix APT balance on account page by using view function #790

Merged
merged 1 commit into from
Sep 24, 2024
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
36 changes: 11 additions & 25 deletions src/api/hooks/useGetAccountAPTBalance.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import {Types} from "aptos";
import {useGetAccountResources} from "./useGetAccountResources";

interface CoinStore {
coin: {
value: string;
};
}
import {useQuery} from "@tanstack/react-query";
import {ResponseError} from "../client";
import {getBalance} from "../index";
import {useGlobalState} from "../../global-config/GlobalConfig";

export function useGetAccountAPTBalance(address: Types.Address) {
const {isLoading, data, error} = useGetAccountResources(address);

if (isLoading || error || !data) {
return null;
}

const coinStore = data.find(
(resource) =>
resource.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
);

if (!coinStore) {
return null;
}

const coinStoreData: CoinStore = coinStore.data as CoinStore;

return coinStoreData?.coin?.value;
const [state] = useGlobalState();
// TODO: Convert all Types.Address to AccountAddress
return useQuery<string, ResponseError>({
queryKey: ["aptBalance", {address}, state.network_value],
queryFn: () => getBalance(state.sdk_v2_client!, address),
retry: false,
});
}
2 changes: 1 addition & 1 deletion src/api/hooks/useGetDelegationState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function useGetDelegationState(

return {
lockedUntilSecs,
balance,
balance: balance.data ?? null,
delegatorBalance,
rewardsRateYearly,
};
Expand Down
31 changes: 31 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import {OCTA} from "../constants";
import {isNumeric} from "../pages/utils";
import {sortTransactions} from "../utils";
import {withResponseError} from "./client";
import {
AccountAddressInput,
Aptos,
APTOS_COIN,
InputViewFunctionData,
TypeTagAddress,
TypeTagU64,
} from "@aptos-labs/ts-sdk";

export async function getTransactions(
requestParameters: {start?: number; limit?: number},
Expand Down Expand Up @@ -216,6 +224,29 @@ export async function getRecentBlocks(
return blocks;
}

export async function getBalance(
client: Aptos,
address: AccountAddressInput,
coinType?: `0x${string}::${string}::${string}`,
): Promise<string> {
const typeArguments = coinType ? [coinType] : [APTOS_COIN];

// TODO: Replace with native SDK call
const payload: InputViewFunctionData = {
function: "0x1::coin::balance",
typeArguments,
functionArguments: [address],
abi: {
parameters: [new TypeTagAddress()],
typeParameters: [{constraints: []}],
returnTypes: [new TypeTagU64()],
},
};
return withResponseError(
client.view<[string]>({payload}).then((res) => res[0]),
);
}

export async function getStake(
client: AptosClient,
delegatorAddress: Types.Address,
Expand Down
5 changes: 3 additions & 2 deletions src/pages/Account/BalanceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ type BalanceCardProps = {

export default function BalanceCard({address}: BalanceCardProps) {
const balance = useGetAccountAPTBalance(address);
console.log("BalanceCard balance", balance);

return balance ? (
return balance.data ? (
<Card height="auto">
<Stack spacing={1.5} marginY={1}>
<Typography fontSize={17} fontWeight={700}>
{`${getFormattedBalanceStr(balance)} APT`}
{`${getFormattedBalanceStr(balance.data)} APT`}
</Typography>
<Stack direction="row" spacing={1} alignItems="center">
<Typography fontSize={12} color={grey[450]}>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/DelegatoryValidator/MyDepositsSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function ActionsCell({
const requirement = getStakeOperationAPTRequirement(
stakes,
getStakeOperationFromStakingStatus(status, canWithdrawPendingInactive),
Number(balance),
Number(balance?.data ?? 0),
);

const buttonDisabled =
Expand Down
2 changes: 1 addition & 1 deletion src/pages/DelegatoryValidator/StakingBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function StakingBarContent({
// or when balance is less than add_stake fee if minimum stake amount is already met
const buttonDisabled =
account !== null &&
Number(balance) <=
Number(balance?.data ?? 0) <=
(Number(stakes[0]) === 0
? MINIMUM_APT_IN_POOL_FOR_EXPLORER * OCTA + Number(addStakeFee)
: Number(addStakeFee));
Expand Down