Skip to content

Commit

Permalink
(feat) Added Poetry to the proyect and the pyproject.toml configurati…
Browse files Browse the repository at this point in the history
…on file. Added pre-commit. Added the use of Flake8 through Flakeheaven.
  • Loading branch information
abel committed Sep 19, 2023
1 parent 8e63e6e commit 8698291
Show file tree
Hide file tree
Showing 144 changed files with 2,806 additions and 504 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
python setup.py sdist bdist_wheel
twine upload --skip-existing dist/*
twine upload --skip-existing dist/*
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,5 @@ cython_debug/

.chain_cookie
.exchange_cookie

.flakeheaven_cache
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
exclude: '^pyinjective/proto/.*'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/flakeheaven/flakeheaven
rev: 3.3.0
hooks:
- id: flakeheaven
name: flakeheaven
description: '`flakeheaven` is a `flake8` wrapper.'
entry: flakeheaven lint
language: python
types_or: [ python, jupyter, markdown, rst, yaml ]
require_serial: true
minimum_pre_commit_version: 2.9.0
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,4 @@ Copyright © 2021 - 2022 Injective Labs Inc. (https://injectivelabs.org/)
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
2 changes: 1 addition & 1 deletion NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Copyright © 2021 - 2022 Injective Labs Inc. (https://injectivelabs.org/)
Originally released by Injective Labs Inc. under: <br />
Apache License <br />
Version 2.0, January 2004 <br />
http://www.apache.org/licenses/
http://www.apache.org/licenses/
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,4 @@ Copyright © 2021 - 2022 Injective Labs Inc. (https://injectivelabs.org/)
Originally released by Injective Labs Inc. under: <br />
Apache License <br />
Version 2.0, January 2004 <br />
http://www.apache.org/licenses/

http://www.apache.org/licenses/
2 changes: 1 addition & 1 deletion compatibility-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ API users must also install the typing_extensions library with the below command

```bash
pip3 install typing_extensions
```
```

The Python 3.7 stdlib has an older version of typing.py so the above change in the code will fix the compatibility issue by importing Literal from typing_extensions
17 changes: 9 additions & 8 deletions compatibility-tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import asyncio
import aiohttp
import logging
import json
import base64
import hashlib
import json
import ecdsa
import sha3
import grpc

from typing import Any, Dict, List
from injective.chain_client._wallet import (
generate_wallet,
privkey_to_address,
privkey_to_pubkey,
pubkey_to_address,
seed_to_privkey,
DEFAULT_BECH32_HRP,
)
Expand All @@ -25,6 +20,7 @@

MIN_GAS_PRICE = 500000000


class Transaction:

def __init__(
Expand Down Expand Up @@ -130,6 +126,7 @@ def _get_sign_message(self) -> Dict[str, Any]:
"msgs": self._msgs,
}


async def main() -> None:
sender_pk = seed_to_privkey(
"physical page glare junk return scale subject river token door mirror title"
Expand All @@ -143,7 +140,9 @@ async def main() -> None:
accounts_rpc = accounts_rpc_grpc.InjectiveAccountsRPCStub(channel)
account_addr = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku"

subacc = await accounts_rpc.SubaccountsList(accounts_rpc_pb.SubaccountsListRequest(account_address = account_addr))
subacc = await accounts_rpc.SubaccountsList(
accounts_rpc_pb.SubaccountsListRequest(account_address=account_addr)
)
for sub in subacc.subaccounts:
print("Primary subaccount:", sub)
break
Expand All @@ -162,7 +161,7 @@ async def main() -> None:
denom="inj",
)
tx.add_exchange_msg_deposit(
subaccount= sub,
subaccount=sub,
amount=10000000000000000,
denom="inj",
)
Expand All @@ -172,6 +171,7 @@ async def main() -> None:
print("Signed Tx:", tx_json)
print("Sent Tx:", await post_tx(tx_json))


async def get_account_num_seq(address: str) -> (int, int):
async with aiohttp.ClientSession() as session:
async with session.request(
Expand All @@ -186,6 +186,7 @@ async def get_account_num_seq(address: str) -> (int, int):
acc = resp['account']['base_account']
return acc['account_number'], acc['sequence']


async def post_tx(tx_json: str):
async with aiohttp.ClientSession() as session:
async with session.request(
Expand All @@ -204,4 +205,4 @@ async def post_tx(tx_json: str):
return resp['txhash']

if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
asyncio.get_event_loop().run_until_complete(main())
15 changes: 9 additions & 6 deletions compatibility-tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

from typing import Any, Dict, List
from injective.chain_client._wallet import (
generate_wallet,
privkey_to_address,
privkey_to_pubkey,
pubkey_to_address,
seed_to_privkey,
DEFAULT_BECH32_HRP,
)
Expand All @@ -22,6 +20,7 @@

MIN_GAS_PRICE = 500000000


class Transaction:

def __init__(
Expand Down Expand Up @@ -50,7 +49,6 @@ def __init__(
self._sync_mode = sync_mode
self._msgs: List[dict] = []


def add_cosmos_bank_msg_send(self, recipient: str, amount: int, denom: str = "inj") -> None:
msg = {
"type": "cosmos-sdk/MsgSend",
Expand Down Expand Up @@ -141,6 +139,7 @@ async def get_account_num_seq(address: str) -> (int, int):
acc = resp['account']['base_account']
return acc['account_number'], acc['sequence']


async def post_tx(tx_json: str):
async with aiohttp.ClientSession() as session:
async with session.request(
Expand All @@ -158,6 +157,7 @@ async def post_tx(tx_json: str):

return resp['txhash']


@pytest.fixture
async def msg_send():
sender_pk = seed_to_privkey(
Expand All @@ -172,7 +172,9 @@ async def msg_send():
accounts_rpc = accounts_rpc_grpc.InjectiveAccountsRPCStub(channel)
account_addr = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku"

subacc = await accounts_rpc.SubaccountsList(accounts_rpc_pb.SubaccountsListRequest(account_address = account_addr))
subacc = await accounts_rpc.SubaccountsList(
accounts_rpc_pb.SubaccountsListRequest(account_address=account_addr)
)
for sub in subacc.subaccounts:
print("Primary subaccount:", sub)
break
Expand All @@ -191,7 +193,7 @@ async def msg_send():
denom="inj",
)
tx.add_exchange_msg_deposit(
subaccount= sub,
subaccount=sub,
amount=10000000000000000,
denom="inj",
)
Expand All @@ -204,6 +206,7 @@ async def msg_send():

return len(tx_result)


@pytest.mark.asyncio
async def test_msg_send(msg_send):
assert msg_send == 64
assert msg_send == 64
22 changes: 17 additions & 5 deletions examples/SendToInjective.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import json
import requests

import asyncio
import logging

from pyinjective.core.network import Network
from pyinjective.sendtocosmos import Peggo

import importlib.resources as pkg_resources
import pyinjective


async def main() -> None:
# select network: testnet, mainnet
network = Network.testnet()
Expand All @@ -25,13 +24,26 @@ async def main() -> None:
receiver = "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku"
amount = 1

data = '{"@type": "/injective.exchange.v1beta1.MsgDeposit","sender": "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku","subaccountId": "0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000","amount": {"denom": "inj","amount": "1000000000000000000"}}'
data = ('{"@type": "/injective.exchange.v1beta1.MsgDeposit",'
'"sender": "inj14au322k9munkmx5wrchz9q30juf5wjgz2cfqku",'
'"subaccountId": "0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000",'
'"amount": {"denom": "inj","amount": "1000000000000000000"}}'
)

import_peggo = pkg_resources.read_text(pyinjective, 'Peggo_ABI.json')
peggo_abi = json.loads(import_peggo)

peggo_composer.sendToInjective(ethereum_endpoint=ethereum_endpoint, private_key=private_key, token_contract=token_contract,
receiver=receiver, amount=amount, maxFeePerGas=maxFeePerGas_Gwei, maxPriorityFeePerGas=maxPriorityFeePerGas_Gwei, data=data, peggo_abi=peggo_abi)
peggo_composer.sendToInjective(
ethereum_endpoint=ethereum_endpoint,
private_key=private_key,
token_contract=token_contract,
receiver=receiver,
amount=amount,
maxFeePerGas=maxFeePerGas_Gwei,
maxPriorityFeePerGas=maxPriorityFeePerGas_Gwei,
data=data,
peggo_abi=peggo_abi
)

if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
19 changes: 12 additions & 7 deletions examples/chain_client/0_LocalOrderHash.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import asyncio

from pyinjective.async_client import AsyncClient
from pyinjective.composer import Composer
from pyinjective.transaction import Transaction
from pyinjective.core.network import Network
from pyinjective.wallet import PrivateKey
from pyinjective.orderhash import OrderHashManager


async def main() -> None:
# select network: local, testnet, mainnet
network = Network.testnet()
Expand All @@ -20,14 +20,14 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
account = await client.get_account(address.to_acc_bech32())
await client.get_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)
subaccount_id_2 = address.get_subaccount_id(index=1)

order_hash_manager = OrderHashManager(
address=address,
network=network,
subaccount_indexes=[0,1,2,7]
subaccount_indexes=[0, 1, 2, 7]
)

# prepare trade info
Expand Down Expand Up @@ -91,7 +91,9 @@ async def main() -> None:
)

# compute order hashes
order_hashes = order_hash_manager.compute_order_hashes(spot_orders=spot_orders, derivative_orders=derivative_orders, subaccount_index=0)
order_hashes = order_hash_manager.compute_order_hashes(
spot_orders=spot_orders, derivative_orders=derivative_orders, subaccount_index=0
)

print("computed spot order hashes", order_hashes.spot)
print("computed derivative order hashes", order_hashes.derivative)
Expand Down Expand Up @@ -123,9 +125,10 @@ async def main() -> None:
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))


# compute order hashes
order_hashes = order_hash_manager.compute_order_hashes(spot_orders=spot_orders, derivative_orders=derivative_orders, subaccount_index=0)
order_hashes = order_hash_manager.compute_order_hashes(
spot_orders=spot_orders, derivative_orders=derivative_orders, subaccount_index=0
)

print("computed spot order hashes", order_hashes.spot)
print("computed derivative order hashes", order_hashes.derivative)
Expand Down Expand Up @@ -213,7 +216,9 @@ async def main() -> None:
)

# compute order hashes
order_hashes = order_hash_manager.compute_order_hashes(spot_orders=spot_orders, derivative_orders=derivative_orders, subaccount_index=1)
order_hashes = order_hash_manager.compute_order_hashes(
spot_orders=spot_orders, derivative_orders=derivative_orders, subaccount_index=1
)

print("computed spot order hashes", order_hashes.spot)
print("computed derivative order hashes", order_hashes.derivative)
Expand Down
2 changes: 1 addition & 1 deletion examples/chain_client/13_MsgIncreasePositionMargin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
account = await client.get_account(address.to_acc_bech32())
await client.get_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)

# prepare trade info
Expand Down
2 changes: 1 addition & 1 deletion examples/chain_client/15_MsgWithdraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
account = await client.get_account(address.to_acc_bech32())
await client.get_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)

# prepare tx msg
Expand Down
2 changes: 1 addition & 1 deletion examples/chain_client/16_MsgSubaccountTransfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
account = await client.get_account(address.to_acc_bech32())
await client.get_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)
dest_subaccount_id = address.get_subaccount_id(index=1)

Expand Down
4 changes: 2 additions & 2 deletions examples/chain_client/17_MsgBatchUpdateOrders.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
account = await client.get_account(address.to_acc_bech32())
await client.get_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)

# prepare trade info
Expand Down Expand Up @@ -136,7 +136,7 @@ async def main() -> None:

# build tx
gas_price = 500000000
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation
gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0')
fee = [composer.Coin(
amount=gas_price * gas_limit,
Expand Down
3 changes: 2 additions & 1 deletion examples/chain_client/18_MsgBid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pyinjective.core.network import Network
from pyinjective.wallet import PrivateKey


async def main() -> None:
# select network: local, testnet, mainnet
network = Network.testnet()
Expand All @@ -18,7 +19,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
account = await client.get_account(address.to_acc_bech32())
await client.get_account(address.to_acc_bech32())

# prepare tx msg
msg = composer.MsgBid(
Expand Down
Loading

0 comments on commit 8698291

Please sign in to comment.