-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
table
function API. Add getTableItemsData
and `getTable…
…ItemsMetadata` queries (#385) Introduce table function API wand add getTableItemsData and getTableItemsMetadata queries
- Loading branch information
Showing
17 changed files
with
682 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { getTableItem, getTableItemsData, getTableItemsMetadata } from "../internal/table"; | ||
import { | ||
TableItemRequest, | ||
LedgerVersionArg, | ||
AnyNumber, | ||
PaginationArgs, | ||
WhereArg, | ||
OrderByArg, | ||
GetTableItemsDataResponse, | ||
GetTableItemsMetadataResponse, | ||
} from "../types"; | ||
import { TableItemsBoolExp, TableMetadatasBoolExp } from "../types/generated/types"; | ||
import { ProcessorType } from "../utils"; | ||
import { AptosConfig } from "./aptosConfig"; | ||
import { waitForIndexerOnVersion } from "./utils"; | ||
|
||
/** | ||
* A class to query all `Table` Aptos related queries | ||
*/ | ||
export class Table { | ||
readonly config: AptosConfig; | ||
|
||
constructor(config: AptosConfig) { | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* Queries for a table item for a table identified by the handle and the key for the item. | ||
* Key and value types need to be passed in to help with key serialization and value deserialization. | ||
* | ||
* Note, this query calls the fullnode server | ||
* | ||
* @example https://api.devnet.aptoslabs.com/v1/accounts/0x1/resource/0x1::coin::CoinInfo%3C0x1::aptos_coin::AptosCoin%3E | ||
* const tableItem = await aptos.getTableItem({ | ||
* handle: "0x1b854694ae746cdbd8d44186ca4929b2b337df21d1c74633be19b2710552fdca", | ||
* data: { | ||
* key_type: "address", // Move type of table key | ||
* value_type: "u128", // Move type of table value | ||
* key: "0x619dc29a0aac8fa146714058e8dd6d2d0f3bdf5f6331907bf91f3acd81e6935" // Value of table key | ||
* }, | ||
* }) | ||
* | ||
* @param args.handle A pointer to where that table is stored | ||
* @param args.data Object that describes table item | ||
* @param args.options.ledgerVersion The ledger version to query, if not provided it will get the latest version | ||
* | ||
* @returns Table item value rendered in JSON | ||
*/ | ||
async getTableItem<T>(args: { handle: string; data: TableItemRequest; options?: LedgerVersionArg }): Promise<T> { | ||
return getTableItem<T>({ aptosConfig: this.config, ...args }); | ||
} | ||
|
||
/** | ||
* Queries for a table items data. | ||
* | ||
* Optional `options.where` param can be passed to filter the response. | ||
* | ||
* Note, this query calls the indexer server | ||
* | ||
* @example | ||
* const data = await aptos.getTableItemsData({ | ||
* options: { where: { | ||
* table_handle: { _eq: "0x1b854694ae746cdbd8d44186ca4929b2b337df21d1c74633be19b2710552fdca" }, | ||
* transaction_version: { _eq: "0" } | ||
* } | ||
* }, | ||
* }); | ||
* | ||
* @returns GetTableItemsDataResponse | ||
*/ | ||
async getTableItemsData(args: { | ||
minimumLedgerVersion?: AnyNumber; | ||
options?: PaginationArgs & WhereArg<TableItemsBoolExp> & OrderByArg<GetTableItemsDataResponse[0]>; | ||
}): Promise<GetTableItemsDataResponse> { | ||
await waitForIndexerOnVersion({ | ||
config: this.config, | ||
minimumLedgerVersion: args.minimumLedgerVersion, | ||
processorType: ProcessorType.DEFAULT, | ||
}); | ||
return getTableItemsData({ aptosConfig: this.config, ...args }); | ||
} | ||
|
||
/** | ||
* Queries for a table items metadata. | ||
* | ||
* Optional `options.where` param can be passed to filter the response. | ||
* | ||
* Note, this query calls the indexer server | ||
* | ||
* @example | ||
* const data = await aptos.getTableItemsMetadata({ | ||
* options: { where: { handle: { _eq: "0x1b854694ae746cdbd8d44186ca4929b2b337df21d1c74633be19b2710552fdca" } } }, | ||
* }); | ||
* | ||
* @returns GetTableItemsMetadataResponse | ||
*/ | ||
async getTableItemsMetadata(args: { | ||
minimumLedgerVersion?: AnyNumber; | ||
options?: PaginationArgs & WhereArg<TableMetadatasBoolExp> & OrderByArg<GetTableItemsMetadataResponse[0]>; | ||
}): Promise<GetTableItemsMetadataResponse> { | ||
await waitForIndexerOnVersion({ | ||
config: this.config, | ||
minimumLedgerVersion: args.minimumLedgerVersion, | ||
processorType: ProcessorType.DEFAULT, | ||
}); | ||
return getTableItemsMetadata({ aptosConfig: this.config, ...args }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
query getTableItemsData( | ||
$where_condition: table_items_bool_exp! | ||
$offset: Int | ||
$limit: Int | ||
$order_by: [table_items_order_by!] | ||
) { | ||
table_items(where: $where_condition, offset: $offset, limit: $limit, order_by: $order_by){ | ||
decoded_key | ||
decoded_value | ||
key | ||
table_handle | ||
transaction_version | ||
write_set_change_index | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
query getTableItemsMetadata( | ||
$where_condition: table_metadatas_bool_exp! | ||
$offset: Int | ||
$limit: Int | ||
$order_by: [table_metadatas_order_by!] | ||
) { | ||
table_metadatas(where: $where_condition, offset: $offset, limit: $limit, order_by: $order_by){ | ||
handle | ||
key_type | ||
value_type | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { AptosConfig } from "../api/aptosConfig"; | ||
import { postAptosFullNode } from "../client"; | ||
import { | ||
TableItemRequest, | ||
LedgerVersionArg, | ||
PaginationArgs, | ||
WhereArg, | ||
OrderByArg, | ||
GetTableItemsDataResponse, | ||
GetTableItemsMetadataResponse, | ||
} from "../types"; | ||
import { GetTableItemsDataQuery, GetTableItemsMetadataQuery } from "../types/generated/operations"; | ||
import { GetTableItemsData, GetTableItemsMetadata } from "../types/generated/queries"; | ||
import { TableItemsBoolExp, TableMetadatasBoolExp } from "../types/generated/types"; | ||
import { queryIndexer } from "./general"; | ||
|
||
export async function getTableItem<T>(args: { | ||
aptosConfig: AptosConfig; | ||
handle: string; | ||
data: TableItemRequest; | ||
options?: LedgerVersionArg; | ||
}): Promise<T> { | ||
const { aptosConfig, handle, data, options } = args; | ||
const response = await postAptosFullNode<TableItemRequest, any>({ | ||
aptosConfig, | ||
originMethod: "getTableItem", | ||
path: `tables/${handle}/item`, | ||
params: { ledger_version: options?.ledgerVersion }, | ||
body: data, | ||
}); | ||
return response.data as T; | ||
} | ||
|
||
export async function getTableItemsData(args: { | ||
aptosConfig: AptosConfig; | ||
options?: PaginationArgs & WhereArg<TableItemsBoolExp> & OrderByArg<GetTableItemsDataResponse[0]>; | ||
}) { | ||
const { aptosConfig, options } = args; | ||
|
||
const graphqlQuery = { | ||
query: GetTableItemsData, | ||
variables: { | ||
where_condition: options?.where, | ||
offset: options?.offset, | ||
limit: options?.limit, | ||
order_by: options?.orderBy, | ||
}, | ||
}; | ||
|
||
const data = await queryIndexer<GetTableItemsDataQuery>({ | ||
aptosConfig, | ||
query: graphqlQuery, | ||
originMethod: "getTableItemsData", | ||
}); | ||
|
||
return data.table_items; | ||
} | ||
|
||
export async function getTableItemsMetadata(args: { | ||
aptosConfig: AptosConfig; | ||
options?: PaginationArgs & WhereArg<TableMetadatasBoolExp> & OrderByArg<GetTableItemsMetadataResponse[0]>; | ||
}): Promise<GetTableItemsMetadataResponse> { | ||
const { aptosConfig, options } = args; | ||
|
||
const graphqlQuery = { | ||
query: GetTableItemsMetadata, | ||
variables: { | ||
where_condition: options?.where, | ||
offset: options?.offset, | ||
limit: options?.limit, | ||
order_by: options?.orderBy, | ||
}, | ||
}; | ||
|
||
const data = await queryIndexer<GetTableItemsMetadataQuery>({ | ||
aptosConfig, | ||
query: graphqlQuery, | ||
originMethod: "getTableItemsMetadata", | ||
}); | ||
|
||
return data.table_metadatas; | ||
} |
Oops, something went wrong.