solana/web3.js/src/vote-account.js

95 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-07-23 18:06:55 -07:00
// @flow
import * as BufferLayout from 'buffer-layout';
import * as Layout from './layout';
import {PublicKey} from './publickey';
export const VOTE_PROGRAM_ID = new PublicKey(
2019-07-24 16:01:07 -07:00
'Vote111111111111111111111111111111111111111',
);
2019-07-23 18:06:55 -07:00
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([
2019-09-26 15:00:32 -07:00
Layout.publicKey('nodePubkey'),
Layout.publicKey('authorizedVoterPubkey'),
Layout.publicKey('authorizedWithdrawerPubkey'),
BufferLayout.u8('commission'),
2019-07-23 18:06:55 -07:00
BufferLayout.nu64(), // votes.length
BufferLayout.seq(
BufferLayout.struct([
BufferLayout.nu64('slot'),
BufferLayout.u32('confirmationCount'),
]),
BufferLayout.offset(BufferLayout.u32(), -8),
'votes',
),
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 {
nodePubkey: PublicKey;
authorizedVoterPubkey: PublicKey;
2019-09-26 15:00:32 -07:00
authorizedWithdrawerPubkey: PublicKey;
2019-07-23 18:06:55 -07:00
commission: number;
2019-09-26 15:00:32 -07:00
votes: Array<Lockout>;
2019-07-23 18:06:55 -07:00
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);
2019-09-26 15:00:32 -07:00
va.authorizedWithdrawerPubkey = new PublicKey(
va.authorizedWithdrawerPubkey,
);
2019-07-23 18:06:55 -07:00
if (!va.rootSlotValid) {
va.rootSlot = null;
}
return va;
}
}