Skip to content

Add support for claiming validator commission #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release History

## Unreleased

- fix: coins parsing fix to support ibc denoms
- feat: add support for claiming validator commission

## 0.9.1

- fix: move googleapis-common-protos to main dependency group to resolve installation issues
Expand Down
25 changes: 24 additions & 1 deletion cosmpy/aerial/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
from google.protobuf.timestamp_pb2 import Timestamp

from cosmpy.aerial.client.bank import create_bank_send_msg
from cosmpy.aerial.client.distribution import create_withdraw_delegator_reward
from cosmpy.aerial.client.distribution import (
create_withdraw_delegator_reward,
create_withdraw_validator_commission,
)
from cosmpy.aerial.client.staking import (
ValidatorStatus,
create_delegate_msg,
Expand Down Expand Up @@ -600,6 +603,26 @@ def claim_rewards(
self, tx, sender, gas_limit=gas_limit, memo=memo
)

def claim_validator_commission(
self,
sender: Wallet,
memo: Optional[str] = None,
gas_limit: Optional[int] = None,
) -> SubmittedTx:
"""claim validator commission.

:param sender: sender
:param memo: memo, defaults to None
:param gas_limit: gas limit, defaults to None
:return: prepare and broadcast the transaction and transaction details
"""
tx = Transaction()
tx.add_message(create_withdraw_validator_commission(sender.address()))

return prepare_and_broadcast_basic_transaction(
self, tx, sender, gas_limit=gas_limit, memo=memo
)

def estimate_gas_for_tx(self, tx: Transaction) -> int:
"""Estimate gas for transaction.

Expand Down
14 changes: 13 additions & 1 deletion cosmpy/aerial/client/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"""Distribution."""

from cosmpy.crypto.address import Address
from cosmpy.protos.cosmos.distribution.v1beta1.tx_pb2 import MsgWithdrawDelegatorReward
from cosmpy.protos.cosmos.distribution.v1beta1.tx_pb2 import (
MsgWithdrawDelegatorReward,
MsgWithdrawValidatorCommission,
)


def create_withdraw_delegator_reward(delegator: Address, validator: Address):
Expand All @@ -33,3 +36,12 @@ def create_withdraw_delegator_reward(delegator: Address, validator: Address):
delegator_address=str(delegator),
validator_address=str(validator),
)


def create_withdraw_validator_commission(validator: Address):
"""Create withdraw validator commission.

:param validator: validator account address "cosmos1..."
:return: withdraw validator commission message
"""
return MsgWithdrawValidatorCommission(validator_address=validator)
22 changes: 22 additions & 0 deletions docs/api/aerial/client/__init__.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,28 @@ claim rewards.

prepare and broadcast the transaction and transaction details

<a id="cosmpy.aerial.client.__init__.LedgerClient.claim_validator_commission"></a>

#### claim`_`validator`_`commission

```python
def claim_validator_commission(sender: Wallet,
memo: Optional[str] = None,
gas_limit: Optional[int] = None) -> SubmittedTx
```

claim rewards.

**Arguments**:

- `sender`: sender
- `memo`: memo, defaults to None
- `gas_limit`: gas limit, defaults to None

**Returns**:

prepare and broadcast the transaction and transaction details

<a id="cosmpy.aerial.client.__init__.LedgerClient.estimate_gas_for_tx"></a>

#### estimate`_`gas`_`for`_`tx
Expand Down
2 changes: 0 additions & 2 deletions examples/aerial_compounder.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def main():

# query, claim and stake rewards after time period
while time_check < time_limit:

begin = time.monotonic()

summary = ledger.query_staking_summary(alice.address())
Expand All @@ -108,7 +107,6 @@ def main():
true_reward = balance_after - balance_before

if true_reward > 0:

print(f"Staking {true_reward} (reward after fees)")

tx = ledger.delegate_tokens(validator.address, true_reward, alice)
Expand Down
2 changes: 0 additions & 2 deletions examples/aerial_stake_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,12 @@ def main():
stake_threshold = 0.10

for _i in range(len(validators_comission)):

# Choose validator with lower commission
validator_index = validators_comission.index(min(validators_comission))

# Verify that it meets the minimum % threshold
validator_stake_pct = validators_stake[validator_index] / total_stake
if validator_stake_pct >= stake_threshold:

# Set the selected validator
validator = validators[validator_index]
break
Expand Down
1 change: 0 additions & 1 deletion examples/aerial_swap_automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def main():
interval = args.interval_time

while True:

# Query LP status
pool = pair_contract.query({"pool": {}})
native_amount = int(pool["assets"][1]["amount"])
Expand Down
2 changes: 0 additions & 2 deletions examples/aerial_topup.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def main():
interval_time = args.interval_time

while True:

wallet_balance = ledger.query_bank_balance(wallet_address)

if wallet_balance < amount:
Expand All @@ -107,7 +106,6 @@ def main():
task_wallet_balance = ledger.query_bank_balance(task_wallet_address)

if task_wallet_balance < minimum_balance:

print("topping up task wallet")
# Top-up task_wallet
msg = any_pb2.Any()
Expand Down