Beispiel #1
0
def do_transfer(
    ctx: RunContext,
    cp1: Account,
    cp2: Account,
    amount: int,
    is_refundable: bool = True
    ) -> typing.Tuple[Deploy, Transfer]:
    """Executes a transfer between 2 counter-parties & returns resulting deploy hash.

    :param ctx: Generator run contextual information.
    :param cp1: Account information of counter party 1.
    :param cp2: Account information of counter party 2.
    :param amount: Amount in motes to be transferred.
    :param is_refundable: Flag indicating whether a refund is required.

    :returns: Dispatched deploy.

    """
    deploy_hash = get_client(ctx).transfer(
        amount=amount,
        from_addr=cp1.public_key,
        private_key=cp1.private_key_as_pem_filepath,
        target_account_hex=cp2.public_key,
        # TODO: allow these to be passed in via standard arguments
        payment_amount=defaults.CLX_TX_FEE,
        gas_price=defaults.CLX_TX_GAS_PRICE
    )

    logger.log(f"PYCLX :: transfer :: {amount} CLX :: {cp1.public_key[:8]} -> {cp2.public_key[:8]} :: {deploy_hash}")

    return (
        factory.create_deploy_for_run(ctx, deploy_hash, DeployStatus.DISPATCHED, DeployType.TRANSFER), 
        factory.create_transfer(ctx, amount, "CLX", cp1, cp2, deploy_hash, is_refundable)
        )
Beispiel #2
0
def do_transfer(
    ctx: ExecutionContext,
    cp1: Account,
    cp2: Account,
    amount: int,
    contract: ClientContract = None,
    is_refundable: bool = True,
    deploy_type: DeployType = DeployType.TRANSFER
) -> typing.Tuple[Deploy, Transfer]:
    """Executes a transfer between 2 counter-parties & returns resulting deploy hash.

    :param ctx: Execution context information.
    :param cp1: Account information of counter party 1.
    :param cp2: Account information of counter party 2.
    :param amount: Amount in motes to be transferred.
    :param contract: The transfer contract to call (if any).
    :param is_refundable: Flag indicating whether a refund is required.
    :param deploy_type: The type of deploy to dispatch.

    :returns: Dispatched deploy & transfer.

    """
    # Set client.
    node, client = utils.get_client(ctx)

    # Transfer using called contract - does not dispatch wasm.
    if contract:
        session_args = ABI.args([
            ABI.account("address", cp2.public_key),
            ABI.big_int("amount", amount)
        ])
        dhash = client.deploy(
            session_hash=bytes.fromhex(contract.chash),
            session_args=session_args,
            from_addr=cp1.public_key,
            private_key=cp1.private_key_as_pem_filepath,
            # TODO: allow these to be passed in via standard arguments
            payment_amount=defaults.CLX_TX_FEE,
            gas_price=defaults.CLX_TX_GAS_PRICE)

    # Transfer using stored contract - dispatches wasm.
    else:
        dhash = client.transfer(
            amount=amount,
            from_addr=cp1.public_key,
            private_key=cp1.private_key_as_pem_filepath,
            target_account_hex=cp2.public_key,
            # TODO: allow these to be passed in via standard arguments
            payment_amount=defaults.CLX_TX_FEE,
            gas_price=defaults.CLX_TX_GAS_PRICE)

    logger.log(
        f"PYCLX :: transfer :: {dhash} :: {amount} CLX :: {cp1.public_key[:8]} -> {cp2.public_key[:8]}"
    )

    return (node, dhash)
Beispiel #3
0
def _yield_events(src: typing.Union[NodeIdentifier, NetworkIdentifier],
                  on_block_added: typing.Optional[typing.Callable],
                  on_block_finalized: typing.Optional[typing.Callable]):
    """Yields events from event source (i.e. a CLX chain).
    
    """
    node, client = get_client(src)
    for event in client.stream_events(block_added=on_block_added is not None,
                                      block_finalized=on_block_finalized
                                      is not None):
        yield node, event
Beispiel #4
0
def _yield_events(network_id: NetworkIdentifier, on_block_added,
                  on_block_finalized):
    """Yields events from event source (i.e. a CLX chain).
    
    """
    # TODO: handle client disconnects.
    logger.log(f"PYCLX :: stream_events :: connecting ...")
    for event in get_client(network_id).stream_events(
            block_added=on_block_added is not None,
            block_finalized=on_block_finalized is not None):
        yield event
Beispiel #5
0
def do_deploy_contract(ctx: RunContext, account: Account, wasm_filepath: str):
    """Deploys a smart contract to chain.

    :param ctx: Generator run contextual information.
    :param account: Account to be associated with contract.
    :param wasm_filepath: Path to smart contract's wasm file.

    :returns: Deploy hash (in hex format).

    """
    pyclx = get_client(ctx)    

    logger.log(f"TODO :: deploy-contract :: {account.key_pair.public_key.as_hex} :: {wasm_filepath}")

    return "TODO: dispatch contract deploy"
Beispiel #6
0
def get_block_deploys(network_id: NetworkIdentifier,
                      block_hash: str) -> typing.List[str]:
    """Queries network for set of deploys associated with a specific block.

    :param network_id: A network identifier.
    :param block_hash: Hash of a block.

    :returns: Block information.

    """
    client = get_client(network_id)

    return (i.deploy.deploy_hash.hex()
            for i in client.showDeploys(block_hash_base16=block_hash,
                                        full_view=False))
Beispiel #7
0
def do_deploy_client_contract(network: Network,
                              contract_type: ClientContractType,
                              contract_name: str) -> str:
    """Deploys a client side smart contract to chain for future reference.

    :param network: Network to which a client contract is being deployed.
    :param contract_type: Type of contract to be deployed.
    :param contract_name: Name of contract as specified in wasm blob.

    :returns: Contract hash (in hex format).

    """
    # Set client.
    _, client = utils.get_client(network)

    # Dispatch deploy.
    session = utils.get_client_contract_path(contract_type)
    session_args = ABI.args([ABI.string_value("target", "hash")])
    dhash = client.deploy(
        session=session,
        session_args=session_args,
        from_addr=network.faucet.public_key,
        private_key=network.faucet.private_key_as_pem_filepath,
        # TODO: allow these to be passed in via standard arguments
        payment_amount=defaults.CLX_TX_FEE,
        gas_price=defaults.CLX_TX_GAS_PRICE)
    logger.log(
        f"PYCLX :: deploy-contract :: {contract_type.value} :: deploy-hash={dhash} -> awaiting processing"
    )

    # Get block hash.
    dinfo = client.showDeploy(dhash, wait_for_processed=True)
    bhash = dinfo.processing_results[0].block_info.summary.block_hash.hex()
    logger.log(
        f"PYCLX :: deploy-contract :: {contract_type.value} :: deploy-hash={dhash} -> processing complete"
    )

    # Get contract hash.
    chash = utils.get_client_contract_hash(client, network.faucet, bhash,
                                           contract_name)
    logger.log(
        f"PYCLX :: deploy-contract :: {contract_type.value} :: contract-hash={chash}"
    )

    return chash
Beispiel #8
0
def get_balance(ctx: RunContext, account: Account) -> int:
    """Returns a chain account balance.

    :param ctx: Generator run contextual information.
    :param account: Account whose balance will be queried.

    :returns: Account balance.

    """
    client = get_client(ctx)
    try:
        balance = client.balance(address=account.public_key,
                                 block_hash=_get_last_block_hash(client))
    except Exception as err:
        if "Failed to find base key at path" in err.details:
            return 0
        raise err
    else:
        return balance
Beispiel #9
0
def get_block(network_id: NetworkIdentifier, block_hash: str) -> Block:
    """Queries network for information pertaining to a specific block.

    :param network_id: A network identifier.
    :param block_hash: Hash of a block.

    :returns: Block information.

    """
    client = get_client(network_id)
    info = client.showBlock(block_hash_base16=block_hash, full_view=False)

    return factory.create_block(
        network_id=network_id,
        block_hash=block_hash,
        deploy_cost_total=info.status.stats.deploy_cost_total,
        deploy_count=info.summary.header.deploy_count,
        deploy_gas_price_avg=info.status.stats.deploy_gas_price_avg,
        rank=info.summary.header.rank,
        size_bytes=info.status.stats.block_size_bytes,
        timestamp=info.summary.header.timestamp,
        validator_id=info.summary.header.validator_public_key.hex())