# Validator Staking

Staking involves depositing the KUB Coin to activate the validator. A validator's role includes storing data, processing transactions, and adding blocks to the blockchain. This active participation helps secure the KUB network, benefiting all users and allowing validators to earn additional KUB Coin rewards.

## Becoming a Pool Node

A pool node on the KUB network allows delegators to delegate their KUB Coin to this node type, increasing staking power and the likelihood that the node's validator will mine a block on the KUB network. Those considering pool staking should have at least 100,000 KUB and a computer connected to the internet, which must maintain 100% uptime.

1. Visit the [StakeManager](https://www.kubscan.com/address/0x443502b3F7C0934576F49CDa084f78640f56A80F?tab=write_contract) contract on KUB Scan
2. Connect your wallet on the **Write Contract** page
3. Select the [stake](https://www.kubscan.com/address/0x443502b3F7C0934576F49CDa084f78640f56A80F?tab=write_contract#b114e888) function (Number 15) and input the following
   1. **signer** - Your public address that was retrieved in the [earlier steps](/run-a-kub-node/run-a-validator-node.md), starts with `0x`
   2. **delegation**  - `true`
   3. **Send Native KUB** - At least `100,000` KUB
4. Click on the **Write** button to confirm the transaction
5. Check your Validator ID by selecting the [getValidatorID](https://www.kubscan.com/address/0x443502b3F7C0934576F49CDa084f78640f56A80F?tab=read_contract#174e6832) function on the **Read Contract** page and input your public address

## StakeManager Contract

The StakeManager contract serves as the core of KUB's staking mechanism. All staking-related actions that users can interact with can be executed through this contract. It enables node runners to stake, unstake, restake, claim rewards, and configure their ValidatorShare contracts. The contract also handles reward distribution and slashing; the related information can be found in the events emitted for clarity.

### Stake

```solidity
stake(address signer, bool delegation) payable
```

`signer` - The address of the EVM instance, starts with `0x`

`delegation` - Create a ValidatorShare contract and enable the delegation; set to `true`

`payable` - KUB Coin required to stake, not exceeding uint128

This method requires KUB Coin to be sent as a value along with the transaction that calls this method. Upon staking, an NFT will be minted and given to the staker. The NFT represents ownership of the newly created node, including the states, configurations, ValidatorShare contract, etc., but not the EVM instance running to operate the chain.

On creation, the infrastructure fee and the commission fee of the pool node will be set to the default value of 0%. You can change the commission fee by yourself after the node creation at the [StakeManager](https://www.kubscan.com/address/0x443502b3F7C0934576F49CDa084f78640f56A80F) contract on the [updateCommissionRate](https://www.kubscan.com/address/0x443502b3F7C0934576F49CDa084f78640f56A80F?tab=write_contract#dcd962b2) method. The initial status of the node will be active. The signer's address of the newly created node will be added to the minimal validator set and can be selected as a block validator in a span.

{% hint style="info" %}
The active status means the node is staked. It does not guarantee that the EVM instance of the node is running, nor that the signer's address is in the minimal validator set.
{% endhint %}

{% hint style="warning" %}
The signer address cannot be used or become a node again unless the node is unstaked first.
{% endhint %}

### Unstake

```solidity
unstake(uint256 validatorId)
```

**`validatorId`** - The token ID of the NFT that is received upon staking

Only owners of the NFTs can invoke this function, and the input validator ID must correspond to the token ID of one of the NFTs in the caller’s possession. Unstaking is not allowed for inactive nodes, and the official node cannot be unstaked using this function.

When a node is unstaked, the ownership NFT of the node will be burned, and the staked funds will be transferred back from the StakeManagerVault contract to the owner. Additionally, the node will be removed from the minimal validator set, and its status will be set to unstaked, resulting in it not being selected as a block validator anymore. The signer's address will also now be available to be used again.

All rewards earned by the model's staker will be sent to the node owner, including validator rewards, validator commissions, and delegator commissions.

The delegation flag inside the ValidatorShare contract will be set to false, preventing further delegation on the ValidatorShare contract side. However, delegators can still claim their rewards and undelegate their funds. The number of nodes will decrease by one, based on the type of the unstaked node.

### Restake

```solidity
restake(uint256 validatorId) payable
```

`validatorId` - The token ID of the NFT that is received upon staking

`payable` - KUB Coin required to stake, not exceeding uint128

Only owners of the NFTs can call this function, and the input validator ID must correspond to the token ID of one of the NFTs in the caller’s possession. The restaking of an inactive node is not possible.

This function increases the staked amount of the node with the funds sent in the transaction. If the node is not part of the minimal validator set before this function is called, and the final staked amount is greater than or equal to the minimum deposit amount, the signer address of the node will be included in the set. The funds will be transferred to the StakeManagerVault contract.

{% hint style="warning" %}
Retrieving a portion of your staked funds is not possible. The only way to retrieve your staked funds is to call the [unstake](#unstake) function.
{% endhint %}

### ClaimRewards

```solidity
claimRewards(uint256 validatorId)
```

`validatorId` - The token ID of the NFT that is received upon staking

Only owners of the NFTs can call this function, and the input validator ID must be the token ID of one of the NFTs in the caller’s possession.&#x20;

The reward from the node that is not active cannot be claimed. The function is solely for claiming the validator reward, which will be transferred only to the owner.

### ClaimCommissionRewards

```solidity
claimCommissionRewards(uint256 validatorId)
```

`validatorId` - The token ID of the NFT that is received upon staking

Only owners of the NFTs can call this function, and the input validator ID must be the token ID of one of the NFTs in the caller’s possession.&#x20;

The reward from the node that is not active cannot be claimed. The function is solely for claiming the commission reward, which will be transferred only to the owner.

### UpdateCommissionRate

```solidity
updateCommissionRate(uint256 validatorId, uint256 newCommissionRate)
```

`validatorId` - The token ID of the NFT that is received upon staking

`newCommissionRate` - The new value of the commission rate

This function allows you to change the validator commission rate. The minimum value is `0` for 0%, and the maximum value is `5000` for 50%. For example, if the value is 1234, the result of the new commission rate will be 12.34%

The commission rate affects both the validator and delegator rewards. The higher this value, the more the rewards are reduced. The taken portions are combined and referred to as the commission fee.

### UpdateMinDelegated

```solidity
updateMinDelegated(uint256 validatorId, uint256 newMinDelegated)
```

`validatorId` - The token ID of the NFT that is received upon staking

**`newMinDelegated`** - The new value of the minimum delegated amount in the *wei* unit

This function allows you to change the minimum delegated amount. The minimum delegated amount is the expected minimum amount of delegated funds within a node.&#x20;

This value is considered during delegation and undelegation. For delegation, if the final total delegated amount is less than the minimum delegated amount, the transaction will be reverted. For undelegation, if the final total delegated amount is less than this value, all delegated amounts will be undelegated instead of the desired value. The minimum delegated amount does not affect the existing delegated amounts of the users until delegation or undelegation is carried out by the users.

### UpdateValidatorDelegation

```solidity
updateValidatorDelegation(uint256 validatorId, bool delegation)
```

`validatorId` - The token ID of the NFT that is received upon staking

**`delegation`** - More delegation is allowed if set to `true`; no more delegation is allowed if set to `false`

This function allows you to change the delegation flag of a node. The delegation flag indicates whether a node allows delegation by delegators.

If the delegation flag is set to false, no more delegations will be possible. However, the existing users, before the delegation flag was set to false, will still be able to undelegate and claim their rewards.

## ValidatorShare Contract

The ValidatorShare contract is the core of the delegation mechanism of the KUB network. All delegation-related actions that users can interact with can be executed through this contract. In this contract, it's possible to delegate, undelegate, and claim your reward.

A ValidatorShare contract is created when a node validator is staked with the ValidatorShareFactory contract by the StakeManager contract. It is not intended to hold any funds through its capabilities. The ValidatorShare contract can be replaced as it is merely an implementation, with the data and funds located in other contracts.

### Delegate

```solidity
delegate() payable
```

`payable` - KUB Coin required to delegate, not exceeding uint128

This method requires KUB Coin to be sent as a value along with the transaction that calls this method. This function can only be called when the delegation flag in the ValidatorShare contract is set to **true**.&#x20;

Upon delegation, a token of the ValidatorShare contract will be minted in the same amount as the delegated amount to the sender. The token is the proof of ownership of the sender’s shares in the contract. It can be transferred and has the same standard functionality as other tokens.

When this function is called, the ValidatorShare contract’s states will be updated, and the pending reward of the sender will be sent to the sender. The sender's final total delegated amount (original delegated amount plus newly delegated amount) must be more than or equal to the minimum delegated amount. The funds will be sent to the StakeManagerVault contract.

### Undelegate

```solidity
undelegate(uint256 claimAmount)
```

**`claimAmount`** - The amount of KUB Coin, in the *wei* unit, that will be retrieved from the ValidatorShare contract

Upon calling this function, the ValidatorShare contract’s states will be updated, and the pending reward of the sender will be sent to the sender. The maximum value of the *claimAmount* argument is the balance of the sender's ownership token, and it cannot be 0.

In the case where the final total delegated amount (the balance of the sender's ownership token as deduced by the claimAmount argument) is less than the minimum delegated amount, the value of the claimAmount argument will be adjusted to the sender's ownership token balance, meaning all shares of the sender will be undelegated. The ownership token will be burned according to the *claimAmount* argument, which may have been adjusted. The funds will be sent from the StakeManagerVault contract to the sender.

### ClaimRewards <a href="#claimrewards" id="claimrewards"></a>

```solidity
claimRewards()
```

Upon calling this function, the ValidatorShare contract’s states will be updated, and the pending reward of the sender will be sent to the sender.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.kubchain.com/run-a-kub-node/run-a-validator-node/validator-staking.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
