Skip to content

Commit

Permalink
Fix use web3py v6
Browse files Browse the repository at this point in the history
  • Loading branch information
llunaCreixent committed Nov 15, 2023
1 parent c1e01af commit 401b8fb
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 73 deletions.
22 changes: 11 additions & 11 deletions safe_relay_service/gas_station/gas_station.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __new__(cls):
EthereumClientProvider(), settings.GAS_STATION_NUMBER_BLOCKS
)
w3 = cls.instance.w3
if w3.isConnected() and int(w3.net.version) > 314158: # Ganache
if w3.is_connected() and int(w3.net.version) > 314158: # Ganache
logger.warning(
"Using mock Gas Station because no `w3.net.version` was detected"
)
Expand Down Expand Up @@ -170,18 +170,18 @@ def get_gas_prices(self) -> GasPrice:
class GasStationMock(GasStation):
def __init__(self, gas_price: Optional[int] = None):
if gas_price is None:
self.lowest = Web3.toWei(1, "gwei")
self.safe_low = Web3.toWei(5, "gwei")
self.standard = Web3.toWei(10, "gwei")
self.fast = Web3.toWei(20, "gwei")
self.fastest = Web3.toWei(50, "gwei")
self.lowest = Web3.to_wei(1, "gwei")
self.safe_low = Web3.to_wei(5, "gwei")
self.standard = Web3.to_wei(10, "gwei")
self.fast = Web3.to_wei(20, "gwei")
self.fastest = Web3.to_wei(50, "gwei")
else:
# Set all gas prices to the same (for running on xDai)
self.lowest = Web3.toWei(gas_price, "gwei")
self.safe_low = Web3.toWei(gas_price, "gwei")
self.standard = Web3.toWei(gas_price, "gwei")
self.fast = Web3.toWei(gas_price, "gwei")
self.fastest = Web3.toWei(gas_price, "gwei")
self.lowest = Web3.to_wei(gas_price, "gwei")
self.safe_low = Web3.to_wei(gas_price, "gwei")
self.standard = Web3.to_wei(gas_price, "gwei")
self.fast = Web3.to_wei(gas_price, "gwei")
self.fastest = Web3.to_wei(gas_price, "gwei")

def calculate_gas_prices(self) -> GasPrice:
return GasPrice(
Expand Down
2 changes: 1 addition & 1 deletion safe_relay_service/gas_station/tests/test_gas_station.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_gas_station_mock(self):

if w3.eth.block_number < number_of_blocks and w3.eth.accounts: # Ganache
# Mine some blocks
eth_balance = w3.toWei(0.00001, "ether")
eth_balance = w3.to_wei(0.00001, "ether")
for _ in range(number_of_blocks - w3.eth.block_number + 2):
w3.eth.wait_for_transaction_receipt(
w3.eth.send_transaction(
Expand Down
6 changes: 3 additions & 3 deletions safe_relay_service/relay/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class SafeCreationAdmin(admin.ModelAdmin):
search_fields = ["=safe__address", "=deployer", "owners"]

def ether_deploy_cost(self, obj: SafeCreation) -> float:
return Web3.fromWei(obj.wei_deploy_cost(), "ether")
return Web3.from_wei(obj.wei_deploy_cost(), "ether")


class SafeCreation2DeployedListFilter(admin.SimpleListFilter):
Expand Down Expand Up @@ -258,7 +258,7 @@ class SafeCreation2Admin(admin.ModelAdmin):
search_fields = ["=safe__address", "owners", "=tx_hash"]

def ether_deploy_cost(self, obj: SafeCreation2) -> float:
return Web3.fromWei(obj.wei_estimated_deploy_cost(), "ether")
return Web3.from_wei(obj.wei_estimated_deploy_cost(), "ether")

def gas_used(self, obj: SafeCreation2) -> Optional[int]:
return obj.gas_used()
Expand Down Expand Up @@ -330,7 +330,7 @@ class SafeMultisigTxAdmin(admin.ModelAdmin):

def refund_benefit_eth(self, obj: SafeMultisigTx) -> Optional[float]:
if (refund_benefit := obj.refund_benefit()) is not None:
refund_benefit_eth = Web3.fromWei(abs(refund_benefit), "ether") * (
refund_benefit_eth = Web3.from_wei(abs(refund_benefit), "ether") * (
-1 if refund_benefit < 0 else 1
)
return "{:.5f}".format(refund_benefit_eth)
Expand Down
2 changes: 1 addition & 1 deletion safe_relay_service/relay/circles.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def estimate_gas_price(self):
return self.gas_price

def pack_address(self, address):
assert Web3.isChecksumAddress(address)
assert Web3.is_checksum_address(address)
return "000000000000000000000000" + address[2:]

def is_circles_token(self, address):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def handle(self, *args, **options):
self.main_account.address, "pending"
)
main_account_balance = self.w3.eth.get_balance(self.main_account.address)
main_account_balance_eth = self.w3.fromWei(main_account_balance, "ether")
main_account_balance_eth = self.w3.from_wei(main_account_balance, "ether")
self.stdout.write(
self.style.SUCCESS(
f"Using {self.main_account.address} as main account with "
Expand Down Expand Up @@ -366,7 +366,7 @@ def send_multiple_txs(
self.main_account.key,
safe_address,
self.ethereum_client.w3.eth.gasPrice,
self.w3.toWei(1, "ether"),
self.w3.to_wei(1, "ether"),
nonce=self.main_account_nonce,
)
self.main_account_nonce += 1
Expand Down
2 changes: 1 addition & 1 deletion safe_relay_service/relay/services/circles_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def pack_address(self, token_address: str) -> str:
:param token_address:
:return: packed string
"""
assert Web3.isChecksumAddress(token_address)
assert Web3.is_checksum_address(token_address)
return MAPPING_NULL_PREFIX + token_address[2:]

def is_circles_token(self, token_address: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion safe_relay_service/relay/services/funding_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def send_eth_to(
if not gas_price:
gas_price = self.gas_station.get_gas_prices().fast

if self.max_eth_to_send and value > Web3.toWei(self.max_eth_to_send, "ether"):
if self.max_eth_to_send and value > Web3.to_wei(self.max_eth_to_send, "ether"):
raise EtherLimitExceeded(
"%d is bigger than %f" % (value, self.max_eth_to_send)
)
Expand Down
2 changes: 1 addition & 1 deletion safe_relay_service/relay/services/stats_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_balances(self, safe_address: str) -> List[Dict[str, Union[str, int]]]:
:param safe_address:
:return: `{'token_address': str, 'balance': int}`. For ether, `token_address` is `None`
"""
assert Web3.isChecksumAddress(
assert Web3.is_checksum_address(
safe_address
), f"Not valid address {safe_address} for getting balances"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def process_addresses(
"""
assert safe_addresses, "Safe addresses cannot be empty!"
assert all(
[Web3.isChecksumAddress(safe_address) for safe_address in safe_addresses]
[Web3.is_checksum_address(safe_address) for safe_address in safe_addresses]
), ("A safe address has invalid checksum: %s" % safe_addresses)

parameters = self.get_block_numbers_for_search(safe_addresses)
Expand Down
20 changes: 10 additions & 10 deletions safe_relay_service/relay/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ 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 Web3.isChecksumAddress(safe_address)
assert Web3.isChecksumAddress(deployer_address)
assert Web3.is_checksum_address(safe_address)
assert Web3.is_checksum_address(deployer_address)
assert mk_contract_address(deployer_address, 0) == safe_address
assert payment > 0

Expand Down Expand Up @@ -360,7 +360,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 Web3.isChecksumAddress(safe_address)
assert Web3.is_checksum_address(safe_address)

redis = RedisRepository().redis
lock_name = f"locks:deploy_create2_safe:{safe_address}"
Expand Down Expand Up @@ -596,7 +596,7 @@ def begin_circles_onboarding_task(self, safe_address: str) -> None:
:param safe_address: Address of the safe to-be-created
"""

assert Web3.isChecksumAddress(safe_address)
assert Web3.is_checksum_address(safe_address)

redis = RedisRepository().redis
lock_name = f"locks:begin_circles_onboarding_task:{safe_address}"
Expand Down Expand Up @@ -639,7 +639,7 @@ def circles_onboarding_safe_task(self, safe_address: str) -> None:
:param safe_address: Address of the safe to-be-created
"""

assert Web3.isChecksumAddress(safe_address)
assert Web3.is_checksum_address(safe_address)

try:
redis = RedisRepository().redis
Expand Down Expand Up @@ -711,8 +711,8 @@ def begin_circles_onboarding_organization_task(
:param owner_address: Address of the first safe owner
"""

assert Web3.isChecksumAddress(safe_address)
assert Web3.isChecksumAddress(owner_address)
assert Web3.is_checksum_address(safe_address)
assert Web3.is_checksum_address(owner_address)

redis = RedisRepository().redis
lock_name = f"locks:begin_circles_onboarding_organization_task:{safe_address}"
Expand Down Expand Up @@ -756,8 +756,8 @@ def circles_onboarding_organization_safe_task(
:param owner_address: Address of the first safe owner
"""

assert Web3.isChecksumAddress(safe_address)
assert Web3.isChecksumAddress(owner_address)
assert Web3.is_checksum_address(safe_address)
assert Web3.is_checksum_address(owner_address)

try:
redis = RedisRepository().redis
Expand Down Expand Up @@ -800,7 +800,7 @@ def circles_onboarding_organization_signup_task(safe_address: str) -> None:
:param safe_address: Address of the created safe
"""

assert Web3.isChecksumAddress(safe_address)
assert Web3.is_checksum_address(safe_address)

# Additional funds for organization deployments (it should at least cover
# one `trust` method call) next to the `organizationSignup` method
Expand Down
4 changes: 2 additions & 2 deletions safe_relay_service/relay/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Meta:
tx_hash = factory.Sequence(lambda n: Web3.keccak(n))
gas = factory.fuzzy.FuzzyInteger(100000, 200000)
gas_price = factory.fuzzy.FuzzyInteger(
Web3.toWei(1, "gwei"), Web3.toWei(20, "gwei")
Web3.to_wei(1, "gwei"), Web3.to_wei(20, "gwei")
)
payment_token = None
value = 0
Expand Down Expand Up @@ -98,7 +98,7 @@ class Meta:
setup_data = factory.Sequence(lambda n: HexBytes("%x" % (n + 1000)))
gas_estimated = factory.fuzzy.FuzzyInteger(100000, 200000)
gas_price_estimated = factory.fuzzy.FuzzyInteger(
Web3.toWei(1, "gwei"), Web3.toWei(20, "gwei")
Web3.to_wei(1, "gwei"), Web3.to_wei(20, "gwei")
)
tx_hash = factory.Sequence(lambda n: Web3.keccak(text="safe-creation-2-%d" % n))
block_number = None
Expand Down
2 changes: 1 addition & 1 deletion safe_relay_service/relay/tests/test_funding_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def test_send_eth_to(self):

with self.assertRaises(EtherLimitExceeded):
self.funding_service.max_eth_to_send = 1
self.funding_service.send_eth_to(to, self.w3.toWei(1.1, "ether"))
self.funding_service.send_eth_to(to, self.w3.to_wei(1.1, "ether"))

FundingServiceProvider.del_singleton()
2 changes: 1 addition & 1 deletion safe_relay_service/relay/tests/test_resend_txs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_resend_txs(self):

w3 = self.w3
# The balance we will send to the safe
safe_balance = w3.toWei(0.02, "ether")
safe_balance = w3.to_wei(0.02, "ether")

# Create Safe
accounts = [self.create_account(), self.create_account()]
Expand Down
2 changes: 1 addition & 1 deletion safe_relay_service/relay/tests/test_transaction_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_create_multisig_tx(self):
w3 = self.w3

# The balance we will send to the safe
safe_balance = w3.toWei(0.02, "ether")
safe_balance = w3.to_wei(0.02, "ether")

# Create Safe
funder_account = self.ethereum_test_account
Expand Down
8 changes: 4 additions & 4 deletions safe_relay_service/relay/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_safe_balances(self):
def test_safe_multisig_tx_post(self):
# Create Safe ------------------------------------------------
w3 = self.ethereum_client.w3
safe_balance = w3.toWei(0.01, "ether")
safe_balance = w3.to_wei(0.01, "ether")
accounts = [self.create_account(), self.create_account()]
# Signatures must be sorted!
accounts.sort(key=lambda account: account.address.lower())
Expand Down Expand Up @@ -368,7 +368,7 @@ def test_safe_multisig_tx_get(self):
def test_safe_multisig_tx_post_gas_token(self):
# Create Safe ------------------------------------------------
w3 = self.ethereum_client.w3
safe_balance = w3.toWei(0.01, "ether")
safe_balance = w3.to_wei(0.01, "ether")
owner_account = self.create_account()
owner = owner_account.address
threshold = 1
Expand Down Expand Up @@ -513,7 +513,7 @@ def test_safe_multisig_tx_estimate(self):
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

initial_funding = self.w3.toWei(0.0001, "ether")
initial_funding = self.w3.to_wei(0.0001, "ether")
to, _ = get_eth_address_with_key()
data = {"to": to, "value": initial_funding // 2, "data": "0x", "operation": 1}

Expand Down Expand Up @@ -586,7 +586,7 @@ def test_safe_multisig_tx_estimates(self):
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

initial_funding = self.w3.toWei(0.0001, "ether")
initial_funding = self.w3.to_wei(0.0001, "ether")

safe = self.deploy_test_safe(
number_owners=3, threshold=2, initial_funding_wei=initial_funding
Expand Down
14 changes: 7 additions & 7 deletions safe_relay_service/relay/tests/test_views_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(Web3.isChecksumAddress(safe_address))
self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"]))
self.assertTrue(Web3.is_checksum_address(safe_address))
self.assertTrue(Web3.is_checksum_address(response_json["paymentReceiver"]))
self.assertEqual(response_json["paymentToken"], NULL_ADDRESS)
self.assertEqual(
int(response_json["payment"]),
Expand Down Expand Up @@ -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(Web3.isChecksumAddress(safe_address))
self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"]))
self.assertTrue(Web3.is_checksum_address(safe_address))
self.assertTrue(Web3.is_checksum_address(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)
Expand Down Expand Up @@ -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(Web3.isChecksumAddress(safe_address))
self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"]))
self.assertTrue(Web3.is_checksum_address(safe_address))
self.assertTrue(Web3.is_checksum_address(response_json["paymentReceiver"]))
self.assertEqual(response_json["paymentToken"], payment_token)
self.assertEqual(
int(response_json["payment"]),
Expand Down Expand Up @@ -204,7 +204,7 @@ def test_safe_multisig_tx_estimate_v2(self):
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

initial_funding = self.w3.toWei(0.0001, "ether")
initial_funding = self.w3.to_wei(0.0001, "ether")
to = Account.create().address
data = {"to": to, "value": initial_funding // 2, "data": "0x", "operation": 1}

Expand Down
12 changes: 6 additions & 6 deletions safe_relay_service/relay/tests/test_views_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(Web3.isChecksumAddress(safe_address))
self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"]))
self.assertTrue(Web3.is_checksum_address(safe_address))
self.assertTrue(Web3.is_checksum_address(response_json["paymentReceiver"]))
self.assertEqual(response_json["paymentToken"], NULL_ADDRESS)
self.assertEqual(
int(response_json["payment"]),
Expand Down Expand Up @@ -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(Web3.isChecksumAddress(safe_address))
self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"]))
self.assertTrue(Web3.is_checksum_address(safe_address))
self.assertTrue(Web3.is_checksum_address(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)
Expand Down Expand Up @@ -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(Web3.isChecksumAddress(safe_address))
self.assertTrue(Web3.isChecksumAddress(response_json["paymentReceiver"]))
self.assertTrue(Web3.is_checksum_address(safe_address))
self.assertTrue(Web3.is_checksum_address(response_json["paymentReceiver"]))
self.assertEqual(response_json["paymentToken"], payment_token)
self.assertEqual(
int(response_json["payment"]),
Expand Down
2 changes: 1 addition & 1 deletion safe_relay_service/relay/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def validate_checksumed_address(address):
if not Web3.isChecksumAddress(address):
if not Web3.is_checksum_address(address):
raise ValidationError(
"%(address)s is not a valid ethereum address",
params={"address": address},
Expand Down
Loading

0 comments on commit 401b8fb

Please sign in to comment.