Skip to content

Commit

Permalink
add create2 code
Browse files Browse the repository at this point in the history
  • Loading branch information
bout3fiddy committed Dec 24, 2023
1 parent 1c800bd commit 6e7c229
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 70 deletions.
130 changes: 79 additions & 51 deletions scripts/deploy_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from boa.network import NetworkEnv
from eth_abi import encode
from eth_account import Account
from eth_utils import keccak
from rich.console import Console as RichConsole

logger = RichConsole(file=sys.stdout)
Expand All @@ -17,123 +18,106 @@
"ethereum:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
"gauge": "",
"gauge": "0x38D9BdA812da2C68dFC6aDE85A7F7a54E77F8325",
},
"ethereum:sepolia": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
# Layer 2
"arbitrum:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"optimism:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"base:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"linea:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"scroll:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"pzkevm:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
# Layer 1
"gnosis:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"polygon:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"avax:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"ftm:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"bsc:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"celo:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"kava:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"aurora:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
},
"mantle:mainnet": {
"math": "",
"views": "",
"plain_amm": "",
"meta_amm": "",
"amm": "",
"factory": "",
"factory_ctor": "", # noqa:E501
},
Expand All @@ -144,25 +128,36 @@ def check_and_deploy(
contract_obj,
contract_designation,
network,
abi_encoded_args,
blueprint: bool = False,
args=[],
):

deployed_contract = deployments[network][contract_designation]

if not deployed_contract:

logger.log(f"Deploying {contract_designation} contract ...")
if not blueprint:
contract = contract_obj.deploy(*args)
if args:
constructor_args = encode(["address", "address"], args)
logger.log(
f"Constructor arguments for {contract_designation}: {constructor_args.hex()}"
)
else:
contract = contract_obj.deploy_as_blueprint()
logger.log(f"Deployed! At: {contract.address}.")
salt = keccak(42069)
compiled_bytecode = contract_obj.compiler_data.bytecode
(
precomputed_address,
deployment_bytecode,
) = deploy_utils.get_create2_deployment_address(
compiled_bytecode,
abi_encoded_args,
salt,
blueprint=blueprint,
blueprint_preamble=b"\xFE\x71\x00",
)

contract = deploy_utils.deploy_via_create2_factory(
deployment_bytecode, salt
)

logger.log(f"Deployed! At: {precomputed_address}.")

else:

logger.log(
f"Deployed {contract_designation} contract exists. Using {deployed_contract} ..."
)
Expand All @@ -183,19 +178,52 @@ def deploy_infra(network, url, account, fork=False):
logger.log("Prodmode ...")
boa.set_env(NetworkEnv(url))
boa.env.add_account(Account.from_key(os.environ[account]))

for _network, data in deploy_utils.curve_dao_network_settings.items():

if _network in network:

owner = data.dao_ownership_contract
fee_receiver = data.fee_receiver_address

assert owner, f"Curve's DAO contracts may not be on {network}."
assert fee_receiver, f"Curve's DAO contracts may not be on {network}."

# --------------------- Deploy math, views, blueprints ---------------------
# --------------------- Initialise contract objects ---------------------

math_contract_obj = boa.load_partial(
"./contracts/main/CurveCryptoMathOptimized2.vy"
)
views_contract_obj = boa.load_partial(
"./contracts/main/CurveCryptoViews2Optimized.vy"
)
amm_contract_obj = boa.load_partial(
"./contracts/main/CurveTwocryptoOptimized.vy"
)
factory_contract_obj = boa.load_partial(
"./contracts/main/CurveTwocryptoFactory.vy"
)

# deploy non-blueprint contracts:
math_contract = check_and_deploy(math_contract_obj, "math", network)
views_contract = check_and_deploy(views_contract_obj, "views", network)

# deploy blueprint:
plain_blueprint = check_and_deploy(
amm_contract_obj, "amm", network, blueprint=True
)

# Factory:
args = [fee_receiver, deploy_utils.FIDDYDEPLOYER]
factory = check_and_deploy(
factory_contract_obj, "factory", network, False, args
)

# Set up implementation addresses in the factory:
# This also checks if create2 deployment went well.
factory.set_pool_implementation(plain_blueprint, 0)
factory.set_views_implementation(views_contract)
factory.set_math_implementation(math_contract)

assert NotImplementedError
if network == "ethereum:mainnet":
factory.set_gauge_implementation(deployments[network]["gauge"])


def main():
Expand Down
34 changes: 15 additions & 19 deletions scripts/deployment_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,24 @@ class CurveNetworkSettings:
}


def deploy_via_create2_deployer(
contract_source_file, abi_encoded_ctor, expected_deployment_address
def get_create2_deployment_address(
compiled_bytecode,
abi_encoded_ctor,
salt,
blueprint=False,
blueprint_preamble=b"\xFE\x71\x00",
):
deployment_bytecode = compiled_bytecode + abi_encoded_ctor

if blueprint:
deployment_bytecode = blueprint_preamble + deployment_bytecode

source_compiler_data = boa.load_partial(contract_source_file).compiler_data
deployment_bytecode = source_compiler_data.bytecode + abi_encoded_ctor
code_hash = keccak(deployment_bytecode)
salt = keccak(42069)
return (
CREATExDEPLOYER.computeAddress(salt, code_hash),
deployment_bytecode,
)

precomputed_address = CREATExDEPLOYER.computeAddress(salt, code_hash)
assert precomputed_address == expected_deployment_address

# deploy at precomputed address:
def deploy_via_create2_factory(deployment_bytecode, salt):
CREATExDEPLOYER.deploy(0, salt, deployment_bytecode)
deployed_contract = boa.load_partial(contract_source_file).at(
precomputed_address
)

# add bytecode check (no ctor):
# Note that this check the runtime bytecode only and not the init bytecode
# Note the right hand side is deploying a contract at some random address
assert (
deployed_contract.bytecode == boa.load(contract_source_file).bytecode
)
return deployed_contract
Empty file.

0 comments on commit 6e7c229

Please sign in to comment.