Skip to content

Commit 6a8694b

Browse files
Merge pull request #110 from InjectiveLabs/f/decouple_web3
Decouple SendToCosmos from the composer
2 parents 2d745ce + f52a439 commit 6a8694b

File tree

6 files changed

+66
-63
lines changed

6 files changed

+66
-63
lines changed

Pipfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pysha3 = "*"
1515
protobuf = "*"
1616
grpcio-tools = "*"
1717
bip32 = "*"
18-
web3 = "*"
1918
json = "*"
2019
requests = "*"
2120
eip712_structs = "*"

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ Note that the [sync client](https://github.com/InjectiveLabs/sdk-python/blob/mas
8484
* Add automatic session renewal for K8S
8585
* Add MsgDelegate and MsgWithdrawDelegatorReward in the composer
8686
* Add typed authz support in the composer
87+
* Decouple SendToCosmos from the composer and remove web3 dependency
8788
* Re-gen ini files
8889

90+
8991
**0.5.6.5**
9092
* Add MsgRelayPriceFeedPrice in the composer
9193
* Add Post-only orders in the composer

examples/SendToCosmos.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
import logging
66

77
from pyinjective.constant import Network
8-
from pyinjective.composer import Composer as ProtoMsgComposer
8+
from pyinjective.sendtocosmos import Peggo
99

1010
import importlib.resources as pkg_resources
1111
import pyinjective
1212

1313
async def main() -> None:
1414
# select network: testnet, mainnet
1515
network = Network.testnet()
16-
composer = ProtoMsgComposer(network=network.string())
16+
peggo_composer = Peggo(network=network.string())
1717

1818
private_key = "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3"
1919
ethereum_endpoint = "https://kovan.infura.io/v3/c518f454950e48aeab12161c49f26e30"
@@ -28,7 +28,7 @@ async def main() -> None:
2828
import_peggo = pkg_resources.read_text(pyinjective, 'Peggo_ABI.json')
2929
peggo_abi = json.loads(import_peggo)
3030

31-
composer.SendToCosmos(ethereum_endpoint=ethereum_endpoint, private_key=private_key, token_contract=token_contract,
31+
peggo_composer.SendToCosmos(ethereum_endpoint=ethereum_endpoint, private_key=private_key, token_contract=token_contract,
3232
receiver=receiver, amount=amount, maxFeePerGas=maxFeePerGas_Gwei, maxPriorityFeePerGas=maxPriorityFeePerGas_Gwei, peggo_abi=peggo_abi)
3333

3434
if __name__ == "__main__":

pyinjective/composer.py

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
from .utils import *
3030
from typing import List
3131

32-
from .wallet import Address
33-
34-
from web3 import Web3, HTTPProvider
35-
3632
class Composer:
3733
def __init__(self, network: str):
3834
self.network = network
@@ -538,60 +534,6 @@ def MsgWithdrawDelegatorReward(
538534
validator_address=validator_address
539535
)
540536

541-
def SendToCosmos(self, ethereum_endpoint: str, private_key: str, token_contract: str, receiver: str, amount: int, maxFeePerGas: int, maxPriorityFeePerGas: int, peggo_abi: str, decimals=18):
542-
if self.network == 'testnet':
543-
peggy_proxy_address = "0xd6Da9dA014806Fdb64bF39b48fcA386AE3420d21"
544-
elif self.network == 'mainnet':
545-
peggy_proxy_address = "0xF955C57f9EA9Dc8781965FEaE0b6A2acE2BAD6f3"
546-
else:
547-
print("SendToCosmos is only supported on Mainnet & Testnet")
548-
web3 = Web3(Web3.HTTPProvider(ethereum_endpoint))
549-
contract = web3.eth.contract(address=peggy_proxy_address, abi=peggo_abi)
550-
551-
token_contract_address = web3.toChecksumAddress(token_contract)
552-
553-
receiver_address = Address.from_acc_bech32(receiver)
554-
receiver_ethereum_address = Address.get_ethereum_address(receiver_address)
555-
receiver_address_checksum = web3.toChecksumAddress(receiver_ethereum_address)
556-
receiver_slice = receiver_address_checksum[2:]
557-
receiver_padded_address = '0x' + receiver_slice.zfill(64)
558-
559-
destination = web3.toBytes(hexstr=receiver_padded_address)
560-
561-
sender_ethereum_address = web3.eth.account.privateKeyToAccount(private_key).address
562-
sender_address_checksum = web3.toChecksumAddress(sender_ethereum_address)
563-
nonce = web3.eth.get_transaction_count(sender_address_checksum)
564-
565-
amount_to_send = int(amount * pow(10, decimals))
566-
567-
gas = contract.functions.sendToCosmos(
568-
token_contract_address,
569-
destination,
570-
amount_to_send,
571-
).estimateGas({'from': sender_address_checksum})
572-
573-
transaction_body = {
574-
'nonce': nonce,
575-
'gas': gas,
576-
'maxFeePerGas': web3.toWei(maxFeePerGas, 'gwei'),
577-
'maxPriorityFeePerGas': web3.toWei(maxPriorityFeePerGas, 'gwei'),
578-
}
579-
580-
tx = contract.functions.sendToCosmos(
581-
token_contract_address,
582-
destination,
583-
amount_to_send,
584-
).buildTransaction(transaction_body)
585-
586-
signed_tx = web3.eth.account.signTransaction(tx, private_key=private_key)
587-
588-
try:
589-
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
590-
print("Transferred {} {} from {} to {}".format(amount, token_contract, sender_ethereum_address, receiver))
591-
print("Transaction hash:", web3.toHex(tx_hash))
592-
except Exception as e:
593-
print("Transaction failed", e)
594-
595537
# data field format: [request-msg-header][raw-byte-msg-response]
596538
# you need to figure out this magic prefix number to trim request-msg-header off the data
597539
# this method handles only exchange responses

pyinjective/sendtocosmos.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from web3 import Web3
2+
3+
from .wallet import Address
4+
5+
class Peggo:
6+
def __init__(self, network: str):
7+
self.network = network
8+
def SendToCosmos(self, ethereum_endpoint: str, private_key: str, token_contract: str, receiver: str, amount: int,
9+
maxFeePerGas: int, maxPriorityFeePerGas: int, peggo_abi: str, decimals=18):
10+
if self.network == 'testnet':
11+
peggy_proxy_address = "0xd6Da9dA014806Fdb64bF39b48fcA386AE3420d21"
12+
elif self.network == 'mainnet':
13+
peggy_proxy_address = "0xF955C57f9EA9Dc8781965FEaE0b6A2acE2BAD6f3"
14+
else:
15+
print("SendToCosmos is only supported on Mainnet & Testnet")
16+
web3 = Web3(Web3.HTTPProvider(ethereum_endpoint))
17+
contract = web3.eth.contract(address=peggy_proxy_address, abi=peggo_abi)
18+
19+
token_contract_address = web3.toChecksumAddress(token_contract)
20+
21+
receiver_address = Address.from_acc_bech32(receiver)
22+
receiver_ethereum_address = Address.get_ethereum_address(receiver_address)
23+
receiver_address_checksum = web3.toChecksumAddress(receiver_ethereum_address)
24+
receiver_slice = receiver_address_checksum[2:]
25+
receiver_padded_address = '0x' + receiver_slice.zfill(64)
26+
27+
destination = web3.toBytes(hexstr=receiver_padded_address)
28+
29+
sender_ethereum_address = web3.eth.account.privateKeyToAccount(private_key).address
30+
sender_address_checksum = web3.toChecksumAddress(sender_ethereum_address)
31+
nonce = web3.eth.get_transaction_count(sender_address_checksum)
32+
33+
amount_to_send = int(amount * pow(10, decimals))
34+
35+
gas = contract.functions.sendToCosmos(
36+
token_contract_address,
37+
destination,
38+
amount_to_send,
39+
).estimateGas({'from': sender_address_checksum})
40+
41+
transaction_body = {
42+
'nonce': nonce,
43+
'gas': gas,
44+
'maxFeePerGas': web3.toWei(maxFeePerGas, 'gwei'),
45+
'maxPriorityFeePerGas': web3.toWei(maxPriorityFeePerGas, 'gwei'),
46+
}
47+
48+
tx = contract.functions.sendToCosmos(
49+
token_contract_address,
50+
destination,
51+
amount_to_send,
52+
).buildTransaction(transaction_body)
53+
54+
signed_tx = web3.eth.account.signTransaction(tx, private_key=private_key)
55+
56+
try:
57+
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
58+
print("Transferred {} {} from {} to {}".format(amount, token_contract, sender_ethereum_address, receiver))
59+
print("Transaction hash:", web3.toHex(tx_hash))
60+
except Exception as e:
61+
print("Transaction failed", e)

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"protobuf",
3232
"grpcio-tools",
3333
"bip32",
34-
"web3",
3534
"json",
3635
"requests",
3736
"eip712_structs",

0 commit comments

Comments
 (0)