Exemplo n.º 1
0
def test_partial_link(all_versions, bytecode):
    assert "_" in bytecode
    assert addr1[2:] not in bytecode
    output = solcx.link_code(bytecode, {"<stdin>:UnlinkedLib": addr1})
    assert output != bytecode
    assert "_" in output
    assert addr1[2:] in output
def test_full_link(bytecode):
    output = solcx.link_code(
        bytecode, {"<stdin>:UnlinkedLib": addr1, "<stdin>:OtherUnlinkedLib": addr2}
    )

    # fully linked bytecode should be able to be interpreted as hex
    assert int(output, 16)

    assert addr1[2:] in output
    assert addr2[2:] in output
Exemplo n.º 3
0
def deployContract(account,
                   file,
                   contract,
                   contract_name,
                   web3,
                   library_address=None):
    # unlocck account
    web3.personal.unlockAccount(account, '', 0)

    cbytecode = contract['contracts'][file][contract_name]['evm']['bytecode'][
        'object']
    cabi = contract['contracts'][file][contract_name]['abi']

    # Link Contract with code
    if library_address != None:
        cbytecode = link_code(cbytecode, library_address)

    instanceContract = web3.eth.contract(abi=cabi, bytecode=cbytecode)

    # caso seja necessario utilizar o compilado
    # instanceContract = readCompiledContractData(contract_name,web3)

    #deploy contract
    tx_hash = instanceContract.constructor().transact({
        'from': account,
        'gas': 1000000
    })
    # tx_hash = instanceContract.constructor().transact({'from': account})

    tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
    iter_count = 1

    while tx_receipt == None or iter_count < 30:
        print("Trying.. " + str(iter_count) + "/30 ...")
        time.sleep(2)
        tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
        iter_count += 1

    if tx_receipt == None:
        import sys
        print("Aborting!!!")
        sys.exit()

    contract_address = tx_receipt['contractAddress']
    # contract_address
    # '0x52DB67A188a2ddAd8433A80C494CbBb15002D125'

    cc = web3.eth.contract(
        address=contract_address,
        abi=instanceContract.abi,
    )

    return cc
Exemplo n.º 4
0
def test_full_link(all_versions, bytecode):
    assert "_" in bytecode
    assert addr1[2:] not in bytecode
    assert addr2[2:] not in bytecode
    output = solcx.link_code(bytecode, {
        "<stdin>:UnlinkedLib": addr1,
        "<stdin>:OtherUnlinkedLib": addr2
    })
    assert output != bytecode
    assert "_" not in output
    assert addr1[2:] in output
    assert addr2[2:] in output
Exemplo n.º 5
0
    def test_with_link(self, artifact, links):
        unlinked_bytecode = artifact['bin']
        get_link_address = self.get_links_address(links)
        linked_bytecode = link_code(unlinked_bytecode, get_link_address)

        contract_factory = self.web3.eth.contract(abi=artifact['abi'],
                                                  bytecode=linked_bytecode)
        # tx_hash = contract_factory.constructor().transact()
        tx_hash = contract_factory.constructor().transact(
            transaction={
                "gas": 12_500_000,
                "gasPrice": self.web3.eth.gasPrice
            })
Exemplo n.º 6
0
    def create_contract(path, args=(), sender=accounts[0], libraries=None):
        if libraries is None:
            libraries = dict()
        abi, hexcode = deployer.builder.get_contract_data(path)

        libraries = _encode_libs(libraries)
        linked_hexcode = link_code(hexcode, libraries)
        factory = w3.eth.contract(abi=abi, bytecode=linked_hexcode)
        tx_hash = factory.constructor(*args).transact({
            'gas': START_GAS,
            'from': sender.address
        })
        tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
        contract = w3.eth.contract(abi=abi, address=tx_receipt.contractAddress)
        return ConvenienceContractWrapper(contract)
Exemplo n.º 7
0
    def create_contract(path, args=(), sender=ethtester.k0, libraries=None):
        if libraries is None:
            libraries = dict()
        abi, hexcode = deployer.builder.get_contract_data(path)
        encoded_args = (
            ContractTranslator(abi).encode_constructor_arguments(args)
            if args else b'')

        libraries = _encode_libs(libraries)
        linked_hexcode = link_code(hexcode, libraries)

        code = ethutils.decode_hex(linked_hexcode) + encoded_args
        address = ethtester.chain.tx(sender=sender,
                                     to=b'',
                                     startgas=START_GAS,
                                     data=code)
        return ethtester.ABIContract(ethtester.chain, abi, address)
Exemplo n.º 8
0
def deploy_n_transact(file_path, mappings=[]):
    # compile all files
    contracts = compile_files(file_path, import_remappings=mappings)
    link_add = {}
    contract_interface, links = separate_main_n_link(file_path, contracts)

    # print (contract_interface)
    
    # first deploy all link libraries
    # here link is refers to the second contarct "stringUtils.sol"
    for link in links:
        link_add[link] = deploy_contract(links[link])    
    
    # now link dependent library code to main contract binary 
    # https://solidity.readthedocs.io/en/v0.4.24/using-the-compiler.html?highlight=library

    if link_add:
        contract_interface['bin'] = link_code(contract_interface['bin'], link_add)    
    
    # return contract receipt and abi(application binary interface)
    return deploy_contract(contract_interface), contract_interface['abi']
def test_solc_version(wrapper_mock, all_versions):
    solc_binary = solcx.install.get_executable(all_versions)
    wrapper_mock.expect(solc_binary=solc_binary)
    solcx.link_code("0x00", {}, solc_version=all_versions)
Exemplo n.º 10
0
def test_solc_binary(wrapper_mock):
    wrapper_mock.expect(solc_binary=Path("path/to/solc"))
    solcx.link_code("0x00", {}, solc_binary=Path("path/to/solc"))
Exemplo n.º 11
0
def test_partial_link(bytecode):
    output = solcx.link_code(bytecode, {"<stdin>:UnlinkedLib": addr1})

    assert output != bytecode
    assert "_" in output
    assert addr1[2:] in output
Exemplo n.º 12
0
 def link(self, contract_bin, libraries):
     for library_name in libraries.keys():
         library_address = libraries[library_name]
         contract_bin = link_code(contract_bin,
                                  {library_name: library_address})
     return contract_bin
Exemplo n.º 13
0
def deploy_contract(contract_interface):
    # Instantiate and deploy contract
    contract = w3.eth.contract(abi=contract_interface['abi'],
                               bytecode=contract_interface['bin'])

    # Get transaction hash from deployed contract
    tx_hash = contract.constructor().transact(
        transaction={'from': w3.eth.accounts[1]})

    # Get tx receipt to get contract address
    tx_receipt = w3.eth.getTransactionReceipt(tx_hash)

    return tx_receipt['contractAddress']  # compile all contract files


# to that we have to deploy library code first then link it
library_address = {
    "stringUtils.sol:StringUtils": deploy_contract(library_link)
}

main_contract['bin'] = link_code(
    main_contract['bin'], library_address
)  # add abi(application binary interface) and transaction reciept in json file

with open('data.json', 'w') as outfile:
    data = {
        "abi": main_contract['abi'],
        "contract_address": deploy_contract(main_contract)
    }
    json.dump(data, outfile, indent=4, sort_keys=True)