diff --git a/src/contract/table-cursor.ts b/src/contract/table-cursor.ts index 304f022..190d217 100644 --- a/src/contract/table-cursor.ts +++ b/src/contract/table-cursor.ts @@ -166,8 +166,8 @@ export class TableCursor { table: this.table, tableParams: { ...this.tableParams, - lower_bound: wrapIndexValue(query.from) || this.tableParams.lower_bound, - upper_bound: wrapIndexValue(query.to) || this.tableParams.upper_bound, + lower_bound: query.from || this.tableParams.lower_bound, + upper_bound: query.to || this.tableParams.upper_bound, }, }) } diff --git a/src/contract/table.ts b/src/contract/table.ts index de48cf9..2fe46b8 100644 --- a/src/contract/table.ts +++ b/src/contract/table.ts @@ -1,12 +1,12 @@ -import {ABI, ABISerializableConstructor, API, Name, NameType, UInt64} from '@wharfkit/session' +import {ABI, ABISerializableConstructor, API, Name, NameType} from '@wharfkit/session' import type {Contract} from '../contract' -import {indexPositionInWords, wrapIndexValue} from '../utils' +import {indexPositionInWords} from '../utils' import {TableCursor} from './table-cursor' export interface QueryOptions { index?: string scope?: NameType - index_type?: API.v1.TableIndexType + key_type?: keyof API.v1.TableIndexTypes } export interface WhereQueryOptions extends QueryOptions { @@ -14,8 +14,8 @@ export interface WhereQueryOptions extends QueryOptions { } export interface WhereQuery { - from: number | string | UInt64 - to: number | string | UInt64 + from: API.v1.TableIndexType | string + to: API.v1.TableIndexType | string } interface FieldToIndex { @@ -95,7 +95,7 @@ export class Table { if (!query) { throw new Error('Index value range must be provided') @@ -113,8 +113,9 @@ export class Table} Promise resolving to a single table row. */ - async get(query, queryOptions?: QueryOptions): Promise { - if (!query) { + async get( + queryValue: API.v1.TableIndexType | string, + queryOptions?: QueryOptions + ): Promise { + if (!queryValue) { throw new Error('Index value must be provided') } const fieldToIndexMapping = await this.getFieldToIndex() + console.log({queryValue}) + const tableRowsParams = { table: this.name, code: this.contract.account, scope: this.contract.account, - type: this.rowType, + type: this.rowType!, limit: 1, - lower_bound: typeof query === 'string' ? Name.from(query) : UInt64.from(query), - upper_bound: typeof query === 'string' ? Name.from(query) : UInt64.from(query), + lower_bound: queryValue, + upper_bound: queryValue, index_position: queryOptions?.index ? fieldToIndexMapping[queryOptions?.index].index_position : 'primary', + key_type: queryOptions?.key_type, } const {rows} = await this.contract.client!.v1.chain.get_table_rows(tableRowsParams) diff --git a/test/data/requests/209e329f05655a9957f80aa9b1c231a159d5f9fe.json b/test/data/requests/209e329f05655a9957f80aa9b1c231a159d5f9fe.json new file mode 100644 index 0000000..7699053 --- /dev/null +++ b/test/data/requests/209e329f05655a9957f80aa9b1c231a159d5f9fe.json @@ -0,0 +1,28 @@ +{ + "request": { + "path": "https://eos.greymass.com/v1/chain/get_table_rows", + "params": { + "method": "POST", + "body": "{\"table\":\"trending\",\"code\":\"decentiumorg\",\"scope\":\"decentiumorg\",\"limit\":1,\"lower_bound\":\"5\",\"upper_bound\":\"5\",\"index_position\":\"primary\",\"key_type\":\"name\",\"json\":true}" + } + }, + "headers": { + "access-control-allow-headers": "X-Requested-With,Accept,Content-Type,Origin", + "access-control-allow-methods": "GET, POST, OPTIONS", + "access-control-allow-origin": "*", + "connection": "close", + "content-length": "38", + "content-type": "application/json", + "date": "Fri, 07 Jul 2023 14:04:10 GMT", + "host": "eos.greymass.com", + "server": "nginx", + "x-cached": "MISS" + }, + "status": 200, + "json": { + "rows": [], + "more": false, + "next_key": "" + }, + "text": "{\"rows\":[],\"more\":false,\"next_key\":\"\"}" +} \ No newline at end of file diff --git a/test/tests/table.ts b/test/tests/table.ts index f7ae722..2797910 100644 --- a/test/tests/table.ts +++ b/test/tests/table.ts @@ -1,5 +1,7 @@ import {assert} from 'chai' +import {UInt64} from '@wharfkit/session' + import {Table, Contract} from '$lib' import {makeClient} from '../utils/mock-client' @@ -91,9 +93,11 @@ suite('Table', () => { }) }) - suite('find', () => { + suite('get', () => { test('should fetch table row correctly when filtering by primary index is used', async () => { - const row = await decentiumTrendingTable.get(5) + const row = await decentiumTrendingTable.get(5, {key_type: 'i64'}) + + console.log({row}) assert.deepEqual(row, { id: 5,