Skip to content

Commit

Permalink
Add new api getFungibleAssetMetadata (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmigo authored Oct 17, 2023
1 parent d1e3155 commit e32ac43
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/api/aptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -33,6 +34,8 @@ export class Aptos {

readonly faucet: Faucet;

readonly fungible_asset: FungibleAsset;

readonly general: General;

readonly staking: Staking;
Expand All @@ -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);
Expand All @@ -64,6 +68,7 @@ export interface Aptos
Collection,
Event,
Faucet,
FungibleAsset,
General,
Staking,
Token,
Expand Down Expand Up @@ -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");
Expand Down
35 changes: 35 additions & 0 deletions src/api/fungible_asset.ts
Original file line number Diff line number Diff line change
@@ -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<GetFungibleAssetMetadataResponse> {
return getFungibleAssetMetadata({ aptosConfig: this.config, ...args });
}
}
43 changes: 43 additions & 0 deletions src/internal/fungible_asset.ts
Original file line number Diff line number Diff line change
@@ -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<GetFungibleAssetMetadataResponse> {
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<GetFungibleAssetMetadataQuery>({
aptosConfig,
query: graphqlQuery,
originMethod: "getFungibleAssetMetadata",
});

return data.fungible_asset_metadata;
}
16 changes: 16 additions & 0 deletions src/internal/queries/getFungibleAssetMetadata.graphql
Original file line number Diff line number Diff line change
@@ -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
}
}
23 changes: 23 additions & 0 deletions src/types/generated/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,29 @@ export type GetEventsQuery = {
}>;
};

export type GetFungibleAssetMetadataQueryVariables = Types.Exact<{
where_condition?: Types.InputMaybe<Types.FungibleAssetMetadataBoolExp>;
offset?: Types.InputMaybe<Types.Scalars["Int"]>;
limit?: Types.InputMaybe<Types.Scalars["Int"]>;
}>;

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<Array<Types.NumActiveDelegatorPerPoolOrderBy> | Types.NumActiveDelegatorPerPoolOrderBy>;
Expand Down
32 changes: 32 additions & 0 deletions src/types/generated/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -514,6 +532,20 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
"query",
);
},
getFungibleAssetMetadata(
variables?: Types.GetFungibleAssetMetadataQueryVariables,
requestHeaders?: Dom.RequestInit["headers"],
): Promise<Types.GetFungibleAssetMetadataQuery> {
return withWrapper(
(wrappedRequestHeaders) =>
client.request<Types.GetFungibleAssetMetadataQuery>(GetFungibleAssetMetadata, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
"getFungibleAssetMetadata",
"query",
);
},
getNumberOfDelegators(
variables: Types.GetNumberOfDelegatorsQueryVariables,
requestHeaders?: Dom.RequestInit["headers"],
Expand Down
2 changes: 2 additions & 0 deletions src/types/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
GetEventsQuery,
GetTokenDataQuery,
GetProcessorStatusQuery,
GetFungibleAssetMetadataQuery,
} from "./generated/operations";

/**
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/e2e/api/fungible_asset.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit e32ac43

Please sign in to comment.