Skip to content

Commit 044250d

Browse files
Merge pull request #120 from InjectiveLabs/f/add_msgexternaltransfer
feat: add MsgExternalTransfer
2 parents 8f61019 + 6fd3367 commit 044250d

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.md

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

7979

8080
### Changelogs
81+
**0.5.6.9**
82+
* Add MsgExternalTransfer to the composer
83+
8184
**0.5.6.8**
8285
* Add skip & limit params to Exchange API methods
8386
* Add more methods in ExplorerRPC
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright 2022 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+
15+
import asyncio
16+
import logging
17+
18+
from pyinjective.composer import Composer as ProtoMsgComposer
19+
from pyinjective.async_client import AsyncClient
20+
from pyinjective.transaction import Transaction
21+
from pyinjective.constant import Network
22+
from pyinjective.wallet import PrivateKey
23+
24+
25+
async def main() -> None:
26+
# select network: local, testnet, mainnet
27+
network = Network.testnet()
28+
composer = ProtoMsgComposer(network=network.string())
29+
30+
# initialize grpc client
31+
client = AsyncClient(network, insecure=False)
32+
await client.sync_timeout_height()
33+
34+
# load account
35+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
36+
pub_key = priv_key.to_public_key()
37+
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
38+
subaccount_id = address.get_subaccount_id(index=0)
39+
dest_subaccount_id = "0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000"
40+
41+
# prepare tx msg
42+
msg = composer.MsgExternalTransfer(
43+
sender=address.to_acc_bech32(),
44+
source_subaccount_id=subaccount_id,
45+
destination_subaccount_id=dest_subaccount_id,
46+
amount=100,
47+
denom="inj"
48+
)
49+
50+
# build sim tx
51+
tx = (
52+
Transaction()
53+
.with_messages(msg)
54+
.with_sequence(address.get_sequence())
55+
.with_account_num(address.get_number())
56+
.with_chain_id(network.chain_id)
57+
)
58+
sim_sign_doc = tx.get_sign_doc(pub_key)
59+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
60+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
61+
62+
# simulate tx
63+
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
64+
if not success:
65+
print(sim_res)
66+
return
67+
68+
# build tx
69+
gas_price = 500000000
70+
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
71+
gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0')
72+
fee = [composer.Coin(
73+
amount=gas_price * gas_limit,
74+
denom=network.fee_denom,
75+
)]
76+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
77+
sign_doc = tx.get_sign_doc(pub_key)
78+
sig = priv_key.sign(sign_doc.SerializeToString())
79+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
80+
81+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
82+
res = await client.send_tx_sync_mode(tx_raw_bytes)
83+
print(res)
84+
print("gas wanted: {}".format(gas_limit))
85+
print("gas fee: {} INJ".format(gas_fee))
86+
87+
if __name__ == "__main__":
88+
logging.basicConfig(level=logging.INFO)
89+
asyncio.get_event_loop().run_until_complete(main())

pyinjective/composer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,22 @@ def MsgWithdraw(self, sender: str, subaccount_id: str, amount: float, denom: str
375375
amount=self.Coin(amount=be_amount, denom=peggy_denom),
376376
)
377377

378+
def MsgExternalTransfer(
379+
self,
380+
sender: str,
381+
source_subaccount_id: str,
382+
destination_subaccount_id: str,
383+
amount: int,
384+
denom: str,
385+
):
386+
387+
return injective_exchange_tx_pb.MsgExternalTransfer(
388+
sender=sender,
389+
source_subaccount_id=source_subaccount_id,
390+
destination_subaccount_id=destination_subaccount_id,
391+
amount=self.Coin(amount=amount, denom=denom),
392+
)
393+
378394
def MsgBid(self, sender: str, bid_amount: float, round: float):
379395

380396
be_amount = amount_to_backend(bid_amount, 18)

0 commit comments

Comments
 (0)