Skip to content

Commit d90af43

Browse files
committed
2 parents a77e18c + 93e0650 commit d90af43

File tree

11 files changed

+623
-7
lines changed

11 files changed

+623
-7
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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, PublicKey, Address
9+
10+
11+
async def main() -> None:
12+
# select network: local, testnet, mainnet
13+
network = Network.testnet()
14+
composer = ProtoMsgComposer(network=network.string())
15+
16+
# initialize grpc client
17+
client = AsyncClient(network, insecure=True)
18+
19+
# load account
20+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
21+
pub_key = priv_key.to_public_key()
22+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
23+
subaccount_id = address.get_subaccount_id(index=0)
24+
25+
# prepare tx msg
26+
# WARNING
27+
# be aware that this grant the grantee unrestricted ability to send this type of msg
28+
# this can be exploited to drain your funds if your grantee becomes malicious
29+
# we will support restrictive MsgGrant in next mainnet upgrade
30+
msg = composer.MsgGrant(
31+
granter = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku",
32+
grantee = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r",
33+
msg_type = "/injective.exchange.v1beta1.MsgCreateSpotLimitOrder",
34+
expire_in=31536000 # 1 year
35+
)
36+
37+
# build sim tx
38+
tx = (
39+
Transaction()
40+
.with_messages(msg)
41+
.with_sequence(address.get_sequence())
42+
.with_account_num(address.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+
sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True)
56+
print("simulation msg response")
57+
print(sim_res_msg)
58+
59+
# build tx
60+
gas_price = 500000000
61+
gas_limit = sim_res.gas_info.gas_used + 15000 # add 15k for gas, fee computation
62+
fee = [composer.Coin(
63+
amount=gas_price * gas_limit,
64+
denom=network.fee_denom,
65+
)]
66+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
67+
sign_doc = tx.get_sign_doc(pub_key)
68+
sig = priv_key.sign(sign_doc.SerializeToString())
69+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
70+
71+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
72+
res = await client.send_tx_block_mode(tx_raw_bytes)
73+
res_msg = ProtoMsgComposer.MsgResponses(res.data)
74+
print("tx response")
75+
print(res)
76+
print("tx msg response")
77+
print(res_msg)
78+
79+
80+
if __name__ == "__main__":
81+
logging.basicConfig(level=logging.INFO)
82+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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, PublicKey, Address
9+
10+
11+
async def main() -> None:
12+
# select network: local, testnet, mainnet
13+
network = Network.testnet()
14+
composer = ProtoMsgComposer(network=network.string())
15+
16+
# initialize grpc client
17+
client = AsyncClient(network, insecure=True)
18+
19+
# load account
20+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
21+
pub_key = priv_key.to_public_key()
22+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
23+
subaccount_id = address.get_subaccount_id(index=0)
24+
25+
# prepare tx msg
26+
market_id = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
27+
granter = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku"
28+
grantee = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
29+
msg0 = composer.MsgCreateSpotLimitOrder(
30+
sender=granter,
31+
market_id=market_id,
32+
subaccount_id=subaccount_id,
33+
fee_recipient=grantee,
34+
price=7.523,
35+
quantity=0.01,
36+
is_buy=True
37+
)
38+
39+
msg = composer.MsgExec(
40+
grantee=grantee,
41+
msgs=[msg0]
42+
)
43+
44+
# build sim tx
45+
tx = (
46+
Transaction()
47+
.with_messages(msg)
48+
.with_sequence(address.get_sequence())
49+
.with_account_num(address.get_number())
50+
.with_chain_id(network.chain_id)
51+
)
52+
sim_sign_doc = tx.get_sign_doc(pub_key)
53+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
54+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
55+
56+
# simulate tx
57+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
58+
if not success:
59+
print(sim_res)
60+
return
61+
62+
sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True)
63+
print("simulation msg response")
64+
print(sim_res_msg)
65+
66+
# build tx
67+
gas_price = 500000000
68+
gas_limit = sim_res.gas_info.gas_used + 15000 # add 15k for gas, fee computation
69+
fee = [composer.Coin(
70+
amount=gas_price * gas_limit,
71+
denom=network.fee_denom,
72+
)]
73+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
74+
sign_doc = tx.get_sign_doc(pub_key)
75+
sig = priv_key.sign(sign_doc.SerializeToString())
76+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
77+
78+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
79+
res = await client.send_tx_block_mode(tx_raw_bytes)
80+
res_msg = ProtoMsgComposer.MsgResponses(res.data)
81+
print("tx response")
82+
print(res)
83+
print("tx msg response")
84+
print(res_msg)
85+
86+
if __name__ == "__main__":
87+
logging.basicConfig(level=logging.INFO)
88+
asyncio.get_event_loop().run_until_complete(main())
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, PublicKey, Address
9+
10+
11+
async def main() -> None:
12+
# select network: local, testnet, mainnet
13+
network = Network.testnet()
14+
composer = ProtoMsgComposer(network=network.string())
15+
16+
# initialize grpc client
17+
client = AsyncClient(network, insecure=True)
18+
19+
# load account
20+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
21+
pub_key = priv_key.to_public_key()
22+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
23+
subaccount_id = address.get_subaccount_id(index=0)
24+
25+
# prepare tx msg
26+
msg = composer.MsgRevoke(
27+
granter = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku",
28+
grantee = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r",
29+
msg_type = "/injective.exchange.v1beta1.MsgCreateSpotLimitOrder"
30+
)
31+
32+
# build sim tx
33+
tx = (
34+
Transaction()
35+
.with_messages(msg)
36+
.with_sequence(address.get_sequence())
37+
.with_account_num(address.get_number())
38+
.with_chain_id(network.chain_id)
39+
)
40+
sim_sign_doc = tx.get_sign_doc(pub_key)
41+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
42+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
43+
44+
# simulate tx
45+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
46+
if not success:
47+
print(sim_res)
48+
return
49+
50+
sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True)
51+
print("simulation msg response")
52+
print(sim_res_msg)
53+
54+
# build tx
55+
gas_price = 500000000
56+
gas_limit = sim_res.gas_info.gas_used + 15000 # add 15k for gas, fee computation
57+
fee = [composer.Coin(
58+
amount=gas_price * gas_limit,
59+
denom=network.fee_denom,
60+
)]
61+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
62+
sign_doc = tx.get_sign_doc(pub_key)
63+
sig = priv_key.sign(sign_doc.SerializeToString())
64+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
65+
66+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
67+
res = await client.send_tx_block_mode(tx_raw_bytes)
68+
res_msg = ProtoMsgComposer.MsgResponses(res.data)
69+
print("tx response")
70+
print(res)
71+
print("tx msg response")
72+
print(res_msg)
73+
74+
if __name__ == "__main__":
75+
logging.basicConfig(level=logging.INFO)
76+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2021 Injective Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Injective Exchange API client for Python. Example only."""
15+
16+
import asyncio
17+
import logging
18+
19+
from pyinjective.async_client import AsyncClient
20+
from pyinjective.constant import Network
21+
22+
async def main() -> None:
23+
network = Network.testnet()
24+
client = AsyncClient(network, insecure=True)
25+
granter = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku"
26+
grantee = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
27+
msg_type_url = "/injective.exchange.v1beta1.MsgCreateDerivativeLimitOrder"
28+
authorizations = await client.get_grants(granter=granter, grantee=grantee, msg_type_url=msg_type_url)
29+
print(authorizations)
30+
31+
if __name__ == '__main__':
32+
logging.basicConfig(level=logging.INFO)
33+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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, insecure=True)
17+
18+
# load account
19+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
20+
pub_key = priv_key.to_public_key()
21+
address = pub_key.to_address().init_num_seq(network.lcd_endpoint)
22+
23+
# prepare tx msg
24+
# WARNING
25+
# be aware that this grant the grantee unrestricted ability to send this type of msg
26+
# this can be exploited to drain your funds if your grantee becomes malicious
27+
# we will support restrictive MsgGrant in next mainnet upgrade
28+
msg = composer.MsgGrant(
29+
granter = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku",
30+
grantee = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r",
31+
msg_type = "/injective.exchange.v1beta1.MsgCreateSpotLimitOrder",
32+
expire_in=31536000 # 1 year
33+
)
34+
35+
# build sim tx
36+
tx = (
37+
Transaction()
38+
.with_messages(msg)
39+
.with_sequence(address.get_sequence())
40+
.with_account_num(address.get_number())
41+
.with_chain_id(network.chain_id)
42+
)
43+
sim_sign_doc = tx.get_sign_doc(pub_key)
44+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
45+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
46+
47+
# simulate tx
48+
(sim_res, success) = client.simulate_tx(sim_tx_raw_bytes)
49+
if not success:
50+
print(sim_res)
51+
return
52+
53+
# build tx
54+
gas_price = 500000000
55+
gas_limit = sim_res.gas_info.gas_used + 15000 # add 15k for gas, fee computation
56+
fee = [composer.Coin(
57+
amount=gas_price * gas_limit,
58+
denom=network.fee_denom,
59+
)]
60+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
61+
sign_doc = tx.get_sign_doc(pub_key)
62+
sig = priv_key.sign(sign_doc.SerializeToString())
63+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
64+
65+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
66+
res = client.send_tx_block_mode(tx_raw_bytes)
67+
68+
# print tx response
69+
print(res)
70+
71+
if __name__ == "__main__":
72+
logging.basicConfig(level=logging.INFO)
73+
asyncio.get_event_loop().run_until_complete(main())

0 commit comments

Comments
 (0)