Skip to content

Commit

Permalink
refactor!: reduce name conflicts (#29)
Browse files Browse the repository at this point in the history
Co-authored-by: unparalleled-js <[email protected]>
  • Loading branch information
fubuloubu and antazoey authored Feb 18, 2023
1 parent d94b763 commit df56cfc
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 585 deletions.
39 changes: 19 additions & 20 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,22 @@ jobs:
- name: Run Tests
run: pytest -m "not fuzzing" -n 0 -s --cov

# NOTE: uncomment this block after you've marked tests with @pytest.mark.fuzzing
# fuzzing:
# runs-on: ubuntu-latest
#
# strategy:
# fail-fast: true
#
# steps:
# - uses: actions/checkout@v3
#
# - name: Setup Python
# uses: actions/setup-python@v4
# with:
# python-version: 3.8
#
# - name: Install Dependencies
# run: pip install .[test]
#
# - name: Run Tests
# run: pytest -m "fuzzing" --no-cov -s
fuzzing:
runs-on: ubuntu-latest

strategy:
fail-fast: true

steps:
- uses: actions/checkout@v3

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.8

- name: Install Dependencies
run: pip install .[test]

- name: Run Tests
run: pytest -m "fuzzing" --no-cov -s
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class Person(EIP712Type):


class Mail(EIP712Message):
_chainId_: "uint256" = 1
_name_: "string" = "Ether Mail"
_verifyingContract_: "address" = "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
_version_: "string" = "1"
_chainId_ = 1
_name_ = "Ether Mail"
_verifyingContract_ = "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
_version_ = "1"

sender: Person
receiver: Person
Expand Down
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
author = "ApeWorX Team"
extensions = [
"myst_parser",
"sphinx_click",
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"sphinx.ext.napoleon",
Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
:caption: User Guides
:maxdepth: 1
userguides/quickstart
userguides/quickstart
```

```{eval-rst}
.. toctree::
:caption: Python Reference
:maxdepth: 2
methoddocs/common.md
methoddocs/messages.md
methoddocs/validation.md
```
4 changes: 2 additions & 2 deletions docs/methoddocs/validation.md → docs/methoddocs/common.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Validation
# Common

```{eval-rst}
.. automodule:: eip712.validation
.. automodule:: eip712.common
:members:
:show-inheritance:
```
111 changes: 111 additions & 0 deletions eip712/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# flake8: noqa F821
# Collection of commonly-used EIP712 message type definitions
from typing import Optional, Type, Union

from .messages import EIP712Message


class EIP2612(EIP712Message):
# NOTE: Subclass this w/ at least one header field

owner: "address" # type: ignore
spender: "address" # type: ignore
value: "uint256" # type: ignore
nonce: "uint256" # type: ignore
deadline: "uint256" # type: ignore


class EIP4494(EIP712Message):
# NOTE: Subclass this w/ at least one header field

spender: "address" # type: ignore
tokenId: "uint256" # type: ignore
nonce: "uint256" # type: ignore
deadline: "uint256" # type: ignore


def create_permit_def(eip=2612, **header_fields):
if eip == 2612:

class Permit(EIP2612):
_name_ = header_fields.get("name", None)
_version_ = header_fields.get("version", None)
_chainId_ = header_fields.get("chainId", None)
_verifyingContract_ = header_fields.get("verifyingContract", None)
_salt_ = header_fields.get("salt", None)

elif eip == 4494:

class Permit(EIP4494):
_name_ = header_fields.get("name", None)
_version_ = header_fields.get("version", None)
_chainId_ = header_fields.get("chainId", None)
_verifyingContract_ = header_fields.get("verifyingContract", None)
_salt_ = header_fields.get("salt", None)

else:
raise ValueError(f"Invalid eip {eip}, must use one of: {EIP2612}, {EIP4494}")

return Permit


class SafeTxV1(EIP712Message):
# NOTE: Subclass this as `SafeTx` w/ at least one header field
to: "address" # type: ignore
value: "uint256" = 0 # type: ignore
data: "bytes" = b"" # type: ignore
operation: "uint8" = 0 # type: ignore
safeTxGas: "uint256" = 0 # type: ignore
dataGas: "uint256" = 0 # type: ignore
gasPrice: "uint256" = 0 # type: ignore
gasToken: "address" = "0x0000000000000000000000000000000000000000" # type: ignore
refundReceiver: "address" = "0x0000000000000000000000000000000000000000" # type: ignore
nonce: "uint256" # type: ignore


class SafeTxV2(EIP712Message):
# NOTE: Subclass this as `SafeTx` w/ at least one header field
to: "address" # type: ignore
value: "uint256" = 0 # type: ignore
data: "bytes" = b"" # type: ignore
operation: "uint8" = 0 # type: ignore
safeTxGas: "uint256" = 0 # type: ignore
baseGas: "uint256" = 0 # type: ignore
gasPrice: "uint256" = 0 # type: ignore
gasToken: "address" = "0x0000000000000000000000000000000000000000" # type: ignore
refundReceiver: "address" = "0x0000000000000000000000000000000000000000" # type: ignore
nonce: "uint256" # type: ignore


SafeTx = Union[SafeTxV1, SafeTxV2]
SAFE_VERSIONS = {"1.0.0", "1.1.0", "1.1.1", "1.2.0", "1.3.0"}


def create_safe_tx_def(
version: str = "1.3.0",
contract_address: Optional[str] = None,
chain_id: Optional[int] = None,
) -> Type[SafeTx]:
if not contract_address:
raise ValueError("Must define 'contract_address'")

if version not in SAFE_VERSIONS:
raise ValueError(f"Unknown version {version}")

major, minor, patch = map(int, version.split("."))

if minor < 3:

class SafeTx(SafeTxV1):
_verifyingContract_ = contract_address

elif not chain_id:
raise ValueError("Must supply 'chain_id=' for Safe versions 1.3.0 or later")

else:

class SafeTx(SafeTxV2): # type: ignore[no-redef]
_chainId_ = chain_id
_verifyingContract_ = contract_address

return SafeTx
Loading

0 comments on commit df56cfc

Please sign in to comment.