Skip to content

Commit f8d73ed

Browse files
Merge pull request #96 from InjectiveLabs/f/update-sdk
SDK Update
2 parents 6d5dd08 + 6e9472f commit f8d73ed

File tree

7 files changed

+302
-6
lines changed

7 files changed

+302
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ python pyinjective/fetch_metadata.py
8585
* Add support for multiple markets in StreamTrades and StreamPosition
8686
* Add support for multiple subaccounts in StreamTrades and StreamPosition
8787
* Add K8S endpoint to mainnet network options
88+
* Add MsgRegisterAsDMM to the composer
89+
* Re-gen ini files
8890

8991

9092
**0.5.6.4**

examples/async/chain_client/23_MsgRelayPriceFeedPrice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async def main() -> None:
3434
# load account
3535
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
3636
pub_key = priv_key.to_public_key()
37-
address = pub_key.to_address().init_num_seq(network.lcd_endpoint)
37+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
3838

3939
price = 45000
4040
price_to_send = [str(int(price * 10 ** 6))]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 Chain Tx/Query client for Python. Example only."""
15+
16+
import asyncio
17+
import logging
18+
19+
from pyinjective.composer import Composer as ProtoMsgComposer
20+
from pyinjective.async_client import AsyncClient
21+
from pyinjective.transaction import Transaction
22+
from pyinjective.constant import Network
23+
from pyinjective.wallet import PrivateKey, PublicKey, Address
24+
25+
26+
async def main() -> None:
27+
# select network: local, testnet, mainnet
28+
network = Network.testnet()
29+
composer = ProtoMsgComposer(network=network.string())
30+
31+
# initialize grpc client
32+
client = AsyncClient(network, insecure=False)
33+
34+
# load account
35+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
36+
pub_key = priv_key.to_public_key()
37+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
38+
39+
# prepare tx msg
40+
msg = composer.MsgRegisterAsDMM(
41+
sender=address.to_acc_bech32(),
42+
dmm_account=address.to_acc_bech32()
43+
)
44+
45+
# build sim tx
46+
tx = (
47+
Transaction()
48+
.with_messages(msg)
49+
.with_sequence(address.get_sequence())
50+
.with_account_num(address.get_number())
51+
.with_chain_id(network.chain_id)
52+
)
53+
sim_sign_doc = tx.get_sign_doc(pub_key)
54+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
55+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
56+
57+
# simulate tx
58+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
59+
if not success:
60+
print(sim_res)
61+
return
62+
63+
# build tx
64+
gas_price = 500000000
65+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
66+
fee = [composer.Coin(
67+
amount=gas_price * gas_limit,
68+
denom=network.fee_denom,
69+
)]
70+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
71+
sign_doc = tx.get_sign_doc(pub_key)
72+
sig = priv_key.sign(sign_doc.SerializeToString())
73+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
74+
75+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
76+
res = await client.send_tx_block_mode(tx_raw_bytes)
77+
print("tx response")
78+
print(res)
79+
80+
if __name__ == "__main__":
81+
logging.basicConfig(level=logging.INFO)
82+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 Chain Tx/Query client for Python. Example only."""
15+
16+
import asyncio
17+
import logging
18+
19+
from pyinjective.composer import Composer as ProtoMsgComposer
20+
from pyinjective.client import Client
21+
from pyinjective.transaction import Transaction
22+
from pyinjective.constant import Network
23+
from pyinjective.wallet import PrivateKey, PublicKey, Address
24+
25+
26+
async def main() -> None:
27+
# select network: local, testnet, mainnet
28+
network = Network.testnet()
29+
composer = ProtoMsgComposer(network=network.string())
30+
31+
# initialize grpc client
32+
client = Client(network, insecure=False)
33+
34+
# load account
35+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
36+
pub_key = priv_key.to_public_key()
37+
address = pub_key.to_address().init_num_seq(network.lcd_endpoint)
38+
39+
# prepare tx msg
40+
msg = composer.MsgRegisterAsDMM(
41+
sender=address.to_acc_bech32(),
42+
dmm_account=address.to_acc_bech32()
43+
)
44+
45+
# build sim tx
46+
tx = (
47+
Transaction()
48+
.with_messages(msg)
49+
.with_sequence(address.get_sequence())
50+
.with_account_num(address.get_number())
51+
.with_chain_id(network.chain_id)
52+
)
53+
sim_sign_doc = tx.get_sign_doc(pub_key)
54+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
55+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
56+
57+
# simulate tx
58+
(sim_res, success) = client.simulate_tx(sim_tx_raw_bytes)
59+
if not success:
60+
print(sim_res)
61+
return
62+
63+
# build tx
64+
gas_price = 500000000
65+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
66+
fee = [composer.Coin(
67+
amount=gas_price * gas_limit,
68+
denom=network.fee_denom,
69+
)]
70+
current_height = client.get_latest_block().block.header.height
71+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(current_height+50)
72+
sign_doc = tx.get_sign_doc(pub_key)
73+
sig = priv_key.sign(sign_doc.SerializeToString())
74+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
75+
76+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
77+
res = client.send_tx_block_mode(tx_raw_bytes)
78+
print("tx response")
79+
print(res)
80+
81+
if __name__ == "__main__":
82+
logging.basicConfig(level=logging.INFO)
83+
asyncio.get_event_loop().run_until_complete(main())

pyinjective/composer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ def MsgBatchCancelSpotOrders(self, sender: str, data: List):
230230
sender=sender, data=data
231231
)
232232

233+
def MsgRegisterAsDMM(self, sender: str, dmm_account: str):
234+
return injective_exchange_tx_pb.MsgRegisterAsDMM(
235+
sender=sender, dmm_account=dmm_account
236+
)
237+
233238
def MsgCreateDerivativeLimitOrder(
234239
self,
235240
market_id: str,

pyinjective/proto/injective/exchange/v1beta1/tx_pb2.py

Lines changed: 95 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyinjective/proto/injective/exchange/v1beta1/tx_pb2_grpc.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ def __init__(self, channel):
115115
request_serializer=injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgIncreasePositionMargin.SerializeToString,
116116
response_deserializer=injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgIncreasePositionMarginResponse.FromString,
117117
)
118+
self.RegisterAsDMM = channel.unary_unary(
119+
'/injective.exchange.v1beta1.Msg/RegisterAsDMM',
120+
request_serializer=injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgRegisterAsDMM.SerializeToString,
121+
response_deserializer=injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgRegisterAsDMMResponse.FromString,
122+
)
118123

119124

120125
class MsgServicer(object):
@@ -260,6 +265,13 @@ def IncreasePositionMargin(self, request, context):
260265
context.set_details('Method not implemented!')
261266
raise NotImplementedError('Method not implemented!')
262267

268+
def RegisterAsDMM(self, request, context):
269+
"""RegisterAsDMM defines a method for registering as a DMM
270+
"""
271+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
272+
context.set_details('Method not implemented!')
273+
raise NotImplementedError('Method not implemented!')
274+
263275

264276
def add_MsgServicer_to_server(servicer, server):
265277
rpc_method_handlers = {
@@ -363,6 +375,11 @@ def add_MsgServicer_to_server(servicer, server):
363375
request_deserializer=injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgIncreasePositionMargin.FromString,
364376
response_serializer=injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgIncreasePositionMarginResponse.SerializeToString,
365377
),
378+
'RegisterAsDMM': grpc.unary_unary_rpc_method_handler(
379+
servicer.RegisterAsDMM,
380+
request_deserializer=injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgRegisterAsDMM.FromString,
381+
response_serializer=injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgRegisterAsDMMResponse.SerializeToString,
382+
),
366383
}
367384
generic_handler = grpc.method_handlers_generic_handler(
368385
'injective.exchange.v1beta1.Msg', rpc_method_handlers)
@@ -713,3 +730,20 @@ def IncreasePositionMargin(request,
713730
injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgIncreasePositionMarginResponse.FromString,
714731
options, channel_credentials,
715732
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
733+
734+
@staticmethod
735+
def RegisterAsDMM(request,
736+
target,
737+
options=(),
738+
channel_credentials=None,
739+
call_credentials=None,
740+
insecure=False,
741+
compression=None,
742+
wait_for_ready=None,
743+
timeout=None,
744+
metadata=None):
745+
return grpc.experimental.unary_unary(request, target, '/injective.exchange.v1beta1.Msg/RegisterAsDMM',
746+
injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgRegisterAsDMM.SerializeToString,
747+
injective_dot_exchange_dot_v1beta1_dot_tx__pb2.MsgRegisterAsDMMResponse.FromString,
748+
options, channel_credentials,
749+
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)

0 commit comments

Comments
 (0)