From f4793c03f21a0eda2d96ec9fc660c39eefcbb31a Mon Sep 17 00:00:00 2001 From: dafuga Date: Tue, 19 Sep 2023 18:04:40 -0700 Subject: [PATCH] chore: added get_voters function --- src/endpoints.ts | 18 +++ src/types.ts | 14 +- test/api.ts | 12 +- ...5270de3881cced9b6bebe07e24133100e3380.json | 137 ++++++++++++++++++ 4 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 test/data/9215270de3881cced9b6bebe07e24133100e3380.json diff --git a/src/endpoints.ts b/src/endpoints.ts index bd727b5..d355957 100644 --- a/src/endpoints.ts +++ b/src/endpoints.ts @@ -1,4 +1,5 @@ import {ABI, APIClient} from '@wharfkit/antelope' +import { GetVotersResponse } from './types'; export class HyperionAPIClient { constructor(private client: APIClient) {} @@ -17,4 +18,21 @@ export class HyperionAPIClient { method: 'GET', }) } + + async get_voters(producer?: string, proxy?: boolean, skip?: number, limit?: number): Promise { + 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: `/v2/state/get_voters${queryParams}`, + method: 'GET', + }); + } } diff --git a/src/types.ts b/src/types.ts index 8a6f38d..8b71f3b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,13 @@ -export interface Response { - code: number +import { Float64, Name, Struct, UInt32 } from '@wharfkit/antelope'; + +@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; } + +@Struct.type('get_voters_response') +export class GetVotersResponse extends Struct { + @Struct.field(Voter, { array: true }) declare voters: Voter[]; +} \ No newline at end of file diff --git a/test/api.ts b/test/api.ts index 67ab288..1b683f2 100644 --- a/test/api.ts +++ b/test/api.ts @@ -3,7 +3,7 @@ import {assert} from 'chai' import {APIClient, FetchProvider} from '@wharfkit/antelope' import {mockFetch} from '@wharfkit/mock-data' -import {HyperionAPIClient} from '$lib' // Replace with the actual import +import {HyperionAPIClient, Types} from '$lib' const ABIResponse = { // Add mock data that mimics the actual ABI snapshot structure @@ -30,4 +30,14 @@ suite('Hyperion API', function () { "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.deepEqual(response.voters[0], { + "account": "killc.ftw", + "weight": 20161076275.827435, + "last_vote": 297527904, + }); + }); }) diff --git a/test/data/9215270de3881cced9b6bebe07e24133100e3380.json b/test/data/9215270de3881cced9b6bebe07e24133100e3380.json new file mode 100644 index 0000000..2f28d13 --- /dev/null +++ b/test/data/9215270de3881cced9b6bebe07e24133100e3380.json @@ -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}]}" +} \ No newline at end of file