Skip to main content

Staking

Solidity Interface & ABI

Staking.sol is an interface through which Solidity contracts can interact with Cosmos SDK staking. This is convenient for developers as they don’t need to know the implementation details behind the x/staking module in the Cosmos SDK. Instead, they can interact with staking functions using the Ethereum interface they are familiar with.

Interface Staking.sol

Find the Solidity interface in the HyperPaxeer/extensions repo.

ABI

Find the ABI in the HyperPaxeer/extensions repo.

Transactions

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.
  • undelegate undelegate defines a method for performing an undelegation from a delegate and a validator.
  • redelegate Redelegate defines a method for performing a redelegation of coins from a delegator and source validator to a destination validator
  • cancelUnbondingDelegation cancelUnbondingDelegation allows delegators to cancel the unbondingDelegation entry and to delegate back to a previous validator.

Queries

  • delegation get the given amount of the bond denomination to a validator.
  • unbondingDelegation unbondingDelegation returns the unbonding delegation
  • validator validator queries validator info for given validator address
  • validators validators queries all validators that match the given status
  • redelegation redelegation queries all redelegations from a source to a destination validator for a given delegator
  • redelegations redelegations queries all redelgations based on the specified criteria: for a given delegator and/or origin validator address and/or destination validator address in a specified pagination manner.

Events

Each of the transactions emits its corresponding event. These are:
  • Delegate Delegate defines an Event emitted when a given amount of tokens are delegated from the delegator address to the validator address
  • Unbond Unbond defines an Event emitted when a given amount of tokens are unbonded from the validator address to the delegator address
  • Redelegate Redelegate defines an Event emitted when a given amount of tokens are redelegated from the source validator address to the destination validator address
  • CancelUnbondingDelegation CancelUnbondingDelegation defines an Event emitted when a given amount of tokens that are in the process of unbonding from the validator address are bonded again

Interact with the Solidity Interface

Below are some examples of how to interact with this Solidity interface from your smart contracts. Make sure to import the precompiled interface, e.g.:

Grant approval for the desired messages

See below a function that grants approval to the smart contract to send all x/staking module messages on behalf of the sender account. In this case, the allowance amount is the maximum amount possible. You can tweak this function to approve only the desired messages and amounts.

Delegate to a validator

The stakeTokens function allows the transaction sender to delegate the specified amount to his/her favorite validator. Keep in mind that, for this transaction to be successful, the user should have approved the MSG_DELEGATE previously (see the approveAllStakingMethodsWithMaxAmount defined in the code snippet above as an example). This function returns the completion time of the staking transaction and emits a Delegate event.

Undelegate from a validator

The unstakeTokens function allows a user to unstake a given amount of tokens. It returns the completion time of the unstaking transaction and emits an Undelegate event.

Redelegate to another validator

With the redelegateTokens function, a user can redelegate a given amount of tokens. It returns the completion time of the redelegate transaction and emits a Redelegate event.

Cancel unbonding from a validator

With the cancelUnbondingDelegation function, a user can cancel an unbonding delegation. This function returns the completion time of the unbonding delegation cancellation transaction and emits a CancelUnbondingDelegation event.

Queries

Similarly to transactions, smart contracts can use query methods. To use these methods, there is no need for authorization, as these are read-only methods. Examples of this are these getDelegation and getUnbondingDelegation functions that return the information for the specified validator address.