diff --git a/src/endpoints/v2.ts b/src/endpoints/v2.ts index 3d8827a..631a573 100644 --- a/src/endpoints/v2.ts +++ b/src/endpoints/v2.ts @@ -6,7 +6,7 @@ import { NameType, PublicKeyType, } from '@wharfkit/antelope' -import {V2} from '../types' +import {Types} from '$lib' export class HyperionV2APIClient { public state: HyperionV2StateAPIClient @@ -17,11 +17,11 @@ export class HyperionV2APIClient { this.history = new HyperionV2HistoryAPIClient(client) } - get_health(): Promise { + get_health(): Promise { return this.client.call({ path: `/v2/health`, method: 'GET', - responseType: V2.GetHealthResponse, + responseType: Types.V2.GetHealthResponse, }) } } @@ -34,7 +34,7 @@ class HyperionV2StateAPIClient { proxy?: boolean, skip?: number, limit?: number - ): Promise { + ): Promise { let queryParams = '' const queryParts: string[] = [] @@ -48,33 +48,33 @@ class HyperionV2StateAPIClient { return this.client.call({ path: `/v2/state/get_voters${queryParams}`, method: 'GET', - responseType: V2.GetVotersResponse, + responseType: Types.V2.GetVotersResponse, }) } - async get_links(account?: NameType): Promise { + async get_links(account?: NameType): Promise { const queryParams = account ? `?account=${account}` : '' return this.client.call({ path: `/v2/state/get_links${queryParams}`, method: 'GET', - responseType: V2.GetLinksResponse, + responseType: Types.V2.GetLinksResponse, }) } - async get_key_accounts(public_key: PublicKeyType): Promise { + async get_key_accounts(public_key: PublicKeyType): Promise { return this.client.call({ path: `/v2/state/get_key_accounts?public_key=${public_key}`, method: 'GET', - responseType: V2.GetKeyAccountsResponse, + responseType: Types.V2.GetKeyAccountsResponse, }) } - async get_tokens(account: NameType): Promise { + async get_tokens(account: NameType): Promise { return this.client.call({ path: `/v2/state/get_tokens?account=${account}`, method: 'GET', - responseType: V2.GetTokensResponse, + responseType: Types.V2.GetTokensResponse, }) } @@ -87,7 +87,7 @@ class HyperionV2StateAPIClient { track?: number | boolean skip?: number limit?: number - }): Promise { + }): Promise { const queryParts: string[] = [] for (const [key, value] of Object.entries(options || {})) { @@ -99,15 +99,15 @@ class HyperionV2StateAPIClient { return this.client.call({ path: `/v2/state/get_proposals${queryParams}`, method: 'GET', - responseType: V2.GetProposalsResponse, + responseType: Types.V2.GetProposalsResponse, }) } - async get_account(account: NameType): Promise { + async get_account(account: NameType): Promise { return this.client.call({ path: `/v2/state/get_account?account=${account}`, method: 'GET', - responseType: V2.GetAccountResponse, + responseType: Types.V2.GetAccountResponse, }) } } @@ -119,7 +119,7 @@ export class HyperionV2HistoryAPIClient { contract: string, block?: number, fetch = false - ): Promise { + ): Promise { if (!block) { const info = await this.client.v1.chain.get_info() @@ -131,7 +131,7 @@ export class HyperionV2HistoryAPIClient { contract )}&block=${block}&fetch=${fetch}`, method: 'GET', - responseType: V2.GetABISnapshotResponse, + responseType: Types.V2.GetABISnapshotResponse, }) } @@ -150,7 +150,7 @@ export class HyperionV2HistoryAPIClient { act_name?: string act_account?: NameType } - ): Promise { + ): Promise { const queryParts: string[] = [`account=${account}`] for (const [key, value] of Object.entries(options || {})) { @@ -162,23 +162,37 @@ export class HyperionV2HistoryAPIClient { return this.client.call({ path: `/v2/history/get_actions${queryParams}`, method: 'GET', - responseType: V2.GetActionsResponse, + responseType: Types.V2.GetActionsResponse, }) } - async get_created_accounts(account: NameType): Promise { + async get_created_accounts( + account: NameType, + options?: { + skip?: number + limit?: number + } + ): Promise { + const queryParts: string[] = [`account=${account}`] + + for (const [key, value] of Object.entries(options || {})) { + queryParts.push(`${key}=${value}`) + } + + const queryParams = queryParts.length ? '?' + queryParts.join('&') : '' + return this.client.call({ - path: `/v2/history/get_created_accounts?account=${account}`, + path: `/v2/history/get_created_accounts${queryParams}`, method: 'GET', - responseType: V2.GetCreatedAccountsResponse, + responseType: Types.V2.GetCreatedAccountsResponse, }) } - async get_creator(account: NameType): Promise { + async get_creator(account: NameType): Promise { return this.client.call({ path: `/v2/history/get_creator?account=${account}`, method: 'GET', - responseType: V2.GetCreatorResponse, + responseType: Types.V2.GetCreatorResponse, }) } @@ -187,11 +201,11 @@ export class HyperionV2HistoryAPIClient { scope: NameType, table: NameType, payer: NameType - ): Promise { + ): Promise { return this.client.call({ path: `/v2/history/get_deltas?code=${code}&scope=${scope}&table=${table}&payer=${payer}`, method: 'GET', - responseType: V2.GetDeltasResponse, + responseType: Types.V2.GetDeltasResponse, }) } @@ -200,19 +214,19 @@ export class HyperionV2HistoryAPIClient { table: NameType, block_num: Int64Type, after_key = '' - ): Promise { + ): Promise { return this.client.call({ path: `/v2/history/get_table_state?code=${code}&table=${table}&block_num=${block_num}&after_key=${after_key}`, method: 'GET', - responseType: V2.GetTableStateResponse, + responseType: Types.V2.GetTableStateResponse, }) } - async get_transaction(id: Checksum256Type): Promise { + async get_transaction(id: Checksum256Type): Promise { return this.client.call({ path: `/v2/history/get_transaction?id=${id}`, method: 'GET', - responseType: V2.GetTransactionResponse, + responseType: Types.V2.GetTransactionResponse, }) } diff --git a/src/types.ts b/src/types.ts index e39a086..2916e90 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,6 +4,7 @@ import { BlockTimestamp, Checksum256, Float64, + Int64, Name, PermissionLevel, Struct, @@ -98,12 +99,41 @@ export namespace V2 { @Struct.field('any') declare data: any } + @Struct.type('account_sequence') + export class AccountSequence extends Struct { + @Struct.field(Name) declare account: Name + @Struct.field(UInt64) declare sequence: UInt64 + } + + @Struct.type('action_receipt') + export class ActionReceipt extends Struct { + @Struct.field(Name) declare receiver: Name + @Struct.field(UInt64) declare global_sequence: UInt64 + @Struct.field(UInt64) declare recv_sequence: UInt64 + @Struct.field(AccountSequence, {array: true}) declare auth_sequence: AccountSequence[] + } + + @Struct.type('action_ram_delta') + export class ActionRamDelta extends Struct { + @Struct.field(Name) declare account: Name + @Struct.field(Int64) declare delta: Int64 + } + @Struct.type('action') export class Action extends Struct { - @Struct.field(UInt32) declare block_num: UInt32 @Struct.field(BlockTimestamp) declare timestamp: BlockTimestamp + @Struct.field(UInt32) declare block_num: UInt32 + @Struct.field(Checksum256, {optional: true}) declare block_id: Checksum256 @Struct.field(Checksum256) declare trx_id: Checksum256 @Struct.field(ActionAct) declare act: ActionAct + @Struct.field(ActionReceipt, {array: true, optional: true}) + declare receipts: ActionReceipt[] + @Struct.field(ActionRamDelta, {array: true, optional: true}) + declare account_ram_deltas: ActionRamDelta[] + @Struct.field(UInt64) declare global_sequence: UInt64 + @Struct.field(Name) declare producer: Name + @Struct.field(UInt64) declare action_ordinal: UInt64 + @Struct.field(UInt64) declare creator_action_ordinal: UInt64 } @Struct.type('get_actions_response') diff --git a/test/api.ts b/test/api.ts index 780f2df..6569ed8 100644 --- a/test/api.ts +++ b/test/api.ts @@ -1,9 +1,17 @@ import {assert} from 'chai' -import {APIClient, Checksum256, FetchProvider, Float64, Name} from '@wharfkit/antelope' +import { + APIClient, + Checksum256, + FetchProvider, + Float64, + Name, + Serializer, + UInt64, +} from '@wharfkit/antelope' import {mockFetch} from '@wharfkit/mock-data' -import {HyperionAPIClient} from '$lib' +import {HyperionAPIClient, Types} from '$lib' const client = new APIClient({ provider: new FetchProvider('https://wax.blokcrafters.io/', {fetch: mockFetch}), @@ -17,119 +25,184 @@ suite('Hyperion API', function () { suite('v2', function () { test('get_health', async function () { - const response = await hyperion.v2.get_health(); - assert.equal(response.host, "wax.blokcrafters.io"); - assert.equal(response.version, "3.3.9-4"); - assert.equal(response.version_hash, "498bc8ec75429476add847921738ae239ae2921a"); - assert.equal(response.features.tables.proposals, true); + const response = await hyperion.v2.get_health() + + assert.instanceOf(response, Types.V2.GetHealthResponse) + assert.equal(response.host, 'wax.blokcrafters.io') + assert.equal(response.version, '3.3.9-4') + assert.equal(response.version_hash, '498bc8ec75429476add847921738ae239ae2921a') + assert.equal(response.features.tables.proposals, true) }) suite('state', function () { test('get_voters', async function () { - const response = await hyperion.v2.state.get_voters('teamgreymass', true, 100, 200); - assert.equal(response.voters.length, 100); - assert.instanceOf(response.voters[0].account, Name); - assert.equal(String(response.voters[0].account), 'j3.rq.wam'); - assert.equal(Number(response.voters[0].weight), 1.5044807131168136e+39); - assert.equal(Number(response.voters[0].last_vote), 266448316); - }); - + const response = await hyperion.v2.state.get_voters('teamgreymass', true, 100, 200) + + assert.instanceOf(response, Types.V2.GetVotersResponse) + assert.equal(response.voters.length, 100) + assert.instanceOf(response.voters[0].account, Name) + assert.equal(String(response.voters[0].account), 'j3.rq.wam') + assert.equal(Number(response.voters[0].weight), 1.5044807131168136e39) + assert.equal(Number(response.voters[0].last_vote), 266448316) + }) + test('get_links', async function () { - const response = await hyperion.v2.state.get_links('teamgreymass'); - assert.isArray(response.links); - assert.equal(response.links.length, 10); - assert.instanceOf(response.links[0].account, Name); - }); - + const response = await hyperion.v2.state.get_links('teamgreymass') + + assert.instanceOf(response, Types.V2.GetLinksResponse) + assert.isArray(response.links) + assert.equal(response.links.length, 10) + assert.instanceOf(response.links[0].account, Name) + }) + test('get_proposals', async function () { const response = await hyperion.v2.state.get_proposals({ skip: 1, - limit: 10 - }); - - assert.isArray(response.proposals); - assert.equal(response.proposals.length, 10); - assert.instanceOf(response.proposals[0].proposer, Name); + limit: 10, + }) + + assert.instanceOf(response, Types.V2.GetProposalsResponse) + assert.isArray(response.proposals) + assert.equal(response.proposals.length, 10) + assert.instanceOf(response.proposals[0].proposer, Name) }) - + test('get_key_accounts', async function () { - const response = await hyperion.v2.state.get_key_accounts("EOS8KmhygTrrvtW7zJd6HXWrNqA5WX9NzScZ37JyXRiwpiJN2g2rR"); - - assert.equal(response.account_names.length, 1); - assert.instanceOf(response.account_names[0], Name); - assert.equal(String(response.account_names[0]), "teamgreymass"); + const response = await hyperion.v2.state.get_key_accounts( + 'EOS8KmhygTrrvtW7zJd6HXWrNqA5WX9NzScZ37JyXRiwpiJN2g2rR' + ) + + assert.instanceOf(response, Types.V2.GetKeyAccountsResponse) + assert.equal(response.account_names.length, 1) + assert.instanceOf(response.account_names[0], Name) + assert.equal(String(response.account_names[0]), 'teamgreymass') }) - + test('get_tokens', async function () { - const response = await hyperion.v2.state.get_tokens("teamgreymass"); - - assert.instanceOf(response.account, Name); - assert.equal(response.tokens.length, 2); - assert.instanceOf(response.tokens[0].amount, Float64); + const response = await hyperion.v2.state.get_tokens('teamgreymass') + + assert.instanceOf(response, Types.V2.GetTokensResponse) + assert.instanceOf(response.account, Name) + assert.equal(response.tokens.length, 2) + assert.instanceOf(response.tokens[0].amount, Float64) }) test('get_account', async function () { - const response = await hyperion.v2.state.get_account("teamgreymass"); - assert.instanceOf(response.account.account_name, Name); - assert.equal(response.tokens.length, 2); - assert.instanceOf(response.tokens[0].amount, Float64); - assert.equal(Number(response.total_actions), 1658951); + const response = await hyperion.v2.state.get_account('teamgreymass') + + assert.instanceOf(response, Types.V2.GetAccountResponse) + assert.instanceOf(response.account.account_name, Name) + assert.equal(response.tokens.length, 2) + assert.instanceOf(response.tokens[0].amount, Float64) + assert.equal(Number(response.total_actions), 1658951) }) }) suite('history', function () { test('get_abi_snapshot', async function () { - const response = await hyperion.v2.history.get_abi_snapshot("eosio.token", 2000, true); - assert.equal(response.abi.version, "eosio::abi/1.1") + const response = await hyperion.v2.history.get_abi_snapshot( + 'eosio.token', + 2000, + true + ) + + assert.instanceOf(response, Types.V2.GetABISnapshotResponse) + assert.equal(response.abi.version, 'eosio::abi/1.1') }) test('get_actions', async function () { - const response = await hyperion.v2.history.get_actions("teamgreymass", { - filter: "eosio.token:*", + const response = await hyperion.v2.history.get_actions('teamgreymass', { + filter: 'eosio.token:*', skip: 100, - limit: 5 - }); - assert.isArray(response.actions); - assert.equal(response.actions.length, 5); - assert.instanceOf(response.actions[0].act.name, Name); - assert.instanceOf(response.actions[0].act.authorization[0].actor, Name); - }); + limit: 5, + }) + + assert.instanceOf(response, Types.V2.GetActionsResponse) + assert.isArray(response.actions) + assert.equal(response.actions.length, 5) + assert.instanceOf(response.actions[0].act.name, Name) + assert.instanceOf(response.actions[0].act.authorization[0].actor, Name) + assert.isArray(response.actions[0].receipts) + assert.instanceOf(response.actions[0].global_sequence, UInt64) + assert.instanceOf(response.actions[0].producer, Name) + assert.instanceOf(response.actions[0].action_ordinal, UInt64) + assert.instanceOf(response.actions[0].creator_action_ordinal, UInt64) + }) test('get_transaction', async function () { - const response = await hyperion.v2.history.get_transaction("a51a3cc53b2ff5d5b25ad44b1e3ef5f796ce3ca60101ea05b3be64e68b684ccb"); - - assert.instanceOf(response.trx_id, Checksum256); - assert.equal(response.actions.length, 5); - assert.instanceOf(response.actions[0].act.name, Name); + const response = await hyperion.v2.history.get_transaction( + 'a51a3cc53b2ff5d5b25ad44b1e3ef5f796ce3ca60101ea05b3be64e68b684ccb' + ) + + assert.instanceOf(response, Types.V2.GetTransactionResponse) + assert.instanceOf(response.trx_id, Checksum256) + assert.equal(response.actions.length, 5) + assert.instanceOf(response.actions[0].act.name, Name) }) test('get_deltas', async function () { - const response = await hyperion.v2.history.get_deltas("eosio.token", "teamgreymass", "accounts", "teamgreymass"); - - assert.equal(response.deltas.length, 10); - assert.instanceOf(response.deltas[0].code, Name); + const response = await hyperion.v2.history.get_deltas( + 'eosio.token', + 'teamgreymass', + 'accounts', + 'teamgreymass' + ) + + assert.instanceOf(response, Types.V2.GetDeltasResponse) + assert.equal(response.deltas.length, 10) + assert.instanceOf(response.deltas[0].code, Name) }) test('get_table_state', async function () { - const response = await hyperion.v2.history.get_table_state("eosio.token", "stat", 267000000); - - assert.instanceOf(response.code, Name); - assert.equal(response.results.length, 1); + const response = await hyperion.v2.history.get_table_state( + 'eosio.token', + 'stat', + 267000000 + ) + + assert.instanceOf(response, Types.V2.GetTableStateResponse) + assert.instanceOf(response.code, Name) + assert.equal(response.results.length, 1) }) test('get_created_accounts', async function () { - const response = await hyperion.v2.history.get_created_accounts("teamgreymass"); - - assert.isArray(response.accounts); - assert.equal(response.accounts.length, 4); - assert.instanceOf(response.accounts[0].name, Name); + const response = await hyperion.v2.history.get_created_accounts('teamgreymass') + + assert.instanceOf(response, Types.V2.GetCreatedAccountsResponse) + assert.isArray(response.accounts) + assert.equal(response.accounts.length, 4) + assert.instanceOf(response.accounts[0].name, Name) + }) + + test('get_created_accounts (limit & skip)', async function () { + const response = await hyperion.v2.history.get_created_accounts('gm', { + limit: 1, + }) + + assert.instanceOf(response, Types.V2.GetCreatedAccountsResponse) + assert.isArray(response.accounts) + assert.equal(response.accounts.length, 1) + assert.instanceOf(response.accounts[0].name, Name) + + const firstName = response.accounts[0].name + const response2 = await hyperion.v2.history.get_created_accounts('gm', { + limit: 1, + skip: 1, + }) + + assert.instanceOf(response2, Types.V2.GetCreatedAccountsResponse) + assert.isArray(response2.accounts) + assert.equal(response2.accounts.length, 1) + assert.instanceOf(response2.accounts[0].name, Name) + assert.notEqual(response2.accounts[0].name, firstName) }) - + test('get_creator', async function () { - const response = await hyperion.v2.history.get_creator("teamgreymass"); - - assert.instanceOf(response.creator, Name); - assert.equal(String(response.creator), "gqyqi.waa"); + const response = await hyperion.v2.history.get_creator('teamgreymass') + + assert.instanceOf(response, Types.V2.GetCreatorResponse) + assert.instanceOf(response.creator, Name) + assert.equal(String(response.creator), 'gqyqi.waa') }) }) - }) + }) }) diff --git a/test/data/0b6d9899da67422657e66f0f5ee1423a2b414cd9.json b/test/data/0b6d9899da67422657e66f0f5ee1423a2b414cd9.json new file mode 100644 index 0000000..58a0a6d --- /dev/null +++ b/test/data/0b6d9899da67422657e66f0f5ee1423a2b414cd9.json @@ -0,0 +1,20 @@ +{ + "request": { + "path": "https://wax.blokcrafters.io/v2/history/get_created_accounts?account=gm&limit=1&skip=1", + "params": { + "method": "GET" + } + }, + "status": 200, + "json": { + "query_time_ms": 21.97, + "accounts": [ + { + "name": "fpcgskpr5.gm", + "timestamp": "2024-02-22T23:16:25.000", + "trx_id": "b4460c4d49d0b4e585af4db2f958670ae97247ec061006604848fc8baf84fa26" + } + ] + }, + "text": "{\"query_time_ms\":21.97,\"accounts\":[{\"name\":\"fpcgskpr5.gm\",\"timestamp\":\"2024-02-22T23:16:25.000\",\"trx_id\":\"b4460c4d49d0b4e585af4db2f958670ae97247ec061006604848fc8baf84fa26\"}]}" +} \ No newline at end of file diff --git a/test/data/74c48a42da156e8dcf2658bbdf9f11e1a42d1bf5.json b/test/data/74c48a42da156e8dcf2658bbdf9f11e1a42d1bf5.json new file mode 100644 index 0000000..2ab0fe8 --- /dev/null +++ b/test/data/74c48a42da156e8dcf2658bbdf9f11e1a42d1bf5.json @@ -0,0 +1,20 @@ +{ + "request": { + "path": "https://wax.blokcrafters.io/v2/history/get_created_accounts?account=gm&limit=1", + "params": { + "method": "GET" + } + }, + "status": 200, + "json": { + "query_time_ms": 22.088, + "accounts": [ + { + "name": "kradz.gm", + "timestamp": "2024-02-23T01:15:21.500", + "trx_id": "7ee8b135615a6721b017a1ac6df912f3ad1393c10986453752e9b0329b5d6cb2" + } + ] + }, + "text": "{\"query_time_ms\":22.088,\"accounts\":[{\"name\":\"kradz.gm\",\"timestamp\":\"2024-02-23T01:15:21.500\",\"trx_id\":\"7ee8b135615a6721b017a1ac6df912f3ad1393c10986453752e9b0329b5d6cb2\"}]}" +} \ No newline at end of file diff --git a/test/tsconfig.json b/test/tsconfig.json index 6216a8d..e196c68 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -6,10 +6,7 @@ "module": "commonjs", "target": "es6", "types": ["mocha", "node"], - "baseUrl": "..", - "paths": { - "$lib": ["."] - }, + "baseUrl": ".." }, "include": ["*.ts", "**/*.ts"] } diff --git a/tsconfig.json b/tsconfig.json index 9da5823..8bdb50a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,10 @@ "sourceMap": true, "strict": true, "useUnknownInCatchVariables": false, - "target": "es2020" + "target": "es2020", + "paths": { + "$lib": ["./src"] + } }, "include": ["src/**/*"] }