2021-07-08 18:01:11 -07:00
|
|
|
import * as BufferLayout from '@solana/buffer-layout';
|
|
|
|
import type {Buffer} from 'buffer';
|
2019-07-23 18:06:55 -07:00
|
|
|
|
|
|
|
import * as Layout from './layout';
|
|
|
|
import {PublicKey} from './publickey';
|
2020-02-12 16:25:22 -08:00
|
|
|
import {toBuffer} from './util/to-buffer';
|
2019-07-23 18:06:55 -07:00
|
|
|
|
2019-12-23 10:46:49 -08:00
|
|
|
export const VOTE_PROGRAM_ID = new PublicKey(
|
2019-07-24 16:01:07 -07:00
|
|
|
'Vote111111111111111111111111111111111111111',
|
|
|
|
);
|
|
|
|
|
2021-03-14 20:01:35 -07:00
|
|
|
export type Lockout = {
|
|
|
|
slot: number;
|
|
|
|
confirmationCount: number;
|
|
|
|
};
|
2019-07-23 18:06:55 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* History of how many credits earned by the end of each epoch
|
|
|
|
*/
|
2021-03-14 20:01:35 -07:00
|
|
|
export type EpochCredits = {
|
|
|
|
epoch: number;
|
|
|
|
credits: number;
|
|
|
|
prevCredits: number;
|
|
|
|
};
|
2019-07-23 18:06:55 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See https://github.com/solana-labs/solana/blob/8a12ed029cfa38d4a45400916c2463fb82bbec8c/programs/vote_api/src/vote_state.rs#L68-L88
|
|
|
|
*
|
2021-03-14 20:01:35 -07:00
|
|
|
* @internal
|
2019-07-23 18:06:55 -07:00
|
|
|
*/
|
|
|
|
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',
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
2021-03-14 20:01:35 -07:00
|
|
|
type VoteAccountArgs = {
|
|
|
|
nodePubkey: PublicKey;
|
|
|
|
authorizedVoterPubkey: PublicKey;
|
|
|
|
authorizedWithdrawerPubkey: PublicKey;
|
|
|
|
commission: number;
|
|
|
|
votes: Array<Lockout>;
|
|
|
|
rootSlot: number | null;
|
|
|
|
epoch: number;
|
|
|
|
credits: number;
|
|
|
|
lastEpochCredits: number;
|
|
|
|
epochCredits: Array<EpochCredits>;
|
|
|
|
};
|
|
|
|
|
2019-07-23 18:06:55 -07:00
|
|
|
/**
|
|
|
|
* 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>;
|
|
|
|
|
2021-03-14 20:01:35 -07:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
constructor(args: VoteAccountArgs) {
|
|
|
|
this.nodePubkey = args.nodePubkey;
|
|
|
|
this.authorizedVoterPubkey = args.authorizedVoterPubkey;
|
|
|
|
this.authorizedWithdrawerPubkey = args.authorizedWithdrawerPubkey;
|
|
|
|
this.commission = args.commission;
|
|
|
|
this.votes = args.votes;
|
|
|
|
this.rootSlot = args.rootSlot;
|
|
|
|
this.epoch = args.epoch;
|
|
|
|
this.credits = args.credits;
|
|
|
|
this.lastEpochCredits = args.lastEpochCredits;
|
|
|
|
this.epochCredits = args.epochCredits;
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:06:55 -07:00
|
|
|
/**
|
|
|
|
* Deserialize VoteAccount from the account data.
|
|
|
|
*
|
|
|
|
* @param buffer account data
|
|
|
|
* @return VoteAccount
|
|
|
|
*/
|
2020-02-14 06:33:11 -08:00
|
|
|
static fromAccountData(
|
|
|
|
buffer: Buffer | Uint8Array | Array<number>,
|
|
|
|
): VoteAccount {
|
2020-02-12 16:25:22 -08:00
|
|
|
const va = VoteAccountLayout.decode(toBuffer(buffer), 0);
|
2021-03-14 20:01:35 -07:00
|
|
|
|
|
|
|
let rootSlot: number | null = va.rootSlot;
|
2019-07-23 18:06:55 -07:00
|
|
|
if (!va.rootSlotValid) {
|
2021-03-14 20:01:35 -07:00
|
|
|
rootSlot = null;
|
2019-07-23 18:06:55 -07:00
|
|
|
}
|
2021-03-14 20:01:35 -07:00
|
|
|
|
|
|
|
return new VoteAccount({
|
|
|
|
nodePubkey: new PublicKey(va.nodePubkey),
|
|
|
|
authorizedVoterPubkey: new PublicKey(va.authorizedVoterPubkey),
|
|
|
|
authorizedWithdrawerPubkey: new PublicKey(va.authorizedWithdrawerPubkey),
|
|
|
|
commission: va.commission,
|
|
|
|
votes: va.votes,
|
|
|
|
rootSlot,
|
|
|
|
epoch: va.epoch,
|
|
|
|
credits: va.credits,
|
|
|
|
lastEpochCredits: va.lastEpochCredits,
|
|
|
|
epochCredits: va.epochCredits,
|
|
|
|
});
|
2019-07-23 18:06:55 -07:00
|
|
|
}
|
|
|
|
}
|