Skip to content

Commit

Permalink
Add debug flag to allow json: true on table requests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Dec 21, 2024
1 parent 1a074d7 commit 7df3e66
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface ContractArgs {
client: APIClient
}

export interface ContractOptions {
debug?: boolean
}

export interface ActionOptions {
authorization?: PermissionLevelType[]
}
Expand All @@ -45,13 +49,14 @@ export class Contract {
readonly abi: ABI
readonly account: Name
readonly client: APIClient
readonly debug: boolean = false

/**
* Constructs a new `Contract` instance.
*
* @param {ContractArgs} args - The required arguments for a contract.
*/
constructor(args: ContractArgs) {
constructor(args: ContractArgs, options: ContractOptions = {}) {
if (!args.abi) {
throw new Error('Contract requires an ABI')
}
Expand All @@ -64,6 +69,9 @@ export class Contract {
throw new Error('Contract requires an APIClient')
}
this.client = args.client
if (options.debug) {
this.debug = options.debug
}
}

public get tableNames(): string[] {
Expand All @@ -82,6 +90,7 @@ export class Contract {
abi: this.abi,
account: this.account,
client: this.client,
debug: this.debug,
defaultScope: scope,
name,
rowType,
Expand Down
13 changes: 12 additions & 1 deletion src/contract/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface TableParams<TableRow = any> {
name: NameType
rowType?: TableRow
fieldToIndex?: FieldToIndex
debug?: boolean
defaultRowLimit?: number
defaultScope?: NameType
}
Expand All @@ -52,6 +53,7 @@ export class Table<RowType = any> {
readonly name: Name
readonly rowType?: RowType
readonly tableABI: ABI.Table
readonly debug: boolean = false

private fieldToIndex?: any

Expand Down Expand Up @@ -82,6 +84,9 @@ export class Table<RowType = any> {
}
this.tableABI = tableABI
this.defaultScope = args.defaultScope
if (args.debug) {
this.debug = true
}
}

/**
Expand Down Expand Up @@ -118,6 +123,7 @@ export class Table<RowType = any> {
: this.defaultScope || this.account,
// Response typing
type: this.rowType,
json: this.debug,
// Filtering
index_position: params.index_position,
key_type: params.key_type,
Expand Down Expand Up @@ -174,7 +180,7 @@ export class Table<RowType = any> {
upper_bound: wrapIndexValue(value),
index_position: params.index_position,
key_type: params.key_type,
json: false,
json: this.debug,
reverse: params.reverse,
}

Expand All @@ -199,6 +205,11 @@ export class Table<RowType = any> {
}
let [row] = rows

// Debug mode will return a JSON result, so just return it
if (this.debug) {
return row
}

if (!this.rowType) {
row = Serializer.decode({
data: row,
Expand Down
21 changes: 16 additions & 5 deletions src/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ export interface ABIDefinition {
export interface ContractKitOptions {
abiCache?: ABICacheInterface
abis?: ABIDefinition[]
debug?: boolean
}

const defaultContractKitOptions: ContractKitOptions = {}

export class ContractKit {
readonly abiCache: ABICacheInterface
readonly client: APIClient
readonly debug: boolean = false

constructor(args: ContractKitArgs, options: ContractKitOptions = defaultContractKitOptions) {
if (args.client) {
Expand All @@ -43,6 +45,10 @@ export class ContractKit {
this.abiCache.setAbi(Name.from(name), ABI.from(abi))
)
}

if (options.debug) {
this.debug = options.debug
}
}

/**
Expand All @@ -54,10 +60,15 @@ export class ContractKit {
async load(contract: NameType): Promise<Contract> {
const account = Name.from(contract)
const abiDef = await this.abiCache.getAbi(account)
return new Contract({
abi: ABI.from(abiDef),
account,
client: this.client,
})
return new Contract(
{
abi: ABI.from(abiDef),
account,
client: this.client,
},
{
debug: this.debug,
}
)
}
}
23 changes: 23 additions & 0 deletions test/data/requests/1c7e42cd1ab2b6b573c3b13c24d5a057c979572f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"table\":\"namebids\",\"code\":\"eosio\",\"scope\":\"eosio\",\"limit\":1,\"key_type\":\"name\",\"json\":true}"
}
},
"status": 200,
"json": {
"rows": [
{
"newname": "",
"high_bidder": "guydgnjygige",
"high_bid": 3000,
"last_bid_time": "2018-06-21T03:51:50.500"
}
],
"more": true,
"next_key": "594475150812905472"
},
"text": "{\"rows\":[{\"newname\":\"\",\"high_bidder\":\"guydgnjygige\",\"high_bid\":3000,\"last_bid_time\":\"2018-06-21T03:51:50.500\"}],\"more\":true,\"next_key\":\"594475150812905472\"}"
}
49 changes: 49 additions & 0 deletions test/data/requests/366fa742787295d5854874aeb6ef78c442b50abb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_table_rows",
"params": {
"method": "POST",
"body": "{\"json\":true,\"limit\":1000,\"table\":\"global\",\"code\":\"eosio\",\"scope\":\"eosio\",\"key_type\":\"name\"}"
}
},
"status": 200,
"json": {
"rows": [
{
"max_block_net_usage": 1048576,
"target_block_net_usage_pct": 10000,
"max_transaction_net_usage": 524288,
"base_per_transaction_net_usage": 12,
"net_usage_leeway": 500,
"context_free_discount_net_usage_num": 20,
"context_free_discount_net_usage_den": 100,
"max_block_cpu_usage": 200000,
"target_block_cpu_usage_pct": 10,
"max_transaction_cpu_usage": 150000,
"min_transaction_cpu_usage": 100,
"max_transaction_lifetime": 3600,
"deferred_trx_expiration_window": 600,
"max_transaction_delay": 3888000,
"max_inline_action_size": 524287,
"max_inline_action_depth": 10,
"max_authority_depth": 10,
"max_ram_size": "418945440768",
"total_ram_bytes_reserved": "333551148743",
"total_ram_stake": "213336801716",
"last_producer_schedule_update": "2024-12-21T19:15:00.500",
"last_pervote_bucket_fill": "2024-12-21T18:53:42.000",
"pervote_bucket": 257850978,
"perblock_bucket": 39779454,
"total_unpaid_blocks": 82609,
"total_activated_stake": "6092900115741",
"thresh_activated_stake_time": "2018-06-20T14:44:52.000",
"last_producer_schedule_size": 21,
"total_producer_vote_weight": "1410584016862351458304.00000000000000000",
"last_name_close": "2024-12-21T02:42:47.500"
}
],
"more": false,
"next_key": ""
},
"text": "{\"rows\":[{\"max_block_net_usage\":1048576,\"target_block_net_usage_pct\":10000,\"max_transaction_net_usage\":524288,\"base_per_transaction_net_usage\":12,\"net_usage_leeway\":500,\"context_free_discount_net_usage_num\":20,\"context_free_discount_net_usage_den\":100,\"max_block_cpu_usage\":200000,\"target_block_cpu_usage_pct\":10,\"max_transaction_cpu_usage\":150000,\"min_transaction_cpu_usage\":100,\"max_transaction_lifetime\":3600,\"deferred_trx_expiration_window\":600,\"max_transaction_delay\":3888000,\"max_inline_action_size\":524287,\"max_inline_action_depth\":10,\"max_authority_depth\":10,\"max_ram_size\":\"418945440768\",\"total_ram_bytes_reserved\":\"333551148743\",\"total_ram_stake\":\"213336801716\",\"last_producer_schedule_update\":\"2024-12-21T19:15:00.500\",\"last_pervote_bucket_fill\":\"2024-12-21T18:53:42.000\",\"pervote_bucket\":257850978,\"perblock_bucket\":39779454,\"total_unpaid_blocks\":82609,\"total_activated_stake\":\"6092900115741\",\"thresh_activated_stake_time\":\"2018-06-20T14:44:52.000\",\"last_producer_schedule_size\":21,\"total_producer_vote_weight\":\"1410584016862351458304.00000000000000000\",\"last_name_close\":\"2024-12-21T02:42:47.500\"}],\"more\":false,\"next_key\":\"\"}"
}
12 changes: 12 additions & 0 deletions test/tests/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ suite('Kit', function () {
assert.instanceOf(kit, ContractKit)
assert.instanceOf(kit.abiCache, ABICache)
})
test('options: debug', async function () {
const kit = new ContractKit(mockContractKitArgs, {
debug: true,
})
assert.instanceOf(kit, ContractKit)
assert.isTrue(kit.debug)
const contract = await kit.load('eosio.token')
assert.instanceOf(contract, Contract)
assert.isTrue(contract.debug)
const table = contract.table('accounts')
assert.isTrue(table.debug)
})
suite('options: abis', function () {
test('untyped', async function () {
const kit = new ContractKit(mockContractKitArgs, {
Expand Down
48 changes: 48 additions & 0 deletions test/tests/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ suite('Table', () => {
[6, 5]
)
})

test('should return deserialized data in debug', async function () {
const table = new Table({
abi: eosio.abi,
account: 'eosio',
client: mockClient,
name: 'global',
debug: true,
})
const row = await table.all()
assert.deepEqual(row, JSON.parse(JSON.stringify(row)))
})
})

suite('get', () => {
Expand Down Expand Up @@ -418,6 +430,18 @@ suite('Table', () => {

assert.isUndefined(row)
})

test('should return deserialized data in debug', async function () {
const table = new Table({
abi: eosio.abi,
account: 'eosio',
client: mockClient,
name: 'namebids',
debug: true,
})
const row = await table.get()
assert.deepEqual(row, JSON.parse(JSON.stringify(row)))
})
})

suite('first', () => {
Expand Down Expand Up @@ -485,6 +509,19 @@ suite('Table', () => {
assert.instanceOf(batch[0].ref.category, Name)
})
})

test('should return deserialized data in debug', async function () {
const table = new Table({
abi: eosio.abi,
account: 'eosio',
client: mockClient,
name: 'global',
debug: true,
})
const cursor = await table.query()
const row = await cursor.all()
assert.deepEqual(row, JSON.parse(JSON.stringify(row)))
})
})

suite('all', () => {
Expand All @@ -496,6 +533,17 @@ suite('Table', () => {
const tableRows = await nameBidTable.all()
assert.instanceOf(tableRows[0].high_bidder, Name)
})
test('should return deserialized data in debug', async function () {
const table = new Table({
abi: eosio.abi,
account: 'eosio',
client: mockClient,
name: 'global',
debug: true,
})
const row = await table.all()
assert.deepEqual(row, JSON.parse(JSON.stringify(row)))
})
})

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

0 comments on commit 7df3e66

Please sign in to comment.