Skip to content

Commit 6342985

Browse files
Merge pull request #198 from InjectiveLabs/f/add_msgcreateinsurancefund
F/add msgcreateinsurancefund
2 parents 13a3cbc + 485b209 commit 6342985

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ Note that the [sync client](https://github.com/InjectiveLabs/sdk-python/blob/mas
7878

7979

8080
### Changelogs
81+
**0.6.1.8**
82+
* Add MsgCreateInsuranceFund in Composer
83+
* Re-gen mainnet denoms
84+
8185
**0.6.1.5**
8286
* Add MsgExecuteContract in Composer
8387

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.MsgCreateInsuranceFund(
27+
sender=address.to_acc_bech32(),
28+
ticker="5202d32a9-1701406800-SF",
29+
quote_denom="USDT",
30+
oracle_base="Frontrunner",
31+
oracle_quote="Frontrunner",
32+
oracle_type=11,
33+
expiry=-2,
34+
initial_deposit=1000
35+
)
36+
37+
# build sim tx
38+
tx = (
39+
Transaction()
40+
.with_messages(msg)
41+
.with_sequence(client.get_sequence())
42+
.with_account_num(client.get_number())
43+
.with_chain_id(network.chain_id)
44+
)
45+
sim_sign_doc = tx.get_sign_doc(pub_key)
46+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
47+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
48+
49+
# simulate tx
50+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
51+
if not success:
52+
print(sim_res)
53+
return
54+
55+
# build tx
56+
gas_price = 500000000
57+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
58+
gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0')
59+
fee = [composer.Coin(
60+
amount=gas_price * gas_limit,
61+
denom=network.fee_denom,
62+
)]
63+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
64+
sign_doc = tx.get_sign_doc(pub_key)
65+
sig = priv_key.sign(sign_doc.SerializeToString())
66+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
67+
68+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
69+
res = await client.send_tx_sync_mode(tx_raw_bytes)
70+
print(res)
71+
print("gas wanted: {}".format(gas_limit))
72+
print("gas fee: {} INJ".format(gas_fee))
73+
74+
if __name__ == "__main__":
75+
logging.basicConfig(level=logging.INFO)
76+
asyncio.get_event_loop().run_until_complete(main())

pyinjective/composer.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,30 @@ def MsgDelegate(
820820
amount=self.Coin(amount=be_amount, denom="inj"),
821821
)
822822

823+
def MsgCreateInsuranceFund(
824+
self,
825+
sender: str,
826+
ticker: str,
827+
quote_denom: str,
828+
oracle_base: str,
829+
oracle_quote: str,
830+
oracle_type: int,
831+
expiry: int,
832+
initial_deposit: int
833+
):
834+
peggy_denom, decimals = Denom.load_peggy_denom(self.network, quote_denom)
835+
be_amount = amount_to_backend(initial_deposit, decimals)
836+
logging.info(
837+
"Loaded send symbol {} ({}) with decimals = {}".format(
838+
quote_denom, peggy_denom, decimals
839+
)
840+
)
841+
842+
return injective_insurance_tx_pb.MsgCreateInsuranceFund(
843+
sender=sender, ticker=ticker, quote_denom=peggy_denom, oracle_base=oracle_base, oracle_quote=oracle_quote,
844+
oracle_type=oracle_type, expiry=expiry, initial_deposit=self.Coin(amount=be_amount, denom=peggy_denom),
845+
)
846+
823847
def MsgWithdrawDelegatorReward(
824848
self, delegator_address: str, validator_address: str
825849
):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
AUTHOR = "Injective Labs"
1919
REQUIRES_PYTHON = ">=3.7.0"
20-
VERSION = "0.6.1.7"
20+
VERSION = "0.6.1.8"
2121

2222
REQUIRED = [
2323
"protobuf",

0 commit comments

Comments
 (0)