bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 36쪽
is recommended to
maintain an updated list via the
[events](/monad-arch/realtime-data/data-sources#how-do-i-receive-real-time-data) framework, rather
than periodically calling this expensive lookup.
</Note>
### getEpoch
Returns the current epoch and timing within the epoch (before or after the boundary block).
**Function selector**
```
getEpoch() : 0x757991a8
```
**Function signature**
```solidity theme={null}
function getEpoch() external returns (uint64 epoch, bool inEpochDelayPeriod);
```
**Gas cost**
200
**Behavior**
If `inEpochDelayPeriod` is false, the boundary block has not been reached yet
and write operations at that time should be effective for `epoch + 1`.
If `inEpochDelayPeriod` is true, the network is past the boundary block and
and write operations at that time should be effective for `epoch + 2`
### getProposerValId
Returns the validator ID of the current block proposer, corresponding to the SECP value of the block author.
**Function selector**
```
getProposerValId() : 0xfbacb0be
```
**Function signature**
```solidity theme={null}
function getProposerValId() external returns (uint64 val_id);
```
**Gas cost**
100
## Syscalls
There are currently three syscalls. Users cannot invoke these directly. They are only
triggered through special system transactions.
### syscallOnEpochChange
Triggered at the end of the epoch delay rounds to finalize accumulator values and update epoch state.
**Function selector**
```
syscallOnEpochChange(uint64) : 0x1d4e9f02
```
**Function signature**
```solidity theme={null}
function syscallOnEpochChange(uint64 epoch) external;
```
**Parameters**
1. `epoch` - the new consensus epoch being entered
**Behavior**
1. If the validator received a request to change stake in the previous epoch and participated in the previous epoch's consensus validator set then it saves the corresponding accumulator value
2. If any validator was active in the previous epoch but becomes inactive in the current epoch,
it also saves their current accumulator value
3. Sets the current epoch in state
<Accordion title="Pseudocode">
```solidity theme={null}
uint64 current_epoch = msg.input.epoch;
for i in snapshot_valset:
if epoch_acc[i][current_epoch] is not empty:
epoch_acc[i][current_epoch].val() = execution_valset[i].acc()
if epoch_acc[i][current_epoch + 1] is not empty:
epoch_acc[i][current_epoch].val() = execution_valset[i].acc()
in_epoch_delay_rounds = false;
epoch = current_epoch;
```
</Accordion>
### syscallReward
Rewards the block-producing validator and their delegators with the configured block reward for every block.
**Function selector**
```
syscallReward(address) : 0x791bdcf3
```
**Function signature**
```solidity theme={null}
function syscallReward(address blockAuthor) external;
```
**Parameters**
1. `blockAuthor` — the address of the validator that produced the block.
**Behavior**
1. If the validator has a nonzero commission, a portion of the reward is allocated to the
validator's `authAddress`.
2. The remaining reward is claimable to the validator's delegators.
Note that the commission is calculated as a percentage of the total block reward.
<Info title="Example">
* Suppose that a validator's personal stake comprises 20% of the total delegation to
their validator.
* The commission is set at 10% of total rewards.
Then the validator receives 10% of the total block reward as their commission. The remaining 90%
of the reward is distributed to the stake pool. Since the validator owns 20% of the pool, they
also receive 20% of that remaining amount.
</Info>
<Accordion title="Pseudocode">
```solidity theme={null}
uint64 val_id = secp_to_val_id[block_author];
DelInfo auth_del = delegator[val_id][val_execution[val_id].auth_address()];
uint256 _commission = REWARD * val_execution[val_id].commission / 1e18;
uint256 _unclaimed_rewards = REWARD - _commission;
// state update
auth_del.rewards() += _commission;
val_execution[val_id].unclaimed_rewards += _unclaimed_rewards;
val_execution[val_id].acc += _unclaimed_rewards / val_consensus[val_id].stake();
mint(STAKING_CONTRACT_ADDRESS, REWARD);
```
</Accordion>
### syscallSnapshot
Sorts the current execution-layer validator set, selects the top `N` staked validators as the upcoming consensus validator set, stores the updated set in state, and clears the previous consensus set.
**Function selector**
```
syscallSnapshot() : 0x157eeb21
```
**Function signature**
```solidity theme={null}
function syscallSnapshot() external;
```
**Behavior**
<Accordion title="Pseudocode">
```solidity theme={null}
uint64[] filter_top_n_validators = sort(execution_valset);
for i in snapshot_valset:
val_snapshot[i].stake = 0;
val_snapshot[i].commission = 0;
snapshot_valset = consensus_valset;
consensus_valset = filter_top_n_validators;
for i in filter_top_n_validators:
val_consensus[i].stake = val_execution[i].stake;
val_consensus[i].commission = val_execution[i].commission;
```
</Accordion>
## Events
The staking precompiles emit standard events that appear in transaction receipts. These events
provide indexed information about validator and delegator actions.
### ValidatorRewarded
Emitted when block reward is allocated via [`syscallReward`](#syscallreward), or when extra MON is sent to a validator's stakers via [`externalReward`](#externalreward).
```solidity theme={null}
event ValidatorRewarded(
uint64 indexed validatorId,
address indexed from,
uint256 amount,
uint64 epoch
);
```
### ValidatorCreated
Emitted when a validator is added via [`addValidator`](#addvalidator).
```solidity theme={null}
event ValidatorCreated(
uint64 indexed validatorId,
address indexed authAddress,
uint256 commission
);
```
### ValidatorStatusChanged
Emitted during [`addValidator`](#addvalidator), [`delegate`](#delegate), [`undelegate`](#undelegate), or [`compound`](#compound). if the validator's flags change.
```solidity theme={null}
event ValidatorStatusChanged(
uint64 indexed validatorId,
uint64 flags
);
```
### Delegate
Emitted when delegation amount is increased, i.e. during [`addValidator`](#addvalidator), [`delegate`](#delegate), or [`compound`](#compound).
```solidity theme={null}
event Delegate(
uint64 indexed validatorId,
address indexed delegator,
uint256 amount,
uint64 activationEpoch
);
```
### Undelegate
Emitted when a delegator calls [`undelegate`](#undelegate).
```solidity theme={null}
event Undelegate(
uint64 indexed validatorId,
address indexed delegator,
uint8 withdrawId,
uint256 amount,
uint64 activationEpoch
);
```
### Withdraw
Emitted when a delegator executes [`withdraw`](#withdraw) successfully.
```solidity theme={null}
event Withdraw(
uint64 indexed validatorId,
address indexed delegator,
uint8 withdrawId,
uint256 amount,
uint64 withdrawEpoch
);
```
### ClaimRewards
Emitted when a delegator claims rewards via [`claimRewards`](#claimrewards).
```solidity theme={null}
event ClaimRewards(
uint64 indexed validatorId,
address indexed delegator,
uint256 amount,
uint64 epoch
);
```
### CommissionChanged
Emitted when a validator changes commission via [`changeCommission`](#changecommission).
```solidity theme={null}
event CommissionChanged(
uint64 indexed validatorId,
uint256 oldCommission,
uint256 newCommission
);
```
### EpochChanged
Emitted when epoch changes via [`syscallOnEpochChange`](#syscallonepochchange).
```solidity theme={null}
event EpochChanged(
uint64 oldEpoch,
uint64 newEpoch
);
```
## Precompile internals
* [Constants](#constants)
* [Validator structs](#validator-structs)
* [Delegator structs](#delegator-structs)
* [State variables](#state-variables)
* [Mappings](#mappings)
### Validator structs
```solidity theme={null}
struct ValExecution // Realtime execution state for one validator
{
uint256 stake; // Upcoming stake pool balance
uint256 acc; // Current accumulator value for validator
uint256 commission; // Proportion of block reward charged as commission, times 1e18; 10% = 1e17
bytes secp_pubkey; // Secp256k1 public key used by consensus
bytes bls_pubkey; // Bls public key used by consensus
uint256 address_flags; // Flags to represent validators' current state
uint256 unclaimed_rewards; // Unclaimed rewards
address auth_address; // Delegator address with authority over validator stake
}
struct ValConsensus // A subset of validator state for the consensus system
{
uint256 stake; // Current active stake
uint256 commission; // Commission rate for current epoch
bytes secp_pubkey; // Secp256k1 public key used by consensus
bytes bls_pubkey; // Bls public key used by consensus
}
```
### Delegator structs
```solidity theme={null}
struct DelInfo
{
uint256 stake; // Current active stake
uint256 acc; // Last checked accumulator
uint256 rewards; // Last checked rewards
uint256 delta_stake; // Stake to be activated next epoch
uint256 next_delta_stake; // Stake to be activated in 2 epochs
uint64 delta_epoch; // Epoch when delta_stake becomes active
uint64 next_delta_epoch; // Epoch when next_delta_stake becomes active
}
struct WithdrawalRequest
{
uint256 amount; // Amount to undelegate from validator
uint256 acc; // Validator accumulator when undelegate was called
uint64 epoch; // Epoch when undelegate stake deactivates
};
struct Accumulator
{
uint256 val; // Current accumulator value
uint256 refcount; // Reference count for this accumulator value
};
```
### State variables
```solidity theme={null}
// Current consensus epoch
uint64 epoch;
// Flag indicating if currently in epoch delay rounds
bool in_epoch_delay_rounds;
// Counter for validator ids
uint64 last_val_id;
// Current execution view of validator set
StorageArray<uint64> execution_valset;
// Previous consensus view of validator set
StorageArray<uint64> snapshot_valset;
// Current consensus view of validator set
StorageArray<uint64> consensus_valset;
```
### Mappings
```solidity theme={null}
//These mappings only exist to ensure the SECP/BLS Keys are unique
mapping (secp_eth_address => uint64) secp_to_val_id;
mapping (bls_eth_address => uint64) bls_to_val_id;
// Keys(val_id, epoch) => Value(acc)
// making note of the validator accumulator at start of epoch.
mapping(uint64 => mapping(uint64 => Accumulator)) epoch_acc;
// Key(val_id)
// Contains the validator info for the execution view. Changes to stake
// or commission are reflected immediately.
mapping(uint64 => ValExecution) val_execution;
// Key(val_id)
// Contains a subset of the validator info relevant to consensus. Changes to
// stake or commission are reflected in the following epoch. This is referenced
// by the reward system call *before* the epoch delay rounds.
mapping(uint64 => ValConsensus) val_consensus;
// Key(val_id)
// Contains a subset of the validator info relevant to consensus. Changes to
// stake or commission are reflected in the following epoch. This is referenced
// by the reward system call *during* the epoch delay rounds.
mapping(uint64 => ValConsensus) val_snapshot;
// Keys(val_id,msg.sender) => DelInfo
mapping(uint64 => mapping(address => DelInfo)) delegator;
// Keys(val_id,msg.sender,withdrawal_id) => WithdrawalRequest
mapping(uint64 => mapping(address => mapping (uint8 => WithdrawalRequest))) withdrawal;
```
## Solidity Staking Interface
*To copy to clipboard, click the button in the top right of the code block.*
```solidity theme={null}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
interface IMonadStaking {
function addValidator(
bytes calldata payload,
bytes calldata signedSecpMessage,
bytes calldata signedBlsMessage
) external payable returns (uint64 validatorId);
function delegate(
uint64 validatorId
) external payable returns (bool success);
function undelegate(
uint64 validatorId,
uint256 amount,
uint8 withdrawId
) external returns (bool success);
function compound(
uint64 validatorId
) external returns (bool success);
function withdraw(
uint64 validatorId,
uint8 withdrawId
) external returns (bool success);
function claimRewards(
uint64 validatorId
) external returns (bool success);
function changeCommission(
uint64 validatorId,
uint256 commission
) external returns (bool success);
function externalReward(
uint64 validatorId
) external payable returns (bool success);
function getValidator(
uint64 validatorId
) external returns (
address authAddress,
uint64 flags,
uint256 stake,
uint256 accRewardPerToken,
uint256 commission,
uint256 unclaimedRewards,
uint256 consensusStake,
uint256 consensusCommission,
uint256 snapshotStake,
uint256 snapshotCommission,
bytes memory secpPubkey,
bytes memory blsPubkey
);
function getDelegator(
uint64 validatorId,
address delegator
) external returns (
uint256 stake,
uint256 accRewardPerToken,
uint256 unclaimedRewards,
uint256 deltaStake,
uint256 nextDeltaStake,
uint64 deltaEpoch,
uint64 nextDeltaEpoch
);
function getWithdrawalRequest(
uint64 validatorId,
address delegator,
uint8 withdrawId
) external returns (
uint256 withdrawalAmount,
uint256 accRewardPerToken,
uint64 withdrawEpoch
);
function getConsensusValidatorSet(
uint32 startIndex
) external returns (bool isDone, uint32 nextIndex, uint64[] memory valIds);
function getSnapshotValidatorSet(
uint32 startIndex
) external returns (bool isDone, uint32 nextIndex, uint64[] memory valIds);
function getExecutionValidatorSet(
uint32 startIndex
) external returns (bool isDone, uint32 nextIndex, uint64[] memory valIds);
function getDelegations(
address delegator,
uint64 startValId
) external returns (bool isDone, uint64 nextValId, uint64[] memory valIds);
function getDelegators(
uint64 validatorId,
address startDelegator
) external returns (bool isDone, address nextDelegator, address[] memory delegators);
function getEpoch() external returns (uint64 epoch, bool inEpochDelayPeriod);
function getProposerValId() external returns (uint64 val_id);
function syscallOnEpochChange(uint64 epoch) external;
function syscallReward(address blockAuthor) external;
function syscallSnapshot() external;
event ValidatorRewarded(
uint64 indexed validatorId,
address indexed from,
uint256 amount,
uint64 epoch
);
event ValidatorCreated(
uint64 indexed validatorId,
address indexed authAddress,
uint256 commission
);
event ValidatorStatusChanged(
uint64 indexed validatorId,
uint64 flags
);
event Delegate(
uint64 indexed validatorId,
address indexed delegator,
uint256 amount,
uint64 activationEpoch
);
event Undelegate(
uint64 indexed validatorId,
address indexed delegator,
uint8 withdrawId,
uint256 amount,
uint64 activationEpoch
);
event Withdraw(
uint64 indexed validatorId,
address indexed delegator,
uint8 withdrawId,
uint256 amount,
uint64 withdrawEpoch
);
event ClaimRewards(
uint64 indexed validatorId,
address indexed delegator,
uint256 amount,
uint64 epoch
);
event CommissionChanged(
uint64 indexed validatorId,
uint256 oldCommission,
uint256 newCommission
);
event EpochChanged(
uint64 oldEpoch,
uint64 newEpoch
);
}
```
## Staking ABI JSON
*To copy to clipboard, click the button in the top right of the code block.*
```json theme={null}
[
{"type":"function","name":"addValidator","inputs":[{"name":"payload","type":"bytes","internalType":"bytes"},{"name":"signedSecpMessage","type":"bytes","internalType":"bytes"},{"name":"signedBlsMessage","type":"bytes","internalType":"bytes"}],"outputs":[{"name":"validatorId","type":"uint64","internalType":"uint64"}],"stateMutability":"payable"},
{"type":"function","name":"changeCommission","inputs":[{"name":"validatorId","type":"uint64","internalType":"uint64"},{"name":"commission","type":"uint256","internalType":"uint256"}],"outputs":[{"name":"success","type":"bool","internalType":"bool"}],"stateMutability":"nonpayable"},
{"type":"function","name":"claimRewards","inputs":[{"name":"validatorId","type":"uint64","internalType":"uint64"}],"outputs":[{"name":"success","type":"bool","internalType":"bool"}],"stateMutability":"nonpayable"},
{"type":"function","name":"compound","inputs":[{"name":"validatorId","type":"uint64","internalType":"uint64"}],"outputs":[{"name":"success","type":"bool","internalType":"bool"}],"stateMutability":"nonpayable"},
{"type":"function","name":"delegate","inputs":[{"name":"validatorId","type":"uint64","internalType":"uint64"}],"outputs":[{"name":"success","type":"bool","internalType":"bool"}],"stateMutability":"payable"},
{"type":"function","name":"externalReward","inputs":[{"name":"validatorId","type":"uint64","internalType":"uint64"}],"outputs":[{"name":"success","type":"bool","internalType":"bool"}],"stateMutability":"payable"},
{"type":"function","name":"getConsensusValidatorSet","inputs":[{"name":"startIndex","type":"uint32","internalType":"uint32"}],"outputs":[{"name":"isDone","type":"bool","internalType":"bool"},{"name":"nextIndex","type":"uint32","internalType":"uint32"},{"name":"valIds","type":"uint64[]","internalType":"uint64[]"}],"stateMutability":"nonpayable"},
{"type":"function","name":"getDelegations","inputs":[{"name":"delegator","type":"address","internalType":"address"},{"name":"startValId","type":"uint64","internalType":"uint64"}],"outputs":[{"name":"isDone","type":"bool","internalType":"bool"},{"name":"nextValId","type":"uint64","internalType":"uint64"},{"name":"valIds","type":"uint64[]","internalType":"uint64[]"}],"stateMutability":"nonpayable"},
{"type":"function","name":"getDelegator","inputs":[{"name":"validatorId","type":"uint64","internalType":"uint64"},{"name":"delegator","type":"address","internalType":"address"}],"outputs":[{"name":"stake","type":"uint256","internalType":"uint256"},{"name":"accRewardPerToken","type":"uint256","internalType":"uint256"},{"name":"unclaimedRewards","type":"uint256","internalType":"uint256"},{"name":"deltaStake","type":"uint256","internalType":"uint256"},{"name":"nextDeltaStake","type":"uint256","internalType":"uint256"},{"name":"deltaEpoch","type":"uint64","internalType":"uint64"},{"name":"nextDeltaEpoch","type":"uint64","internalType":"uint64"}],"stateMutability":"nonpayable"},
{"type":"function","name":"getDelegators","inputs":[{"name":"validatorId","type":"uint64","internalType":"uint64"},{"name":"startDelegator","type":"address","internalType":"address"}],"outputs":[{"name":"isDone","type":"bool","internalType":"bool"},{"name":"nextDelegator","type":"address","internalType":"address"},{"name":"delegators","type":"address[]","internalType":"address[]"}],"stateMutability":"nonpayable"},
{"type":"function","name":"getEpoch","inputs":[],"outputs":[{"name":"epoch","type":"uint64","internalType":"uint64"},{"name":"inEpochDelayPeriod","type":"bool","internalType":"bool"}],"stateMutability":"nonpayable"},
{"type":"function","name":"getProposerValId","inputs":[],"outputs":[{"name":"val_id","type":"uint64","internalType": "uint64"}],
"stateMutability":"nonpayable"},
{"type":"function","name":"getExecutionValidatorSet","inputs":[{"name":"startIndex","type":"uint32","internalType":"uint32"}],"outputs":[{"name":"isDone","type":"bool","internalType":"bool"},{"name":"nextIndex","type":"uint32","internalType":"uint32"},{"name":"valIds","type":"uint64[]","internalType":"uint64[]"}],"stateMutability":"nonpayable"},
{"type":"function","name":"getSnapshotValidatorSet","inputs":[{"name":"startIndex","type":"uint32","internalType":"uint32"}],"outputs":[{"name":"isDone","type":"bool","internalType":"bool"},{"name":"nextIndex","type":"uint32","internalType":"uint32"},{"name":"valIds","type":"uint64[]","internalType":"uint64[]"}],"stateMutability":"nonpayable"},
{"type":"function","name":"getValidator","inputs":[{"name":"validatorId","type":"uint64","internalType":"uint64"}],"outputs":[{"name":"authAddress","type":"address","internalType":"address"},{"name":"flags","type":"uint64","internalType":"uint64"},{"name":"stake","type":"uint256","internalType":"uint256"},{"name":"accRewardPerToken","type":프로젝트로 돌아가기