From 3820bca894ef2a002a678156d42be4a0a02f0cf3 Mon Sep 17 00:00:00 2001 From: Uxio Fuentefria Date: Wed, 23 Mar 2022 11:22:05 +0100 Subject: [PATCH] Remove pyethereum dependency --- .github/workflows/python.yml | 2 +- docker-compose.dev.yml | 2 +- requirements.txt | 1 - safe_relay_service/relay/tasks.py | 14 ++++++-------- safe_relay_service/relay/tests/factories.py | 8 +++----- safe_relay_service/relay/tests/test_serializers.py | 7 +++++-- safe_relay_service/relay/tests/test_views_v2.py | 14 +++++++------- safe_relay_service/relay/tests/test_views_v3.py | 14 +++++++------- scripts/create_multiple_safes.py | 10 +++++----- 9 files changed, 35 insertions(+), 37 deletions(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index a0938caf..bcfa0e56 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -32,7 +32,7 @@ jobs: steps: - name: Setup and run ganache run: | - docker run --detach --publish 8545:8545 --network-alias ganache -e DOCKER=true trufflesuite/ganache-cli:latest --defaultBalanceEther 10000 --gasLimit 10000000 -a 30 --noVMErrorsOnRPCResponse --chainId 1337 --networkId 1337 -d + docker run --detach --publish 8545:8545 --network-alias ganache -e DOCKER=true trufflesuite/ganache:latest --defaultBalanceEther 10000 --gasLimit 10000000 -a 30 --chain.chainId 1337 --chain.networkId 1337 -d - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v3 diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 55fb260b..3bec02ee 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -30,6 +30,6 @@ services: ganache: image: trufflesuite/ganache-cli - command: -d --defaultBalanceEther 10000 -a 10 --noVMErrorsOnRPCResponse --gasLimit 10000000 --host 0.0.0.0 --chainId 1337 --networkId 1337 + command: -d --defaultBalanceEther 10000 --gasLimit 10000000 -a 30 --chain.chainId 1337 --chain.networkId 1337 ports: - "8545:8545" diff --git a/requirements.txt b/requirements.txt index 6e39c42f..bcf7eeff 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,7 +14,6 @@ djangorestframework==3.13.1 djangorestframework-camel-case==1.3.0 docutils==0.18.1 drf-yasg[validation]==1.20.0 -ethereum==2.3.2 gnosis-py[django]==3.9.0 gunicorn[gevent]==20.1.0 hexbytes==0.2.2 diff --git a/safe_relay_service/relay/tasks.py b/safe_relay_service/relay/tasks.py index c5847e61..2361caca 100644 --- a/safe_relay_service/relay/tasks.py +++ b/safe_relay_service/relay/tasks.py @@ -6,11 +6,12 @@ from celery import app from celery.utils.log import get_task_logger -from ethereum.utils import check_checksum, checksum_encode, mk_contract_address from redis.exceptions import LockError +from web3 import Web3 from gnosis.eth import EthereumClientProvider, TransactionAlreadyImported from gnosis.eth.constants import NULL_ADDRESS +from gnosis.eth.utils import mk_contract_address from safe_relay_service.gas_station.gas_station import GasStationProvider @@ -60,12 +61,9 @@ def fund_deployer_task(self, safe_address: str, retry: bool = True) -> None: payment = safe_creation.payment # These asserts just to make sure we are not wasting money - assert check_checksum(safe_address) - assert check_checksum(deployer_address) - assert ( - checksum_encode(mk_contract_address(sender=deployer_address, nonce=0)) - == safe_address - ) + assert Web3.isChecksumAddress(safe_address) + assert Web3.isChecksumAddress(deployer_address) + assert mk_contract_address(deployer_address, 0) == safe_address assert payment > 0 redis = RedisRepository().redis @@ -360,7 +358,7 @@ def deploy_create2_safe_task(self, safe_address: str, retry: bool = True) -> Non :param retry: if True, retries are allowed, otherwise don't retry """ - assert check_checksum(safe_address) + assert Web3.isChecksumAddress(safe_address) redis = RedisRepository().redis lock_name = f"locks:deploy_create2_safe:{safe_address}" diff --git a/safe_relay_service/relay/tests/factories.py b/safe_relay_service/relay/tests/factories.py index 98b498a2..4167c940 100644 --- a/safe_relay_service/relay/tests/factories.py +++ b/safe_relay_service/relay/tests/factories.py @@ -4,7 +4,6 @@ import factory.fuzzy from eth_account import Account -from ethereum.utils import checksum_encode, mk_contract_address from factory.django import DjangoModelFactory from hexbytes import HexBytes from web3 import Web3 @@ -19,6 +18,7 @@ SIGNATURE_V_MAX_VALUE, SIGNATURE_V_MIN_VALUE, ) +from gnosis.eth.utils import mk_contract_address from ..models import ( BannedSigner, @@ -52,7 +52,7 @@ class Meta: safe = factory.SubFactory( SafeContractFactory, address=factory.LazyAttribute( - lambda o: checksum_encode(mk_contract_address(o.factory_parent.deployer, 0)) + lambda o: mk_contract_address(o.factory_parent.deployer, 0) ), ) funder = factory.LazyFunction(lambda: Account.create().address) @@ -82,9 +82,7 @@ class Meta: safe = factory.SubFactory( SafeContractFactory, address=factory.LazyAttribute( - lambda o: checksum_encode( - mk_contract_address(o.factory_parent.proxy_factory, 0) - ) + lambda o: mk_contract_address(o.factory_parent.proxy_factory, 0) ), ) master_copy = factory.LazyFunction(lambda: Account.create().address) diff --git a/safe_relay_service/relay/tests/test_serializers.py b/safe_relay_service/relay/tests/test_serializers.py index 9b8d18ff..f8a5dd83 100644 --- a/safe_relay_service/relay/tests/test_serializers.py +++ b/safe_relay_service/relay/tests/test_serializers.py @@ -2,7 +2,6 @@ from django.test import TestCase from eth_account import Account -from ethereum.transactions import secpk1n from faker import Faker from hexbytes import HexBytes @@ -25,8 +24,12 @@ class TestSerializers(TestCase): + SECPK1N = ( + 115792089237316195423570985008687907852837564279074904382605163141518161494337 + ) + def test_safe_creation_serializer(self): - s = secpk1n // 2 + s = self.SECPK1N // 2 owners = [Account.create().address for _ in range(3)] invalid_checksumed_address = get_eth_address_with_invalid_checksum() diff --git a/safe_relay_service/relay/tests/test_views_v2.py b/safe_relay_service/relay/tests/test_views_v2.py index 3c3b8da7..496946e9 100644 --- a/safe_relay_service/relay/tests/test_views_v2.py +++ b/safe_relay_service/relay/tests/test_views_v2.py @@ -4,10 +4,10 @@ from django.urls import reverse from eth_account import Account -from ethereum.utils import check_checksum from faker import Faker from rest_framework import status from rest_framework.test import APITestCase +from web3 import Web3 from gnosis.eth.constants import NULL_ADDRESS from gnosis.eth.utils import get_eth_address_with_invalid_checksum @@ -68,8 +68,8 @@ def test_safe_creation(self): self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json["safe"] - self.assertTrue(check_checksum(safe_address)) - self.assertTrue(check_checksum(response_json["paymentReceiver"])) + self.assertTrue(Web3.isChecksumAddress(safe_address)) + self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"])) self.assertEqual(response_json["paymentToken"], NULL_ADDRESS) self.assertEqual( int(response_json["payment"]), @@ -129,8 +129,8 @@ def test_safe_creation_with_fixed_cost(self): self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json["safe"] - self.assertTrue(check_checksum(safe_address)) - self.assertTrue(check_checksum(response_json["paymentReceiver"])) + self.assertTrue(Web3.isChecksumAddress(safe_address)) + self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"])) self.assertEqual(response_json["paymentToken"], NULL_ADDRESS) self.assertEqual(response_json["payment"], str(fixed_creation_cost)) self.assertGreater(int(response_json["gasEstimated"]), 0) @@ -164,8 +164,8 @@ def test_safe_creation_with_payment_token(self): self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json["safe"] - self.assertTrue(check_checksum(safe_address)) - self.assertTrue(check_checksum(response_json["paymentReceiver"])) + self.assertTrue(Web3.isChecksumAddress(safe_address)) + self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"])) self.assertEqual(response_json["paymentToken"], payment_token) self.assertEqual( int(response_json["payment"]), diff --git a/safe_relay_service/relay/tests/test_views_v3.py b/safe_relay_service/relay/tests/test_views_v3.py index a60b2027..954f2b0a 100644 --- a/safe_relay_service/relay/tests/test_views_v3.py +++ b/safe_relay_service/relay/tests/test_views_v3.py @@ -4,10 +4,10 @@ from django.urls import reverse from eth_account import Account -from ethereum.utils import check_checksum from faker import Faker from rest_framework import status from rest_framework.test import APITestCase +from web3 import Web3 from gnosis.eth.constants import NULL_ADDRESS from gnosis.safe import Safe @@ -66,8 +66,8 @@ def test_safe_creation(self): self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json["safe"] - self.assertTrue(check_checksum(safe_address)) - self.assertTrue(check_checksum(response_json["paymentReceiver"])) + self.assertTrue(Web3.isChecksumAddress(safe_address)) + self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"])) self.assertEqual(response_json["paymentToken"], NULL_ADDRESS) self.assertEqual( int(response_json["payment"]), @@ -125,8 +125,8 @@ def test_safe_creation_with_fixed_cost(self): self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json["safe"] - self.assertTrue(check_checksum(safe_address)) - self.assertTrue(check_checksum(response_json["paymentReceiver"])) + self.assertTrue(Web3.isChecksumAddress(safe_address)) + self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"])) self.assertEqual(response_json["paymentToken"], NULL_ADDRESS) self.assertEqual(response_json["payment"], str(fixed_creation_cost)) self.assertGreater(int(response_json["gasEstimated"]), 0) @@ -160,8 +160,8 @@ def test_safe_creation_with_payment_token(self): self.assertEqual(response.status_code, status.HTTP_201_CREATED) response_json = response.json() safe_address = response_json["safe"] - self.assertTrue(check_checksum(safe_address)) - self.assertTrue(check_checksum(response_json["paymentReceiver"])) + self.assertTrue(Web3.isChecksumAddress(safe_address)) + self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"])) self.assertEqual(response_json["paymentToken"], payment_token) self.assertEqual( int(response_json["payment"]), diff --git a/scripts/create_multiple_safes.py b/scripts/create_multiple_safes.py index b9833212..f7f4822d 100644 --- a/scripts/create_multiple_safes.py +++ b/scripts/create_multiple_safes.py @@ -3,7 +3,7 @@ from urllib.parse import urljoin import requests -from ethereum.utils import checksum_encode, privtoaddr +from eth_account import Account from web3 import HTTPProvider, Web3 from gnosis.safe.tests.utils import generate_valid_s @@ -58,9 +58,9 @@ def notify_safes(): def deploy_safes(number, owners, private_key): + funder = Account.from_key(private_key) safes = [] - funding_public_key = checksum_encode(privtoaddr(private_key)) - funding_nonce = w3.eth.getTransactionCount(funding_public_key, "pending") + funder_nonce = w3.eth.getTransactionCount(funder.address, "pending") for _ in range(number): payload_json = generate_payload(owners) r = requests.post(SAFES_URL, json=payload_json) @@ -70,11 +70,11 @@ def deploy_safes(number, owners, private_key): payment = int(safe_created["payment"]) logging.info("Created safe=%s, need payment=%d", safe_address, payment) - send_eth(private_key, safe_address, payment, funding_nonce) + send_eth(private_key, safe_address, payment, funder_nonce) logging.info("Sent payment=%s to safe=%s", payment, safe_address) r = requests.put(get_safes_notify_url(safe_address)) assert r.ok - funding_nonce += 1 + funder_nonce += 1 safes.append(safe_address) with open("safes.txt", mode="a") as safes_file: