Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish the token related API for alpha #88

Merged
merged 10 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions src/api/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
// SPDX-License-Identifier: Apache-2.0

import { AptosConfig } from "./aptosConfig";
import { getTokenData } from "../internal/token";
import { GetTokenDataResponse, HexInput } from "../types";
import { getCurrentTokenOwnership, getOwnedTokens, getTokenActivity, getTokenData } from "../internal/token";
import {
GetCurrentTokenOwnershipResponse,
GetOwnedTokensResponse,
GetTokenActivityResponse,
GetTokenDataResponse,
HexInput,
OrderBy,
PaginationArgs,
} from "../types";

/**
* A class to query all `Token` related queries on Aptos.
Expand All @@ -24,4 +32,40 @@ export class Token {
async getTokenData(args: { tokenAddress: HexInput }): Promise<GetTokenDataResponse> {
return getTokenData({ aptosConfig: this.config, ...args });
}

/**
* This gets token ownership data given the address of a token.
*
* @param args.tokenAddress The address of the token
* @returns GetCurrentTokenOwnershipResponse containing relevant ownership data of the token.
*/
async getCurrentTokenOwnership(args: { tokenAddress: HexInput }): Promise<GetCurrentTokenOwnershipResponse> {
return getCurrentTokenOwnership({ aptosConfig: this.config, ...args });
}

/**
* This gets the tokens that the given address owns.
*
* @param args.ownerAddress The address of the owner
* @returns GetOwnedTokensResponse containing ownership data of the tokens belonging to the ownerAddresss.
*/
async getOwnedTokens(args: { ownerAddress: HexInput }): Promise<GetOwnedTokensResponse> {
return getOwnedTokens({ aptosConfig: this.config, ...args });
}

/**
* This gets the activity data given the address of a token.
*
* @param args.tokenAddress The address of the token
* @returns GetTokenActivityResponse containing relevant activity data to the token.
*/
async getTokenActivity(args: {
tokenAddress: HexInput;
options?: {
pagination?: PaginationArgs;
orderBy?: OrderBy<GetTokenActivityResponse[0]>;
};
}): Promise<GetTokenActivityResponse> {
return getTokenActivity({ aptosConfig: this.config, ...args });
}
}
17 changes: 17 additions & 0 deletions src/internal/queries/TokenActivitiesFieldsFragment.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fragment TokenActivitiesFields on token_activities_v2 {
after_value
before_value
entry_function_id_str
event_account_address
event_index
from_address
is_fungible_v2
property_version_v1
to_address
token_amount
token_data_id
token_standard
transaction_timestamp
transaction_version
type
}
11 changes: 11 additions & 0 deletions src/internal/queries/getTokenActivity.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import "./TokenActivitiesFieldsFragment";
query getTokenActivity(
$where_condition: token_activities_v2_bool_exp!
$offset: Int
$limit: Int
$order_by: [token_activities_v2_order_by!]
) {
token_activities_v2(where: $where_condition, order_by: $order_by, offset: $offset, limit: $limit) {
...TokenActivitiesFields
}
}
11 changes: 11 additions & 0 deletions src/internal/queries/getTokenCurrentOwner.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import "./CurrentTokenOwnershipFieldsFragment";
query getCurrentTokenOwnership(
$where_condition: current_token_ownerships_v2_bool_exp!
$offset: Int
$limit: Int
$order_by: [current_token_ownerships_v2_order_by!]
) {
current_token_ownerships_v2(where: $where_condition, offset: $offset, limit: $limit, order_by: $order_by) {
...CurrentTokenOwnershipFields
}
}
100 changes: 97 additions & 3 deletions src/internal/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@

import { AptosConfig } from "../api/aptosConfig";
import { Hex } from "../core";
import { GetTokenDataResponse, HexInput } from "../types";
import { GetTokenDataQuery } from "../types/generated/operations";
import { GetTokenData } from "../types/generated/queries";
import {
GetCurrentTokenOwnershipResponse,
GetOwnedTokensResponse,
GetTokenActivityResponse,
GetTokenDataResponse,
HexInput,
OrderBy,
PaginationArgs,
} from "../types";
import { GetCurrentTokenOwnershipQuery, GetTokenActivityQuery, GetTokenDataQuery } from "../types/generated/operations";
import { GetCurrentTokenOwnership, GetTokenActivity, GetTokenData } from "../types/generated/queries";
import { CurrentTokenOwnershipsV2BoolExp, TokenActivitiesBoolExp } from "../types/generated/types";
import { queryIndexer } from "./general";

export async function getTokenData(args: {
Expand Down Expand Up @@ -40,3 +49,88 @@ export async function getTokenData(args: {

return data.current_token_datas_v2[0];
}

export async function getCurrentTokenOwnership(args: {
aptosConfig: AptosConfig;
tokenAddress: HexInput;
}): Promise<GetCurrentTokenOwnershipResponse> {
const { aptosConfig, tokenAddress } = args;

const whereCondition: CurrentTokenOwnershipsV2BoolExp = {
token_data_id: { _eq: Hex.fromHexInput(tokenAddress).toString() },
};

const graphqlQuery = {
query: GetCurrentTokenOwnership,
variables: {
where_condition: whereCondition,
},
};

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

return data.current_token_ownerships_v2[0];
}

export async function getOwnedTokens(args: {
aptosConfig: AptosConfig;
ownerAddress: HexInput;
}): Promise<GetOwnedTokensResponse> {
const { aptosConfig, ownerAddress } = args;

const whereCondition: CurrentTokenOwnershipsV2BoolExp = {
owner_address: { _eq: Hex.fromHexInput(ownerAddress).toString() },
};

const graphqlQuery = {
query: GetCurrentTokenOwnership,
variables: {
where_condition: whereCondition,
},
};

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

return data.current_token_ownerships_v2;
}

export async function getTokenActivity(args: {
aptosConfig: AptosConfig;
tokenAddress: HexInput;
options?: {
pagination?: PaginationArgs;
orderBy?: OrderBy<GetTokenActivityResponse[0]>;
};
}): Promise<GetTokenActivityResponse> {
const { aptosConfig, tokenAddress, options } = args;

const whereCondition: TokenActivitiesBoolExp = {
token_data_id_hash: { _eq: Hex.fromHexInput(tokenAddress).toString() },
};

const graphqlQuery = {
query: GetTokenActivity,
variables: {
where_condition: whereCondition,
offset: options?.pagination?.offset,
limit: options?.pagination?.limit,
order_by: options?.orderBy,
},
};

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

return data.token_activities_v2;
}
100 changes: 100 additions & 0 deletions src/types/generated/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ export type CurrentTokenOwnershipFieldsFragment = {
} | null;
};

export type TokenActivitiesFieldsFragment = {
after_value?: string | null;
before_value?: string | null;
entry_function_id_str?: string | null;
event_account_address: string;
event_index: any;
from_address?: string | null;
is_fungible_v2?: boolean | null;
property_version_v1: any;
to_address?: string | null;
token_amount: any;
token_data_id: string;
token_standard: string;
transaction_timestamp: any;
transaction_version: any;
type: string;
};

export type GetAccountCoinsCountQueryVariables = Types.Exact<{
address?: Types.InputMaybe<Types.Scalars["String"]>;
}>;
Expand Down Expand Up @@ -481,6 +499,88 @@ export type GetProcessorStatusQuery = {
processor_status: Array<{ last_success_version: any; processor: string; last_updated: any }>;
};

export type GetTokenActivityQueryVariables = Types.Exact<{
where_condition: Types.TokenActivitiesV2BoolExp;
offset?: Types.InputMaybe<Types.Scalars["Int"]>;
limit?: Types.InputMaybe<Types.Scalars["Int"]>;
order_by?: Types.InputMaybe<Array<Types.TokenActivitiesV2OrderBy> | Types.TokenActivitiesV2OrderBy>;
}>;

export type GetTokenActivityQuery = {
token_activities_v2: Array<{
after_value?: string | null;
before_value?: string | null;
entry_function_id_str?: string | null;
event_account_address: string;
event_index: any;
from_address?: string | null;
is_fungible_v2?: boolean | null;
property_version_v1: any;
to_address?: string | null;
token_amount: any;
token_data_id: string;
token_standard: string;
transaction_timestamp: any;
transaction_version: any;
type: string;
}>;
};

export type GetCurrentTokenOwnershipQueryVariables = Types.Exact<{
where_condition: Types.CurrentTokenOwnershipsV2BoolExp;
offset?: Types.InputMaybe<Types.Scalars["Int"]>;
limit?: Types.InputMaybe<Types.Scalars["Int"]>;
order_by?: Types.InputMaybe<Array<Types.CurrentTokenOwnershipsV2OrderBy> | Types.CurrentTokenOwnershipsV2OrderBy>;
}>;

export type GetCurrentTokenOwnershipQuery = {
current_token_ownerships_v2: Array<{
token_standard: string;
token_properties_mutated_v1?: any | null;
token_data_id: string;
table_type_v1?: string | null;
storage_id: string;
property_version_v1: any;
owner_address: string;
last_transaction_version: any;
last_transaction_timestamp: any;
is_soulbound_v2?: boolean | null;
is_fungible_v2?: boolean | null;
amount: any;
current_token_data?: {
collection_id: string;
description: string;
is_fungible_v2?: boolean | null;
largest_property_version_v1?: any | null;
last_transaction_timestamp: any;
last_transaction_version: any;
maximum?: any | null;
supply: any;
token_data_id: string;
token_name: string;
token_properties: any;
token_standard: string;
token_uri: string;
current_collection?: {
collection_id: string;
collection_name: string;
creator_address: string;
current_supply: any;
description: string;
last_transaction_timestamp: any;
last_transaction_version: any;
max_supply?: any | null;
mutable_description?: boolean | null;
mutable_uri?: boolean | null;
table_handle_v1?: string | null;
token_standard: string;
total_minted_v2?: any | null;
uri: string;
} | null;
} | null;
}>;
};

export type GetTokenDataQueryVariables = Types.Exact<{
where_condition?: Types.InputMaybe<Types.CurrentTokenDatasV2BoolExp>;
offset?: Types.InputMaybe<Types.Scalars["Int"]>;
Expand Down
Loading
Loading