Skip to content

Commit

Permalink
[ECO-2488] Fix emojicoin balance query by using the view function ins…
Browse files Browse the repository at this point in the history
…tead of the external indexer API (#404)
  • Loading branch information
xbtmatt authored Nov 21, 2024
1 parent 1021725 commit c36f3e7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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,
Expand Down
54 changes: 52 additions & 2 deletions src/typescript/sdk/src/emojicoin_dot_fun/aptos-framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down Expand Up @@ -365,3 +365,53 @@ export class ExistsAt extends ViewFunctionPayloadBuilder<[boolean]> {
return res;
}
}

export type BalancePayloadMoveArguments = {
owner: AccountAddress;
};

/**
*```
* #[view]
* public fun balance<CoinType>(
* 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<Uint64String> {
const [res] = await new Balance(args).view(args);
return res;
}
}
1 change: 1 addition & 0 deletions src/typescript/sdk/src/emojicoin_dot_fun/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * as AptosFramework from "./aptos-framework";
export * as EmojicoinDotFun from "./emojicoin-dot-fun";
export * from "./types";
export * from "./utils";

0 comments on commit c36f3e7

Please sign in to comment.