Skip to content

Commit

Permalink
Implemented scope cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Aug 15, 2023
1 parent 08bf28d commit 9207624
Show file tree
Hide file tree
Showing 15 changed files with 11,942 additions and 74 deletions.
7 changes: 7 additions & 0 deletions src/contract/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {ABIDef, Action, ActionType, AnyAction} from '@wharfkit/antelope'

export class ContractAction extends Action {
static from<T>(object: ActionType | AnyAction, abi?: ABIDef): T {
return super.from(object, abi) as T
}
}
56 changes: 56 additions & 0 deletions src/contract/scope-cursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {API} from '@wharfkit/antelope'
import {TableCursor} from './table-cursor'

export class TableScopeCursor extends TableCursor {
/**
* Fetch the next batch of rows from the cursor.
*
* @param rowsPerAPIRequest The number of rows to fetch per API request.
* @returns A promise containing the next batch of rows.
*/
async next(
rowsPerAPIRequest: number = Number.MAX_SAFE_INTEGER
): Promise<API.v1.GetTableByScopeResponseRow[]> {
// If the cursor has deemed its at the end, return an empty array
if (this.endReached) {
return []
}

// Set the lower_bound, and override if the cursor has a next_key value
let lower_bound = this.params.lower_bound
if (this.next_key) {
lower_bound = this.next_key
}

// Determine the maximum number of remaining rows for the cursor
const rowsRemaining = this.maxRows - this.rowsCount

// Find the lowest amount between rows remaining, rows per request, or the provided query params limit
const limit = Math.min(rowsRemaining, rowsPerAPIRequest, this.params.limit)

// Assemble and perform the v1/chain/get_table_rows query
const query: API.v1.GetTableByScopeParams = {
code: this.params.code,
table: this.params.table,
limit,
lower_bound: lower_bound ? String(lower_bound) : undefined,
upper_bound: this.params.upper_bound ? String(this.params.upper_bound) : undefined,
}

const result = await this.client!.v1.chain.get_table_by_scope(query)

// Retrieve the rows from the result, decoding if needed
const rows: API.v1.GetTableByScopeResponseRow[] = result.rows

// Persist cursor state for subsequent calls
this.next_key = result.more
this.rowsCount += rows.length

// Determine if we've reached the end of the cursor
if (!result.more || rows.length === 0 || this.rowsCount === this.maxRows) {
this.endReached = true
}

return rows
}
}
60 changes: 2 additions & 58 deletions src/contract/table-cursor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {ABI, ABIDef, API, APIClient, Name, Serializer} from '@wharfkit/antelope'
import {wrapIndexValue} from '../utils'
import {ABI, ABIDef, API, APIClient, Name} from '@wharfkit/antelope'

/** Mashup of valid types for an APIClient call to v1.chain.get_table_rows */
export type TableRowParamsTypes =
Expand Down Expand Up @@ -94,62 +93,7 @@ export abstract class TableCursor<RowType = any> {
* @param rowsPerAPIRequest The number of rows to fetch per API request.
* @returns A promise containing the next batch of rows.
*/
async next(rowsPerAPIRequest: number = Number.MAX_SAFE_INTEGER): Promise<RowType[]> {
// If the cursor has deemed its at the end, return an empty array
if (this.endReached) {
return []
}

// Set the lower_bound, and override if the cursor has a next_key value
let lower_bound = this.params.lower_bound
if (this.next_key) {
lower_bound = this.next_key
}

// Determine the maximum number of remaining rows for the cursor
const rowsRemaining = this.maxRows - this.rowsCount

// Find the lowest amount between rows remaining, rows per request, or the provided query params limit
const limit = Math.min(rowsRemaining, rowsPerAPIRequest, this.params.limit)

// Assemble and perform the v1/chain/get_table_rows query
const query = {
...this.params,
limit,
lower_bound: wrapIndexValue(lower_bound),
upper_bound: wrapIndexValue(this.params.upper_bound),
}

const result = await this.client!.v1.chain.get_table_rows(query)

// Determine if we need to decode the rows, based on if:
// - json parameter is false, meaning hex data will be returned
// - type parameter is not set, meaning the APIClient will not automatically decode
const requiresDecoding =
this.params.json === false && !(query as API.v1.GetTableRowsParamsTyped).type

// Retrieve the rows from the result, decoding if needed
const rows: RowType[] = requiresDecoding
? result.rows.map((data) =>
Serializer.decode({
data,
abi: this.abi,
type: this.type,
})
)
: result.rows

// Persist cursor state for subsequent calls
this.next_key = result.next_key
this.rowsCount += rows.length

// Determine if we've reached the end of the cursor
if (!result.next_key || rows.length === 0 || this.rowsCount === this.maxRows) {
this.endReached = true
}

return rows
}
abstract next(rowsPerAPIRequest?: number): Promise<RowType[]>

/**
* Reset the internal state of the cursor
Expand Down
20 changes: 19 additions & 1 deletion src/contract/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {ABI, ABIDef, API, APIClient, Name, NameType, Serializer} from '@wharfkit
import {indexPositionInWords, wrapIndexValue} from '../utils'
import {TableCursor} from './table-cursor'
import {TableRowCursor} from './row-cursor'
import {TableScopeCursor} from './scope-cursor'

export interface QueryParams {
index?: string
Expand Down Expand Up @@ -220,5 +221,22 @@ export class Table<RowType = any> {
return fieldToIndex
}

scopes() {}
scopes(params: QueryParams = {}): TableScopeCursor {
const tableRowsParams: any = {
// Table query
code: this.account,
table: this.name,
// Filtering
lower_bound: wrapIndexValue(params.from),
upper_bound: wrapIndexValue(params.to),
limit: params.rowsPerAPIRequest || this.defaultRowLimit,
}

return new TableScopeCursor({
abi: this.abi,
client: this.client,
maxRows: params.maxRows,
params: tableRowsParams,
})
}
}
2 changes: 1 addition & 1 deletion src/index-module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './contract'
export * from './contract/table'
export * from './contract/row-cursor'
// export * from './contract/scope-cursor'
export * from './contract/scope-cursor'
export * from './kit'
export * from './utils'
86 changes: 86 additions & 0 deletions test/data/requests/0ca21fdda73edaa99a47135d3ec7e0e1c4fd9901.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_table_by_scope",
"params": {
"method": "POST",
"body": "{\"code\":\"eosio.msig\",\"table\":\"proposal\",\"limit\":10,\"lower_bound\":\"1charcha.ftw\"}"
}
},
"status": 200,
"json": {
"rows": [
{
"code": "eosio.msig",
"scope": "1charcha.ftw",
"table": "proposal",
"payer": "1charcha.ftw",
"count": 1
},
{
"code": "eosio.msig",
"scope": "1cjfya.ftw",
"table": "proposal",
"payer": "1cjfya.ftw",
"count": 1
},
{
"code": "eosio.msig",
"scope": "1farah.ftw",
"table": "proposal",
"payer": "1farah.ftw",
"count": 3
},
{
"code": "eosio.msig",
"scope": "1luntxd.ftw",
"table": "proposal",
"payer": "1luntxd.ftw",
"count": 1
},
{
"code": "eosio.msig",
"scope": "1muarte.ftw",
"table": "proposal",
"payer": "1muarte.ftw",
"count": 1
},
{
"code": "eosio.msig",
"scope": "1mycrypto213",
"table": "proposal",
"payer": "1mycrypto213",
"count": 2
},
{
"code": "eosio.msig",
"scope": "1plsd.ftw",
"table": "proposal",
"payer": "1plsd.ftw",
"count": 1
},
{
"code": "eosio.msig",
"scope": "1rtkjeosdac1",
"table": "proposal",
"payer": "1rtkjeosdac1",
"count": 1
},
{
"code": "eosio.msig",
"scope": "1shahram.ftw",
"table": "proposal",
"payer": "1shahram.ftw",
"count": 1
},
{
"code": "eosio.msig",
"scope": "1slapaho.ftw",
"table": "proposal",
"payer": "1slapaho.ftw",
"count": 1
}
],
"more": "1stdwn33.ftw"
},
"text": "{\"rows\":[{\"code\":\"eosio.msig\",\"scope\":\"1charcha.ftw\",\"table\":\"proposal\",\"payer\":\"1charcha.ftw\",\"count\":1},{\"code\":\"eosio.msig\",\"scope\":\"1cjfya.ftw\",\"table\":\"proposal\",\"payer\":\"1cjfya.ftw\",\"count\":1},{\"code\":\"eosio.msig\",\"scope\":\"1farah.ftw\",\"table\":\"proposal\",\"payer\":\"1farah.ftw\",\"count\":3},{\"code\":\"eosio.msig\",\"scope\":\"1luntxd.ftw\",\"table\":\"proposal\",\"payer\":\"1luntxd.ftw\",\"count\":1},{\"code\":\"eosio.msig\",\"scope\":\"1muarte.ftw\",\"table\":\"proposal\",\"payer\":\"1muarte.ftw\",\"count\":1},{\"code\":\"eosio.msig\",\"scope\":\"1mycrypto213\",\"table\":\"proposal\",\"payer\":\"1mycrypto213\",\"count\":2},{\"code\":\"eosio.msig\",\"scope\":\"1plsd.ftw\",\"table\":\"proposal\",\"payer\":\"1plsd.ftw\",\"count\":1},{\"code\":\"eosio.msig\",\"scope\":\"1rtkjeosdac1\",\"table\":\"proposal\",\"payer\":\"1rtkjeosdac1\",\"count\":1},{\"code\":\"eosio.msig\",\"scope\":\"1shahram.ftw\",\"table\":\"proposal\",\"payer\":\"1shahram.ftw\",\"count\":1},{\"code\":\"eosio.msig\",\"scope\":\"1slapaho.ftw\",\"table\":\"proposal\",\"payer\":\"1slapaho.ftw\",\"count\":1}],\"more\":\"1stdwn33.ftw\"}"
}
30 changes: 30 additions & 0 deletions test/data/requests/16978d9aa599819a2e0c519e805c979aac765121.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_table_by_scope",
"params": {
"method": "POST",
"body": "{\"code\":\"eosio.msig\",\"table\":\"proposal\",\"limit\":2}"
}
},
"status": 200,
"json": {
"rows": [
{
"code": "eosio.msig",
"scope": "1111111zzzzz",
"table": "proposal",
"payer": "1111111zzzzz",
"count": 1
},
{
"code": "eosio.msig",
"scope": "113332.pcash",
"table": "proposal",
"payer": "113332.pcash",
"count": 1
}
],
"more": "11rippche24y"
},
"text": "{\"rows\":[{\"code\":\"eosio.msig\",\"scope\":\"1111111zzzzz\",\"table\":\"proposal\",\"payer\":\"1111111zzzzz\",\"count\":1},{\"code\":\"eosio.msig\",\"scope\":\"113332.pcash\",\"table\":\"proposal\",\"payer\":\"113332.pcash\",\"count\":1}],\"more\":\"11rippche24y\"}"
}
30 changes: 30 additions & 0 deletions test/data/requests/2193f0c21bbcfbc0e4adddf35a7c4162d04f9df1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_table_by_scope",
"params": {
"method": "POST",
"body": "{\"code\":\"eosio.msig\",\"table\":\"proposal\",\"limit\":2,\"lower_bound\":\"11rippche24y\"}"
}
},
"status": 200,
"json": {
"rows": [
{
"code": "eosio.msig",
"scope": "11rippche24y",
"table": "proposal",
"payer": "11rippche24y",
"count": 1
},
{
"code": "eosio.msig",
"scope": "121moe.ftw",
"table": "proposal",
"payer": "121moe.ftw",
"count": 1
}
],
"more": "12235213.ftw"
},
"text": "{\"rows\":[{\"code\":\"eosio.msig\",\"scope\":\"11rippche24y\",\"table\":\"proposal\",\"payer\":\"11rippche24y\",\"count\":1},{\"code\":\"eosio.msig\",\"scope\":\"121moe.ftw\",\"table\":\"proposal\",\"payer\":\"121moe.ftw\",\"count\":1}],\"more\":\"12235213.ftw\"}"
}
Loading

0 comments on commit 9207624

Please sign in to comment.