Skip to content

Commit

Permalink
Fixed terms_of_service dependency issues. Black formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Feb 2, 2024
1 parent 5d81e5f commit 0c7e97c
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 33 deletions.
2 changes: 0 additions & 2 deletions eth_defi/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ def deploy_contract(
return instance




def get_or_create_contract_registry(web3: Web3) -> ContractRegistry:
"""Get a contract registry associated with a Web3 connection.
Expand Down
3 changes: 1 addition & 2 deletions eth_defi/enzyme/generic_adapter_vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ def deploy_vault_with_generic_adapter(
logger.info("VaultUSDCPaymentForwarder is %s deployed at %s", payment_forwarder.address, tx_hash.hex())

if not mock_guard:

# When swap is performed, the tokens will land on the integration contract
# and this contract must be listed as the receiver.
# Enzyme will then internally move tokens to its vault from here.
Expand Down Expand Up @@ -297,7 +296,7 @@ def deploy_vault_with_generic_adapter(
match web3.eth.chain_id:
case 137:
uniswap_v3_router = UNISWAP_V3_DEPLOYMENTS["polygon"]["router"]
uniswap_v2_router= None
uniswap_v2_router = None
case 1:
uniswap_v2_router = UNISWAP_V2_DEPLOYMENTS["ethereum"]["router"]
uniswap_v3_router = UNISWAP_V3_DEPLOYMENTS["ethereum"]["router"]
Expand Down
2 changes: 1 addition & 1 deletion eth_defi/etherscan/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Etherscan integration.
Verify smart contracts on Etherscan, Polygonscan, BSCScan and other Etherscan software-as-a-service offerings.
"""
"""
2 changes: 1 addition & 1 deletion eth_defi/etherscan/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
Currently no Web3.py direct integration available.
Use :py:func:`eth_defi.foundry.forge.deploy_contract_with_forge` to deploy contracts that are verified on Etherscan.
"""
"""
2 changes: 1 addition & 1 deletion eth_defi/foundry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Forge smart contract development toolchain integration.
- Compile and deploy verified smart contracts
"""
"""
36 changes: 17 additions & 19 deletions eth_defi/foundry/forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#: Crash unless forge completes in 3 minutes
#:
DEFAULT_TIMEOUT = 3*60
DEFAULT_TIMEOUT = 3 * 60


class ForgeFailed(Exception):
Expand Down Expand Up @@ -208,28 +208,29 @@ def deploy_contract_with_forge(
cmd_line = [
forge,
"create",
"--rpc-url", json_rpc_url,
"--nonce", str(deployer.allocate_nonce()),
"--rpc-url",
json_rpc_url,
"--nonce",
str(deployer.allocate_nonce()),
]

if etherscan_api_key:
# Tuned retry parameters
# https://github.com/foundry-rs/foundry/issues/6953
cmd_line += [
"--etherscan-api-key", etherscan_api_key,
"--etherscan-api-key",
etherscan_api_key,
"--verify",
"--retries", "10",
"--delay", "30",
"--retries",
"10",
"--delay",
"30",
]

cmd_line += [
f"{src_contract_file}:{contract_name}"
]
cmd_line += [f"{src_contract_file}:{contract_name}"]

if constructor_args:
cmd_line += [
"--constructor-args"
]
cmd_line += ["--constructor-args"]
for arg in constructor_args:
cmd_line.append(arg)

Expand All @@ -245,7 +246,8 @@ def deploy_contract_with_forge(
cmd_line = [
forge,
"create",
"--private-key", deployer.private_key.hex(),
"--private-key",
deployer.private_key.hex(),
] + cmd_line[2:]

# Py 3.11 only
Expand All @@ -270,11 +272,7 @@ def deploy_contract_with_forge(

# Mad Web3.py API
contract_address = ChecksumAddress(HexAddress(HexStr(contract_address)))
instance = get_deployed_contract(
web3,
contract_abi,
contract_address
)
instance = get_deployed_contract(web3, contract_abi, contract_address)

if register_for_tracing:
instance.name = contract_name
Expand All @@ -287,4 +285,4 @@ def deploy_contract_with_forge(
RaisedException=ForgeFailed,
)

return instance, tx_hash
return instance, tx_hash
1 change: 1 addition & 0 deletions eth_defi/terms_of_service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Copied around due to Python packaging issues"""
63 changes: 63 additions & 0 deletions eth_defi/terms_of_service/acceptance_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""The default terms of service message that is signed.
- The terms of service itself is not signed as it is too big to fit to small input boxes of the wallet.
Instead, we sign a message that refers to the terms of service.
- Uses EIP-151
- See `EthAccout.sign_message implementation <https://github.com/ethereum/eth-account/blob/b4883627557839bed906c89b93eec0fbbb017ec5/eth_account/account.py#L535>`__.
"""
import datetime

from eth_account.datastructures import SignedMessage
from eth_account.messages import encode_defunct, _hash_eip191_message
from eth_account.signers.local import LocalAccount

DEFAULT_ACCEPTANCE_MESSAGE_TEMPLATE = """
I read and agree on terms of service (version {version}) to use
smart contract software deployed on a blockchain.
The terms of service text was published {human_date} at {link}.
The unique identifier hash for this terms of service tgext was {hash}.
""".strip()

INITIAL_ACCEPTANCE_MESSAGE = """
I read and agree on terms of service (version 1) to use
smart contract software deployed on a blockchain.
The terms of service text was published 10.1.2024 at https://example.com.
The unique identifier hash for this terms of service text was 0x0000000000000000000000000000000000000000.
""".strip()


def generate_acceptance_message(version: int, date: datetime.datetime, link: str, hash: bytes, template=DEFAULT_ACCEPTANCE_MESSAGE_TEMPLATE):
assert type(version) == int
assert type(link) == str
assert type(hash) == bytes
assert len(hash) == 32, "Must be 256-bit sha of terms of service file"
assert isinstance(date, datetime.datetime)
human_date = date.strftime("%Y-%m-%d")
return template.format(version=version, link=link, hash=hash.hex(), human_date=human_date)


def get_signing_hash(message: str) -> bytes:
assert type(message) == str
signable_message = encode_defunct(text=message)
hash = _hash_eip191_message(signable_message)
return hash


def sign_terms_of_service(
user: LocalAccount,
signable_message: str,
) -> tuple[bytes, bytes]:
"""Sign terms of service for a local dev test account.
:return:
Tuple (message hash, signed message)
"""

assert isinstance(user, LocalAccount)
message_hash = get_signing_hash(signable_message)
signed_message = user.signHash(message_hash)
# import ipdb ; ipdb.set_trace()
return message_hash, signed_message.signature
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ Sponsor = "https://tradingstrategy.ai"
[tool.poetry.dependencies]
python = ">=3.10,<3.13"

# Only soft dependency in tests, but RTD complains
terms-of-service = {path = "contracts/terms-of-service", develop = true}

# https://github.com/apache/arrow/pull/35412
# Last checked 2023-07, still broken
urllib3 = "<2"
Expand Down Expand Up @@ -67,6 +64,11 @@ sphinxcontrib-htmlhelp = {version = "2.0.1", optional = true} # Version pindowns
sphinxcontrib-serializinghtml = {version = "1.1.5", optional = true} # Version pindowns https://github.com/sphinx-doc/sphinxcontrib-serializinghtml/blob/master/CHANGES
sphinxcontrib-qthelp = {version = "1.0.3", optional = true} # Version pindowns https://github.com/sphinx-doc/sphinxcontrib-qthelp/blob/master/CHANGES


# Removed
# Only soft dependency in tests, but RTD complains
# terms-of-service = {path = "contracts/terms-of-service", develop = true}

[tool.poetry.dev-dependencies]
pytest = "^7.4.3"
pytest-mock = "^3.7.0"
Expand Down
3 changes: 2 additions & 1 deletion tests/enzyme/test_guard_enzyme_uniswap_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
from eth_account import Account
from eth_account.signers.local import LocalAccount
from eth_typing import HexAddress
from terms_of_service.acceptance_message import (
from eth_defi.terms_of_service.acceptance_message import (
generate_acceptance_message,
get_signing_hash,
sign_terms_of_service,
)

from web3 import Web3
from web3.contract import Contract

Expand Down
2 changes: 1 addition & 1 deletion tests/enzyme/test_guard_enzyme_uniswap_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from eth_account import Account
from eth_account.signers.local import LocalAccount
from eth_typing import ChecksumAddress, HexAddress
from terms_of_service.acceptance_message import (
from eth_defi.terms_of_service.acceptance_message import (
generate_acceptance_message,
get_signing_hash,
sign_terms_of_service,
Expand Down
3 changes: 1 addition & 2 deletions tests/rpc/test_forge_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def web3(anvil: AnvilLaunch):

@pytest.fixture
def deployer(web3) -> LocalAccount:
"""Create a priavte key with balance.
"""
"""Create a priavte key with balance."""
_deployer = web3.eth.accounts[0]
account: LocalAccount = Account.create()
stash = web3.eth.get_balance(_deployer)
Expand Down
1 change: 1 addition & 0 deletions tests/test_reorganisation_monitor_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Allow to override for a private node to run test faster
JSON_RPC_POLYGON = os.environ.get("JSON_RPC_POLYGON", "https://polygon-rpc.com")


@pytest.mark.skipif(os.environ.get("CI") is not None, reason="Too flaky to run on Github because public Polygon endpoint is crap")
def test_polygon_block_headers():
"""Polygon block header data is downloaded."""
Expand Down

0 comments on commit 0c7e97c

Please sign in to comment.