Skip to content

Commit 434c299

Browse files
authored
Merge pull request #25 from InjectiveLabs/update-examples
update constructor and examples
2 parents 8536c5b + b0cf419 commit 434c299

File tree

5 files changed

+329
-1
lines changed

5 files changed

+329
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import asyncio
2+
import logging
3+
4+
from pyinjective.composer import Composer as ProtoMsgComposer
5+
from pyinjective.client import Client
6+
from pyinjective.transaction import Transaction
7+
from pyinjective.constant import Network
8+
from pyinjective.wallet import PrivateKey, PublicKey, Address
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+
client = Client(network.grpc_endpoint, insecure=True)
17+
18+
# load account
19+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
20+
pub_key = priv_key.to_public_key()
21+
address = pub_key.to_address()
22+
subaccount_id = address.get_subaccount_id(index=0)
23+
24+
# prepare trade info
25+
market_id = "0x0f4209dbe160ce7b09559c69012d2f5fd73070f8552699a9b77aebda16ccdeb1"
26+
27+
# prepare tx msg
28+
msg = composer.MsgIncreasePositionMargin(
29+
sender=address.to_acc_bech32(),
30+
market_id=market_id,
31+
source_subaccount_id=subaccount_id,
32+
destination_subaccount_id=subaccount_id,
33+
amount="50000000000000000000000000"
34+
)
35+
36+
acc_num, acc_seq = await address.get_num_seq(network.lcd_endpoint)
37+
gas_price = 500000000
38+
gas_limit = 200000
39+
fee = [composer.Coin(
40+
amount=str(gas_price * gas_limit),
41+
denom=network.fee_denom,
42+
)]
43+
44+
# build tx
45+
tx = (
46+
Transaction()
47+
.with_messages(msg)
48+
.with_sequence(acc_seq)
49+
.with_account_num(acc_num)
50+
.with_chain_id(network.chain_id)
51+
.with_gas(gas_limit)
52+
.with_fee(fee)
53+
.with_memo("")
54+
.with_timeout_height(0)
55+
)
56+
57+
# build signed tx
58+
sign_doc = tx.get_sign_doc(pub_key)
59+
sig = priv_key.sign(sign_doc.SerializeToString())
60+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
61+
62+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
63+
res = client.send_tx_block_mode(tx_raw_bytes)
64+
65+
# print tx response
66+
print(res)
67+
68+
if __name__ == "__main__":
69+
logging.basicConfig(level=logging.INFO)
70+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import asyncio
2+
import logging
3+
4+
from pyinjective.composer import Composer as ProtoMsgComposer
5+
from pyinjective.client import Client
6+
from pyinjective.transaction import Transaction
7+
from pyinjective.constant import Network
8+
from pyinjective.wallet import PrivateKey, PublicKey, Address
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+
client = Client(network.grpc_endpoint, insecure=True)
17+
18+
# load account
19+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
20+
pub_key = priv_key.to_public_key()
21+
address = pub_key.to_address()
22+
subaccount_id = address.get_subaccount_id(index=0)
23+
24+
# prepare trade info
25+
market_id = "0x31200279ada822061217372150d567be124f02df157650395d1d6ce58a8207aa"
26+
27+
# prepare tx msg
28+
msg = composer.MsgLiquidatePosition(
29+
sender=address.to_acc_bech32(),
30+
market_id=market_id,
31+
subaccount_id="0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000"
32+
)
33+
34+
acc_num, acc_seq = await address.get_num_seq(network.lcd_endpoint)
35+
gas_price = 500000000
36+
gas_limit = 200000
37+
fee = [composer.Coin(
38+
amount=str(gas_price * gas_limit),
39+
denom=network.fee_denom,
40+
)]
41+
42+
# build tx
43+
tx = (
44+
Transaction()
45+
.with_messages(msg)
46+
.with_sequence(acc_seq)
47+
.with_account_num(acc_num)
48+
.with_chain_id(network.chain_id)
49+
.with_gas(gas_limit)
50+
.with_fee(fee)
51+
.with_memo("")
52+
.with_timeout_height(0)
53+
)
54+
55+
# build signed tx
56+
sign_doc = tx.get_sign_doc(pub_key)
57+
sig = priv_key.sign(sign_doc.SerializeToString())
58+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
59+
60+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
61+
res = client.send_tx_block_mode(tx_raw_bytes)
62+
63+
# print tx response
64+
print(res)
65+
66+
if __name__ == "__main__":
67+
logging.basicConfig(level=logging.INFO)
68+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import asyncio
2+
import logging
3+
4+
from pyinjective.composer import Composer as ProtoMsgComposer
5+
from pyinjective.client import Client
6+
from pyinjective.transaction import Transaction
7+
from pyinjective.constant import Network
8+
from pyinjective.wallet import PrivateKey, PublicKey, Address
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+
client = Client(network.grpc_endpoint, insecure=True)
17+
18+
# load account
19+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
20+
pub_key = priv_key.to_public_key()
21+
address = pub_key.to_address()
22+
subaccount_id = address.get_subaccount_id(index=0)
23+
24+
# prepare tx msg
25+
msg = composer.MsgWithdraw(
26+
sender=address.to_acc_bech32(),
27+
subaccount_id=subaccount_id,
28+
amount=1000000000000000000,
29+
denom="inj"
30+
)
31+
32+
acc_num, acc_seq = await address.get_num_seq(network.lcd_endpoint)
33+
gas_price = 500000000
34+
gas_limit = 200000
35+
fee = [composer.Coin(
36+
amount=str(gas_price * gas_limit),
37+
denom=network.fee_denom,
38+
)]
39+
40+
# build tx
41+
tx = (
42+
Transaction()
43+
.with_messages(msg)
44+
.with_sequence(acc_seq)
45+
.with_account_num(acc_num)
46+
.with_chain_id(network.chain_id)
47+
.with_gas(gas_limit)
48+
.with_fee(fee)
49+
.with_memo("")
50+
.with_timeout_height(0)
51+
)
52+
53+
# build signed tx
54+
sign_doc = tx.get_sign_doc(pub_key)
55+
sig = priv_key.sign(sign_doc.SerializeToString())
56+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
57+
58+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
59+
res = client.send_tx_block_mode(tx_raw_bytes)
60+
61+
# print tx response
62+
print(res)
63+
64+
if __name__ == "__main__":
65+
logging.basicConfig(level=logging.INFO)
66+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import asyncio
2+
import logging
3+
4+
from pyinjective.composer import Composer as ProtoMsgComposer
5+
from pyinjective.client import Client
6+
from pyinjective.transaction import Transaction
7+
from pyinjective.constant import Network
8+
from pyinjective.wallet import PrivateKey, PublicKey, Address
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+
client = Client(network.grpc_endpoint, insecure=True)
17+
18+
# load account
19+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
20+
pub_key = priv_key.to_public_key()
21+
address = pub_key.to_address()
22+
subaccount_id = address.get_subaccount_id(index=0)
23+
dest_subaccount_id = address.get_subaccount_id(index=1)
24+
25+
# prepare tx msg
26+
msg = composer.MsgSubaccountTransfer(
27+
sender=address.to_acc_bech32(),
28+
source_subaccount_id=subaccount_id,
29+
destination_subaccount_id=dest_subaccount_id,
30+
amount=1000000000000000000,
31+
denom="inj"
32+
)
33+
34+
acc_num, acc_seq = await address.get_num_seq(network.lcd_endpoint)
35+
gas_price = 500000000
36+
gas_limit = 200000
37+
fee = [composer.Coin(
38+
amount=str(gas_price * gas_limit),
39+
denom=network.fee_denom,
40+
)]
41+
42+
# build tx
43+
tx = (
44+
Transaction()
45+
.with_messages(msg)
46+
.with_sequence(acc_seq)
47+
.with_account_num(acc_num)
48+
.with_chain_id(network.chain_id)
49+
.with_gas(gas_limit)
50+
.with_fee(fee)
51+
.with_memo("")
52+
.with_timeout_height(0)
53+
)
54+
55+
# build signed tx
56+
sign_doc = tx.get_sign_doc(pub_key)
57+
sig = priv_key.sign(sign_doc.SerializeToString())
58+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
59+
60+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
61+
res = client.send_tx_block_mode(tx_raw_bytes)
62+
63+
# print tx response
64+
print(res)
65+
66+
if __name__ == "__main__":
67+
logging.basicConfig(level=logging.INFO)
68+
asyncio.get_event_loop().run_until_complete(main())

pyinjective/composer.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ def DerivativeOrder(
8989
trigger_price=trigger_price
9090
)
9191

92-
9392
def MsgSend(self, from_address: str, to_address: str, amount: int, denom: str):
9493
return cosmos_bank_tx_pb.MsgSend(
9594
from_address=from_address,
@@ -263,3 +262,60 @@ def MsgBatchCancelDerivativeOrders(
263262
sender=sender,
264263
data=data
265264
)
265+
266+
def MsgLiquidatePosition(
267+
self,
268+
sender: str,
269+
subaccount_id: str,
270+
market_id: str
271+
):
272+
return injective_exchange_tx_pb.MsgLiquidatePosition(
273+
sender=sender,
274+
subaccount_id=subaccount_id,
275+
market_id=market_id
276+
)
277+
278+
def MsgIncreasePositionMargin (
279+
self,
280+
sender: str,
281+
source_subaccount_id: str,
282+
destination_subaccount_id: str,
283+
market_id: str,
284+
amount: str
285+
):
286+
return injective_exchange_tx_pb.MsgIncreasePositionMargin(
287+
sender=sender,
288+
source_subaccount_id=source_subaccount_id,
289+
destination_subaccount_id=destination_subaccount_id,
290+
market_id=market_id,
291+
amount=amount
292+
)
293+
294+
def MsgSubaccountTransfer (
295+
self,
296+
sender: str,
297+
source_subaccount_id: str,
298+
destination_subaccount_id: str,
299+
amount: int,
300+
denom: str
301+
):
302+
303+
return injective_exchange_tx_pb.MsgSubaccountTransfer(
304+
sender=sender,
305+
source_subaccount_id=source_subaccount_id,
306+
destination_subaccount_id=destination_subaccount_id,
307+
amount=self.Coin(amount=str(amount),denom=denom)
308+
)
309+
310+
def MsgWithdraw(
311+
self,
312+
sender: str,
313+
subaccount_id: str,
314+
amount: int,
315+
denom: str
316+
):
317+
return injective_exchange_tx_pb.MsgWithdraw(
318+
sender=sender,
319+
subaccount_id=subaccount_id,
320+
amount=self.Coin(amount=str(amount),denom=denom)
321+
)

0 commit comments

Comments
 (0)