Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Sep 8, 2023
1 parent 1ae1783 commit 44c1440
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
16 changes: 10 additions & 6 deletions boa/util/eip5202.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from typing import Optional, Any
import eth_typing
from vyper.utils import keccak256
from typing import Any, Optional

from eth_utils import to_canonical_address, to_checksum_address
from vyper.utils import keccak256


# TODO replace return type with upcoming AddressType wrapper
def get_create2_address(blueprint_bytecode: bytes, deployer_address: Any, salt: bytes) -> str:
def get_create2_address(
blueprint_bytecode: bytes, deployer_address: Any, salt: bytes
) -> str:
_, _, initcode = parse_erc5202(blueprint_bytecode)

initcode_hash = keccak256(initcode)

prefix = b"\xFF"
addr = to_canonical_address(deployer_address)
if len(salt) != 32:
raise ValueError(f"bad salt (must be bytes32): {salt}")
raise ValueError(f"bad salt (must be bytes32): {salt!r}")

create2_hash = keccak256(prefix + addr + salt + initcode_hash)

Expand Down Expand Up @@ -45,7 +47,9 @@ def parse_erc5202(blueprint_bytecode: bytes) -> tuple[int, Optional[bytes], byte
if n_length_bytes == 0b11:
raise ValueError("Reserved bits are set")

data_length = int.from_bytes(blueprint_bytecode[3 : 3 + n_length_bytes], byteorder="big")
data_length = int.from_bytes(
blueprint_bytecode[3 : 3 + n_length_bytes], byteorder="big"
)

if n_length_bytes == 0:
preamble_data = None
Expand Down
22 changes: 14 additions & 8 deletions tests/unitary/test_blueprints.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from eth_utils import to_canonical_address

import boa
from boa.util.eip5202 import get_create2_address
from eth_utils import to_canonical_address

blueprint_code = """
@external
def some_function() -> uint256:
return 5
"""
"""

factory_code = """
@external
@external
def create_child(blueprint: address, salt: bytes32) -> address:
return create_from_blueprint(blueprint, code_offset=3, salt=salt)
"""

"""


def test_create2_address():
blueprint = boa.loads_partial(blueprint_code).deploy_as_blueprint()
factory = boa.loads(factory_code)
Expand All @@ -23,5 +25,9 @@ def test_create2_address():
child_contract_address = factory.create_child(blueprint.address, salt)

# TODO: make a util function on boa.env to get code
blueprint_bytecode = boa.env.vm.state.get_code(to_canonical_address(blueprint.address))
assert child_contract_address == get_create2_address(blueprint_bytecode, factory.address, salt)
blueprint_bytecode = boa.env.vm.state.get_code(
to_canonical_address(blueprint.address)
)
assert child_contract_address == get_create2_address(
blueprint_bytecode, factory.address, salt
)
2 changes: 2 additions & 0 deletions tests/unitary/utils/test_parse_eip5202.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

ERC5202_VERSION = 0


def test_parse_erc5202():
blueprint_bytecode = b"\xFE\x71\x00abcd"
assert parse_erc5202(blueprint_bytecode) == (ERC5202_VERSION, None, b"abcd")


def test_parse_erc5202_with_data():
blueprint_bytecode = b"\xFE\x71\x01\x04dataabcd"
assert parse_erc5202(blueprint_bytecode) == (ERC5202_VERSION, b"data", b"abcd")

0 comments on commit 44c1440

Please sign in to comment.