diff --git a/module.flow.js b/module.flow.js index 50c6f4e430b0e5..32eca0075c6597 100644 --- a/module.flow.js +++ b/module.flow.js @@ -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; + nodePubkey: PublicKey; + authorizedVoterPubkey: PublicKey; + commission: number; + rootSlot: number | null; + epoch: number; + credits: number; + lastEpochCredits: number; + epochCredits: Array; + static fromAccountData(buffer: Buffer): VoteAccount; + } + // === src/transaction.js === declare export type TransactionSignature = string; diff --git a/src/index.js b/src/index.js index 0dcc62cc27a867..05e25fb4771e12 100644 --- a/src/index.js +++ b/src/index.js @@ -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, diff --git a/src/vote-account.js b/src/vote-account.js new file mode 100644 index 00000000000000..ca523c54156ee9 --- /dev/null +++ b/src/vote-account.js @@ -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; + nodePubkey: PublicKey; + authorizedVoterPubkey: PublicKey; + commission: number; + rootSlot: number | null; + epoch: number; + credits: number; + lastEpochCredits: number; + epochCredits: Array; + + /** + * 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; + } +}