Skip to content

Commit

Permalink
chore: passing lower and upper_bound without data wrapping in cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jul 7, 2023
1 parent ab236e2 commit 7007914
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function codegen(contractName, abi) {
'@wharfkit/session'
)
const importContractStatement = generateImportStatement(
['Contract', 'Table', 'TableCursor', 'QueryOptions', 'WhereQueryOptions', 'WhereQuery'],
['Contract', 'Table', 'TableCursor', 'QueryOptions', 'QueryOptions', 'Query'],
'@wharfkit/contract'
)

Expand Down
4 changes: 2 additions & 2 deletions src/codegen/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ export async function generateTableClass(contractName, namespaceName, table, abi
undefined,
ts.factory.createIdentifier('query'),
undefined,
ts.factory.createTypeReferenceNode('WhereQuery'),
ts.factory.createTypeReferenceNode('Query'),
undefined
),
ts.factory.createParameterDeclaration(
undefined,
undefined,
ts.factory.createIdentifier('queryOptions'),
undefined,
ts.factory.createTypeReferenceNode('WhereQueryOptions'),
ts.factory.createTypeReferenceNode('QueryOptions'),
undefined
)
)
Expand Down
32 changes: 9 additions & 23 deletions src/contract/table-cursor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {API, isInstanceOf, UInt64} from '@wharfkit/session'
import {Name} from '@wharfkit/session'
import {wrapIndexValue} from '../utils'
import {QueryOptions, Table, WhereQuery} from './table'
import {QueryOptions, Table, Query} from './table'

interface TableCursorParams {
table: Table
tableParams: API.v1.GetTableRowsParams
next_key?: Name | UInt64 | undefined
next_key?: API.v1.TableIndexType | string
indexPositionField?: string
}

Expand All @@ -18,7 +18,7 @@ interface TableCursorParams {
*/
export class TableCursor<TableRow> {
private table: Table
private next_key: Name | UInt64 | undefined
private next_key: API.v1.TableIndexType | string | undefined
private tableParams: API.v1.GetTableRowsParams
private endReached = false
private indexPositionField?: string
Expand Down Expand Up @@ -80,25 +80,11 @@ export class TableCursor<TableRow> {
return []
}

let lower_bound
let upper_bound

if (this.tableParams.lower_bound) {
lower_bound = isInstanceOf(this.tableParams.lower_bound, Name)
? Name.from(this.tableParams.lower_bound)
: UInt64.from(this.tableParams.lower_bound)
}
let lower_bound = this.tableParams.lower_bound
let upper_bound = this.tableParams.upper_bound

if (this.next_key) {
lower_bound = isInstanceOf(this.next_key, Name)
? Name.from(this.next_key)
: UInt64.from(this.next_key)
}

if (this.tableParams.upper_bound) {
upper_bound = isInstanceOf(this.tableParams.upper_bound, Name)
? Name.from(this.tableParams.upper_bound)
: UInt64.from(this.tableParams.upper_bound)
lower_bound = this.next_key
}

let indexPosition = this.tableParams.index_position || 'primary'
Expand All @@ -116,8 +102,8 @@ export class TableCursor<TableRow> {
const {rows, next_key} = await this.table.contract.client!.v1.chain.get_table_rows({
...this.tableParams,
limit: Math.min(this.tableParams.limit - this.rowsCount, 1000000),
lower_bound: lower_bound ? lower_bound : undefined,
upper_bound: upper_bound ? upper_bound : undefined,
lower_bound,
upper_bound,
index_position: indexPosition,
})

Expand Down Expand Up @@ -161,7 +147,7 @@ export class TableCursor<TableRow> {
*
* @returns A new cursor with updated parameters.
*/
query(query: WhereQuery, queryOptions?: QueryOptions) {
query(query: Query, queryOptions?: QueryOptions) {
return new TableCursor({
table: this.table,
tableParams: {
Expand Down
20 changes: 9 additions & 11 deletions src/contract/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import type {Contract} from '../contract'
import {indexPositionInWords} from '../utils'
import {TableCursor} from './table-cursor'

export interface QueryOptions {
export interface FindOptions {
index?: string
scope?: NameType
key_type?: keyof API.v1.TableIndexTypes
}

export interface WhereQueryOptions extends QueryOptions {
export interface QueryOptions extends FindOptions {
limit?: number
}

export interface WhereQuery {
export interface Query {
from: API.v1.TableIndexType | string
to: API.v1.TableIndexType | string
}
Expand Down Expand Up @@ -94,8 +94,8 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
* @returns {TableCursor<TableRow>} Promise resolving to a `TableCursor` of the filtered table rows.
*/
query(
query: WhereQuery,
{limit = 10, scope = this.contract.account, index, key_type}: WhereQueryOptions = {}
query: Query,
{limit = 10, scope = this.contract.account, index, key_type}: QueryOptions = {}
): TableCursor<TableRow> {
if (!query) {
throw new Error('Index value range must be provided')
Expand Down Expand Up @@ -134,7 +134,7 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
*/
async get(
queryValue: API.v1.TableIndexType | string,
queryOptions?: QueryOptions
{scope = this.contract.account, index, key_type}: QueryOptions = {}
): Promise<TableRow> {
if (!queryValue) {
throw new Error('Index value must be provided')
Expand All @@ -147,15 +147,13 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
const tableRowsParams = {
table: this.name,
code: this.contract.account,
scope: this.contract.account,
scope,
type: this.rowType!,
limit: 1,
lower_bound: queryValue,
upper_bound: queryValue,
index_position: queryOptions?.index
? fieldToIndexMapping[queryOptions?.index].index_position
: 'primary',
key_type: queryOptions?.key_type,
// index_position: index ? fieldToIndexMapping[index].index_position : 'primary',
key_type: key_type,
}

const {rows} = await this.contract.client!.v1.chain.get_table_rows(tableRowsParams)
Expand Down
4 changes: 2 additions & 2 deletions src/example-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export namespace _Blog {
declare email: string
}

export interface UsersWhereQueryParams {
export interface UsersQueryParams {
name: {
from: string
to: string
Expand Down Expand Up @@ -49,7 +49,7 @@ export namespace _Blog {
}

static query(
queryParams: _Blog.types.UsersWhereQueryParams,
queryParams: _Blog.types.UsersQueryParams,
{limit = 10} = {},
client: APIClient
): TableCursor<_Blog.types.UsersRow> {
Expand Down
8 changes: 4 additions & 4 deletions test/codegen-samples/rewards.gm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export namespace _RewardsGm {
export class config {
static fieldToIndex = {}
static query(
queryParams: _RewardsGm.types.ConfigWhereQueryParams,
queryParams: _RewardsGm.types.ConfigQueryParams,
getTableRowsOptions: GetTableRowsOptions,
client: APIClient
): TableCursor<_RewardsGm.types.Config> {
Expand Down Expand Up @@ -103,7 +103,7 @@ export namespace _RewardsGm {
export class users {
static fieldToIndex = {}
static query(
queryParams: _RewardsGm.types.UsersWhereQueryParams,
queryParams: _RewardsGm.types.UsersQueryParams,
getTableRowsOptions: GetTableRowsOptions,
client: APIClient
): TableCursor<_RewardsGm.types.User_row> {
Expand Down Expand Up @@ -166,7 +166,7 @@ export namespace _RewardsGm {
account: NameType
weight: UInt16Type
}
export interface ConfigWhereQueryParams {
export interface ConfigQueryParams {
token_symbol?: {
from: Symbol
to: Symbol
Expand All @@ -185,7 +185,7 @@ export namespace _RewardsGm {
oracle_account?: NameType
oracle_pairs?: Oracle_pair
}
export interface UsersWhereQueryParams {
export interface UsersQueryParams {
account?: {
from: NameType
to: NameType
Expand Down
8 changes: 4 additions & 4 deletions test/data/contracts/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
// export class accounts {
// static fieldToIndex = {balance: {type: 'balance', index_position: 'primary'}}
// static query(
// queryParams: _EosioToken.types.AccountsWhereQueryParams,
// queryParams: _EosioToken.types.AccountsQueryParams,
// getTableRowsOptions: GetTableRowsOptions,
// client: APIClient
// ): TableCursor<_EosioToken.types.Account> {
Expand Down Expand Up @@ -137,7 +137,7 @@
// export class stat {
// static fieldToIndex = {supply: {type: 'supply', index_position: 'primary'}}
// static query(
// queryParams: _EosioToken.types.StatWhereQueryParams,
// queryParams: _EosioToken.types.StatQueryParams,
// getTableRowsOptions: GetTableRowsOptions,
// client: APIClient
// ): TableCursor<_EosioToken.types.Currency_stats> {
Expand Down Expand Up @@ -224,7 +224,7 @@
// quantity: AssetType
// memo: string
// }
// export interface AccountsWhereQueryParams {
// export interface AccountsQueryParams {
// balance?: {
// from: AssetType
// to: AssetType
Expand All @@ -233,7 +233,7 @@
// export interface AccountsFindQueryParams {
// balance?: AssetType
// }
// export interface StatWhereQueryParams {
// export interface StatQueryParams {
// supply?: {
// from: AssetType
// to: AssetType
Expand Down

0 comments on commit 7007914

Please sign in to comment.