Skip to content

Commit

Permalink
Merge pull request crypto-org-chain#40 from macong-cdc/refactor
Browse files Browse the repository at this point in the history
Problem: unneeded files in the repo(fix crypto-org-chain#23)
  • Loading branch information
linfeng-crypto authored Feb 7, 2022
2 parents 379fa24 + 1afd256 commit c4f7c12
Show file tree
Hide file tree
Showing 24 changed files with 984 additions and 358 deletions.
17 changes: 0 additions & 17 deletions .bumpversion.cfg

This file was deleted.

4 changes: 0 additions & 4 deletions .coveragerc

This file was deleted.

16 changes: 16 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
[mypy]
# NOTE!
# exclude, files config does not work for https://github.com/pre-commit/mirrors-mypy configuration
exclude = (?x)(
example/
| chainlibpy/generated/
| chainlibpy/amino/ # TODO to fix type errors in this directory
) # leave two spaces before ")" to prevent parsing error
warn_unreachable = True
warn_unused_ignores = True
warn_redundant_casts = True
Expand All @@ -11,6 +18,12 @@ strict_equality = True
implicit_reexport = False
no_implicit_optional = True

[mypy-chainlibpy.generated.*]
ignore_missing_imports = True

[mypy-pystarport.*]
ignore_missing_imports = True

[mypy-tests.*]
disallow_untyped_defs = False

Expand All @@ -19,3 +32,6 @@ ignore_missing_imports = True

[mypy-mnemonic.*]
ignore_missing_imports = True

[mypy-bech32.*]
ignore_missing_imports = True
56 changes: 40 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,48 +1,72 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 9136088a246768144165fcc3ecc3d31bb686920a # frozen: v3.3.0
rev: v4.1.0
hooks:
- id: check-yaml
- id: check-toml

- repo: https://github.com/pre-commit/pygrep-hooks
rev: 4f4c0a4cda27980be153cca2cb7710c9fec57ba3 # frozen: v1.7.0
rev: v1.9.0
hooks:
- id: python-use-type-annotations
- id: python-check-blanket-noqa
- repo: https://github.com/timothycrosley/isort
rev: 6bb47b7acc1554ecb59d2855e9110c447162f674 # frozen: 5.6.4

- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: e66be67b9b6811913470f70c28b4d50f94d05b22 # frozen: 20.8b1
rev: 21.12b0
hooks:
- id: black
exclude: ^chainlibpy/generated/

- repo: https://github.com/pre-commit/mirrors-mypy
rev: f3bfcb5479b4fa73b3fbb95a6390420575f20b51 # frozen: v0.790
rev: v0.931
hooks:
- id: mypy
args:
# Suppress errors resulting from no access to dependencies
- --ignore-missing-imports
- --no-warn-unused-ignores
# Allow multiple scripts (no .py postfix in name) to be checked in a single mypy invocation
- --scripts-are-modules
# NOTE: this hook does NOT read "files" and "exclude" configs from mypy configuration files
files: ^chainlibpy/
exclude: ^chainlibpy/(generated/|amino/) # TODO to fix type errors in amino directory
# NOTE: need to add additional_dependencies explicitly
additional_dependencies:
- grpc-stubs==1.24.7
- types-PyYAML==6.0.4
- types-protobuf==3.19.8
- types-requests==2.27.8
- types-toml==0.10.3

- repo: https://gitlab.com/pycqa/flake8
rev: bb6a530e28acab8d3551043b3e8709db8bcbac6b # frozen: 3.8.4
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
- flake8-builtins
# TODO FIX chainlibpy/amino/message.py:284:5: A003 class attribute "id" is shadowing a python builtin
# when enable flake8-builtins option
# - flake8-builtins
- flake8-comprehensions

- repo: https://github.com/myint/docformatter
rev: de0bf8fa254d25a01383fecdb6335bea01daeae3 # frozen: v1.3.1
rev: v1.4
hooks:
- id: docformatter
args:
- ./chainlibpy
- --recursive
- --in-place
- --exclude
- chainlibpy/generated

- repo: https://github.com/executablebooks/mdformat
rev: 492440cdb4f3ca87eff24dea09c85881b0e5d597 # frozen: 0.4.0
rev: 0.7.13
hooks:
- id: mdformat
args:
- CHANGELOG.md
- CONTRIBUTING.md
- README.md
additional_dependencies:
- mdformat-black
- mdformat-toc
15 changes: 9 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ This log documents all public API breaking backwards incompatible changes.

## 2.2.0 - to be released

[#32](https://github.com/crypto-org-chain/chainlibpy/issues/32) Add test environment\
Require Python >= 3.8\
[#25](https://github.com/crypto-org-chain/chainlibpy/issues/25) Add mainnet and testnet-croeseid-4 network configurations\
[#24](https://github.com/crypto-org-chain/chainlibpy/pull/24) Fix unable to use secure gRPC channel to interact with chain
[#26](https://github.com/crypto-org-chain/chainlibpy/issues/26) [#27](https://github.com/crypto-org-chain/chainlibpy/issues/27) [#28](https://github.com/crypto-org-chain/chainlibpy/issues/28) Refactor protobuf message to class to add more functionalities and hide protobuf complexity

[#32](https://github.com/crypto-org-chain/chainlibpy/issues/32) Add test environment

Require Python >= 3.8

*Dec 7, 2021*
[#25](https://github.com/crypto-org-chain/chainlibpy/issues/25) Add mainnet and testnet-croeseid-4 network configurations

[#24](https://github.com/crypto-org-chain/chainlibpy/pull/24) Fix unable to use secure gRPC channel to interact with chain

## 2.1.0
## 2.1.0 - 7/Dec/2021

[#28](https://github.com/crypto-org-chain/chainlibpy/pull/21) Migrate to gRPC which supports `chain-main` using `Cosmos SDK` version v0.43/0.44

Expand Down
71 changes: 36 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
- [Usage](#usage)
- [Generating a wallet](#generating-a-wallet)
- [Signing and broadcasting a transaction](#signing-and-broadcasting-a-transaction)
- [Using secure gRPC channel](#using-secure-grpc-channel)
- [Interact with mainnet or testnet](#interact-with-mainnet-or-testnet)
- [Acknowledgement](#acknowledgement)
- [Development](#development)
- [Set up development environment](#set-up-development-environment)
- [Add pre-commit git hook](#add-pre-commit-git-hook)
- [Generate gRPC code](#generate-grpc-code)
- [Tox](#tox)

Expand Down Expand Up @@ -50,37 +51,21 @@ print(wallet.address)

### Signing and broadcasting a transaction<a name="signing-and-broadcasting-a-transaction"></a>

```python
from chainlibpy.generated.cosmos.base.v1beta1.coin_pb2 import Coin
from chainlibpy.grpc_client import GrpcClient
from chainlibpy.transaction import sign_transaction
from chainlibpy.wallet import Wallet

# Refer to example/transaction.py for how to obtain CONSTANT values below
DENOM = "basecro"
MNEMONIC_PHRASE = "first ... last"
TO_ADDRESS = "cro...add"
AMOUNT = [Coin(amount="10000", denom=DENOM)]
CHAIN_ID = "chainmaind"
GRPC_ENDPOINT = "0.0.0.0:26653"

wallet = Wallet(MNEMONIC_PHRASE)
client = GrpcClient(wallet, CHAIN_ID, GRPC_ENDPOINT)

from_address = wallet.address
account_number = client.query_account_data(wallet.address).account_number

msg = client.get_packed_send_msg(wallet.address, TO_ADDRESS, AMOUNT)
tx = client.generate_tx([msg], [wallet.address], [wallet.public_key])
sign_transaction(tx, wallet.private_key, CHAIN_ID, account_number)
client.broadcast_tx(tx)
```
Please refer to `example/transaction.py` for how to start a local testnet with `pystarport` and change information below to run the examples successfully.

You may also refer to `example/transaction.py` on how to use a high level function `bank_send()` to sign and broadcast a transaction
```diff
# Obtained from {directory_started_pystarport}/data/chainmaind/accounts.json
# To recover one of the genesis account
- MNEMONIC_PHRASE = "first ... last"
+ MNEMONIC_PHRASE = "REMEMBER TO CHANGE"
# Obtained from {directory_started_pystarport}/data/chainmaind/accounts.json
- TO_ADDRESS = "cro...add"
+ TO_ADDRESS = "REMEMBER TO CHANGE"
```

### Using secure gRPC channel<a name="using-secure-grpc-channel"></a>
### Interact with mainnet or testnet<a name="interact-with-mainnet-or-testnet"></a>

Please refer to `example/secure_channel_example.py` on how to use secure gRPC channel with server certificate
Please refer to `example/secure_channel_example.py` on how to use secure gRPC channel with server certificate to interact with mainnet or testnet.

## Acknowledgement<a name="acknowledgement"></a>

Expand All @@ -98,12 +83,20 @@ Thanks to [eth-utils](https://github.com/ethereum/eth-utils) for the following:

### Set up development environment<a name="set-up-development-environment"></a>

More about [poetry](https://python-poetry.org/docs/).
Run command below to install dependencies (more about [poetry](https://python-poetry.org/docs/)):

```bash
poetry install
```

### Add pre-commit git hook<a name="add-pre-commit-git-hook"></a>

To set up the git hook scripts, so that [`pre-commit`](https://pre-commit.com/) will run automatically on `git commit`:

```bash
pre-commit install
```

### Generate gRPC code<a name="generate-grpc-code"></a>

```bash
Expand All @@ -117,29 +110,37 @@ poetry shell
./generated_protos.sh -COSMOS_REF=v0.44.5
```

If more generated gRPC code is needed in the future, please add the `.proto` files needed here in `generated_protos.sh`:
If more generated gRPC code is needed in the future, please add the path to `.proto` file needed here in `generated_protos.sh`:

```bash
```diff
# Add .proto files here to generate respective gRPC code
PROTO_FILES="
$COSMOS_SDK_DIR/proto/cosmos/auth/v1beta1/auth.proto
+$COSMOS_SDK_DIR/proto/other.proto
...
```

### Tox<a name="tox"></a>

[Tox](https://tox.wiki/en/latest/) is a tool to automate and standardize testing processes in Python.

For this project, the list of environment that will be run when invoking `tox` command is `py{38,39}`. Hence we need to set up Python 3.8 and 3.9 for this project. Run command below to set a local application-specific Python version (in this case 3.8 and 3.9) with [pyenv](https://github.com/pyenv/pyenv):

```bash
pyenv local 3.8.a 3.9.b
```

`a` and `b` are python versions installed on your computer by `pyenv`. More about [pyenv](https://github.com/pyenv/pyenv).
**Note:** `a` and `b` are python versions installed on your computer by `pyenv`.

After running command above, a `.python-version` file will be generated, which means python versions inside `.python-version` are presented for this project. Now, running command `tox` should succeed without prompting environment missing error.

After this command, a `.python-version` file will be generated at project root directory, which means python versions inside `.python-version` are presented for this project. So running `tox` command with `py{38,39}` configuration should succeed.\
Then run to verify. Command below is recommended to run before pushing a commit.
Run command below to verify:

```bash
poetry run tox
# or
poetry shell
tox
```

It is also recommended to run `tox` command before pushing a commit.
17 changes: 14 additions & 3 deletions chainlibpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
from .cro_coin import MAX_CRO_SUPPLY, CROCoin # noqa: F401
from .grpc_client import CRO_NETWORK, GrpcClient, NetworkConfig # noqa: F401
from .wallet import Wallet # noqa: F401
from .cro_coin import MAX_CRO_SUPPLY, CROCoin
from .grpc_client import CRO_NETWORK, GrpcClient, NetworkConfig
from .transaction import Transaction
from .wallet import Wallet

__all__ = [
"CROCoin",
"MAX_CRO_SUPPLY",
"CRO_NETWORK",
"GrpcClient",
"NetworkConfig",
"Transaction",
"Wallet",
]
Loading

0 comments on commit c4f7c12

Please sign in to comment.