-
Notifications
You must be signed in to change notification settings - Fork 800
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: epoch rewards
This commit introduces the `EpochRewards` sysvar to the `@solana/sysvars` package.
- Loading branch information
1 parent
f08ab63
commit cc3a5ac
Showing
6 changed files
with
117 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,19 @@ | ||
import { getSysvarEpochRewardsCodec } from '../epoch-rewards'; | ||
|
||
describe('epoch rewards', () => { | ||
it('decode', () => { | ||
// prettier-ignore | ||
const epochRewardsState = new Uint8Array([ | ||
0, 45, 49, 1, 0, 0, 0, 0, // distributionCompleteBlockHeight | ||
134, 74, 2, 0, 0, 0, 0, 0, // distributedRewards | ||
0, 132, 215, 23, 0, 0, 0, 0, // totalRewards | ||
]); | ||
expect(getSysvarEpochRewardsCodec().decode(epochRewardsState)).toMatchObject({ | ||
distributedRewards: 150_150n, | ||
distributionCompleteBlockHeight: 20_000_000n, | ||
totalRewards: 400_000_000n, | ||
}); | ||
}); | ||
// TODO: This account does not seem to exist on-chain yet. | ||
it.todo('fetch'); | ||
}); |
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,83 @@ | ||
import { assertAccountExists, decodeAccount, type FetchAccountConfig } from '@solana/accounts'; | ||
import { | ||
combineCodec, | ||
type FixedSizeCodec, | ||
type FixedSizeDecoder, | ||
type FixedSizeEncoder, | ||
getStructDecoder, | ||
getStructEncoder, | ||
getU64Decoder, | ||
getU64Encoder, | ||
} from '@solana/codecs'; | ||
import type { GetAccountInfoApi } from '@solana/rpc-api'; | ||
import type { Rpc } from '@solana/rpc-spec'; | ||
|
||
import { fetchEncodedSysvarAccount, SYSVAR_EPOCH_REWARDS_ADDRESS } from './sysvar'; | ||
|
||
type SysvarEpochRewardsSize = 24; | ||
|
||
/** | ||
* The `EpochRewards` sysvar. | ||
* | ||
* Tracks the progress of epoch rewards distribution. It includes: | ||
* - Total rewards for the current epoch, in lamports. | ||
* - Rewards for the current epoch distributed so far, in lamports. | ||
* - Distribution completed block height, i.e. distribution of all staking rewards for the current | ||
* epoch will be completed at this block height. | ||
* | ||
* Note that `EpochRewards` only lasts for a handful of blocks at the start of | ||
* an epoch. When all rewards have been distributed, the sysvar is deleted. | ||
* See https://github.com/solana-labs/solana/blob/e0203f22dc83cb792fa97f91dbe6e924cbd08af1/docs/src/runtime/sysvars.md?plain=1#L155-L168 | ||
*/ | ||
export type SysvarEpochRewards = Readonly<{ | ||
distributedRewards: bigint; | ||
distributionCompleteBlockHeight: bigint; | ||
totalRewards: bigint; | ||
}>; | ||
|
||
export function getSysvarEpochRewardsEncoder(): FixedSizeEncoder<SysvarEpochRewards, SysvarEpochRewardsSize> { | ||
return getStructEncoder([ | ||
['distributionCompleteBlockHeight', getU64Encoder()], | ||
['distributedRewards', getU64Encoder()], | ||
['totalRewards', getU64Encoder()], | ||
]) as FixedSizeEncoder<SysvarEpochRewards, SysvarEpochRewardsSize>; | ||
} | ||
|
||
export function getSysvarEpochRewardsDecoder(): FixedSizeDecoder<SysvarEpochRewards, SysvarEpochRewardsSize> { | ||
return getStructDecoder([ | ||
['distributionCompleteBlockHeight', getU64Decoder()], | ||
['distributedRewards', getU64Decoder()], | ||
['totalRewards', getU64Decoder()], | ||
]) as FixedSizeDecoder<SysvarEpochRewards, SysvarEpochRewardsSize>; | ||
} | ||
|
||
export function getSysvarEpochRewardsCodec(): FixedSizeCodec< | ||
SysvarEpochRewards, | ||
SysvarEpochRewards, | ||
SysvarEpochRewardsSize | ||
> { | ||
return combineCodec(getSysvarEpochRewardsEncoder(), getSysvarEpochRewardsDecoder()); | ||
} | ||
|
||
/** | ||
* Fetch the `EpochRewards` sysvar. | ||
* | ||
* Tracks the progress of epoch rewards distribution. It includes: | ||
* - Total rewards for the current epoch, in lamports. | ||
* - Rewards for the current epoch distributed so far, in lamports. | ||
* - Distribution completed block height, i.e. distribution of all staking rewards for the current | ||
* epoch will be completed at this block height. | ||
* | ||
* Note that `EpochRewards` only lasts for a handful of blocks at the start of | ||
* an epoch. When all rewards have been distributed, the sysvar is deleted. | ||
* See https://github.com/solana-labs/solana/blob/e0203f22dc83cb792fa97f91dbe6e924cbd08af1/docs/src/runtime/sysvars.md?plain=1#L155-L168 | ||
*/ | ||
export async function fetchSysvarEpochRewards( | ||
rpc: Rpc<GetAccountInfoApi>, | ||
config?: FetchAccountConfig, | ||
): Promise<SysvarEpochRewards> { | ||
const account = await fetchEncodedSysvarAccount(rpc, SYSVAR_EPOCH_REWARDS_ADDRESS, config); | ||
assertAccountExists(account); | ||
const decoded = decodeAccount(account, getSysvarEpochRewardsDecoder()); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './clock'; | ||
export * from './epoch-rewards'; | ||
export * from './sysvar'; |
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