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.
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.
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.
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.
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.
Copy
string[] private stakingMethods = [
MSG_DELEGATE,
MSG_UNDELEGATE,
MSG_REDELEGATE,
MSG_CANCEL_UNDELEGATION
];
/// @dev Approves this smart contract to perform all staking transactions with the maximum amount of tokens on behalf of the transaction signer.
/// @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(
address(this),
type(uint256).max,
stakingMethods
);
require(success, "Failed to approve staking methods");
}
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.
Copy
/// @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 aegaxd.
/// @return success Boolean to inform if the operation was successful or not.
function stakeTokens(
string memory _validatorAddr,
uint256 _amount
) public returns (bool success) {
return STAKING_CONTRACT.delegate(msg.sender, _validatorAddr, _amount);
}
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.
Copy
/// @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 aevmos.
/// @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);
}
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.
Copy
/// @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 aegaxd.
/// @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
);
}
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.
Copy
/// @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 aegaxd.
/// @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
);
}
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.
Copy
/// @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);
}