Skip to content

Commit

Permalink
chore: added get_voters function
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Sep 20, 2023
1 parent 54ac5c4 commit f26d558
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ABI, APIClient} from '@wharfkit/antelope'
import {GetVotersResponse} from './types'

export class HyperionAPIClient {
constructor(private client: APIClient) {}
Expand All @@ -17,4 +18,27 @@ export class HyperionAPIClient {
method: 'GET',
})
}

async get_voters(
producer?: string,
proxy?: boolean,
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: `/v2/state/get_voters${queryParams}`,
method: 'GET',
responseType: GetVotersResponse,
})
}
}
14 changes: 12 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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[]
}
10 changes: 9 additions & 1 deletion test/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,4 +30,12 @@ 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(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}]}"
}

0 comments on commit f26d558

Please sign in to comment.