Skip to content

Commit

Permalink
Allow passing scope as 2nd param to contract.table
Browse files Browse the repository at this point in the history
This matches the syntax used in the CDT, as discussed here:

https://github.com/orgs/wharfkit/discussions/5#discussioncomment-6515701
  • Loading branch information
aaroncox committed Aug 22, 2023
1 parent 69a3295 commit 3b2e81c
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 14 deletions.
13 changes: 12 additions & 1 deletion src/codegen/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ function generateTableMethod(abi: ABI.Def): ts.MethodDeclaration {
undefined
)

const scopeParameter = ts.factory.createParameterDeclaration(
undefined,
undefined,
undefined,
'scope',
ts.factory.createToken(ts.SyntaxKind.QuestionToken),
ts.factory.createTypeReferenceNode('NameType'),
undefined
)

// 4. Generate the function body.
const methodBody = ts.factory.createBlock(
[
Expand All @@ -205,6 +215,7 @@ function generateTableMethod(abi: ABI.Def): ts.MethodDeclaration {
undefined,
[
ts.factory.createIdentifier('name'),
ts.factory.createIdentifier('scope'),
ts.factory.createIdentifier('TableMap[name]'),
]
)
Expand All @@ -220,7 +231,7 @@ function generateTableMethod(abi: ABI.Def): ts.MethodDeclaration {
'table',
undefined,
[typeParameter],
[nameParameter],
[nameParameter, scopeParameter],
undefined,
methodBody
)
Expand Down
2 changes: 1 addition & 1 deletion src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class Contract {
return this.tableNames.includes(String(name))
}

public table<RowType = any>(name: NameType, rowType?): Table<RowType> {
public table<RowType = any>(name: NameType, scope?: NameType, rowType?): Table<RowType> {
if (!this.hasTable(name)) {
throw new Error(`Contract (${this.account}) does not have a table named (${name})`)
}
Expand Down
13 changes: 11 additions & 2 deletions src/contract/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface TableParams<TableRow = any> {
rowType?: TableRow
fieldToIndex?: FieldToIndex
defaultRowLimit?: number
defaultScope?: NameType
}

export interface GetTableRowsOptions {
Expand All @@ -52,6 +53,7 @@ export class Table<RowType = any> {

private fieldToIndex?: any

public defaultScope?: NameType
public defaultRowLimit = 1000

/**
Expand All @@ -77,6 +79,7 @@ export class Table<RowType = any> {
throw new Error(`Table ${this.name} not found in ABI`)
}
this.tableABI = tableABI
this.defaultScope = args.defaultScope
}

/**
Expand Down Expand Up @@ -110,7 +113,10 @@ export class Table<RowType = any> {
// Table query
table: this.name,
code: this.account,
scope: params.scope !== undefined ? String(params.scope) : this.account,
scope:
params.scope !== undefined
? String(params.scope)
: this.defaultScope || this.account,
// Response typing
type: this.rowType,
// Filtering
Expand Down Expand Up @@ -151,7 +157,10 @@ export class Table<RowType = any> {
const tableRowsParams = {
table: this.name,
code: this.account,
scope: params.scope !== undefined ? String(params.scope) : this.account,
scope:
params.scope !== undefined
? String(params.scope)
: this.defaultScope || this.account,
type: this.rowType!,
limit: 1,
lower_bound: wrapIndexValue(value),
Expand Down
7 changes: 5 additions & 2 deletions test/data/contracts/mock-eosio.msig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export namespace EosioMsig {
): Action {
return super.action(name, data, options)
}
table<T extends 'approvals' | 'approvals2' | 'invals' | 'proposal'>(name: T) {
return super.table(name, TableMap[name])
table<T extends 'approvals' | 'approvals2' | 'invals' | 'proposal'>(
name: T,
scope?: NameType
) {
return super.table(name, scope, TableMap[name])
}
}
export interface ActionNameParams {
Expand Down
4 changes: 2 additions & 2 deletions test/data/contracts/mock-eosio.token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export namespace EosioToken {
): Action {
return super.action(name, data, options)
}
table<T extends 'accounts' | 'stat'>(name: T) {
return super.table(name, TableMap[name])
table<T extends 'accounts' | 'stat'>(name: T, scope?: NameType) {
return super.table(name, scope, TableMap[name])
}
}
export interface ActionNameParams {
Expand Down
4 changes: 2 additions & 2 deletions test/data/contracts/mock-eosio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ export namespace Eosio {
| 'userres'
| 'voters'
| 'limitauthchg'
>(name: T) {
return super.table(name, TableMap[name])
>(name: T, scope?: NameType) {
return super.table(name, scope, TableMap[name])
}
}
export interface ActionNameParams {
Expand Down
4 changes: 2 additions & 2 deletions test/data/contracts/mock-rewards.gm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export namespace RewardsGm {
): Action {
return super.action(name, data, options)
}
table<T extends 'config' | 'users'>(name: T) {
return super.table(name, TableMap[name])
table<T extends 'config' | 'users'>(name: T, scope?: NameType) {
return super.table(name, scope, TableMap[name])
}
}
export interface ActionNameParams {
Expand Down
18 changes: 18 additions & 0 deletions test/data/requests/ac83d654fdb4c4ef32e6909e26c66f73c34f5f3e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"request": {
"path": "https://jungle4.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"table\":\"accounts\",\"code\":\"eosio.token\",\"scope\":\"eosio.token\",\"limit\":1,\"lower_bound\":\"\",\"upper_bound\":\"\",\"index_position\":\"primary\",\"key_type\":\"name\",\"json\":false}"
}
},
"status": 200,
"json": {
"rows": [
"811d20000000000004454f5300000000"
],
"more": true,
"next_key": "76193916147018"
},
"text": "{\"rows\":[\"811d20000000000004454f5300000000\"],\"more\":true,\"next_key\":\"76193916147018\"}"
}
18 changes: 18 additions & 0 deletions test/data/requests/b1cf0891251fa520755ab314e09cdaac9a7d5b7f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"request": {
"path": "https://jungle4.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"table\":\"accounts\",\"code\":\"eosio.token\",\"scope\":\"eosio.token\",\"limit\":1,\"index_position\":\"primary\",\"key_type\":\"name\",\"json\":false}"
}
},
"status": 200,
"json": {
"rows": [
"811d20000000000004454f5300000000"
],
"more": true,
"next_key": "76193916147018"
},
"text": "{\"rows\":[\"811d20000000000004454f5300000000\"],\"more\":true,\"next_key\":\"76193916147018\"}"
}
19 changes: 19 additions & 0 deletions test/data/requests/f6cb9e791a1f8ba9f62dc3fdfb46fdef3b872052.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"request": {
"path": "https://jungle4.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"json\":false,\"limit\":1000,\"table\":\"accounts\",\"code\":\"eosio.token\",\"scope\":\"eosio.token\",\"key_type\":\"name\"}"
}
},
"status": 200,
"json": {
"rows": [
"811d20000000000004454f5300000000",
"80841e0000000000044a554e474c4500"
],
"more": false,
"next_key": ""
},
"text": "{\"rows\":[\"811d20000000000004454f5300000000\",\"80841e0000000000044a554e474c4500\"],\"more\":false,\"next_key\":\"\"}"
}
6 changes: 5 additions & 1 deletion test/tests/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ suite('Contract', async function () {
})
suite('table', function () {
test('should accept rowType', async function () {
const table = systemContract.table<ProducerInfo>('producers', ProducerInfo)
const table = systemContract.table<ProducerInfo>(
'producers',
undefined,
ProducerInfo
)
assert.instanceOf(table, Table)
const producer = await table.get('lioninjungle')
assert.instanceOf(producer, ProducerInfo)
Expand Down
22 changes: 21 additions & 1 deletion test/tests/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ suite('Table', () => {
})
assert.instanceOf(table, Table)
})
test('defaultScope', () => {
const table = new Table({
abi: decentiumorg.abi,
account: decentiumorg.account,
client: mockClient,
name: Name.from('trending'),
defaultScope: 'foo',
})
assert.instanceOf(table, Table)
assert.equal(table.defaultScope, 'foo')
})
})

suite('cursor', () => {
Expand Down Expand Up @@ -350,11 +361,20 @@ suite('Table', () => {
})

test('should just get first row without params', async function () {
const table = eosio.table<EosioGlobalState>('global', EosioGlobalState)
const table = eosio.table<EosioGlobalState>('global', undefined, EosioGlobalState)
const row = await table.get()
assert.instanceOf(row, EosioGlobalState)
assert.instanceOf(row.pervote_bucket, Int64)
})

test('should use scope from table call', async function () {
const contractKit = new ContractKit({
client: makeClient('https://jungle4.greymass.com'),
})
const contract = await contractKit.load('eosio.token')
const result = await contract.table('accounts', 'wharfkittest').get()
assert.instanceOf(result.balance, Asset)
})
})

suite('first', () => {
Expand Down

0 comments on commit 3b2e81c

Please sign in to comment.