Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing all Hyperion v2 calls #1

Merged
merged 7 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"prepare": "make"
},
"dependencies": {
"@wharfkit/antelope": "^0.9.1",
"@wharfkit/antelope": "^0.10.0-rc1",
"tslib": "^2.0.3"
},
"resolutions": {
Expand Down
54 changes: 37 additions & 17 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import {API, APIClient} from '@wharfkit/antelope'
import {ABI, APIClient} from '@wharfkit/antelope'
import {GetVotersResponse} from './types'

export class ExampleAPI {
export class HyperionAPIClient {
constructor(private client: APIClient) {}

/**
* Define the calls for the API
*/
// async get_raw_abi(accountName: NameType) {
// return this.call({
// path: '/v1/chain/get_raw_abi',
// params: {account_name: Name.from(accountName)},
// responseType: GetRawAbiResponse,
// })
// }

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

block = Number(info.last_irreversible_block_num)
}

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

async get_voters(
producer?: string,
proxy?: boolean,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make all the inputs be typed, e.g. this one being a NameType?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep!

skip?: number,
limit?: number
): Promise<GetVotersResponse> {
let queryParams = ''
const queryParts: string[] = []

if (producer) queryParts.push(`producer=${encodeURIComponent(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: '/v1/chain/get_info',
responseType: API.v1.GetInfoResponse,
path: `/v2/state/get_voters${queryParams}`,
method: 'GET',
responseType: GetVotersResponse,
})
}
}
34 changes: 10 additions & 24 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
/**
* Define the API response types
*
* Example and mock below
*/
import {Float64, Name, Struct, UInt32} from '@wharfkit/antelope'

import {Struct} from '@wharfkit/antelope'

@Struct.type('example_response')
export class ExampleResponse extends Struct {
@Struct.field('string') declare foo: string
@Struct.type('voter')
export class Voter extends Struct {
@Struct.field('name') declare account: Name
@Struct.field('float64') declare weight: Float64
@Struct.field('uint32') declare last_vote: UInt32
}

// import {
// Blob,
// Checksum256,
// Name,
// Struct,
// } from '@wharfkit/antelope'

// @Struct.type('get_raw_abi_response')
// export class GetRawAbiResponse extends Struct {
// @Struct.field('name') declare account_name: Name
// @Struct.field('checksum256') declare code_hash: Checksum256
// @Struct.field('checksum256') declare abi_hash: Checksum256
// @Struct.field(Blob) declare abi: Blob
// }
@Struct.type('get_voters_response')
export class GetVotersResponse extends Struct {
@Struct.field(Voter, {array: true}) declare voters: Voter[]
}
35 changes: 26 additions & 9 deletions test/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,39 @@ import {assert} from 'chai'
import {APIClient, FetchProvider} from '@wharfkit/antelope'
import {mockFetch} from '@wharfkit/mock-data'

import {ExampleAPI} from '$lib'
import {HyperionAPIClient, Types} from '$lib'

const ABIResponse = {
// Add mock data that mimics the actual ABI snapshot structure
};

// Setup an APIClient
const client = new APIClient({
provider: new FetchProvider('https://jungle4.greymass.com', {fetch: mockFetch}),
provider: new FetchProvider('https://eos.hyperion.eosrio.io/', {fetch: mockFetch}),
})

// Setup the API
const example = new ExampleAPI(client)
const hyperion = new HyperionAPIClient(client)

suite('api', function () {
suite('Hyperion API', function () {
this.slow(200)
this.timeout(10 * 10000)

test('call test api', async function () {
const res = await example.get_info()
assert.equal(res.server_version, '905c5cc9')
test('get_abi_snapshot', async function () {
const response = await hyperion.get_abi_snapshot("eosio.token", 2000, true);
assert.deepEqual(response, {
"block_num": null,
"error": "abi not found for eosio.token until block 2000",
"last_indexed_block": 331963268,
"last_indexed_block_time": "2023-09-20T00:35:04.500",
"present": false,
"query_time_ms": 4,
});
})

test('get_voters', async function () {
const response = await hyperion.get_voters('eoscafeblock', true, 100, 200);
assert.equal(response.voters.length, 24);
assert(response.voters[0].account.equals('killc.ftw'));
assert(response.voters[0].weight.equals(20161076275.827435));
assert(response.voters[0].last_vote.equals(297527904));
});
})
137 changes: 137 additions & 0 deletions test/data/9215270de3881cced9b6bebe07e24133100e3380.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"request": {
"path": "https://eos.hyperion.eosrio.io/v2/state/get_voters?producer=eoscafeblock&proxy=true&skip=100&limit=200",
"params": {
"method": "GET"
}
},
"status": 200,
"json": {
"query_time_ms": 10.267,
"last_indexed_block": 331966018,
"last_indexed_block_time": "2023-09-20T00:57:59.500",
"voters": [
{
"account": "killc.ftw",
"weight": 20161076275.827435,
"last_vote": 297527904
},
{
"account": "lilyoda12345",
"weight": 15164256984.237717,
"last_vote": 321496531
},
{
"account": "fddsfweghdfd",
"weight": 14755561472,
"last_vote": 282378753
},
{
"account": "butthead.ftw",
"weight": 10524346886.033388,
"last_vote": 327820867
},
{
"account": "httpsjmc.ftw",
"weight": 8449496093.827727,
"last_vote": 298379213
},
{
"account": "vecrazylazyy",
"weight": 7540108464.555911,
"last_vote": 272264082
},
{
"account": "dariusb.ftw",
"weight": 2366668638.478459,
"last_vote": 299903987
},
{
"account": "ghostwritter",
"weight": 1825622665.1887114,
"last_vote": 331476321
},
{
"account": "metamaskbc12",
"weight": 1095142587.150208,
"last_vote": 307011199
},
{
"account": "furkza.ftw",
"weight": 180920288.5627205,
"last_vote": 284399586
},
{
"account": "tanb5.ftw",
"weight": 0,
"last_vote": 269829286
},
{
"account": "exh121.ftw",
"weight": 0,
"last_vote": 274065500
},
{
"account": "5422gameseos",
"weight": 0,
"last_vote": 277702462
},
{
"account": "kurka134.ftw",
"weight": 0,
"last_vote": 303928120
},
{
"account": "hereticsteyn",
"weight": 0,
"last_vote": 310308429
},
{
"account": "omgant.ftw",
"weight": 0,
"last_vote": 329472633
},
{
"account": "gogoen.ftw",
"weight": 0,
"last_vote": 274148470
},
{
"account": "ariajack.ftw",
"weight": 0,
"last_vote": 281208030
},
{
"account": "sanihims.ftw",
"weight": 0,
"last_vote": 283638883
},
{
"account": "mawe2323.ftw",
"weight": 0,
"last_vote": 285576963
},
{
"account": "blanniem.ftw",
"weight": 0,
"last_vote": 290828124
},
{
"account": "gobrentb.ftw",
"weight": 0,
"last_vote": 302093796
},
{
"account": "supasaiyajay",
"weight": 0,
"last_vote": 314092695
},
{
"account": "adrianst1212",
"weight": 0,
"last_vote": 328663340
}
]
},
"text": "{\"query_time_ms\":10.267,\"last_indexed_block\":331966018,\"last_indexed_block_time\":\"2023-09-20T00:57:59.500\",\"voters\":[{\"account\":\"killc.ftw\",\"weight\":20161076275.827435,\"last_vote\":297527904},{\"account\":\"lilyoda12345\",\"weight\":15164256984.237717,\"last_vote\":321496531},{\"account\":\"fddsfweghdfd\",\"weight\":14755561472,\"last_vote\":282378753},{\"account\":\"butthead.ftw\",\"weight\":10524346886.033388,\"last_vote\":327820867},{\"account\":\"httpsjmc.ftw\",\"weight\":8449496093.827727,\"last_vote\":298379213},{\"account\":\"vecrazylazyy\",\"weight\":7540108464.555911,\"last_vote\":272264082},{\"account\":\"dariusb.ftw\",\"weight\":2366668638.478459,\"last_vote\":299903987},{\"account\":\"ghostwritter\",\"weight\":1825622665.1887114,\"last_vote\":331476321},{\"account\":\"metamaskbc12\",\"weight\":1095142587.150208,\"last_vote\":307011199},{\"account\":\"furkza.ftw\",\"weight\":180920288.5627205,\"last_vote\":284399586},{\"account\":\"tanb5.ftw\",\"weight\":0,\"last_vote\":269829286},{\"account\":\"exh121.ftw\",\"weight\":0,\"last_vote\":274065500},{\"account\":\"5422gameseos\",\"weight\":0,\"last_vote\":277702462},{\"account\":\"kurka134.ftw\",\"weight\":0,\"last_vote\":303928120},{\"account\":\"hereticsteyn\",\"weight\":0,\"last_vote\":310308429},{\"account\":\"omgant.ftw\",\"weight\":0,\"last_vote\":329472633},{\"account\":\"gogoen.ftw\",\"weight\":0,\"last_vote\":274148470},{\"account\":\"ariajack.ftw\",\"weight\":0,\"last_vote\":281208030},{\"account\":\"sanihims.ftw\",\"weight\":0,\"last_vote\":283638883},{\"account\":\"mawe2323.ftw\",\"weight\":0,\"last_vote\":285576963},{\"account\":\"blanniem.ftw\",\"weight\":0,\"last_vote\":290828124},{\"account\":\"gobrentb.ftw\",\"weight\":0,\"last_vote\":302093796},{\"account\":\"supasaiyajay\",\"weight\":0,\"last_vote\":314092695},{\"account\":\"adrianst1212\",\"weight\":0,\"last_vote\":328663340}]}"
}
18 changes: 18 additions & 0 deletions test/data/aa490d31c69413cc7a57f14e10d31884a6ebff04.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"request": {
"path": "https://eos.hyperion.eosrio.io/v2/history/get_abi_snapshot?contract=eosio.token&block=2000&fetch=true",
"params": {
"method": "GET"
}
},
"status": 200,
"json": {
"block_num": null,
"present": false,
"error": "abi not found for eosio.token until block 2000",
"query_time_ms": 4,
"last_indexed_block": 331963268,
"last_indexed_block_time": "2023-09-20T00:35:04.500"
},
"text": "{\"block_num\":null,\"present\":false,\"error\":\"abi not found for eosio.token until block 2000\",\"query_time_ms\":4,\"last_indexed_block\":331963268,\"last_indexed_block_time\":\"2023-09-20T00:35:04.500\"}"
}
32 changes: 0 additions & 32 deletions test/data/b5ae1f53ef4cf6544fe89a0deb7a7d1043f43149.json

This file was deleted.

12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,18 @@
pako "^2.0.4"
tslib "^2.1.0"

"@wharfkit/antelope@^0.10.0-rc1":
version "0.10.0-rc1"
resolved "https://registry.yarnpkg.com/@wharfkit/antelope/-/antelope-0.10.0-rc1.tgz#c3b83a2aaae5bebd4393455f2598f0fd9d17fb60"
integrity sha512-+rvXSGNEbzclMfg3ntyJ09jrjEdM56APV2WGoJHwEEAFjvHfKNUxunybxv6UrG3EgpCDcmSk+kiWQvbGevhCdw==
dependencies:
bn.js "^4.11.9"
brorand "^1.1.0"
elliptic "^6.5.4"
hash.js "^1.0.0"
pako "^2.1.0"
tslib "^2.0.3"

"@wharfkit/antelope@^0.7.3", "@wharfkit/antelope@^0.9.1":
version "0.9.1"
resolved "https://registry.yarnpkg.com/@wharfkit/antelope/-/antelope-0.9.1.tgz#ff6cfbb6f3c4e373407764cfce44a539dbb521ae"
Expand Down
Loading