-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experimental): sysvars package: stake history
This commit introduces the `StakeHistory` sysvar to the `@solana/sysvars` package.
- Loading branch information
1 parent
8c307a9
commit d3800f7
Showing
6 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { GetAccountInfoApi } from '@solana/rpc-api'; | ||
import type { Rpc } from '@solana/rpc-spec'; | ||
|
||
import { fetchSysvarStakeHistory, getSysvarStakeHistoryCodec } from '../stake-history'; | ||
import { createLocalhostSolanaRpc } from './__setup__'; | ||
|
||
describe('stake history', () => { | ||
let rpc: Rpc<GetAccountInfoApi>; | ||
beforeEach(() => { | ||
rpc = createLocalhostSolanaRpc(); | ||
}); | ||
it('decode', () => { | ||
// prettier-ignore | ||
const stakeHistoryState = new Uint8Array([ | ||
2, 0, 0, 0, // array length | ||
0, 208, 237, 144, 46, 0, 0, 0, // effective | ||
0, 160, 219, 33, 93, 0, 0, 0, // activating | ||
0, 112, 201, 178, 139, 0, 0, 0, // deactivating | ||
0, 160, 219, 33, 93, 0, 0, 0, // effective | ||
0, 112, 201, 178, 139, 0, 0, 0, // activating | ||
0, 64, 183, 67, 186, 0, 0, 0, // deactivating | ||
]); | ||
expect(getSysvarStakeHistoryCodec().decode(stakeHistoryState)).toMatchObject( | ||
expect.arrayContaining([ | ||
{ | ||
activating: 400_000_000_000n, | ||
deactivating: 600_000_000_000n, | ||
effective: 200_000_000_000n, | ||
}, | ||
{ | ||
activating: 600_000_000_000n, | ||
deactivating: 800_000_000_000n, | ||
effective: 400_000_000_000n, | ||
}, | ||
]), | ||
); | ||
}); | ||
it('fetch', async () => { | ||
expect.assertions(1); | ||
const stakeHistory = await fetchSysvarStakeHistory(rpc); | ||
expect(stakeHistory).toMatchObject(expect.any(Array)); // Not always populated on test validator | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { assertAccountExists, decodeAccount, type FetchAccountConfig } from '@solana/accounts'; | ||
import { | ||
combineCodec, | ||
getArrayDecoder, | ||
getArrayEncoder, | ||
getStructDecoder, | ||
getStructEncoder, | ||
type VariableSizeCodec, | ||
type VariableSizeDecoder, | ||
type VariableSizeEncoder, | ||
} from '@solana/codecs'; | ||
import type { GetAccountInfoApi } from '@solana/rpc-api'; | ||
import type { Rpc } from '@solana/rpc-spec'; | ||
import { getLamportsDecoder, getLamportsEncoder, type LamportsUnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types'; | ||
|
||
import { fetchEncodedSysvarAccount, SYSVAR_STAKE_HISTORY_ADDRESS } from './sysvar'; | ||
|
||
type Entry = Readonly<{ | ||
activating: LamportsUnsafeBeyond2Pow53Minus1; | ||
deactivating: LamportsUnsafeBeyond2Pow53Minus1; | ||
effective: LamportsUnsafeBeyond2Pow53Minus1; | ||
}>; | ||
|
||
/** | ||
* The `StakeHistory` sysvar. | ||
* | ||
* History of stake activations and de-activations. | ||
*/ | ||
export type SysvarStakeHistory = Entry[]; | ||
|
||
export function getSysvarStakeHistoryEncoder(): VariableSizeEncoder<SysvarStakeHistory> { | ||
return getArrayEncoder( | ||
getStructEncoder([ | ||
['effective', getLamportsEncoder()], | ||
['activating', getLamportsEncoder()], | ||
['deactivating', getLamportsEncoder()], | ||
]), | ||
); | ||
} | ||
|
||
export function getSysvarStakeHistoryDecoder(): VariableSizeDecoder<SysvarStakeHistory> { | ||
return getArrayDecoder( | ||
getStructDecoder([ | ||
['effective', getLamportsDecoder()], | ||
['activating', getLamportsDecoder()], | ||
['deactivating', getLamportsDecoder()], | ||
]), | ||
); | ||
} | ||
|
||
export function getSysvarStakeHistoryCodec(): VariableSizeCodec<SysvarStakeHistory> { | ||
return combineCodec(getSysvarStakeHistoryEncoder(), getSysvarStakeHistoryDecoder()); | ||
} | ||
|
||
/** | ||
* Fetch the `StakeHistory` sysvar. | ||
* | ||
* History of stake activations and de-activations. | ||
*/ | ||
export async function fetchSysvarStakeHistory( | ||
rpc: Rpc<GetAccountInfoApi>, | ||
config?: FetchAccountConfig, | ||
): Promise<SysvarStakeHistory> { | ||
const account = await fetchEncodedSysvarAccount(rpc, SYSVAR_STAKE_HISTORY_ADDRESS, config); | ||
assertAccountExists(account); | ||
const decoded = decodeAccount(account, getSysvarStakeHistoryDecoder()); | ||
return decoded.data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters