Skip to content

Commit ca0a5f6

Browse files
authored
Merge pull request #234 from HighStakesSwitzerland/dev
add MsgWithdrawValidatorCommission
2 parents 1dfae88 + e6461c6 commit ca0a5f6

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
import asyncio
3+
import logging
4+
5+
from pyinjective.composer import Composer as ProtoMsgComposer
6+
from pyinjective.async_client import AsyncClient
7+
from pyinjective.transaction import Transaction
8+
from pyinjective.constant import Network
9+
from pyinjective.wallet import PrivateKey
10+
11+
12+
async def main() -> None:
13+
"""For a validator to withdraw his rewards & commissions simultaneously"""
14+
# select network: local, testnet, mainnet
15+
network = Network.testnet()
16+
composer = ProtoMsgComposer(network=network.string())
17+
18+
# initialize grpc client
19+
client = AsyncClient(network, insecure=False)
20+
await client.sync_timeout_height()
21+
22+
# load account
23+
# private key is that from the validator's wallet
24+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
25+
pub_key = priv_key.to_public_key()
26+
address = pub_key.to_address()
27+
account = await client.get_account(address.to_acc_bech32())
28+
29+
# prepare tx msg
30+
validator_address = "injvaloper1ultw9r29l8nxy5u6thcgusjn95vsy2caw722q5"
31+
32+
msg0 = composer.MsgWithdrawDelegatorReward(
33+
delegator_address=address.to_acc_bech32(),
34+
validator_address=validator_address
35+
)
36+
37+
msg1 = composer.MsgWithdrawValidatorCommission(
38+
validator_address=validator_address
39+
)
40+
41+
# build sim tx
42+
tx = (
43+
Transaction()
44+
.with_messages(msg0, msg1)
45+
.with_sequence(client.get_sequence())
46+
.with_account_num(client.get_number())
47+
.with_chain_id(network.chain_id)
48+
)
49+
sim_sign_doc = tx.get_sign_doc(pub_key)
50+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
51+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
52+
53+
# simulate tx
54+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
55+
if not success:
56+
print(sim_res)
57+
return
58+
59+
# build tx
60+
gas_price = 500000000
61+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
62+
gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0')
63+
fee = [composer.Coin(
64+
amount=gas_price * gas_limit,
65+
denom=network.fee_denom,
66+
)]
67+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
68+
sign_doc = tx.get_sign_doc(pub_key)
69+
sig = priv_key.sign(sign_doc.SerializeToString())
70+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
71+
72+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
73+
res = await client.send_tx_sync_mode(tx_raw_bytes)
74+
print(res)
75+
print("gas wanted: {}".format(gas_limit))
76+
print("gas fee: {} INJ".format(gas_fee))
77+
78+
if __name__ == "__main__":
79+
asyncio.get_event_loop().run_until_complete(main())

pyinjective/composer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,14 @@ def MsgWithdrawDelegatorReward(
881881
delegator_address=delegator_address, validator_address=validator_address
882882
)
883883

884+
def MsgWithdrawValidatorCommission(
885+
self, validator_address: str
886+
):
887+
888+
return cosmos_distribution_tx_pb.MsgWithdrawValidatorCommission(
889+
validator_address=validator_address
890+
)
891+
884892
def MsgVote(
885893
self,
886894
proposal_id: str,

0 commit comments

Comments
 (0)