Skip to content

Commit

Permalink
Follow-up work (#2)
Browse files Browse the repository at this point in the history
* chore: handling different API versions

* chore: added get_health and get_account functions

* chore: added get_transfers

* enhancement: throwing error when non-implemented method is called

* style: linted

* chore: quick fix to readme
  • Loading branch information
dafuga authored Sep 26, 2023
1 parent 9271282 commit 3fc4a47
Show file tree
Hide file tree
Showing 8 changed files with 2,125 additions and 460 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# @wharfkit/api-client-template
# @wharfkit/api-client-hyperion

Template for creating new api-client instances for use in Wharf.
API client for the Hyperion API.

## Running Tests

```
make test
```

The browser test suite for the current version of the library is available at: https://wharfkit.github.io/antelope/tests.html
204 changes: 8 additions & 196 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,201 +1,13 @@
import {
APIClient,
Asset,
Checksum256Type,
Int64Type,
NameType,
PublicKeyType,
} from '@wharfkit/antelope'
import {
GetABISnapshotResponse,
GetActionsResponse,
GetCreatedAccountsResponse,
GetCreatorResponse,
GetDeltasResponse,
GetKeyAccountsResponse,
GetLinksResponse,
GetProposalsResponse,
GetTableStateResponse,
GetTokensResponse,
GetTransactionResponse,
GetVotersResponse,
} from './types'
import {APIClient} from '@wharfkit/antelope'
import {HyperionV1APIClient} from './endpoints/v1'
import {HyperionV2APIClient} from './endpoints/v2'

export class HyperionAPIClient {
constructor(private client: APIClient) {}
public v1: HyperionV1APIClient
public v2: HyperionV2APIClient

async get_abi_snapshot(
contract: string,
block?: number,
fetch = false
): Promise<GetABISnapshotResponse> {
if (!block) {
const info = await this.client.v1.chain.get_info()

block = Number(info.last_irreversible_block_num)
}

return this.client.call({
path: `/v2/history/get_abi_snapshot?contract=${encodeURIComponent(
contract
)}&block=${block}&fetch=${fetch}`,
method: 'GET',
responseType: GetABISnapshotResponse,
})
}

async get_voters(
producer?: NameType,
proxy?: boolean,
skip?: number,
limit?: number
): Promise<GetVotersResponse> {
let queryParams = ''
const queryParts: string[] = []

if (producer) queryParts.push(`producer=${producer}`)
if (proxy !== undefined) queryParts.push(`proxy=${proxy}`)
if (skip !== undefined) queryParts.push(`skip=${skip}`)
if (limit !== undefined) queryParts.push(`limit=${limit}`)

queryParams = queryParts.length ? '?' + queryParts.join('&') : ''

return this.client.call({
path: `/v2/state/get_voters${queryParams}`,
method: 'GET',
responseType: GetVotersResponse,
})
}

async get_links(account?: NameType): Promise<GetLinksResponse> {
const queryParams = account ? `?account=${account}` : ''

return this.client.call({
path: `/v2/state/get_links${queryParams}`,
method: 'GET',
responseType: GetLinksResponse,
})
}

async get_proposals(options?: {
proposer?: NameType
proposal?: NameType
account?: NameType
requested?: string
provided?: string
track?: number | boolean
skip?: number
limit?: number
}): Promise<GetProposalsResponse> {
const queryParts: string[] = []

for (const [key, value] of Object.entries(options || {})) {
queryParts.push(`${key}=${value}`)
}

const queryParams = queryParts.length ? '?' + queryParts.join('&') : ''

return this.client.call({
path: `/v2/state/get_proposals${queryParams}`,
method: 'GET',
responseType: GetProposalsResponse,
})
}

async get_actions(
account: NameType,
options?: {
filter?: string
skip?: number
limit?: number
sort?: string
after?: string
before?: string
transfer_to?: NameType
transfer_from?: NameType
transfer_symbol?: Asset.Symbol
act_name?: string
act_account?: NameType
}
): Promise<GetActionsResponse> {
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_actions${queryParams}`,
method: 'GET',
responseType: GetActionsResponse,
})
}

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

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

async get_deltas(
code: NameType,
scope: NameType,
table: NameType,
payer: NameType
): Promise<GetDeltasResponse> {
return this.client.call({
path: `/v2/history/get_deltas?code=${code}&scope=${scope}&table=${table}&payer=${payer}`,
method: 'GET',
responseType: GetDeltasResponse,
})
}

async get_table_state(
code: NameType,
table: NameType,
block_num: Int64Type,
after_key = ''
): Promise<GetTableStateResponse> {
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: GetTableStateResponse,
})
}

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,
})
constructor(private client: APIClient) {
this.v1 = new HyperionV1APIClient(client)
this.v2 = new HyperionV2APIClient(client)
}
}
49 changes: 49 additions & 0 deletions src/endpoints/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {APIClient} from '@wharfkit/antelope'

export class HyperionV1APIClient {
public history: HyperionV1HistoryAPIClient
public chain: HyperionV1ChainAPIClient
public trace_api: HyperionV1TraceApiClient

constructor(private client: APIClient) {
this.history = new HyperionV1HistoryAPIClient(client)
this.chain = new HyperionV1ChainAPIClient(client)
this.trace_api = new HyperionV1TraceApiClient(client)
}
}

class HyperionV1HistoryAPIClient {
constructor(private client: APIClient) {}

async get_actions(): Promise<void> {
throw new Error('Method not implemented.')
}

async get_controlled_accounts(): Promise<void> {
throw new Error('Method not implemented.')
}

async get_key_accounts(): Promise<void> {
throw new Error('Method not implemented.')
}

async get_transaction(): Promise<void> {
throw new Error('Method not implemented.')
}
}

class HyperionV1ChainAPIClient {
constructor(private client: APIClient) {}

async get_block(): Promise<void> {
throw new Error('Method not implemented.')
}
}

class HyperionV1TraceApiClient {
constructor(private client: APIClient) {}

async get_block(): Promise<void> {
throw new Error('Method not implemented.')
}
}
Loading

0 comments on commit 3fc4a47

Please sign in to comment.