Beispiel #1
0
def deploy_contract(project: Project,
                    chain,
                    deploy_address,
                    contract_def: dict,
                    chain_name: str,
                    need_unlock=True) -> Contract:
    """Deploy a single contract.

    :param need_unlock: Do the account unlock procedure (disable for testrpc)
    """

    web3 = chain.web3

    contract_name = contract_def["contract_name"]

    version = web3.version.node

    # Goes through geth account unlock process if needed
    if need_unlock and version.startswith("Geth"):
        # TODO:  Parity does not support this, must unlock on a command line, or web ui?
        if is_account_locked(web3, deploy_address):
            # Deploy can last max 1 h
            request_account_unlock(chain, deploy_address, timeout=3600)

    transaction = {"from": deploy_address}
    kwargs = dict(**contract_def["arguments"])  # Unwrap YAML CommentedMap

    print("Starting", contract_name, "deployment, with arguments ", kwargs)

    # TODO: Workaround when deploying the same contract twice in run,
    # because Populus contract_identity allows only one contract_identity per contract class
    # print(chain.registrar.contract_addresses)
    # if "JSONFile" in chain.registrar.registrar_backends:
    #    del chain.registrar.registrar_backends["JSONFile"]
    # chain.registrar.registrar_backends["Memory"].contract_addresses = defaultdict(set)

    try:
        contract, txhash = chain.provider.deploy_contract(
            contract_name,
            deploy_transaction=transaction,
            deploy_kwargs=kwargs)
    except Exception as e:
        raise RuntimeError(
            "Could not deploy contract {}, constructor arguments {}".format(
                contract_name, kwargs)) from e

    contract_def["address"] = contract.address
    print(contract_name, "address is", contract.address)

    constructor_args = get_constructor_arguments(contract, kwargs=kwargs)
    print(contract_name, "constructor arguments payload is", constructor_args)
    contract_def["constructor_args"] = constructor_args

    libraries = get_libraries(chain, contract_name, contract)
    print(contract_name, "libraries are", libraries)
    contract_def["libraries"] = libraries

    return contract
def deploy_contract(project: Project, chain, deploy_address, contract_def: dict, chain_name: str, need_unlock=True) -> Contract:
    """Deploy a single contract.

    :param need_unlock: Do the account unlock procedure (disable for testrpc)
    """

    web3 = chain.web3

    contract_name = contract_def["contract_name"]

    version = web3.version.node

    # Goes through geth account unlock process if needed
    if need_unlock and version.startswith("Geth"):
        # TODO:  Parity does not support this, must unlock on a command line, or web ui?
        if is_account_locked(web3, deploy_address):
            # Deploy can last max 1 h
            request_account_unlock(chain, deploy_address, timeout=3600)

    # Use non-default gas price for speedier processing
    gas_price = int(web3.eth.gasPrice * 1.2)

    transaction = {"from": deploy_address, "gasPrice": gas_price}
    kwargs = dict(**contract_def["arguments"])  # Unwrap YAML CommentedMap

    print("Starting", contract_name, "deployment, with arguments ", kwargs)

    # TODO: Workaround when deploying the same contract twice in run,
    # because Populus contract_identity allows only one contract_identity per contract class
    if "JSONFile" in chain.registrar.registrar_backends:
        del chain.registrar.registrar_backends["JSONFile"]
    chain.registrar.registrar_backends["Memory"].contract_addresses = defaultdict(set)

    try:
        contract, txhash = chain.provider.deploy_contract(contract_name, deploy_transaction=transaction, deploy_kwargs=kwargs)
    except Exception as e:
        raise RuntimeError("Could not deploy contract {}, constructor arguments {}".format(contract_name, kwargs)) from e

    contract_def["address"] = contract.address
    print(contract_name, "address is", contract.address)

    constructor_args = get_constructor_arguments(contract, kwargs=kwargs)
    print(contract_name, "constructor arguments payload is", constructor_args)
    contract_def["constructor_args"] = constructor_args

    libraries = get_libraries(chain, contract_name, contract)
    print(contract_name, "libraries are", libraries)
    contract_def["libraries"] = libraries

    return contract
Beispiel #3
0
def deploy_contract(project: Project,
                    chain,
                    deploy_address,
                    contract_def: dict,
                    chain_name: str,
                    need_unlock=True) -> Contract:
    """Deploy a single contract.

    :param need_unlock: Do the account unlock procedure (disable for testrpc)
    """

    web3 = chain.web3

    contract_name = contract_def["contract_name"]

    # Goes through geth account unlock process if needed
    if need_unlock:
        if is_account_locked(web3, deploy_address):
            # Deploy can last max 1 h
            request_account_unlock(chain, deploy_address, timeout=3600)

    transaction = {"from": deploy_address}
    kwargs = dict(**contract_def["arguments"])  # Unwrap YAML CommentedMap

    print("Starting", contract_name, "deployment, with arguments ", kwargs)
    try:
        contract, txhash = chain.provider.deploy_contract(
            contract_name,
            deploy_transaction=transaction,
            deploy_kwargs=kwargs)
    except Exception as e:
        raise RuntimeError(
            "Could not deploy contract {}, constructor arguments {}".format(
                contract_name, kwargs)) from e

    contract_def["address"] = contract.address
    print(contract_name, "address is", contract.address)

    constructor_args = get_constructor_arguments(contract, kwargs=kwargs)
    print(contract_name, "constructor arguments payload is", constructor_args)
    contract_def["constructor_args"] = constructor_args

    libraries = get_libraries(chain, contract_name, contract)
    print(contract_name, "libraries are", libraries)
    contract_def["libraries"] = libraries

    return contract
Beispiel #4
0
def main(chain, address, owner, days, minimum, verify):
    """Deploy a PresaleFundCollector contract.

    Example:

     deploy-presale --chain=ropsten --address=0x3c2d4e5eae8c4a31ccc56075b5fd81307b1627c6  --owner=0x3c2d4e5eae8c4a31ccc56075b5fd81307b1627c6 --days=30 --minimum=30

    """
    project = Project()

    # Parse command line args to presale constructor args
    minimum = to_wei(minimum, "ether")
    freeze_ends_at = int(utc_time() + days * 24 * 3600)

    contract_name = "PresaleFundCollector"

    # This is configured in populus.json
    # We are working on a testnet
    print(
        "Make sure {} chain is running, you can connect to it, or you'll get timeout"
        .format(chain))

    with project.get_chain(chain) as c:

        web3 = c.web3
        print("Web3 provider is", web3.currentProvider)
        print("Deploy address is", address)
        if not address:
            sys.exit(
                "You need to explicitly give the address from where we are deploying from"
            )

        print("Deploy address balance is",
              from_wei(web3.eth.getBalance(address), "ether"))

        # Goes through geth account unlock process if needed
        if is_account_locked(web3, address):
            request_account_unlock(c, address, None)

        transaction = {"from": address}
        args = [owner, freeze_ends_at, minimum]

        # This does deployment with all dependencies linked in
        print("Deploying contracts")
        presale, txhash = c.provider.deploy_contract(
            contract_name, deploy_transaction=transaction, deploy_args=args)
        print("Deploying presale, tx hash is", txhash)
        print("Presale contract address is", presale.address)

        libraries = get_libraries(c, contract_name, presale)
        print("Linked libraries are", libraries)

        # This is needed for Etherscan contract verification
        # https://etherscanio.freshdesk.com/support/solutions/articles/16000053599-contract-verification-constructor-arguments
        constructor_args = get_constructor_arguments(presale, args)
        print("Presale constructor arguments is", constructor_args)

        if verify:
            print("Verifying contract")
            verify_contract(project=project,
                            chain_name=chain,
                            address=presale.address,
                            contract_name="PresaleFundCollector",
                            contract_filename="PresaleFundCollector.sol",
                            constructor_args=constructor_args,
                            libraries=libraries)

        # Do some contract reads to see everything looks ok
        print("Presale freeze ends at", presale.call().freezeEndsAt())
        print("Presale minimum buy in (wei) is",
              presale.call().weiMinimumLimit())

        # Estimate invest() gas cost
        estimation = presale.estimateGas(transaction={
            "from": address,
            "value": to_wei(1000, "ether")
        }).invest()
        print("Presale.invest() estimated gas cost is", estimation)

        sig_data = presale._prepare_transaction("invest")
        print("Presale.invest() data payload is", sig_data["data"])

        sig_data = presale._prepare_transaction("refund")
        print("Presale.refund() data payload is", sig_data["data"])

        print("All done! Enjoy your decentralized future.")
Beispiel #5
0
def test_extract_libraries(chain: TesterChain, uncapped_flatprice: Contract):
    """We get library information of deployed contract."""

    libraries = get_libraries(chain, "UncappedCrowdsale", uncapped_flatprice)
    assert libraries["SafeMathLib"].startswith("0x")
def test_extract_libraries(chain: TestRPCChain, uncapped_flatprice: Contract):
    """We get library information of deployed contract."""

    libraries = get_libraries(chain, "UncappedCrowdsale", uncapped_flatprice)
    assert libraries["SafeMathLib"].startswith("0x")