Skip to content

Commit

Permalink
enhancement: added errors when wrong params are passed to find and where
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jul 6, 2023
1 parent dea43e4 commit f07d439
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
12 changes: 12 additions & 0 deletions src/contract/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,16 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
query: WhereQuery,
{limit = 10, scope = this.contract.account, index}: WhereQueryOptions = {}
): TableCursor<TableRow> {
if (!query) {
throw new Error('Index value range must be provided')
}

const {from, to} = query

if (!from && !to) {
throw new Error('Index value for "from" or "to" must be provided')
}

const lower_bound = from
? typeof from === 'string'
? Name.from(from)
Expand Down Expand Up @@ -134,6 +142,10 @@ export class Table<TableRow extends ABISerializableConstructor = ABISerializable
* @returns {Promise<TableRow>} Promise resolving to a single table row.
*/
async find(query, queryOptions?: QueryOptions): Promise<TableRow> {
if (!query) {
throw new Error('Index value must be provided')
}

const fieldToIndexMapping = await this.getFieldToIndex()

const tableRowsParams = {
Expand Down
30 changes: 15 additions & 15 deletions test/data/b5ae1f53ef4cf6544fe89a0deb7a7d1043f43149.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
"json": {
"server_version": "905c5cc9",
"chain_id": "73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d",
"head_block_num": 84730189,
"last_irreversible_block_num": 84729861,
"last_irreversible_block_id": "050ce00508d12790b8143dcf28e77ecb91ac9a28d1a1bdaab0ec0f46d32e2064",
"head_block_id": "050ce14db85929679c1a40861b7fc834cab8010bd4e8b1c6d2940c3bf6b101bb",
"head_block_time": "2023-06-30T06:27:13.500",
"head_block_producer": "hippopotamus",
"virtual_block_cpu_limit": 200000000,
"head_block_num": 85860485,
"last_irreversible_block_num": 85860156,
"last_irreversible_block_id": "051e1f3c57f975d1d4fe1e93781dd7fccc91dfe4a00e8bad43ef3b24442db211",
"head_block_id": "051e20850b33c65ccd1cc7465d35bc4734101bfa61fa7115d497ac6209a205c2",
"head_block_time": "2023-07-06T20:04:32.500",
"head_block_producer": "batinthedark",
"virtual_block_cpu_limit": 250000000,
"virtual_block_net_limit": 1048576000,
"block_cpu_limit": 200000,
"block_cpu_limit": 250000,
"block_net_limit": 1048576,
"server_version_string": "v3.1.3",
"fork_db_head_block_num": 84730189,
"fork_db_head_block_id": "050ce14db85929679c1a40861b7fc834cab8010bd4e8b1c6d2940c3bf6b101bb",
"fork_db_head_block_num": 85860485,
"fork_db_head_block_id": "051e20850b33c65ccd1cc7465d35bc4734101bfa61fa7115d497ac6209a205c2",
"server_full_version_string": "v3.1.3-905c5cc900b4e88aed4ab6912009127bf9f4f140",
"total_cpu_weight": "120605025312211",
"total_net_weight": "117529266276322",
"earliest_available_block_num": 84542790,
"last_irreversible_block_time": "2023-06-30T06:24:29.500"
"total_cpu_weight": "120605033683215",
"total_net_weight": "117529266988326",
"earliest_available_block_num": 85684866,
"last_irreversible_block_time": "2023-07-06T20:01:47.500"
},
"text": "{\"server_version\":\"905c5cc9\",\"chain_id\":\"73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d\",\"head_block_num\":84730189,\"last_irreversible_block_num\":84729861,\"last_irreversible_block_id\":\"050ce00508d12790b8143dcf28e77ecb91ac9a28d1a1bdaab0ec0f46d32e2064\",\"head_block_id\":\"050ce14db85929679c1a40861b7fc834cab8010bd4e8b1c6d2940c3bf6b101bb\",\"head_block_time\":\"2023-06-30T06:27:13.500\",\"head_block_producer\":\"hippopotamus\",\"virtual_block_cpu_limit\":200000000,\"virtual_block_net_limit\":1048576000,\"block_cpu_limit\":200000,\"block_net_limit\":1048576,\"server_version_string\":\"v3.1.3\",\"fork_db_head_block_num\":84730189,\"fork_db_head_block_id\":\"050ce14db85929679c1a40861b7fc834cab8010bd4e8b1c6d2940c3bf6b101bb\",\"server_full_version_string\":\"v3.1.3-905c5cc900b4e88aed4ab6912009127bf9f4f140\",\"total_cpu_weight\":\"120605025312211\",\"total_net_weight\":\"117529266276322\",\"earliest_available_block_num\":84542790,\"last_irreversible_block_time\":\"2023-06-30T06:24:29.500\"}"
"text": "{\"server_version\":\"905c5cc9\",\"chain_id\":\"73e4385a2708e6d7048834fbc1079f2fabb17b3c125b146af438971e90716c4d\",\"head_block_num\":85860485,\"last_irreversible_block_num\":85860156,\"last_irreversible_block_id\":\"051e1f3c57f975d1d4fe1e93781dd7fccc91dfe4a00e8bad43ef3b24442db211\",\"head_block_id\":\"051e20850b33c65ccd1cc7465d35bc4734101bfa61fa7115d497ac6209a205c2\",\"head_block_time\":\"2023-07-06T20:04:32.500\",\"head_block_producer\":\"batinthedark\",\"virtual_block_cpu_limit\":250000000,\"virtual_block_net_limit\":1048576000,\"block_cpu_limit\":250000,\"block_net_limit\":1048576,\"server_version_string\":\"v3.1.3\",\"fork_db_head_block_num\":85860485,\"fork_db_head_block_id\":\"051e20850b33c65ccd1cc7465d35bc4734101bfa61fa7115d497ac6209a205c2\",\"server_full_version_string\":\"v3.1.3-905c5cc900b4e88aed4ab6912009127bf9f4f140\",\"total_cpu_weight\":\"120605033683215\",\"total_net_weight\":\"117529266988326\",\"earliest_available_block_num\":85684866,\"last_irreversible_block_time\":\"2023-07-06T20:01:47.500\"}"
}
2 changes: 1 addition & 1 deletion test/tests/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ suite('Table', () => {
})
test('should fetch table row correctly with default filtering', async () => {
// curl http://eos.greymass.com/v1/chain/get_table_rows -d '{"table":"producers","limit":10,"code":"eosio","scope":"eosio","json":true, "lower_bound": "teamgreymass", "upper_bound": "teamgreymass"}'
const row = await producersTable.find({owner: 'teamgreymass'})
const row = await producersTable.find('teamgreymass')

assert.deepEqual(row, {
owner: 'teamgreymass',
Expand Down

0 comments on commit f07d439

Please sign in to comment.