diff --git a/src/api/aptos.ts b/src/api/aptos.ts index f7cff84cf..b81701b05 100644 --- a/src/api/aptos.ts +++ b/src/api/aptos.ts @@ -7,6 +7,7 @@ import { Coin } from "./coin"; import { Collection } from "./collection"; import { Event } from "./event"; import { Faucet } from "./faucet"; +import { FungibleAsset } from "./fungible_asset"; import { General } from "./general"; import { Staking } from "./staking"; import { Token } from "./token"; @@ -33,6 +34,8 @@ export class Aptos { readonly faucet: Faucet; + readonly fungible_asset: FungibleAsset; + readonly general: General; readonly staking: Staking; @@ -50,6 +53,7 @@ export class Aptos { this.collection = new Collection(this.config); this.event = new Event(this.config); this.faucet = new Faucet(this.config); + this.fungible_asset = new FungibleAsset(this.config); this.general = new General(this.config); this.staking = new Staking(this.config); this.token = new Token(this.config); @@ -64,6 +68,7 @@ export interface Aptos Collection, Event, Faucet, + FungibleAsset, General, Staking, Token, @@ -96,6 +101,7 @@ applyMixin(Aptos, Coin, "coin"); applyMixin(Aptos, Collection, "collection"); applyMixin(Aptos, Event, "event"); applyMixin(Aptos, Faucet, "faucet"); +applyMixin(Aptos, FungibleAsset, "fungible_asset"); applyMixin(Aptos, General, "general"); applyMixin(Aptos, Staking, "staking"); applyMixin(Aptos, Token, "token"); diff --git a/src/api/fungible_asset.ts b/src/api/fungible_asset.ts new file mode 100644 index 000000000..9d78c3440 --- /dev/null +++ b/src/api/fungible_asset.ts @@ -0,0 +1,35 @@ +// Copyright © Aptos Foundation +// SPDX-License-Identifier: Apache-2.0 + +import { GetFungibleAssetMetadataResponse, PaginationArgs } from "../types"; +import { AptosConfig } from "./aptos_config"; +import { getFungibleAssetMetadata } from "../internal/fungible_asset"; +import { FungibleAssetMetadataBoolExp } from "../types/generated/types"; + +/** + * A class to query all `FungibleAsset` related queries on Aptos. + */ +export class FungibleAsset { + readonly config: AptosConfig; + + constructor(config: AptosConfig) { + this.config = config; + } + + /** + * Queries the current fungible asset metadata. + * + * This query returns the fungible asset metadata for all fungible assets. + * It can be filtered by creator address and asset type. + * + * @returns getFungibleAssetMetadata A list of fungible asset metadata + */ + async getFungibleAssetMetadata(args: { + options?: { + pagination?: PaginationArgs; + where?: FungibleAssetMetadataBoolExp; + }; + }): Promise { + return getFungibleAssetMetadata({ aptosConfig: this.config, ...args }); + } +} diff --git a/src/internal/fungible_asset.ts b/src/internal/fungible_asset.ts new file mode 100644 index 000000000..b23624391 --- /dev/null +++ b/src/internal/fungible_asset.ts @@ -0,0 +1,43 @@ +// Copyright © Aptos Foundation +// SPDX-License-Identifier: Apache-2.0 + +/** + * This file contains the underlying implementations for exposed API surface in + * the {@link api/fungible_asset}. By moving the methods out into a separate file, + * other namespaces and processes can access these methods without depending on the entire + * fungible_asset namespace and without having a dependency cycle error. + */ + +import { AptosConfig } from "../api/aptos_config"; +import { GetFungibleAssetMetadataResponse, PaginationArgs } from "../types"; +import { queryIndexer } from "./general"; +import { GetFungibleAssetMetadata } from "../types/generated/queries"; +import { GetFungibleAssetMetadataQuery } from "../types/generated/operations"; +import { FungibleAssetMetadataBoolExp } from "../types/generated/types"; + +export async function getFungibleAssetMetadata(args: { + aptosConfig: AptosConfig; + options?: { + pagination?: PaginationArgs; + where?: FungibleAssetMetadataBoolExp; + }; +}): Promise { + const { aptosConfig, options } = args; + + const graphqlQuery = { + query: GetFungibleAssetMetadata, + variables: { + where_condition: options?.where, + limit: options?.pagination?.limit, + offset: options?.pagination?.offset, + }, + }; + + const data = await queryIndexer({ + aptosConfig, + query: graphqlQuery, + originMethod: "getFungibleAssetMetadata", + }); + + return data.fungible_asset_metadata; +} diff --git a/src/internal/queries/getFungibleAssetMetadata.graphql b/src/internal/queries/getFungibleAssetMetadata.graphql new file mode 100644 index 000000000..70956103a --- /dev/null +++ b/src/internal/queries/getFungibleAssetMetadata.graphql @@ -0,0 +1,16 @@ +query getFungibleAssetMetadata($where_condition: fungible_asset_metadata_bool_exp, $offset: Int, $limit: Int) { + fungible_asset_metadata(where: $where_condition, offset: $offset, limit: $limit) { + icon_uri + project_uri + supply_aggregator_table_handle_v1 + supply_aggregator_table_key_v1 + creator_address + asset_type + decimals + last_transaction_timestamp + last_transaction_version + name + symbol + token_standard + } +} diff --git a/src/types/generated/operations.ts b/src/types/generated/operations.ts index 70c604972..c2a4f2428 100644 --- a/src/types/generated/operations.ts +++ b/src/types/generated/operations.ts @@ -396,6 +396,29 @@ export type GetEventsQuery = { }>; }; +export type GetFungibleAssetMetadataQueryVariables = Types.Exact<{ + where_condition?: Types.InputMaybe; + offset?: Types.InputMaybe; + limit?: Types.InputMaybe; +}>; + +export type GetFungibleAssetMetadataQuery = { + fungible_asset_metadata: Array<{ + icon_uri?: string | null; + project_uri?: string | null; + supply_aggregator_table_handle_v1?: string | null; + supply_aggregator_table_key_v1?: string | null; + creator_address: string; + asset_type: string; + decimals: number; + last_transaction_timestamp: any; + last_transaction_version: any; + name: string; + symbol: string; + token_standard: string; + }>; +}; + export type GetNumberOfDelegatorsQueryVariables = Types.Exact<{ where_condition: Types.NumActiveDelegatorPerPoolBoolExp; order_by?: Types.InputMaybe | Types.NumActiveDelegatorPerPoolOrderBy>; diff --git a/src/types/generated/queries.ts b/src/types/generated/queries.ts index a126318df..ddcedaad0 100644 --- a/src/types/generated/queries.ts +++ b/src/types/generated/queries.ts @@ -265,6 +265,24 @@ export const GetEvents = ` } } `; +export const GetFungibleAssetMetadata = ` + query getFungibleAssetMetadata($where_condition: fungible_asset_metadata_bool_exp, $offset: Int, $limit: Int) { + fungible_asset_metadata(where: $where_condition, offset: $offset, limit: $limit) { + icon_uri + project_uri + supply_aggregator_table_handle_v1 + supply_aggregator_table_key_v1 + creator_address + asset_type + decimals + last_transaction_timestamp + last_transaction_version + name + symbol + token_standard + } +} + `; export const GetNumberOfDelegators = ` query getNumberOfDelegators($where_condition: num_active_delegator_per_pool_bool_exp!, $order_by: [num_active_delegator_per_pool_order_by!]) { num_active_delegator_per_pool(where: $where_condition, order_by: $order_by) { @@ -514,6 +532,20 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = "query", ); }, + getFungibleAssetMetadata( + variables?: Types.GetFungibleAssetMetadataQueryVariables, + requestHeaders?: Dom.RequestInit["headers"], + ): Promise { + return withWrapper( + (wrappedRequestHeaders) => + client.request(GetFungibleAssetMetadata, variables, { + ...requestHeaders, + ...wrappedRequestHeaders, + }), + "getFungibleAssetMetadata", + "query", + ); + }, getNumberOfDelegators( variables: Types.GetNumberOfDelegatorsQueryVariables, requestHeaders?: Dom.RequestInit["headers"], diff --git a/src/types/indexer.ts b/src/types/indexer.ts index 84d356a31..a7c5400ee 100644 --- a/src/types/indexer.ts +++ b/src/types/indexer.ts @@ -24,6 +24,7 @@ import { GetEventsQuery, GetTokenDataQuery, GetProcessorStatusQuery, + GetFungibleAssetMetadataQuery, } from "./generated/operations"; /** @@ -53,6 +54,7 @@ export type GetDelegatedStakingActivitiesResponse = GetDelegatedStakingActivitie export type GetCollectionDataResponse = GetCollectionDataQuery["current_collections_v2"][0]; export type GetTokenDataResponse = GetTokenDataQuery["current_token_datas_v2"][0]; export type GetProcessorStatusResponse = GetProcessorStatusQuery["processor_status"]; +export type GetFungibleAssetMetadataResponse = GetFungibleAssetMetadataQuery["fungible_asset_metadata"]; /** * A generic type that being passed by each function and holds an diff --git a/tests/e2e/api/fungible_asset.test.ts b/tests/e2e/api/fungible_asset.test.ts new file mode 100644 index 000000000..f1c3e0cdf --- /dev/null +++ b/tests/e2e/api/fungible_asset.test.ts @@ -0,0 +1,23 @@ +// Copyright © Aptos Foundation +// SPDX-License-Identifier: Apache-2.0 + +import { Aptos, AptosConfig, Network } from "../../../src"; +import { FungibleAssetMetadataBoolExp } from "../../../src/types/generated/types"; + +describe("FungibleAsset", () => { + test("it should fetch fungible asset metadata", async () => { + const config = new AptosConfig({ network: Network.LOCAL }); + const aptos = new Aptos(config); + const APT_COIN_TYPE = "0x1::aptos_coin::AptosCoin"; + const data = await aptos.getFungibleAssetMetadata({ + options: { + where: { + asset_type: { _eq: APT_COIN_TYPE }, + } as FungibleAssetMetadataBoolExp, + }, + }); + expect(data.length).toEqual(1); + expect(data[0]).toHaveProperty("asset_type"); + expect(data[0].asset_type).toEqual(APT_COIN_TYPE); + }); +});