Example #1
0
def deploy_unw_eth(
    *,
    web3: Web3,
    transaction_options: Dict = None,
    private_key: bytes = None,
    exchange_address=None,
):
    if transaction_options is None:
        transaction_options = {}

    unw_eth = deploy(
        "UnwEth",
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )

    increase_transaction_options_nonce(transaction_options)

    if exchange_address is not None:
        if exchange_address is not None:
            function_call = unw_eth.functions.addAuthorizedAddress(exchange_address)
            send_function_call_transaction(
                function_call,
                web3=web3,
                transaction_options=transaction_options,
                private_key=private_key,
            )
            increase_transaction_options_nonce(transaction_options)

    return unw_eth
Example #2
0
def deploy_network(
    web3,
    name,
    symbol,
    decimals,
    expiration_time,
    fee_divisor=0,
    default_interest_rate=0,
    custom_interests=True,
    prevent_mediator_interests=False,
    exchange_address=None,
    currency_network_contract_name=None,
    transaction_options: Dict = None,
    private_key=None,
    authorized_addresses=None,
):
    if transaction_options is None:
        transaction_options = {}
    if authorized_addresses is None:
        authorized_addresses = []

    # CurrencyNetwork is the standard contract to deploy, If we're running
    # tests or trying to export data for testing the python implementation of
    # private functions, we may want deploy the TestCurrencyNetwork contract
    # instead.
    if currency_network_contract_name is None:
        currency_network_contract_name = "CurrencyNetwork"

    currency_network = deploy(
        currency_network_contract_name,
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)

    if exchange_address is not None:
        authorized_addresses.append(exchange_address)

    init_function_call = currency_network.functions.init(
        name,
        symbol,
        decimals,
        fee_divisor,
        default_interest_rate,
        custom_interests,
        prevent_mediator_interests,
        expiration_time,
        authorized_addresses,
    )

    send_function_call_transaction(
        init_function_call,
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)

    return currency_network
Example #3
0
def initialize_auction_contracts(
    *,
    web3,
    transaction_options=None,
    contracts: DeployedAuctionContracts,
    release_timestamp,
    private_key=None,
) -> None:
    if transaction_options is None:
        transaction_options = {}

    deposit_init = contracts.locker.functions.init(release_timestamp,
                                                   contracts.slasher.address,
                                                   contracts.auction.address)
    send_function_call_transaction(
        deposit_init,
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)

    slasher_init = contracts.slasher.functions.init(contracts.locker.address)
    send_function_call_transaction(
        slasher_init,
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)
Example #4
0
def deploy_proxied_identity(
    web3,
    factory_address,
    implementation_address,
    signature,
    *,
    transaction_options: Dict = None,
    private_key: bytes = None,
):
    if transaction_options is None:
        transaction_options = {}

    owner = recover_proxy_deployment_signature_owner(web3, factory_address,
                                                     implementation_address,
                                                     signature)

    interface = get_pinned_proxy_interface()
    initcode = build_initcode(
        contract_abi=interface["abi"],
        contract_bytecode=interface["bytecode"],
        constructor_args=[owner],
    )

    factory_interface = get_contract_interface("IdentityProxyFactory")
    factory = web3.eth.contract(address=factory_address,
                                abi=factory_interface["abi"])

    function_call = factory.functions.deployProxy(initcode,
                                                  implementation_address,
                                                  signature)
    receipt = send_function_call_transaction(
        function_call,
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)

    deployment_event = factory.events.ProxyDeployment().processReceipt(receipt)
    proxy_address = deployment_event[0]["args"]["proxyAddress"]

    computed_proxy_address = build_create2_address(factory_address, initcode)
    assert (
        computed_proxy_address == proxy_address
    ), "The computed proxy address does not match the deployed address found via events"

    identity_interface = get_contract_interface("Identity")
    proxied_identity = web3.eth.contract(address=proxy_address,
                                         abi=identity_interface["abi"])
    return proxied_identity
Example #5
0
def deploy_exchange(
    *, web3: Web3, transaction_options: Dict = None, private_key: bytes = None
):
    if transaction_options is None:
        transaction_options = {}

    exchange = deploy(
        "Exchange",
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)
    return exchange
Example #6
0
def deploy_identity_implementation(*,
                                   web3: Web3,
                                   transaction_options: Dict = None,
                                   private_key: bytes = None):
    if transaction_options is None:
        transaction_options = {}

    indentity_implementation = deploy(
        "Identity",
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)
    return indentity_implementation
Example #7
0
def deploy_identity(
    web3, owner_address, chain_id=None, transaction_options: Dict = None
):
    if transaction_options is None:
        transaction_options = {}
    identity = deploy("Identity", web3=web3, transaction_options=transaction_options)
    increase_transaction_options_nonce(transaction_options)
    if chain_id is None:
        chain_id = get_chain_id(web3)
    function_call = identity.functions.init(owner_address, chain_id)
    send_function_call_transaction(
        function_call, web3=web3, transaction_options=transaction_options
    )
    increase_transaction_options_nonce(transaction_options)

    return identity
def whitelist_addresses(
    auction_contract: Contract,
    whitelist: Sequence[str],
    *,
    batch_size,
    web3,
    transaction_options=None,
    private_key=None,
) -> int:
    """Add all not yet whitelisted addresses in `whitelist` to the whitelisted addresses in the auction contract.
    Returns the number of new whitelisted addresses"""

    if transaction_options is None:
        transaction_options = {}

    # only whitelist addresses that are not whitelisted yet
    filtered_whitelist = missing_whitelisted_addresses(auction_contract,
                                                       whitelist)

    assert batch_size > 0
    chunks = [
        filtered_whitelist[i:i + batch_size]
        for i in range(0, len(filtered_whitelist), batch_size)
    ]

    for chunk in chunks:
        assert len(chunk) <= batch_size

        add_to_whitelist_call = auction_contract.functions.addToWhitelist(
            chunk)

        send_function_call_transaction(
            add_to_whitelist_call,
            web3=web3,
            transaction_options=transaction_options,
            private_key=private_key,
        )

        increase_transaction_options_nonce(transaction_options)

    return len(filtered_whitelist)
Example #9
0
def deploy_identity_proxy_factory(
    *,
    chain_id=None,
    web3: Web3,
    transaction_options: Dict = None,
    private_key: bytes = None,
):
    if transaction_options is None:
        transaction_options = {}

    if chain_id is None:
        chain_id = get_chain_id(web3)

    identity_proxy_factory = deploy(
        "IdentityProxyFactory",
        constructor_args=(chain_id, ),
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)
    return identity_proxy_factory
Example #10
0
def deploy_auction_contracts(
    *,
    web3,
    transaction_options: Dict = None,
    private_key=None,
    auction_options: AuctionOptions,
    already_deployed_contracts:
    DeployedContractsAddresses = DeployedContractsAddresses(),
) -> DeployedAuctionContracts:

    use_token = auction_options.token_address is not None

    if transaction_options is None:
        transaction_options = {}

    if (already_deployed_contracts.auction is not None
            and already_deployed_contracts.locker is None):
        raise ValueError(
            "Cannot deploy new locker if auction already deployed due to constructor of auction."
        )

    compiled_contracts = load_contracts_json(__name__)

    deposit_locker_abi = (compiled_contracts["TokenDepositLocker"]["abi"]
                          if use_token else
                          compiled_contracts["ETHDepositLocker"]["abi"])
    deposit_locker_bin = (compiled_contracts["TokenDepositLocker"]["bytecode"]
                          if use_token else
                          compiled_contracts["ETHDepositLocker"]["bytecode"])

    validator_slasher_abi = compiled_contracts["ValidatorSlasher"]["abi"]
    validator_slasher_bin = compiled_contracts["ValidatorSlasher"]["bytecode"]

    auction_abi = (compiled_contracts["TokenValidatorAuction"]["abi"]
                   if use_token else
                   compiled_contracts["ETHValidatorAuction"]["abi"])
    auction_bin = (compiled_contracts["TokenValidatorAuction"]["bytecode"]
                   if use_token else
                   compiled_contracts["ETHValidatorAuction"]["bytecode"])

    if already_deployed_contracts.locker is not None:
        deposit_locker_contract: Contract = web3.eth.contract(
            abi=deposit_locker_abi,
            bytecode=deposit_locker_bin,
            address=already_deployed_contracts.locker,
        )
    else:
        deposit_locker_contract = deploy_compiled_contract(
            abi=deposit_locker_abi,
            bytecode=deposit_locker_bin,
            web3=web3,
            transaction_options=transaction_options,
            private_key=private_key,
        )
        increase_transaction_options_nonce(transaction_options)

    if already_deployed_contracts.slasher is not None:
        validator_slasher_contract = web3.eth.contract(
            abi=validator_slasher_abi,
            bytecode=validator_slasher_bin,
            address=already_deployed_contracts.slasher,
        )
    else:
        validator_slasher_contract = deploy_compiled_contract(
            abi=validator_slasher_abi,
            bytecode=validator_slasher_bin,
            web3=web3,
            transaction_options=transaction_options,
            private_key=private_key,
        )
        increase_transaction_options_nonce(transaction_options)

    if already_deployed_contracts.auction is not None:
        auction_contract: Contract = web3.eth.contract(
            abi=auction_abi,
            bytecode=auction_bin,
            address=already_deployed_contracts.auction,
        )
    else:
        auction_constructor_args: Tuple = (
            auction_options.start_price,
            auction_options.auction_duration,
            auction_options.minimal_number_of_participants,
            auction_options.maximal_number_of_participants,
            deposit_locker_contract.address,
        )
        if use_token:
            auction_constructor_args += (auction_options.token_address, )

        auction_contract = deploy_compiled_contract(
            abi=auction_abi,
            bytecode=auction_bin,
            web3=web3,
            constructor_args=auction_constructor_args,
            transaction_options=transaction_options,
            private_key=private_key,
        )
        increase_transaction_options_nonce(transaction_options)

    contracts = DeployedAuctionContracts(deposit_locker_contract,
                                         validator_slasher_contract,
                                         auction_contract)

    return contracts
Example #11
0
def initialize_auction_contracts(
    *,
    web3,
    transaction_options=None,
    contracts: DeployedAuctionContracts,
    release_timestamp,
    token_address=None,
    private_key=None,
) -> None:
    if transaction_options is None:
        transaction_options = {}

    if contracts.slasher is None:
        raise RuntimeError("Slasher contract not set")

    if not contracts.locker.functions.initialized().call():
        init_args: Tuple = (
            release_timestamp,
            contracts.slasher.address,
            contracts.auction.address,
        )
        if token_address is not None:
            init_args += (token_address, )

        deposit_init = contracts.locker.functions.init(*init_args)
        send_function_call_transaction(
            deposit_init,
            web3=web3,
            transaction_options=transaction_options,
            private_key=private_key,
        )
        increase_transaction_options_nonce(transaction_options)
    else:
        if (contracts.locker.functions.depositorsProxy().call() !=
                contracts.auction.address):
            raise ValueError(
                "Locker is already initialized but address of auction in locker and given auction contract do not match"
            )
        if contracts.locker.functions.slasher().call(
        ) != contracts.slasher.address:
            raise ValueError(
                "Locker is already initialized but address of slasher in locker and given slasher contract do not match"
            )

    if not contracts.slasher.functions.initialized().call():
        slasher_init = contracts.slasher.functions.init(
            contracts.locker.address)
        send_function_call_transaction(
            slasher_init,
            web3=web3,
            transaction_options=transaction_options,
            private_key=private_key,
        )
        increase_transaction_options_nonce(transaction_options)
    else:
        if (contracts.slasher.functions.depositContract().call() !=
                contracts.locker.address):
            raise ValueError(
                "Slasher is already initialized but address of locker in slasher and given locker contract do not match"
            )
    if contracts.auction.functions.depositLocker().call(
    ) != contracts.locker.address:
        raise ValueError(
            "Address of deposit locker in auction contract does not match with address of locker"
        )
Example #12
0
def deploy_auction_contracts(
    *,
    web3,
    transaction_options: Dict = None,
    private_key=None,
    auction_options: AuctionOptions,
) -> DeployedAuctionContracts:

    if transaction_options is None:
        transaction_options = {}

    compiled_contracts = load_contracts_json(__name__)

    deposit_locker_abi = compiled_contracts["DepositLocker"]["abi"]
    deposit_locker_bin = compiled_contracts["DepositLocker"]["bytecode"]

    validator_slasher_abi = compiled_contracts["ValidatorSlasher"]["abi"]
    validator_slasher_bin = compiled_contracts["ValidatorSlasher"]["bytecode"]

    auction_abi = compiled_contracts["ValidatorAuction"]["abi"]
    auction_bin = compiled_contracts["ValidatorAuction"]["bytecode"]

    deposit_locker_contract: Contract = deploy_compiled_contract(
        abi=deposit_locker_abi,
        bytecode=deposit_locker_bin,
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)

    validator_slasher_contract = deploy_compiled_contract(
        abi=validator_slasher_abi,
        bytecode=validator_slasher_bin,
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)

    auction_constructor_args = (
        auction_options.start_price,
        auction_options.auction_duration,
        auction_options.minimal_number_of_participants,
        auction_options.maximal_number_of_participants,
        deposit_locker_contract.address,
    )
    auction_contract: Contract = deploy_compiled_contract(
        abi=auction_abi,
        bytecode=auction_bin,
        web3=web3,
        constructor_args=auction_constructor_args,
        transaction_options=transaction_options,
        private_key=private_key,
    )
    increase_transaction_options_nonce(transaction_options)

    contracts = DeployedAuctionContracts(deposit_locker_contract,
                                         validator_slasher_contract,
                                         auction_contract)

    return contracts