From 53566ccaacbc362b465839dc3c9645537cc94378 Mon Sep 17 00:00:00 2001 From: Aaron Cox Date: Mon, 24 Jul 2023 23:28:25 -0700 Subject: [PATCH] Make query in `get()` optional to just fetch first row --- src/contract/table.ts | 2 +- ...3db67a54d5cb0135682b1ad03efaa2560a95ff.json | 18 ++++++++++++++++++ test/tests/table.ts | 8 ++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/data/requests/5d3db67a54d5cb0135682b1ad03efaa2560a95ff.json diff --git a/src/contract/table.ts b/src/contract/table.ts index 20f5f67..889070a 100644 --- a/src/contract/table.ts +++ b/src/contract/table.ts @@ -143,7 +143,7 @@ export class Table { * @returns {Promise} Promise resolving to a single table row. */ async get( - queryValue: API.v1.TableIndexType | string, + queryValue?: API.v1.TableIndexType | string, {scope = this.account, index, key_type}: QueryOptions = {} ): Promise { const fieldToIndexMapping = this.getFieldToIndex() diff --git a/test/data/requests/5d3db67a54d5cb0135682b1ad03efaa2560a95ff.json b/test/data/requests/5d3db67a54d5cb0135682b1ad03efaa2560a95ff.json new file mode 100644 index 0000000..813dc66 --- /dev/null +++ b/test/data/requests/5d3db67a54d5cb0135682b1ad03efaa2560a95ff.json @@ -0,0 +1,18 @@ +{ + "request": { + "path": "https://eos.greymass.com/v1/chain/get_table_rows", + "params": { + "method": "POST", + "body": "{\"table\":\"global\",\"code\":\"eosio\",\"scope\":\"eosio\",\"limit\":1,\"index_position\":\"primary\",\"key_type\":\"name\",\"json\":false}" + } + }, + "status": 200, + "json": { + "rows": [ + "000010000000000010270000000008000c000000f40100001400000064000000400d03000a000000f049020064000000100e00005802000080533b00ffff07000a000a00008c34915b000000cc968e630f000000d3fb69400a000000714fa458803b52ce49010600ff8a7a0e000000002983000200000000be2001001da5259d8a05000000e15fd7136f05001500ef04cfcfcf914244f4c4a358" + ], + "more": false, + "next_key": "" + }, + "text": "{\"rows\":[\"000010000000000010270000000008000c000000f40100001400000064000000400d03000a000000f049020064000000100e00005802000080533b00ffff07000a000a00008c34915b000000cc968e630f000000d3fb69400a000000714fa458803b52ce49010600ff8a7a0e000000002983000200000000be2001001da5259d8a05000000e15fd7136f05001500ef04cfcfcf914244f4c4a358\"],\"more\":false,\"next_key\":\"\"}" +} \ No newline at end of file diff --git a/test/tests/table.ts b/test/tests/table.ts index f4ace90..77da0ee 100644 --- a/test/tests/table.ts +++ b/test/tests/table.ts @@ -4,6 +4,7 @@ import ContractKit, {Contract, Table, TableCursor} from '$lib' import {Asset, Int64, Name, Serializer, Struct, TimePoint} from '@greymass/eosio' import {makeClient} from '@wharfkit/mock-data' +import {EosioGlobalState} from '$test/data/structs/eosio' const mockClient = makeClient('https://eos.greymass.com') @@ -323,6 +324,13 @@ suite('Table', () => { const row = await producersTable.get('teamgreymass') assert.instanceOf(row.owner, Name) }) + + test('should just get first row without params', async function () { + const table = eosio.table('global', EosioGlobalState) + const row = await table.get() + assert.instanceOf(row, EosioGlobalState) + assert.instanceOf(row.pervote_bucket, Int64) + }) }) suite('first', () => {