Skip to content

Commit

Permalink
add api query getFungibleAssetActivities
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin committed Oct 17, 2023
1 parent 6ab7b88 commit 5efb885
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 9 deletions.
25 changes: 21 additions & 4 deletions src/api/fungible_asset.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import { GetFungibleAssetMetadataResponse, PaginationArgs } from "../types";
import { GetFungibleAssetActivitiesResponse, GetFungibleAssetMetadataResponse, PaginationArgs } from "../types";
import { AptosConfig } from "./aptos_config";
import { getFungibleAssetMetadata } from "../internal/fungible_asset";
import { FungibleAssetMetadataBoolExp } from "../types/generated/types";
import { getFungibleAssetActivities, getFungibleAssetMetadata } from "../internal/fungible_asset";
import { FungibleAssetActivitiesBoolExp, FungibleAssetMetadataBoolExp } from "../types/generated/types";

/**
* A class to query all `FungibleAsset` related queries on Aptos.
Expand All @@ -24,12 +24,29 @@ export class FungibleAsset {
*
* @returns getFungibleAssetMetadata A list of fungible asset metadata
*/
async getFungibleAssetMetadata(args: {
async getFungibleAssetMetadata(args?: {
options?: {
pagination?: PaginationArgs;
where?: FungibleAssetMetadataBoolExp;
};
}): Promise<GetFungibleAssetMetadataResponse> {
return getFungibleAssetMetadata({ aptosConfig: this.config, ...args });
}

/**
* Queries the fungible asset activities
*
* This query returns the fungible asset activities.
* It can be filtered by owner address, asset type, and type.
*
* @returns GetFungibleAssetActivitiesResponse A list of fungible asset metadata
*/
async getFungibleAssetActivities(args?: {
options?: {
pagination?: PaginationArgs;
where?: FungibleAssetActivitiesBoolExp;
};
}): Promise<GetFungibleAssetActivitiesResponse> {
return getFungibleAssetActivities({ aptosConfig: this.config, ...args });
}
}
35 changes: 31 additions & 4 deletions src/internal/fungible_asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
*/

import { AptosConfig } from "../api/aptos_config";
import { GetFungibleAssetMetadataResponse, PaginationArgs } from "../types";
import { GetFungibleAssetActivitiesResponse, 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";
import { GetFungibleAssetActivities, GetFungibleAssetMetadata } from "../types/generated/queries";
import { GetFungibleAssetActivitiesQuery, GetFungibleAssetMetadataQuery } from "../types/generated/operations";
import { FungibleAssetActivitiesBoolExp, FungibleAssetMetadataBoolExp } from "../types/generated/types";

export async function getFungibleAssetMetadata(args: {
aptosConfig: AptosConfig;
Expand Down Expand Up @@ -41,3 +41,30 @@ export async function getFungibleAssetMetadata(args: {

return data.fungible_asset_metadata;
}

export async function getFungibleAssetActivities(args: {
aptosConfig: AptosConfig;
options?: {
pagination?: PaginationArgs;
where?: FungibleAssetActivitiesBoolExp;
};
}): Promise<GetFungibleAssetActivitiesResponse> {
const { aptosConfig, options } = args;

const graphqlQuery = {
query: GetFungibleAssetActivities,
variables: {
where_condition: options?.where,
limit: options?.pagination?.limit,
offset: options?.pagination?.offset,
},
};

const data = await queryIndexer<GetFungibleAssetActivitiesQuery>({
aptosConfig,
query: graphqlQuery,
originMethod: "getFungibleAssetActivities",
});

return data.fungible_asset_activities;
}
20 changes: 20 additions & 0 deletions src/internal/queries/getFungibleAssetActivities.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
query getFungibleAssetActivities($where_condition: fungible_asset_activities_bool_exp, $offset: Int, $limit: Int) {
fungible_asset_activities(where: $where_condition, offset: $offset, limit: $limit) {
amount
asset_type
block_height
entry_function_id_str
event_index
gas_fee_payer_address
is_frozen
is_gas_fee
is_transaction_success
owner_address
storage_id
storage_refund_amount
token_standard
transaction_timestamp
transaction_version
type
}
}
27 changes: 27 additions & 0 deletions src/types/generated/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,33 @@ export type GetEventsQuery = {
}>;
};

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

export type GetFungibleAssetActivitiesQuery = {
fungible_asset_activities: Array<{
amount?: any | null;
asset_type: string;
block_height: any;
entry_function_id_str?: string | null;
event_index: any;
gas_fee_payer_address?: string | null;
is_frozen?: boolean | null;
is_gas_fee: boolean;
is_transaction_success: boolean;
owner_address: string;
storage_id: string;
storage_refund_amount: any;
token_standard: string;
transaction_timestamp: any;
transaction_version: any;
type: string;
}>;
};

export type GetFungibleAssetMetadataQueryVariables = Types.Exact<{
where_condition?: Types.InputMaybe<Types.FungibleAssetMetadataBoolExp>;
offset?: Types.InputMaybe<Types.Scalars["Int"]>;
Expand Down
40 changes: 40 additions & 0 deletions src/types/generated/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,32 @@ export const GetEvents = `
}
}
`;
export const GetFungibleAssetActivities = `
query getFungibleAssetActivities($where_condition: fungible_asset_activities_bool_exp, $offset: Int, $limit: Int) {
fungible_asset_activities(
where: $where_condition
offset: $offset
limit: $limit
) {
amount
asset_type
block_height
entry_function_id_str
event_index
gas_fee_payer_address
is_frozen
is_gas_fee
is_transaction_success
owner_address
storage_id
storage_refund_amount
token_standard
transaction_timestamp
transaction_version
type
}
}
`;
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) {
Expand Down Expand Up @@ -532,6 +558,20 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
"query",
);
},
getFungibleAssetActivities(
variables?: Types.GetFungibleAssetActivitiesQueryVariables,
requestHeaders?: Dom.RequestInit["headers"],
): Promise<Types.GetFungibleAssetActivitiesQuery> {
return withWrapper(
(wrappedRequestHeaders) =>
client.request<Types.GetFungibleAssetActivitiesQuery>(GetFungibleAssetActivities, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
"getFungibleAssetActivities",
"query",
);
},
getFungibleAssetMetadata(
variables?: Types.GetFungibleAssetMetadataQueryVariables,
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 @@ -25,6 +25,7 @@ import {
GetTokenDataQuery,
GetProcessorStatusQuery,
GetFungibleAssetMetadataQuery,
GetFungibleAssetActivitiesQuery,
} from "./generated/operations";

/**
Expand Down Expand Up @@ -55,6 +56,7 @@ export type GetCollectionDataResponse = GetCollectionDataQuery["current_collecti
export type GetTokenDataResponse = GetTokenDataQuery["current_token_datas_v2"][0];
export type GetProcessorStatusResponse = GetProcessorStatusQuery["processor_status"];
export type GetFungibleAssetMetadataResponse = GetFungibleAssetMetadataQuery["fungible_asset_metadata"];
export type GetFungibleAssetActivitiesResponse = GetFungibleAssetActivitiesQuery["fungible_asset_activities"];

/**
* A generic type that being passed by each function and holds an
Expand Down
21 changes: 20 additions & 1 deletion tests/e2e/api/fungible_asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { Aptos, AptosConfig, Network } from "../../../src";
import { FungibleAssetMetadataBoolExp } from "../../../src/types/generated/types";
import { FungibleAssetActivitiesBoolExp, FungibleAssetMetadataBoolExp } from "../../../src/types/generated/types";

describe("FungibleAsset", () => {
test("it should fetch fungible asset metadata", async () => {
Expand All @@ -20,4 +20,23 @@ describe("FungibleAsset", () => {
expect(data[0]).toHaveProperty("asset_type");
expect(data[0].asset_type).toEqual(APT_COIN_TYPE);
});

test("it should fetch fungible asset activities with correct number and asset type ", 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.getFungibleAssetActivities({
options: {
pagination: {
limit: 2,
},
where: {
asset_type: { _eq: APT_COIN_TYPE },
} as FungibleAssetActivitiesBoolExp,
},
});
expect(data.length).toEqual(2);
expect(data[0].asset_type).toEqual(APT_COIN_TYPE);
expect(data[1].asset_type).toEqual(APT_COIN_TYPE);
});
});

0 comments on commit 5efb885

Please sign in to comment.