feat: add vote account decoder (#418)
This commit is contained in:
parent
41346436f6
commit
c929516f52
|
@ -158,6 +158,31 @@ declare module '@solana/web3.js' {
|
||||||
static fromConfigData(buffer: Buffer): ?ValidatorInfo;
|
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 ===
|
// === src/transaction.js ===
|
||||||
declare export type TransactionSignature = string;
|
declare export type TransactionSignature = string;
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ export {SystemProgram} from './system-program';
|
||||||
export {Token, TokenAmount} from './token-program';
|
export {Token, TokenAmount} from './token-program';
|
||||||
export {Transaction, TransactionInstruction} from './transaction';
|
export {Transaction, TransactionInstruction} from './transaction';
|
||||||
export {ValidatorInfo} from './validator-info';
|
export {ValidatorInfo} from './validator-info';
|
||||||
|
export {VoteAccount} from './vote-account';
|
||||||
export {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
export {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
||||||
export {
|
export {
|
||||||
sendAndConfirmRawTransaction,
|
sendAndConfirmRawTransaction,
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue