Skip to content

Commit 2f5e1c6

Browse files
committed
2 parents 74c9153 + 0660aaa commit 2f5e1c6

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import asyncio
2+
import logging
3+
4+
from pyinjective.composer import Composer as ProtoMsgComposer
5+
from pyinjective.async_client import AsyncClient
6+
from pyinjective.transaction import Transaction
7+
from pyinjective.constant import Network
8+
from pyinjective.wallet import PrivateKey
9+
10+
async def main() -> None:
11+
# select network: local, testnet, mainnet
12+
network = Network.testnet()
13+
composer = ProtoMsgComposer(network=network.string())
14+
15+
# initialize grpc client
16+
# set custom cookie location (optional) - defaults to current dir
17+
client = AsyncClient(network, insecure=False)
18+
await client.sync_timeout_height()
19+
20+
# load account
21+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
22+
pub_key = priv_key.to_public_key()
23+
address = pub_key.to_address()
24+
account = await client.get_account(address.to_acc_bech32())
25+
26+
msg = composer.MsgUnderwrite(
27+
sender=address.to_acc_bech32(),
28+
market_id="0x141e3c92ed55107067ceb60ee412b86256cedef67b1227d6367b4cdf30c55a74",
29+
quote_denom="USDT",
30+
amount=100
31+
)
32+
33+
# build sim tx
34+
tx = (
35+
Transaction()
36+
.with_messages(msg)
37+
.with_sequence(client.get_sequence())
38+
.with_account_num(client.get_number())
39+
.with_chain_id(network.chain_id)
40+
)
41+
sim_sign_doc = tx.get_sign_doc(pub_key)
42+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
43+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
44+
45+
# simulate tx
46+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
47+
if not success:
48+
print(sim_res)
49+
return
50+
51+
# build tx
52+
gas_price = 500000000
53+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
54+
gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0')
55+
fee = [composer.Coin(
56+
amount=gas_price * gas_limit,
57+
denom=network.fee_denom,
58+
)]
59+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
60+
sign_doc = tx.get_sign_doc(pub_key)
61+
sig = priv_key.sign(sign_doc.SerializeToString())
62+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
63+
64+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
65+
res = await client.send_tx_sync_mode(tx_raw_bytes)
66+
print(res)
67+
print("gas wanted: {}".format(gas_limit))
68+
print("gas fee: {} INJ".format(gas_fee))
69+
70+
if __name__ == "__main__":
71+
logging.basicConfig(level=logging.INFO)
72+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import asyncio
2+
import logging
3+
4+
from pyinjective.composer import Composer as ProtoMsgComposer
5+
from pyinjective.async_client import AsyncClient
6+
from pyinjective.transaction import Transaction
7+
from pyinjective.constant import Network
8+
from pyinjective.wallet import PrivateKey
9+
10+
async def main() -> None:
11+
# select network: local, testnet, mainnet
12+
network = Network.testnet()
13+
composer = ProtoMsgComposer(network=network.string())
14+
15+
# initialize grpc client
16+
# set custom cookie location (optional) - defaults to current dir
17+
client = AsyncClient(network, insecure=False)
18+
await client.sync_timeout_height()
19+
20+
# load account
21+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
22+
pub_key = priv_key.to_public_key()
23+
address = pub_key.to_address()
24+
account = await client.get_account(address.to_acc_bech32())
25+
26+
msg = composer.MsgRequestRedemption(
27+
sender=address.to_acc_bech32(),
28+
market_id="0x141e3c92ed55107067ceb60ee412b86256cedef67b1227d6367b4cdf30c55a74",
29+
share_denom="share15",
30+
amount=100 # raw chain value
31+
)
32+
33+
# build sim tx
34+
tx = (
35+
Transaction()
36+
.with_messages(msg)
37+
.with_sequence(client.get_sequence())
38+
.with_account_num(client.get_number())
39+
.with_chain_id(network.chain_id)
40+
)
41+
sim_sign_doc = tx.get_sign_doc(pub_key)
42+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
43+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
44+
45+
# simulate tx
46+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
47+
if not success:
48+
print(sim_res)
49+
return
50+
51+
# build tx
52+
gas_price = 500000000
53+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
54+
gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0')
55+
fee = [composer.Coin(
56+
amount=gas_price * gas_limit,
57+
denom=network.fee_denom,
58+
)]
59+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
60+
sign_doc = tx.get_sign_doc(pub_key)
61+
sig = priv_key.sign(sign_doc.SerializeToString())
62+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
63+
64+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
65+
res = await client.send_tx_sync_mode(tx_raw_bytes)
66+
print(res)
67+
print("gas wanted: {}".format(gas_limit))
68+
print("gas fee: {} INJ".format(gas_fee))
69+
70+
if __name__ == "__main__":
71+
logging.basicConfig(level=logging.INFO)
72+
asyncio.get_event_loop().run_until_complete(main())

pyinjective/composer.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
from .proto.cosmos.gov.v1beta1 import tx_pb2 as cosmos_gov_tx_pb
2929

30+
from .proto.injective.insurance.v1beta1 import tx_pb2 as injective_insurance_tx_pb
31+
3032
from pyinjective.proto.cosmos.base.v1beta1 import coin_pb2 as cosmos_dot_base_dot_v1beta1_dot_coin__pb2
3133

3234
from .proto.cosmwasm.wasm.v1 import tx_pb2 as wasm_tx_pb
@@ -844,6 +846,37 @@ def MsgCreateInsuranceFund(
844846
oracle_type=oracle_type, expiry=expiry, initial_deposit=self.Coin(amount=be_amount, denom=peggy_denom),
845847
)
846848

849+
def MsgUnderwrite(
850+
self,
851+
sender: str,
852+
market_id: str,
853+
quote_denom: str,
854+
amount: int,
855+
):
856+
peggy_denom, decimals = Denom.load_peggy_denom(self.network, quote_denom)
857+
be_amount = amount_to_backend(amount, decimals)
858+
logging.info(
859+
"Loaded send symbol {} ({}) with decimals = {}".format(
860+
quote_denom, peggy_denom, decimals
861+
)
862+
)
863+
864+
return injective_insurance_tx_pb.MsgUnderwrite(
865+
sender=sender, market_id=market_id, deposit=self.Coin(amount=be_amount, denom=peggy_denom),
866+
)
867+
868+
def MsgRequestRedemption(
869+
self,
870+
sender: str,
871+
market_id: str,
872+
share_denom: str,
873+
amount: int,
874+
):
875+
876+
return injective_insurance_tx_pb.MsgRequestRedemption(
877+
sender=sender, market_id=market_id, amount=self.Coin(amount=amount, denom=share_denom),
878+
)
879+
847880
def MsgWithdrawDelegatorReward(
848881
self, delegator_address: str, validator_address: str
849882
):

0 commit comments

Comments
 (0)