Skip to content

Commit

Permalink
feat: add vote account decoder (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines authored Jul 24, 2019
1 parent 36d5599 commit a642156
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
25 changes: 25 additions & 0 deletions module.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,31 @@ declare module '@solana/web3.js' {
static fromConfigData(buffer: Buffer): ?ValidatorInfo;
}

// === src/vote-account.js ===
declare export type Lockout = {|
slot: number,
confirmationCount: number,
|};

declare export type EpochCredits = {|
epoch: number,
credits: number,
prevCredits: number,
|};

declare export class VoteAccount {
votes: Array<Lockout>;
nodePubkey: PublicKey;
authorizedVoterPubkey: PublicKey;
commission: number;
rootSlot: number | null;
epoch: number;
credits: number;
lastEpochCredits: number;
epochCredits: Array<EpochCredits>;
static fromAccountData(buffer: Buffer): VoteAccount;
}

// === src/transaction.js ===
declare export type TransactionSignature = string;

Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {SystemProgram} from './system-program';
export {Token, TokenAmount} from './token-program';
export {Transaction, TransactionInstruction} from './transaction';
export {ValidatorInfo} from './validator-info';
export {VoteAccount} from './vote-account';
export {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
export {
sendAndConfirmRawTransaction,
Expand Down
85 changes: 85 additions & 0 deletions src/vote-account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// @flow
import * as BufferLayout from 'buffer-layout';

import * as Layout from './layout';
import {PublicKey} from './publickey';

export type Lockout = {|
slot: number,
confirmationCount: number,
|};

/**
* History of how many credits earned by the end of each epoch
*/
export type EpochCredits = {|
epoch: number,
credits: number,
prevCredits: number,
|};

/**
* See https://github.com/solana-labs/solana/blob/8a12ed029cfa38d4a45400916c2463fb82bbec8c/programs/vote_api/src/vote_state.rs#L68-L88
*
* @private
*/
const VoteAccountLayout = BufferLayout.struct([
BufferLayout.nu64(), // votes.length
BufferLayout.seq(
BufferLayout.struct([
BufferLayout.nu64('slot'),
BufferLayout.u32('confirmationCount'),
]),
BufferLayout.offset(BufferLayout.u32(), -8),
'votes',
),
Layout.publicKey('nodePubkey'),
Layout.publicKey('authorizedVoterPubkey'),
BufferLayout.u8('commission'),
BufferLayout.u8('rootSlotValid'),
BufferLayout.nu64('rootSlot'),
BufferLayout.nu64('epoch'),
BufferLayout.nu64('credits'),
BufferLayout.nu64('lastEpochCredits'),
BufferLayout.nu64(), // epochCredits.length
BufferLayout.seq(
BufferLayout.struct([
BufferLayout.nu64('epoch'),
BufferLayout.nu64('credits'),
BufferLayout.nu64('prevCredits'),
]),
BufferLayout.offset(BufferLayout.u32(), -8),
'epochCredits',
),
]);

/**
* VoteAccount class
*/
export class VoteAccount {
votes: Array<Lockout>;
nodePubkey: PublicKey;
authorizedVoterPubkey: PublicKey;
commission: number;
rootSlot: number | null;
epoch: number;
credits: number;
lastEpochCredits: number;
epochCredits: Array<EpochCredits>;

/**
* Deserialize VoteAccount from the account data.
*
* @param buffer account data
* @return VoteAccount
*/
static fromAccountData(buffer: Buffer): VoteAccount {
const va = VoteAccountLayout.decode(buffer, 0);
va.nodePubkey = new PublicKey(va.nodePubkey);
va.authorizedVoterPubkey = new PublicKey(va.authorizedVoterPubkey);
if (!va.rootSlotValid) {
va.rootSlot = null;
}
return va;
}
}

0 comments on commit a642156

Please sign in to comment.