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.
Redelegate defines an Event emitted when a given amount of tokens are redelegated from the source validator address to the destination validator address
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
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.
string[] private stakingMethods = [
MSG_DELEGATE,
MSG_UNDELEGATE,
MSG_REDELEGATE,
MSG_CANCEL_UNDELEGATION
];
/// @dev Approves all staking transactions with the maximum amount of tokens.
/// @dev This creates a Cosmos Authorization Grant for the given methods.
/// @dev This emits an Approval event.
function approveAllStakingMethodsWithMaxAmount() public {
bool success = STAKING_CONTRACT.approve(
msg.sender,
type(uint256).max,
stakingMethods
);
require(success, "Failed to approve staking methods");
}
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.
/// @dev stake a given amount of tokens. Returns the completion time of the staking transaction.
/// @dev This emits an Delegate event.
/// @param _validatorAddr The address of the validator.
/// @param _amount The amount of tokens to stake in financiyo.
/// @return completionTime The completion time of the staking transaction.
function stakeTokens(
string memory _validatorAddr,
uint256 _amount
) public returns (int64 completionTime) {
return STAKING_CONTRACT.delegate(msg.sender, _validatorAddr, _amount);
}
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.
/// @dev unstake a given amount of tokens. Returns the completion time of the unstaking transaction.
/// @dev This emits an Undelegate event.
/// @param _validatorAddr The address of the validator.
/// @param _amount The amount of tokens to unstake in financiyo.
/// @return completionTime The completion time of the unstaking transaction.
function unstakeTokens(
string memory _validatorAddr,
uint256 _amount
) public returns (int64 completionTime) {
return STAKING_CONTRACT.undelegate(msg.sender, _validatorAddr, _amount);
}
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.
/// @dev redelegate a given amount of tokens. Returns the completion time of the redelegate transaction.
/// @dev This emits a Redelegate event.
/// @param _validatorSrcAddr The address of the source validator.
/// @param _validatorDstAddr The address of the destination validator.
/// @param _amount The amount of tokens to redelegate in financiyo.
/// @return completionTime The completion time of the redelegate transaction.
function redelegateTokens(
string memory _validatorSrcAddr,
string memory _validatorDstAddr,
uint256 _amount
) public returns (int64 completionTime) {
return
STAKING_CONTRACT.redelegate(
msg.sender,
_validatorSrcAddr,
_validatorDstAddr,
_amount
);
}
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.
/// @dev cancel an unbonding delegation. Returns the completion time of the unbonding delegation cancellation transaction.
/// @dev This emits an CancelUnbondingDelegation event.
/// @param _validatorAddr The address of the validator.
/// @param _amount The amount of tokens to cancel the unbonding delegation in financiyo.
/// @param _creationHeight The creation height of the unbonding delegation.
function cancelUnbondingDelegation(
string memory _validatorAddr,
uint256 _amount,
uint256 _creationHeight
) public returns (bool success) {
return
STAKING_CONTRACT.cancelUnbondingDelegation(
msg.sender,
_validatorAddr,
_amount,
_creationHeight
);
}
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.
/// @dev Returns the delegation information for a given validator for the msg sender.
/// @param _validatorAddr The address of the validator.
/// @return shares and balance. The delegation information for a given validator for the msg sender.
function getDelegation(
string memory _validatorAddr
) public view returns (uint256 shares, Coin memory balance) {
return STAKING_CONTRACT.delegation(msg.sender, _validatorAddr);
}
/// @dev Returns the unbonding delegation information for a given validator for the msg sender.
/// @param _validatorAddr The address of the validator.
/// @return entries The unbonding delegation entries for a given validator for the msg sender.
function getUnbondingDelegation(
string memory _validatorAddr
) public view returns (UnbondingDelegationEntry[] memory entries) {
return STAKING_CONTRACT.unbondingDelegation(msg.sender, _validatorAddr);
}