Exemplo n.º 1
0
def contract_decode_data(contract_file, encoded_data, sophia_type,
                         compiler_url, json_):
    try:
        set_global_options(json_, False, False)
        c = CompilerClient(compiler_url=compiler_url)
        result = c.decode_data(sophia_type, encoded_data)
        _print_object(result, title="contract")
    except OpenAPIClientException as e:
        _print_object(e.data, title="compiler error")
    except Exception as e:
        _print_error(e, exit_code=1)
Exemplo n.º 2
0
def contract_encode_calldata(contract_file, function_name, arguments,
                             compiler_url, json_):
    try:
        set_global_options(json_, False, False)
        with open(contract_file) as fp:
            code = fp.read()
            c = CompilerClient(compiler_url=compiler_url)
            arguments = [] if arguments is None else arguments.split(",")
            result = c.encode_calldata(code, function_name, *arguments)
            _print_object(result, title="contract")
    except OpenAPIClientException as e:
        _print_object(e.data, title="compiler error")
    except Exception as e:
        _print_error(e, exit_code=1)
Exemplo n.º 3
0
def contract_aci(contract_file, compiler_url, json_):
    try:
        set_global_options(json_, False, False)
        with open(contract_file) as fp:
            code = fp.read()
            c = CompilerClient(compiler_url=compiler_url)
            result = c.aci(code)
            if click.confirm(
                    f'Save contract ACI to file ({contract_file}.aci.json) ?',
                    default=True,
                    show_default=True):
                with open(f"{contract_file}.aci.json", "w") as fp:
                    fp.write(json.dumps(Munch.toDict(result), indent=2))
            _print_object(result, title="contract")
    except OpenAPIClientException as e:
        _print_object(e.data, title="compiler error")
    except Exception as e:
        _print_error(e, exit_code=1)
Exemplo n.º 4
0
def inspect(obj, height, force, wait, json_):
    try:
        set_global_options(json_, force, wait)
        if obj.endswith(".test") or obj.endswith(".chain"):
            data = _node_cli().get_name_entry_by_name(name=obj)
            _print_object(data, title="name")
        elif obj.startswith("kh_") or obj.startswith("mh_"):
            v = _node_cli().get_block_by_hash(obj)
            _print_object(v, title="block")
        elif obj.startswith("th_"):
            # v = _node_cli().get_transaction_by_hash(hash=obj)
            v = _node_cli().get_transaction(obj)
            _print_object(v, title="transaction")
        elif obj.startswith("ak_"):
            if height is not None and height > 0:
                account = _node_cli().get_account_by_pubkey_and_height(
                    pubkey=obj, height=height)
                _print_object(account, title=f"account at {height}")
                return
            account = _node_cli().get_account_by_pubkey(pubkey=obj)
            _print_object(account, title="account")
        elif obj.startswith("ct_"):
            v = _node_cli().get_contract(pubkey=obj)
            _print_object(v, title="contract")
        elif obj.startswith("ok_"):
            cli = _node_cli()
            data = dict(oracle=cli.get_oracle_by_pubkey(pubkey=obj),
                        queries=cli.get_oracle_queries_by_pubkey(pubkey=obj))
            _print_object(data, title="oracle context")
        elif obj.startswith("tx_"):
            v = _node_cli().verify(obj)
            _print_object(v, title="tx")
        elif obj.startswith("cb_"):
            v = CompilerClient.decode_bytecode(obj)
            _print_object(v, title="contract")
        elif obj.isdigit() and int(obj) >= 0:
            v = _node_cli().get_key_block_by_height(height=int(obj))
            _print_object(v, title="block")
        else:
            raise ValueError(f"input not recognized: {obj}")
    except Exception as e:
        _print_error(e)
Exemplo n.º 5
0
def compiler_fixture(scope="module"):
    # Instantiate the node client for the tests
    compiler = CompilerClient(COMPILER_URL)
    return Munch.fromDict({"COMPILER": compiler})
def test_example_contract_native(chain_fixture):

    NODE_URL = os.environ.get('TEST_URL', 'http://127.0.0.1:3013')
    NODE_INTERNAL_URL = os.environ.get('TEST_DEBUG_URL',
                                       'http://127.0.0.1:3113')
    COMPILER_URL = os.environ.get('TEST_COMPILER__URL',
                                  'https://compiler.aepps.com')

    node_cli = NodeClient(
        Config(
            external_url=NODE_URL,
            internal_url=NODE_INTERNAL_URL,
            blocking_mode=True,
        ))

    compiler = CompilerClient(compiler_url=COMPILER_URL)

    sender_account = chain_fixture.ALICE

    # genrate ALICE account (and transfer AE to alice account)
    alice = Account.generate()

    node_cli.spend(sender_account, alice.get_address(), 5000000000000000000)

    CONTRACT_FILE = os.path.join(os.path.dirname(__file__),
                                 "testdata/CryptoHamster.aes")

    # read contract file
    with open(CONTRACT_FILE, 'r') as file:
        crypto_hamster_contract = file.read()
    """
    Initialize the contract instance
    Note: To disable use of dry-run endpoint add the parameter
    use_dry_run=False
    """
    crypto_hamster = ContractNative(client=node_cli,
                                    compiler=compiler,
                                    account=alice,
                                    source=crypto_hamster_contract)

    # deploy the contract
    # (also pass the args of thr init function - if any)
    tx = crypto_hamster.deploy()
    print(f"contract address: {crypto_hamster.address}")

    # PART 2 Call the contract
    CONTRACT_ID = crypto_hamster.address

    # CONTRACT_ID is the address of the deployed contract
    crypto_hamster.at(CONTRACT_ID)

    # call the contract method (stateful)
    tx_info, tx_result = crypto_hamster.add_test_value(1, 2)

    print(f"Transaction Hash: {tx_info.tx_hash}")
    print(f"Transaction Result/Return Data: {tx_result}")

    assert (tx_result == 3)
    assert (hasattr(tx_info, 'tx_hash'))

    # call contract method (not stateful)
    tx_info, tx_result = crypto_hamster.get_hamster_dna(
        "SuperCryptoHamster", None)

    print(f"Transaction Result/Return Data: {tx_result}")

    assert tx_result is not None
    assert not hasattr(tx_info, 'tx_hash')
Exemplo n.º 7
0
def compiler_fixture(scope="module"):
    # Instantiate the node client for the tests
    compiler = CompilerClient(COMPILER_URL)
    return namedtupled.map({"COMPILER": compiler}, _nt_name="TestData")