Staking

Solidity Interface & ABIarrow-up-right

Staking.sol provides an interface for Solidity contracts to interact with Cosmos SDK staking. This simplifies the process for developers, as they do not need to understand the implementation details of the Cosmos SDK's x/staking module. Instead, they can use the familiar Ethereum interface to access staking functions.

Interface Staking.solarrow-up-right

Transactionsarrow-up-right

The Staking solidity interface includes the following transactions

  • delegate

    delegate defines a method for performing a delegation of coins from a delegator to a validator.

    function delegate(
        address delegatorAddress,
        string memory validatorAddress,
        uint256 amount
    ) external returns (bool success);
  • undelegate

    The undelegate method facilitates the process of undelegating from a delegate and a validator.

    function undelegate(
        address delegatorAddress,
        string memory validatorAddress,
        uint256 amount
    ) external returns (int64 completionTime);
  • redelegate

    The redelegate method facilitates the redelegation of coins from a delegator and source validator to a destination validator.

    function redelegate(
        address delegatorAddress,
        string memory validatorSrcAddress,
        string memory validatorDstAddress,
        uint256 amount
    ) external returns (int64 completionTime);
  • cancelUnbondingDelegation

    The cancelUnbondingDelegation method allows delegators to cancel an unbonding delegation entry and redelegate to a previous validator.

    function cancelUnbondingDelegation(
        address delegatorAddress,
        string memory validatorAddress,
        uint256 amount,
        uint256 creationHeight
    ) external returns (bool success);

  • delegation

    Retrieve the specified amount of the bond denomination for a validator.

  • unbondingDelegation

    The unbondingDelegation method returns the current unbonding delegation.

  • validator

    The validator method retrieves validator information for a specified validator address.

  • validators

    The validators method retrieves all validators matching the specified status.

  • redelegation

    The redelegation method retrieves all redelegations from a source validator to a destination validator for a specified delegator.

  • redelegations

    The redelegations method retrieves all redelegations based on specified criteria, including a given delegator, origin validator address, and/or destination validator address, with support for pagination.

Each transaction emits a corresponding event. These events include:

  • Delegate

    The Delegate event is emitted when a specified amount of tokens are delegated from the delegator address to the validator address.

  • Unbond

    The Unbond event is emitted when a specified amount of tokens are unbonded from the validator address to the delegator address.

  • Redelegate

    The Redelegate event is defined to be emitted when a specified amount of tokens are redelegated from the source validator address to the destination validator address.

  • CancelUnbondingDelegation

    The CancelUnbondingDelegation event is defined to be emitted when tokens in the process of unbonding from the validator address are bonded again with a specified amount.

Utilize the Solidity Interface:

Below are examples demonstrating interaction with this Solidity interface from your smart contracts.

Ensure to import the precompiled interface, for instance:

Grant Approval for Desired Messages:

Refer to the function below, which grants approval to the smart contract for sending all x/staking module messages on behalf of the sender account. In this instance, the allowance amount is set to the maximum possible. You may adjust this function to approve specific messages and amounts as needed.

Delegate to a validatorarrow-up-right

The stakeTokens function enables the transaction sender to delegate the specified amount to their preferred validator. It's important to note that for this transaction to succeed, the user must have previously approved the MSG_DELEGATE (as exemplified by the approveAllStakingMethodsWithMaxAmount in the provided code snippet). Upon execution, this function provides the completion time of the staking transaction and emits a Delegate event.

Undelegate from a validatorarrow-up-right

The unstakeTokens function permits a user to unstake a specified amount of tokens. Upon execution, it provides the completion time of the unstaking transaction and emits an Undelegate event.

Redelegate to another validatorarrow-up-right

The redelegateTokens function enables a user to redelegate a specified amount of tokens. Upon execution, it provides the completion time of the redelegation transaction and emits a Redelegate event.

Cancel unbonding from a validatorarrow-up-right

The cancelUnbondingDelegation function allows a user to cancel an unbonding delegation. This function returns the completion time of the unbonding delegation cancellation transaction and emits a CancelUnbondingDelegation event.

Similar to transactions, smart contracts can utilize query methods. Authorization is not required for these read-only methods. Examples include the getDelegation and getUnbondingDelegation functions, which retrieve information for the specified validator address.

Last updated