From c36f3e7c75ef4dbcccb7365e35e92e7c67066c27 Mon Sep 17 00:00:00 2001 From: Matt <90358481+xbtmatt@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:47:43 -0800 Subject: [PATCH] [ECO-2488] Fix emojicoin balance query by using the view function instead of the external indexer API (#404) --- .../lib/hooks/queries/use-wallet-balance.ts | 12 ++--- .../src/emojicoin_dot_fun/aptos-framework.ts | 54 ++++++++++++++++++- .../sdk/src/emojicoin_dot_fun/index.ts | 1 + 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/typescript/frontend/src/lib/hooks/queries/use-wallet-balance.ts b/src/typescript/frontend/src/lib/hooks/queries/use-wallet-balance.ts index 175617aa9..e969218d4 100644 --- a/src/typescript/frontend/src/lib/hooks/queries/use-wallet-balance.ts +++ b/src/typescript/frontend/src/lib/hooks/queries/use-wallet-balance.ts @@ -3,6 +3,7 @@ import { useQuery } from "@tanstack/react-query"; import { withResponseError } from "./client"; import { useCallback, useMemo, useRef, useState } from "react"; import { type TypeTagInput } from "@sdk/emojicoin_dot_fun"; +import { Balance } from "@sdk/emojicoin_dot_fun/aptos-framework"; /** * __NOTE: If you're using this for a connected user's APT balance, you should use__ @@ -60,12 +61,11 @@ export const useWalletBalance = ({ return res; } return withResponseError( - aptos - .getAccountCoinAmount({ - accountAddress, - coinType: coinType.toString() as `${string}::${string}::${string}`, - }) - .then((r) => BigInt(r)) + Balance.view({ + aptos, + owner: accountAddress, + typeTags: [coinType], + }).then((res) => BigInt(res)) ); }, placeholderData: (previousBalance) => previousBalance ?? 0, diff --git a/src/typescript/sdk/src/emojicoin_dot_fun/aptos-framework.ts b/src/typescript/sdk/src/emojicoin_dot_fun/aptos-framework.ts index 9a2b87ee4..f069dc6f8 100644 --- a/src/typescript/sdk/src/emojicoin_dot_fun/aptos-framework.ts +++ b/src/typescript/sdk/src/emojicoin_dot_fun/aptos-framework.ts @@ -2,13 +2,13 @@ import { MoveVector, AccountAddress, + type Aptos, U64, type AccountAddressInput, type Uint64, type AptosConfig, type InputGenerateTransactionOptions, buildTransaction, - type Aptos, type Account, type WaitForTransactionOptions, type UserTransactionResponse, @@ -21,7 +21,7 @@ import { EntryFunctionTransactionBuilder, ViewFunctionPayloadBuilder, } from "./payload-builders"; -import { type TypeTagInput } from "."; +import { type Uint64String, type TypeTagInput } from "."; import { getAptosClient } from "../utils/aptos-client"; export type MintPayloadMoveArguments = { @@ -365,3 +365,53 @@ export class ExistsAt extends ViewFunctionPayloadBuilder<[boolean]> { return res; } } + +export type BalancePayloadMoveArguments = { + owner: AccountAddress; +}; + +/** + *``` + * #[view] + * public fun balance( + * owner: address, + * ): u64 + *``` + * */ + +export class Balance extends ViewFunctionPayloadBuilder<[Uint64String]> { + public readonly moduleAddress = AccountAddress.ONE; + + public readonly moduleName = "coin"; + + public readonly functionName = "balance"; + + public readonly args: BalancePayloadMoveArguments; + + public readonly typeTags: [TypeTag]; // [CoinType] + + constructor(args: { + owner: AccountAddressInput; // address + typeTags: [TypeTagInput]; // [CoinType] + }) { + super(); + const { owner, typeTags } = args; + + this.args = { + owner: AccountAddress.from(owner), + }; + this.typeTags = typeTags.map((typeTag) => + typeof typeTag === "string" ? parseTypeTag(typeTag) : typeTag + ) as [TypeTag]; + } + + static async view(args: { + aptos: Aptos | AptosConfig; + owner: AccountAddressInput; // address + typeTags: [TypeTagInput]; // [CoinType] + options?: LedgerVersionArg; + }): Promise { + const [res] = await new Balance(args).view(args); + return res; + } +} diff --git a/src/typescript/sdk/src/emojicoin_dot_fun/index.ts b/src/typescript/sdk/src/emojicoin_dot_fun/index.ts index cf822b613..4525fb057 100644 --- a/src/typescript/sdk/src/emojicoin_dot_fun/index.ts +++ b/src/typescript/sdk/src/emojicoin_dot_fun/index.ts @@ -1,3 +1,4 @@ +export * as AptosFramework from "./aptos-framework"; export * as EmojicoinDotFun from "./emojicoin-dot-fun"; export * from "./types"; export * from "./utils";