StakingClient
Client for staking actions on HUMAN Protocol.
Internally selects network config based on the Web3 chain id.
Example
from eth_typing import URI
from web3 import Web3
from web3.middleware import SignAndSendRawMiddlewareBuilder
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.staking import StakingClient
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key("YOUR_PRIVATE_KEY")
w3.eth.default_account = gas_payer.address
w3.middleware_onion.inject(
SignAndSendRawMiddlewareBuilder.build("YOUR_PRIVATE_KEY"),
"SignAndSendRawMiddlewareBuilder",
layer=0,
)
staking_client = StakingClient(w3)
StakingClient ¶
Client for interacting with the staking smart contract.
This client provides methods to stake, unstake, withdraw, and slash HMT tokens, as well as query staker information on the Human Protocol network.
Attributes:
| Name | Type | Description |
|---|---|---|
w3 |
Web3
|
Web3 instance configured for the target network. |
network |
dict
|
Network configuration for the current chain. |
hmtoken_contract |
Contract
|
Contract instance for the HMT token. |
factory_contract |
Contract
|
Contract instance for the escrow factory. |
staking_contract |
Contract
|
Contract instance for the staking contract. |
Initialize a StakingClient instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
w3
|
Web3
|
Web3 instance configured for the target network. Must have a valid provider and chain ID. |
required |
Raises:
| Type | Description |
|---|---|
StakingClientError
|
If chain ID is invalid, network configuration is missing, or network configuration is empty. |
Example
approve_stake ¶
Approve HMT tokens for staking.
Grants the staking contract permission to transfer HMT tokens from the caller's account. This must be called before staking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
int
|
Amount of HMT tokens to approve in token's smallest unit (must be greater than 0). |
required |
tx_options
|
Optional[TransactionOptions]
|
Optional transaction parameters such as gas limit. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
StakingClientError
|
If the amount is not positive or the transaction fails. |
get_staker_info ¶
Retrieve comprehensive staking information for a staker.
Fetches on-chain staking data including staked amount, locked amount, lock expiration, and withdrawable amount.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
staker_address
|
str
|
Ethereum address of the staker. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Staker info with keys:
|
Raises:
| Type | Description |
|---|---|
StakingClientError
|
If the staker address is invalid or the query fails. |
slash ¶
Slash a staker's stake for a given escrow.
Penalizes a staker by reducing their staked amount and distributing rewards to the slasher for detecting misbehavior or violations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slasher
|
str
|
Address of the entity performing the slash (receives rewards). |
required |
staker
|
str
|
Address of the staker to be slashed. |
required |
escrow_address
|
str
|
Address of the escrow associated with the violation. |
required |
amount
|
int
|
Amount to slash in token's smallest unit (must be greater than 0 and within staker's allocation to the escrow). |
required |
tx_options
|
Optional[TransactionOptions]
|
Optional transaction parameters such as gas limit. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
StakingClientError
|
If the amount is invalid, escrow address is invalid, or the transaction fails. |
stake ¶
Stake HMT tokens.
Deposits HMT tokens into the staking contract. The tokens must be approved first
using approve_stake().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
int
|
Amount of HMT tokens to stake in token's smallest unit (must be greater than 0 and within approved/balance limits). |
required |
tx_options
|
Optional[TransactionOptions]
|
Optional transaction parameters such as gas limit. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
StakingClientError
|
If the amount is invalid or the transaction fails. |
Example
from eth_typing import URI
from web3 import Web3
from web3.middleware import SignAndSendRawMiddlewareBuilder
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.staking import StakingClient
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key("YOUR_PRIVATE_KEY")
w3.eth.default_account = gas_payer.address
w3.middleware_onion.inject(
SignAndSendRawMiddlewareBuilder.build("YOUR_PRIVATE_KEY"),
"SignAndSendRawMiddlewareBuilder",
layer=0,
)
staking_client = StakingClient(w3)
amount = Web3.to_wei(5, "ether")
staking_client.approve_stake(amount)
staking_client.stake(amount)
unstake ¶
Unstake HMT tokens.
Initiates the unstaking process for the specified amount. The tokens will be locked for a period before they can be withdrawn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
int
|
Amount of HMT tokens to unstake in token's smallest unit (must be greater than 0 and less than or equal to unlocked staked amount). |
required |
tx_options
|
Optional[TransactionOptions]
|
Optional transaction parameters such as gas limit. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
StakingClientError
|
If the amount is invalid or the transaction fails. |
withdraw ¶
Withdraw unlocked unstaked HMT tokens.
Withdraws all available unstaked tokens that have completed the unlocking period and transfers them back to the caller's account.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tx_options
|
Optional[TransactionOptions]
|
Optional transaction parameters such as gas limit. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
StakingClientError
|
If the transaction fails or no tokens are available to withdraw. |
StakingClientError ¶
Bases: Exception
Exception raised when errors occur during staking operations.