Skip to content

Commit

Permalink
chore: added get_tokens and get_transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Sep 21, 2023
1 parent cdd1c7e commit df03a9a
Show file tree
Hide file tree
Showing 5 changed files with 448 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {APIClient, Asset, Int64Type, NameType, UInt64Type} from '@wharfkit/antelope'
import {GetABISnapshotResponse, GetActionsResponse, GetCreatedAccountsResponse, GetCreatorResponse, GetDeltasResponse, GetKeyAccountsResponse, GetLinksResponse, GetProposalsResponse, GetTableStateResponse, GetVotersResponse} from './types'
import {APIClient, Asset, Checksum256Type, Int64Type, Name, NameType, PublicKeyType, UInt64Type} from '@wharfkit/antelope'
import {GetABISnapshotResponse, GetActionsResponse, GetCreatedAccountsResponse, GetCreatorResponse, GetDeltasResponse, GetKeyAccountsResponse, GetLinksResponse, GetProposalsResponse, GetTableStateResponse, GetTokensResponse, GetTransactionResponse, GetVotersResponse} from './types'

export class HyperionAPIClient {
constructor(private client: APIClient) {}
Expand Down Expand Up @@ -138,11 +138,27 @@ export class HyperionAPIClient {
});
}

async get_key_accounts(public_key: string): Promise<GetKeyAccountsResponse> {
async get_key_accounts(public_key: PublicKeyType): Promise<GetKeyAccountsResponse> {
return this.client.call({
path: `/v2/state/get_key_accounts?public_key=${public_key}`,
method: 'GET',
responseType: GetKeyAccountsResponse,
});
}

async get_tokens(account: NameType): Promise<GetTokensResponse> {
return this.client.call({
path: `/v2/state/get_tokens?account=${account}`,
method: 'GET',
responseType: GetTokensResponse,
});
}

async get_transaction(id: Checksum256Type): Promise<GetTransactionResponse> {
return this.client.call({
path: `/v2/history/get_transaction?id=${id}`,
method: 'GET',
responseType: GetTransactionResponse,
});
}
}
27 changes: 26 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,29 @@ export class GetTableStateResponse extends Struct {
@Struct.type('get_key_accounts_response')
export class GetKeyAccountsResponse extends Struct {
@Struct.field(Name, { array: true }) declare account_names: Name[];
}
}

@Struct.type('token_info')
export class TokenInfo extends Struct {
@Struct.field('string') declare symbol: string;
@Struct.field(Float64) declare amount: Float64;
@Struct.field('string') declare contract: 'string';
@Struct.field(UInt32, { optional: true }) declare precision?: UInt32;
@Struct.field('string', { optional: true }) declare error?: string;
}
@Struct.type('get_tokens_response')
export class GetTokensResponse extends Struct {
@Struct.field(Name) declare account: Name;
@Struct.field(Float64) declare query_time_ms: Float64;
@Struct.field(TokenInfo, { array: true }) declare tokens: TokenInfo[];
}

@Struct.type('get_transaction_response')
export class GetTransactionResponse extends Struct {
@Struct.field(Float64) declare query_time_ms: Float64;
@Struct.field('bool') declare executed: boolean;
@Struct.field(Checksum256) declare trx_id: Checksum256;
@Struct.field(UInt32) declare lib: UInt32;
@Struct.field('bool') declare cached_lib: boolean;
@Struct.field(Action, { array: true }) declare actions: Action[];
}
18 changes: 17 additions & 1 deletion test/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {assert} from 'chai'

import {APIClient, FetchProvider, Name, UInt64} from '@wharfkit/antelope'
import {APIClient, Checksum256, FetchProvider, Float64, Name, UInt64} from '@wharfkit/antelope'
import {mockFetch} from '@wharfkit/mock-data'

import {HyperionAPIClient} from '$lib'
Expand Down Expand Up @@ -101,4 +101,20 @@ suite('Hyperion API', function () {
assert.instanceOf(response.account_names[0], Name);
assert.equal(String(response.account_names[0]), "teamgreymass");
})

test('get_tokens', async function () {
const response = await hyperion.get_tokens("teamgreymass");

assert.instanceOf(response.account, Name);
assert.equal(response.tokens.length, 2);
assert.instanceOf(response.tokens[0].amount, Float64);
})

test('get_transaction', async function () {
const response = await hyperion.get_transaction("a51a3cc53b2ff5d5b25ad44b1e3ef5f796ce3ca60101ea05b3be64e68b684ccb");

assert.instanceOf(response.trx_id, Checksum256);
assert.equal(response.actions.length, 5);
assert.instanceOf(response.actions[0].act.name, Name);
})
})
Loading

0 comments on commit df03a9a

Please sign in to comment.