def test_request_faucet_coins(
    basic_transfer_request: EthereumToSifchainTransferRequest,
    source_ethereum_address: str,
    rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
    rowan_source_integrationtest_env_transfer_request:
    EthereumToSifchainTransferRequest):
    basic_transfer_request.ethereum_address = source_ethereum_address
    basic_transfer_request.check_wait_blocks = True
    target_rowan_balance = 10**18
    request, credentials = generate_test_account(
        basic_transfer_request,
        rowan_source_integrationtest_env_transfer_request,
        rowan_source_integrationtest_env_credentials,
        target_ceth_balance=0,
        target_rowan_balance=target_rowan_balance)
    time.sleep(10)

    sifaddress = request.sifchain_address
    logging.info("get balances just to have those commands in the history")
    balance = test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node, "rowan")

    get_faucet_balance(basic_transfer_request.sifnodecli_node)
    basic_transfer_request.amount = 10**17
    basic_transfer_request.sifchain_symbol = "rowan"
    basic_transfer_request.sifchain_address = sifaddress
    request_faucet_coins(basic_transfer_request, credentials)
    time.sleep(10)
    get_faucet_balance(basic_transfer_request.sifnodecli_node)
    balance = test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node, "rowan")
Exemple #2
0
def test_eth_to_ceth_and_back_to_eth(
    basic_transfer_request: EthereumToSifchainTransferRequest,
    source_ethereum_address: str,
    rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
    rowan_source_integrationtest_env_transfer_request:
    EthereumToSifchainTransferRequest,
    ethereum_network,
    smart_contracts_dir,
    rowan_amount,
    ceth_amount,
    bridgetoken_address,
):
    # logging.info("shark")
    # x = 4.9908339925e+18
    # s = "{:d}".format(int(x))
    # logging.info(f"xiss: {s}")
    # logging.info(f"xiss: {x}")
    # assert False
    basic_transfer_request.ethereum_address = source_ethereum_address
    basic_transfer_request.check_wait_blocks = True
    logging.info(f"rq: {basic_transfer_request}")
    request, credentials = generate_test_account(
        basic_transfer_request,
        rowan_source_integrationtest_env_transfer_request,
        rowan_source_integrationtest_env_credentials,
        target_ceth_balance=ceth_amount,
        target_rowan_balance=rowan_amount)
    # send some test account ceth back to a new ethereum address
    request.ethereum_address, _ = test_utilities.create_ethereum_address(
        smart_contracts_dir, ethereum_network)
    logging.info(f"get balance of test account")
    test_utilities.get_sifchain_addr_balance(
        request.sifchain_address,
        sifnodecli_node=request.sifnodecli_node,
        denom="ceth")
    logging.info(
        "send erowan back to ethereum chain, saving 100k for ceth transfer fees"
    )
    request.sifchain_symbol = "rowan"
    request.ethereum_symbol = bridgetoken_address
    request.amount = rowan_amount - 200000
    burn_lock_functions.transfer_sifchain_to_ethereum(request, credentials)
    test_utilities.get_eth_balance(request)

    logging.info("send eth back to ethereum chain")
    logging.info("get ceth balance to decide how much to return")
    request.sifchain_symbol = "ceth"
    request.ethereum_symbol = "eth"
    ceth_balance = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address,
        sifnodecli_node=request.sifnodecli_node,
        denom="ceth")
    request.amount = ceth_balance - request.ceth_amount
    burn_lock_functions.transfer_sifchain_to_ethereum(request, credentials)
    logging.info("get final eth balance")
    test_utilities.get_eth_balance(request)
    test_utilities.get_sifchain_addr_balance(
        request.sifchain_address,
        sifnodecli_node=request.sifnodecli_node,
        denom="ceth")
def test_two_currencies_with_different_capitalization_should_not_interfere_with_each_other(
    smart_contracts_dir,
    bridgebank_address,
    solidity_json_path,
):
    new_account_key = (
        "a" + get_shell_output("uuidgen").replace("-", "").lower())[:5]
    amount = amount_in_wei(9)

    new_currency = create_new_currency(amount, new_account_key,
                                       smart_contracts_dir, bridgebank_address,
                                       solidity_json_path)
    (request1,
     _) = build_request_for_new_sifchain_address(new_currency, amount)
    burn_lock_functions.transfer_ethereum_to_sifchain(request1, 10)
    balance_1 = get_sifchain_addr_balance(request1.sifchain_address,
                                          request1.sifnodecli_node,
                                          request1.sifchain_symbol)
    assert (balance_1 == request1.amount)

    new_currency = create_new_currency(amount, new_account_key.upper(),
                                       smart_contracts_dir, bridgebank_address,
                                       solidity_json_path)
    (request2,
     _) = build_request_for_new_sifchain_address(new_currency, amount + 70000)
    burn_lock_functions.transfer_ethereum_to_sifchain(request2, 10)

    balance_1_again = get_sifchain_addr_balance(request1.sifchain_address,
                                                request1.sifnodecli_node,
                                                request1.sifchain_symbol)

    assert (balance_1 == balance_1_again)
def test_create_pools(
    basic_transfer_request: EthereumToSifchainTransferRequest,
    source_ethereum_address: str,
    rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
    rowan_source_integrationtest_env_transfer_request:
    EthereumToSifchainTransferRequest):
    basic_transfer_request.ethereum_address = source_ethereum_address
    basic_transfer_request.check_wait_blocks = True
    target_rowan_balance = 10**19
    target_ceth_balance = 10**19
    request, credentials = generate_test_account(
        basic_transfer_request,
        rowan_source_integrationtest_env_transfer_request,
        rowan_source_integrationtest_env_credentials,
        target_ceth_balance=target_ceth_balance,
        target_rowan_balance=target_rowan_balance)

    sifaddress = request.sifchain_address
    # wait for balance
    test_utilities.wait_for_sifchain_addr_balance(
        sifaddress, "rowan", target_rowan_balance,
        basic_transfer_request.sifnodecli_node)
    test_utilities.wait_for_sifchain_addr_balance(
        sifaddress, "ceth", target_ceth_balance,
        basic_transfer_request.sifnodecli_node)

    pools = get_pools(basic_transfer_request.sifnodecli_node)
    change_amount = 10**18
    sifchain_fees = 100000  # Should probably make this a constant
    basic_transfer_request.amount = change_amount
    basic_transfer_request.sifchain_symbol = "ceth"
    basic_transfer_request.sifchain_address = sifaddress
    current_ceth_balance = target_ceth_balance
    current_rowan_balance = target_rowan_balance

    # Only works the first time, fails later.  Make this flexible for manual and private net testing for now.
    if pools is None:
        create_pool(basic_transfer_request, credentials)
        get_pools(basic_transfer_request.sifnodecli_node)
        current_ceth_balance = current_ceth_balance - change_amount
        current_rowan_balance = current_rowan_balance - change_amount - sifchain_fees
        assert (test_utilities.get_sifchain_addr_balance(
            sifaddress, basic_transfer_request.sifnodecli_node,
            "rowan") == current_rowan_balance)
        assert (test_utilities.get_sifchain_addr_balance(
            sifaddress, basic_transfer_request.sifnodecli_node,
            "ceth") == current_ceth_balance)

    # check for failure if we try to create a pool twice
    txn = create_pool(basic_transfer_request, credentials)
    assert (txn["code"] == 14)
    get_pools(basic_transfer_request.sifnodecli_node)
    current_rowan_balance = current_rowan_balance - sifchain_fees
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "rowan") == current_rowan_balance)
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "ceth") == current_ceth_balance)
Exemple #5
0
def generate_test_account(
    base_transfer_request: EthereumToSifchainTransferRequest,
    rowan_source_integrationtest_env_transfer_request:
    EthereumToSifchainTransferRequest,
    rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
    target_ceth_balance: int = 10**18,
    target_rowan_balance: int = 10**18
) -> (EthereumToSifchainTransferRequest, SifchaincliCredentials):
    """Creates a test account with ceth and rowan"""
    new_account_key = get_shell_output("uuidgen")
    credentials = sifchain_cli_credentials_for_test(new_account_key)
    new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials,
                                                      keyname=new_account_key)
    new_sifaddr = new_addr["address"]
    credentials.from_key = new_addr["name"]

    initial_rowan = test_utilities.get_sifchain_addr_balance(
        rowan_source_integrationtest_env_transfer_request.sifchain_address,
        rowan_source_integrationtest_env_transfer_request.sifnodecli_node,
        "rowan")

    if target_rowan_balance > 0:
        rowan_request: EthereumToSifchainTransferRequest = copy.deepcopy(
            rowan_source_integrationtest_env_transfer_request)
        rowan_request.sifchain_destination_address = new_sifaddr
        rowan_request.amount = target_rowan_balance
        logging.debug(
            f"transfer {target_rowan_balance} to {new_sifaddr} from {rowan_request.sifchain_address}"
        )
        test_utilities.send_from_sifchain_to_sifchain(
            rowan_request, rowan_source_integrationtest_env_credentials)

    request: EthereumToSifchainTransferRequest = copy.deepcopy(
        base_transfer_request)
    request.sifchain_address = new_sifaddr
    request.amount = target_ceth_balance
    request.sifchain_symbol = "ceth"
    request.ethereum_symbol = "eth"
    logging.debug(
        f"transfer {target_ceth_balance} eth to {new_sifaddr} from {base_transfer_request.ethereum_address}"
    )
    burn_lock_functions.transfer_ethereum_to_sifchain(request)

    ending_rowan = test_utilities.get_sifchain_addr_balance(
        rowan_source_integrationtest_env_transfer_request.sifchain_address,
        rowan_source_integrationtest_env_transfer_request.sifnodecli_node,
        "rowan")

    rowan_used = initial_rowan - ending_rowan
    rowan_fees = rowan_used - target_rowan_balance
    # logging.info(f"rowan fees from source is {rowan_fees}")
    logging.info(
        f"created sifchain addr {new_sifaddr} with {test_utilities.display_currency_value(target_ceth_balance)} ceth and {test_utilities.display_currency_value(target_rowan_balance)} rowan"
    )
    return request, credentials
Exemple #6
0
def test_transfer_eth_to_ceth_using_replay_blocks(integration_dir,
                                                  smart_contracts_dir,
                                                  ensure_relayer_restart):
    starting_block = test_utilities.current_ethereum_block_number(
        smart_contracts_dir)
    logging.info("stopping ebrelayer")
    test_utilities.get_shell_output("pkill -9 ebrelayer || true")
    request, credentials = build_request()
    logging.info("(no transactions should happen without a relayer)")
    # test_utilities.whitelist_token(request.ethereum_symbol, request.smart_contracts_dir, True)
    logging.info(
        f"send {request.amount / 10**18} eth ({request.amount} wei) to {request.sifchain_address}"
    )
    test_utilities.send_from_ethereum_to_sifchain(request)
    # test_utilities.get_shell_output(f"{integration_dir}/sifchain_start_ebrelayer.sh")

    logging.info("replay blocks using ebrelayer replayEthereum")
    ews = test_utilities.get_required_env_var("ETHEREUM_WEBSOCKET_ADDRESS")
    bra = test_utilities.get_required_env_var("BRIDGE_REGISTRY_ADDRESS")
    mon = test_utilities.get_required_env_var("MONIKER")
    mn = test_utilities.get_required_env_var("MNEMONIC")
    cn = test_utilities.get_required_env_var("CHAINNET")
    ending_block = test_utilities.current_ethereum_block_number(
        smart_contracts_dir) + 1
    cmd = f"""ebrelayer replayEthereum tcp://0.0.0.0:26657 {ews} {bra} {mon} '{mn}' {starting_block} {ending_block} 1 2 --chain-id {cn} --gas 5000000000000 \
  --gas-prices 0.5rowan"""
    test_utilities.get_shell_output(cmd)
    time.sleep(15)
    logging.info(
        f"check the ending balance of {request.sifchain_address} after replaying blocks"
    )
    ending_balance = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnodecli_node,
        request.sifchain_symbol)
    assert (ending_balance == request.amount)

    # now do it again
    test_utilities.get_shell_output(cmd)
    time.sleep(5)
    ending_balance2 = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnodecli_node,
        request.sifchain_symbol)
    assert (ending_balance2 == request.amount)

    # now start ebrelayer and do another transfer
    test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks + 1,
                                             smart_contracts_dir)
    test_utilities.get_shell_output(
        f"{integration_dir}/sifchain_start_ebrelayer.sh")
    burn_lock_functions.transfer_ethereum_to_sifchain(request, 15)
    ending_balance3 = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnodecli_node,
        request.sifchain_symbol)
    assert (ending_balance3 == request.amount * 2)
Exemple #7
0
def test_transfer_eth_to_ceth_using_replay_blocks(
        integration_dir, smart_contracts_dir, solidity_json_path,
        source_ethereum_address, validator_address, ensure_relayer_restart):
    starting_block = test_utilities.current_ethereum_block_number(
        smart_contracts_dir)
    logging.info("stopping ebrelayer")
    test_utilities.get_shell_output("pkill -9 ebrelayer || true")
    request, credentials = build_request(smart_contracts_dir,
                                         source_ethereum_address,
                                         solidity_json_path)
    logging.info("(no transactions should happen without a relayer)")
    logging.info(
        f"send {request.amount / 10 ** 18} eth ({request.amount} wei) to {request.sifchain_address}"
    )
    test_utilities.send_from_ethereum_to_sifchain(request)

    logging.info("make sure no balances changed while the relayer was offline")
    test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks,
                                             smart_contracts_dir)
    time.sleep(5)
    balance_with_no_relayer = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnoded_node,
        request.sifchain_symbol)
    assert (balance_with_no_relayer == 0)

    logging.info("replay blocks using ebrelayer replayEthereum")
    ews = test_utilities.get_required_env_var("ETHEREUM_WEBSOCKET_ADDRESS")
    bra = test_utilities.get_required_env_var("BRIDGE_REGISTRY_ADDRESS")
    mon = test_utilities.get_required_env_var("MONIKER")
    mn = test_utilities.get_required_env_var("MNEMONIC")
    cn = test_utilities.get_required_env_var("CHAINNET")
    ending_block = test_utilities.current_ethereum_block_number(
        smart_contracts_dir) + 1
    cmd = f"""yes | ebrelayer replayEthereum tcp://0.0.0.0:26657 {ews} {bra} {mon} '{mn}' {starting_block} {ending_block} 1 2 --chain-id {cn} --gas 5000000000000 \
 --keyring-backend test --node tcp://0.0.0.0:26657 --from {mon}"""
    test_utilities.get_shell_output(cmd)
    time.sleep(5)
    logging.info(
        f"check the ending balance of {request.sifchain_address} after replaying blocks"
    )
    ending_balance = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnoded_node,
        request.sifchain_symbol)
    assert (ending_balance == request.amount)

    # now do it again
    test_utilities.get_shell_output(cmd)
    time.sleep(5)
    ending_balance2 = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnoded_node,
        request.sifchain_symbol)
    assert (ending_balance2 == request.amount)
def transfer_sifchain_to_ethereum(
        transfer_request: EthereumToSifchainTransferRequest,
        credentials: SifchaincliCredentials,
        max_seconds: int = 90
):
    logging.debug(f"transfer_sifchain_to_ethereum_json: {transfer_request.as_json()}")

    original_log_level = decrease_log_level()
    ethereum_starting_balance = get_eth_balance(transfer_request)

    sifchain_starting_balance = get_sifchain_addr_balance(
        transfer_request.sifchain_address,
        transfer_request.sifnodecli_node,
        transfer_request.sifchain_symbol
    )

    status = {
        "action": "transfer_sifchain_to_ethereum",
        "ethereum_starting_balance": ethereum_starting_balance,
        "sifchain_starting_balance": sifchain_starting_balance,
    }
    logging.debug(status)

    force_log_level(original_log_level)
    raw_output = send_from_sifchain_to_ethereum(transfer_request, credentials)
    original_log_level = decrease_log_level()

    target_balance = ethereum_starting_balance + transfer_request.amount

    wait_for_eth_balance(
        transfer_request=transfer_request,
        target_balance=ethereum_starting_balance + transfer_request.amount,
        max_seconds=max_seconds
    )

    sifchain_ending_balance = get_sifchain_addr_balance(
        transfer_request.sifchain_address,
        transfer_request.sifnodecli_node,
        transfer_request.sifchain_symbol
    )

    result = {
        **status,
        "sifchain_ending_balance": sifchain_ending_balance,
        "ethereum_ending_balance": target_balance,
    }
    logging.debug(f"transfer_sifchain_to_ethereum_complete_json: {json.dumps(result)}")
    force_log_level(original_log_level)
    return result
def transfer_sifchain_to_sifchain(
        transfer_request: EthereumToSifchainTransferRequest,
        credentials: SifchaincliCredentials,
        max_seconds: int = 30):
    logging.debug(
        f"transfer_sifchain_to_sifchain: {transfer_request.as_json()}")

    try:
        sifchain_starting_balance = get_sifchain_addr_balance(
            transfer_request.sifchain_destination_address,
            transfer_request.sifnodecli_node, transfer_request.sifchain_symbol)
    except Exception as e:
        # this is a new account, so the balance is 0
        sifchain_starting_balance = 0

    status = {
        "action": "transfer_sifchain_to_sifchain",
        "sifchain_starting_balance": sifchain_starting_balance,
    }
    logging.info(status)

    send_from_sifchain_to_sifchain(transfer_request, credentials)
    target_balance = transfer_request.amount + sifchain_starting_balance
    wait_for_sifchain_addr_balance(
        sifchain_address=transfer_request.sifchain_destination_address,
        symbol=transfer_request.sifchain_symbol,
        target_balance=target_balance,
        sifchaincli_node=transfer_request.sifnodecli_node,
        max_seconds=max_seconds,
        debug_prefix=f"transfer_sifchain_to_sifchain {transfer_request}")

    return {
        **status,
        "sifchain_ending_balance": target_balance,
    }
Exemple #10
0
def test_ceth_receiver_account(
        basic_transfer_request: EthereumToSifchainTransferRequest,
        source_ethereum_address: str,
        rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
        rowan_source_integrationtest_env_transfer_request: EthereumToSifchainTransferRequest,
        ethereum_network,
        smart_contracts_dir,
        bridgetoken_address,
        validator_address,
        ethbridge_module_address,
        sifchain_admin_account_credentials,
):
    admin_account = test_utilities.get_required_env_var("SIFCHAIN_ADMIN_ACCOUNT")
    ceth_rescue_account, ceth_rescue_account_credentials = integration_env_credentials.create_new_sifaddr_and_credentials()
    basic_transfer_request.sifchain_address = validator_address
    admin_user_credentials = sifchain_admin_account_credentials
    test_utilities.update_ceth_receiver_account(
        receiver_account=ceth_rescue_account,
        admin_account=admin_account,
        transfer_request=basic_transfer_request,
        credentials=admin_user_credentials
    )
    test_fee_charged_to_transfer_rowan_to_erowan(
        basic_transfer_request=basic_transfer_request,
        source_ethereum_address=source_ethereum_address,
        rowan_source_integrationtest_env_credentials=rowan_source_integrationtest_env_credentials,
        rowan_source_integrationtest_env_transfer_request=rowan_source_integrationtest_env_transfer_request,
        ethereum_network=ethereum_network,
        smart_contracts_dir=smart_contracts_dir,
        bridgetoken_address=bridgetoken_address,
    )
    received_ceth_charges = test_utilities.get_sifchain_addr_balance(ceth_rescue_account,
                                                                     basic_transfer_request.sifnodecli_node, "ceth")
    assert received_ceth_charges == test_utilities.burn_gas_cost
Exemple #11
0
def transfer_sifchain_to_sifchain(
    transfer_request: EthereumToSifchainTransferRequest,
    credentials: SifchaincliCredentials,
):
    logging.debug(
        f"transfer_sifchain_to_sifchain: {transfer_request.as_json()}")

    sifchain_starting_balance = get_sifchain_addr_balance(
        transfer_request.sifchain_destination_address,
        transfer_request.sifnodecli_node, transfer_request.sifchain_symbol)

    status = {
        "action": "transfer_sifchain_to_sifchain",
        "sifchain_starting_balance": sifchain_starting_balance,
    }
    logging.info(status)

    send_from_sifchain_to_sifchain(
        from_address=transfer_request.sifchain_address,
        to_address=transfer_request.sifchain_destination_address,
        amount=transfer_request.amount,
        currency=transfer_request.sifchain_symbol,
        yes_password=credentials.keyring_passphrase)
    target_balance = transfer_request.amount + sifchain_starting_balance
    wait_for_sifchain_addr_balance(
        transfer_request.sifchain_destination_address,
        transfer_request.sifchain_symbol, transfer_request.sifnodecli_node,
        target_balance, 30,
        f"transfer_sifchain_to_sifchain {transfer_request}")

    return {
        **status,
        "sifchain_ending_balance": target_balance,
    }
Exemple #12
0
def test_rescue_ceth(
        basic_transfer_request: EthereumToSifchainTransferRequest,
        source_ethereum_address: str,
        rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
        rowan_source_integrationtest_env_transfer_request: EthereumToSifchainTransferRequest,
        sifchain_fees_int,
        ethbridge_module_address,
        sifchain_admin_account
):
    """
    does a lock of rowan (using another test) that should result
    in ceth being sent to a place it can be rescued from
    """
    basic_transfer_request.ethereum_address = source_ethereum_address
    admin_user_credentials = SifchaincliCredentials(
        from_key=sifchain_admin_account
    )
    small_amount = 100
    test_account_request, test_account_credentials = generate_test_account(
        basic_transfer_request,
        rowan_source_integrationtest_env_transfer_request=rowan_source_integrationtest_env_transfer_request,
        rowan_source_integrationtest_env_credentials=rowan_source_integrationtest_env_credentials,
        target_ceth_balance=test_utilities.burn_gas_cost + small_amount,
        target_rowan_balance=sifchain_fees_int
    )
    logging.info("get the starting balance for the ethbridge module - that's where fees should be going")
    ethbridge_module_balance = test_utilities.get_sifchain_addr_balance(
        ethbridge_module_address,
        basic_transfer_request.sifnodecli_node,
        "ceth"
    )
    test_account_request.amount = small_amount
    burn_lock_functions.transfer_sifchain_to_ethereum(test_account_request, test_account_credentials)
    logging.info(
        f"test account {test_account_request.sifchain_address} should now have no ceth")
    logging.info("ethbridge should have the fee that was paid")
    test_utilities.wait_for_sifchain_addr_balance(
        ethbridge_module_address,
        "ceth",
        ethbridge_module_balance + test_utilities.burn_gas_cost,
        test_account_request.sifnodecli_node
    )
    logging.info(f"rescue ceth into {test_account_request.sifchain_address}")
    test_utilities.rescue_ceth(
        receiver_account=test_account_request.sifchain_address,
        admin_account=sifchain_admin_account,
        amount=test_utilities.burn_gas_cost,
        transfer_request=basic_transfer_request,
        credentials=admin_user_credentials
    )
    test_utilities.wait_for_sifchain_addr_balance(
        test_account_request.sifchain_address,
        "ceth",
        test_utilities.burn_gas_cost,
        test_account_request.sifnodecli_node,
        max_seconds=10,
        debug_prefix="wait for rescue ceth"
    )
Exemple #13
0
def test_do_not_transfer_if_fee_allowed_is_too_low(
    basic_transfer_request: EthereumToSifchainTransferRequest,
    source_ethereum_address: str,
    rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
    rowan_source_integrationtest_env_transfer_request:
    EthereumToSifchainTransferRequest,
    ethereum_network,
    smart_contracts_dir,
    bridgetoken_address,
):
    basic_transfer_request.ethereum_address = source_ethereum_address
    target_ceth_balance = 10**18
    target_rowan_balance = 10**18
    request, credentials = generate_test_account(
        basic_transfer_request,
        rowan_source_integrationtest_env_transfer_request,
        rowan_source_integrationtest_env_credentials,
        target_ceth_balance=target_ceth_balance,
        target_rowan_balance=target_rowan_balance)
    # send some test account ceth back to a new ethereum address
    request.ethereum_address, _ = test_utilities.create_ethereum_address(
        smart_contracts_dir, ethereum_network)
    request.sifchain_symbol = "rowan"
    request.ethereum_symbol = bridgetoken_address
    request.amount = 31500

    logging.info(
        "try to transfer rowan to erowan with a ceth_amount that's too low")
    with pytest.raises(Exception):
        request.ceth_amount = test_utilities.lock_gas_cost - 1
        burn_lock_functions.transfer_sifchain_to_ethereum(request, credentials)
    ending_ceth_balance = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnodecli_node, "ceth")
    assert ending_ceth_balance == target_ceth_balance

    logging.info("try with not owning enough ceth to cover the offer")
    with pytest.raises(Exception):
        request.ceth_amount = target_ceth_balance + 1
        burn_lock_functions.transfer_sifchain_to_ethereum(request, credentials)
    ending_ceth_balance = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnodecli_node, "ceth")
    assert ending_ceth_balance == target_ceth_balance
Exemple #14
0
def test_sifchain_transactions_with_offline_relayer(
    basic_transfer_request: EthereumToSifchainTransferRequest,
    rowan_source_integrationtest_env_credentials: test_utilities.
    SifchaincliCredentials,
    rowan_source_integrationtest_env_transfer_request:
    EthereumToSifchainTransferRequest,
    rowan_source,
    smart_contracts_dir,
    source_ethereum_address,
    integration_dir,
):
    basic_transfer_request.ethereum_address = source_ethereum_address
    request, credentials = generate_test_account(
        basic_transfer_request,
        rowan_source_integrationtest_env_transfer_request,
        rowan_source_integrationtest_env_credentials,
        target_ceth_balance=10**19,
        target_rowan_balance=10**19,
    )
    logging.info("shut down ebrelayer")
    time.sleep(10)
    test_utilities.get_shell_output(f"pkill -9 ebrelayer || true")

    logging.info("prepare transactions to be sent while ebrelayer is offline")
    amount = 9000

    new_eth_addrs = test_utilities.create_ethereum_addresses(
        smart_contracts_dir, basic_transfer_request.ethereum_network, 2)

    request.amount = amount
    request.sifchain_symbol = "ceth"
    request.ethereum_symbol = "eth"

    logging.info("send transactions while ebrelayer is offline")

    for a in new_eth_addrs:
        request.ethereum_address = a["address"]
        sifchain_balance = test_utilities.get_sifchain_addr_balance(
            request.sifchain_address, request.sifnoded_node, "ceth")
        logging.info(
            f"sifchain balance is {sifchain_balance}, request is {request}")
        test_utilities.send_from_sifchain_to_ethereum(transfer_request=request,
                                                      credentials=credentials)
        time.sleep(5)

    logging.info("restart ebrelayer")
    test_utilities.get_shell_output(
        f"{integration_dir}/sifchain_start_ebrelayer.sh")
    time.sleep(15)
    test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks * 2,
                                             request.smart_contracts_dir)
    for a in new_eth_addrs:
        request.ethereum_address = a["address"]
        test_utilities.wait_for_eth_balance(request, amount, 600)
Exemple #15
0
def test_ebrelayer_restart(
    basic_transfer_request: EthereumToSifchainTransferRequest,
    source_ethereum_address: str,
    integration_dir,
):
    basic_transfer_request.ethereum_address = source_ethereum_address
    request, credentials = generate_minimal_test_account(
        base_transfer_request=basic_transfer_request,
        target_ceth_balance=10**15)
    balance = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnodecli_node, "ceth")
    logging.info(
        "restart ebrelayer normally, leaving the last block db in place")
    test_utilities.get_shell_output(
        f"{integration_dir}/sifchain_start_ebrelayer.sh")
    test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks,
                                             request.smart_contracts_dir)
    time.sleep(5)
    assert balance == test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnodecli_node, "ceth")
Exemple #16
0
def test_fee_charged_to_transfer_rowan_to_erowan(
    basic_transfer_request: EthereumToSifchainTransferRequest,
    source_ethereum_address: str,
    rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
    rowan_source_integrationtest_env_transfer_request:
    EthereumToSifchainTransferRequest,
    ethereum_network,
    smart_contracts_dir,
    bridgetoken_address,
):
    basic_transfer_request.ethereum_address = source_ethereum_address
    logging.info(
        f"credentials: {rowan_source_integrationtest_env_credentials}")
    request, credentials = generate_test_account(
        basic_transfer_request,
        rowan_source_integrationtest_env_transfer_request,
        rowan_source_integrationtest_env_credentials,
        target_ceth_balance=10**18,
        target_rowan_balance=10**18)
    # send some test account ceth back to a new ethereum address
    request.ethereum_address, _ = test_utilities.create_ethereum_address(
        smart_contracts_dir, ethereum_network)
    logging.info(
        f"sending rowan to erowan and checking that a ceth fee was charged")
    request.sifchain_symbol = "rowan"
    request.ethereum_symbol = bridgetoken_address
    request.amount = 31500

    # get the starting ceth balance, transfer some rowan to erowan, get the ending ceth
    # balance.  The difference is the fee charged and should be equal to request.ceth_amount

    starting_ceth_balance = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnodecli_node, "ceth")
    burn_lock_functions.transfer_sifchain_to_ethereum(request, credentials)
    ending_ceth_balance = test_utilities.get_sifchain_addr_balance(
        request.sifchain_address, request.sifnodecli_node, "ceth")
    fee = starting_ceth_balance - ending_ceth_balance
    assert fee == test_utilities.lock_gas_cost
Exemple #17
0
def test_can_mint_token_and_peg_it_for_everything_in_whitelist(
        basic_transfer_request: EthereumToSifchainTransferRequest,
        smart_contracts_dir, bridgebank_address, solidity_json_path,
        operator_address, ethereum_network, source_ethereum_address,
        rowan_source):
    logging.info(
        "token_refresh needs to use the operator private key, setting that to ETHEREUM_PRIVATE_KEY"
    )
    os.environ["ETHEREUM_PRIVATE_KEY"] = test_utilities.get_required_env_var(
        "OPERATOR_PRIVATE_KEY")
    request = copy.deepcopy(basic_transfer_request)
    request.sifchain_address = rowan_source
    request.ethereum_address = source_ethereum_address
    amount_in_tokens = int(test_utilities.get_required_env_var("TOKEN_AMOUNT"))

    tokens = test_utilities.get_whitelisted_tokens(request)
    logging.info(f"whitelisted tokens: {tokens}")

    for t in tokens:
        destination_symbol = "c" + t["symbol"]
        if t["symbol"] == "erowan":
            destination_symbol = "rowan"
        try:
            logging.info(f"sending {t}")
            request.amount = amount_in_tokens * (10**int(t["decimals"]))
            request.ethereum_symbol = t["token"]
            request.sifchain_symbol = destination_symbol
            request.ethereum_address = operator_address
            test_utilities.mint_tokens(request, operator_address)
            test_utilities.send_from_ethereum_to_sifchain(request)
        except Exception as e:
            # try to get as many tokens across the bridge as you can,
            # don't stop if one of them fails
            logging.info(f"failed to mint and send for {t}, error was {e}")
    logging.info(f"sent new batch of tokens to {rowan_source}")
    test_utilities.get_sifchain_addr_balance(rowan_source,
                                             request.sifnoded_node, "rowan")
Exemple #18
0
def transfer_ethereum_to_sifchain(
        transfer_request: EthereumToSifchainTransferRequest,
        max_seconds: int = default_timeout_for_ganache):
    logging.debug(
        f"transfer_ethereum_to_sifchain {transfer_request.as_json()}")
    assert transfer_request.ethereum_address
    assert transfer_request.sifchain_address

    # it's possible that this is the first transfer to the address, so there's
    # no balance to retrieve.  Catch that exception.

    original_log_level = decrease_log_level()

    try:
        sifchain_starting_balance = get_sifchain_addr_balance(
            transfer_request.sifchain_address,
            transfer_request.sifnodecli_node, transfer_request.sifchain_symbol)
    except:
        logging.debug(
            f"transfer_ethereum_to_sifchain failed to get starting balance, this is probably a new account"
        )
        sifchain_starting_balance = 0

    status = {
        "action": "transfer_ethereum_to_sifchain",
        "sifchain_starting_balance": sifchain_starting_balance,
        "transfer_request": transfer_request.__dict__,
    }
    logging.debug(
        f"transfer_ethereum_to_sifchain_json: {json.dumps(status)}", )

    force_log_level(original_log_level)
    starting_block = send_from_ethereum_to_sifchain(transfer_request)
    logging.debug(
        f"send_from_ethereum_to_sifchain ethereum block number: {starting_block}"
    )
    original_log_level = decrease_log_level()

    half_n_wait_blocks = n_wait_blocks / 2
    logging.debug("wait half the blocks, transfer should not complete")
    if transfer_request.manual_block_advance:
        advance_n_ethereum_blocks(half_n_wait_blocks,
                                  transfer_request.smart_contracts_dir)
        time.sleep(5)
    else:
        wait_for_ethereum_block_number(block_number=starting_block +
                                       half_n_wait_blocks,
                                       transfer_request=transfer_request)

    # we still may not have an account
    try:
        sifchain_balance_before_required_elapsed_blocks = get_sifchain_addr_balance(
            transfer_request.sifchain_address,
            transfer_request.sifnodecli_node, transfer_request.sifchain_symbol)
    except:
        sifchain_balance_before_required_elapsed_blocks = 0

    if transfer_request.check_wait_blocks and sifchain_balance_before_required_elapsed_blocks != sifchain_starting_balance:
        print_error_message(
            f"balance should not have changed yet.  Starting balance {sifchain_starting_balance},"
            f" current balance {sifchain_balance_before_required_elapsed_blocks}"
        )

    if transfer_request.manual_block_advance:
        advance_n_ethereum_blocks(half_n_wait_blocks,
                                  transfer_request.smart_contracts_dir)
    else:
        wait_for_ethereum_block_number(block_number=starting_block +
                                       n_wait_blocks,
                                       transfer_request=transfer_request)

    target_balance = sifchain_starting_balance + transfer_request.amount

    # You can't get the balance of an account that doesn't exist yet,
    # so wait for the account to be there before asking for the balance
    logging.debug(f"wait for account {transfer_request.sifchain_address}")
    wait_for_sif_account(sif_addr=transfer_request.sifchain_address,
                         sifchaincli_node=transfer_request.sifnodecli_node,
                         max_seconds=max_seconds)

    wait_for_sifchain_addr_balance(
        sifchain_address=transfer_request.sifchain_address,
        symbol=transfer_request.sifchain_symbol,
        sifchaincli_node=transfer_request.sifnodecli_node,
        target_balance=target_balance,
        max_seconds=max_seconds,
        debug_prefix=
        f"transfer_ethereum_to_sifchain waiting for balance {transfer_request}"
    )

    force_log_level(original_log_level)

    result = {
        **status,
        "sifchain_ending_balance": target_balance,
    }
    logging.debug(f"transfer_ethereum_to_sifchain completed {result}")
    return result
Exemple #19
0
def test_bulk_transfers_from_sifchain(
        basic_transfer_request: EthereumToSifchainTransferRequest,
        rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
        rowan_source_integrationtest_env_transfer_request: EthereumToSifchainTransferRequest,
        smart_contracts_dir,
        source_ethereum_address,
        rowan_source,
        rowan_source_key,
        bridgebank_address,
        bridgetoken_address,
        ethereum_network,
        sifchain_fees_int,
):
    test_transfer_amount = 100  # just a tiny number of wei to move to confirm things are working
    tokens = test_utilities.get_required_env_var("TOKENS", "ceth,rowan").split(",")
    logging.info(f"tokens to be transferred are: {tokens}")
    logging.info("create new ethereum and sifchain addresses")
    basic_transfer_request.ethereum_address = source_ethereum_address
    n_transfers = int(test_utilities.get_optional_env_var("NTRANSFERS", 2))
    n_transactions = n_transfers * len(tokens)
    new_addresses_and_keys = list(map(lambda x: create_new_sifaddr_and_key(), range(n_transactions)))
    logging.debug(f"new_addresses_and_keys: {new_addresses_and_keys}")
    credentials_for_account_with_ceth = SifchaincliCredentials(from_key=rowan_source_key)
    request: EthereumToSifchainTransferRequest = copy.deepcopy(basic_transfer_request)
    ceth_amount = n_transactions * (test_utilities.highest_gas_cost + 100)
    request.amount = ceth_amount
    request.ethereum_address = source_ethereum_address
    request.sifchain_address = rowan_source
    addresses_to_populate = copy.deepcopy(new_addresses_and_keys)
    test_transfers = []
    for a in range(n_transfers):
        for t in tokens:
            request.sifchain_destination_address, from_key = addresses_to_populate.pop()

            # send ceth to pay for the burn
            request.amount = test_utilities.burn_gas_cost
            request.sifchain_symbol = "ceth"
            burn_lock_functions.transfer_sifchain_to_sifchain(request, credentials_for_account_with_ceth)

            # send rowan to pay the fee
            request.amount = sifchain_fees_int
            request.sifchain_symbol = "rowan"
            burn_lock_functions.transfer_sifchain_to_sifchain(request, credentials_for_account_with_ceth)

            # send the token itself
            request.amount = test_transfer_amount
            request.sifchain_symbol = t
            burn_lock_functions.transfer_sifchain_to_sifchain(request, credentials_for_account_with_ceth)
            transfer = (request.sifchain_destination_address, from_key, request.sifchain_symbol, request.amount)

            test_utilities.get_sifchain_addr_balance(request.sifchain_destination_address, request.sifnodecli_node, t)

            test_transfers.append(transfer)

    logging.debug(f"test_transfers is {test_transfers}")

    text_file = open("pfile.cmds", "w")
    simple_credentials = SifchaincliCredentials(
        keyring_passphrase=None,
        keyring_backend="test",
        from_key=None,
        sifnodecli_homedir=None
    )

    logging.info(f"all accounts are on sifchain and have the correct balance")

    new_eth_addrs = test_utilities.create_ethereum_addresses(
        smart_contracts_dir,
        basic_transfer_request.ethereum_network,
        n_transactions
    )
    logging.debug(f"new eth addrs: {new_eth_addrs}")

    ethereum_transfers = []
    for sifaddr, from_key, sifsymbol, amount in test_transfers:
        destination_ethereum_address_element = new_eth_addrs.pop()
        r = copy.deepcopy(basic_transfer_request)
        r.sifchain_symbol = sifsymbol
        r.sifchain_address = sifaddr
        r.ethereum_address = destination_ethereum_address_element["address"]
        r.amount = amount
        simple_credentials.from_key = from_key
        c = test_utilities.send_from_sifchain_to_ethereum_cmd(r, simple_credentials)
        ethereum_symbol = test_utilities.sifchain_symbol_to_ethereum_symbol(sifsymbol)
        transfer = (r.ethereum_address, ethereum_symbol, amount)
        ethereum_transfers.append(transfer)
        text_file.write(f"{c}\n")
    text_file.close()
    test_utilities.get_shell_output("cat pfile.cmds | parallel --trim lr -v {}")
    whitelist = test_utilities.get_whitelisted_tokens(basic_transfer_request)
    test_utilities.advance_n_ethereum_blocks(test_utilities.n_wait_blocks, smart_contracts_dir)
    for ethereum_address, ethereum_symbol, amount in ethereum_transfers:
        r = copy.deepcopy(basic_transfer_request)
        r.ethereum_address = ethereum_address
        r.ethereum_symbol = test_utilities.get_token_ethereum_address(
            ethereum_symbol,
            whitelist
        )
        r.amount = amount
        test_utilities.wait_for_eth_balance(
            transfer_request=r,
            target_balance=amount,
            max_seconds=60 * 60 * 10
        )
Exemple #20
0
    new_account_key = 'user1'
    credentials = sifchain_cli_credentials_for_test(new_account_key)
    new_addr = burn_lock_functions.create_new_sifaddr(credentials=credentials,
                                                      keyname=new_account_key)
    credentials.from_key = new_addr["name"]
    request = EthereumToSifchainTransferRequest(
        sifchain_address=new_addr["address"],
        smart_contracts_dir=smart_contracts_dir,
        ethereum_address=ethereum_address,
        ethereum_private_key_env_var="ETHEREUM_PRIVATE_KEY",
        bridgebank_address=get_required_env_var("BRIDGE_BANK_ADDRESS"),
        ethereum_network=(os.environ.get("ETHEREUM_NETWORK") or ""),
        amount=9 * 10**18,
        ceth_amount=2 * (10**16))
    return request, credentials


# if there's an existing user1 key, just remove it.  Otherwise, adding a duplicate key will just hang
try:
    test_utilities.get_shell_output(
        f"sifnoded keys delete user1 --home /home/vagrant/.sifnoded --keyring-backend test -o json"
    )
except:
    logging.debug("no key to delete, this is normal in a fresh environment")
request, credentials = build_request()
# test_utilities.set_lock_burn_limit(smart_contracts_dir, request.ethereum_symbol, request.amount)
burn_lock_functions.transfer_ethereum_to_sifchain(request)
test_utilities.get_sifchain_addr_balance(request.sifchain_address,
                                         request.sifnoded_node, "ceth")
logging.info(f"created account for key {credentials.from_key}")
def test_pools(
        basic_transfer_request: EthereumToSifchainTransferRequest,
        source_ethereum_address: str,
        rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
        rowan_source_integrationtest_env_transfer_request:
    EthereumToSifchainTransferRequest, solidity_json_path):
    # max symbol length in clp is 10
    new_currency_symbol = ("a" +
                           get_shell_output("uuidgen").replace("-", ""))[:8]
    target_new_currency_balance = 5 * 10**18
    logging.info(f"create new currency")
    new_currency = test_utilities.create_new_currency(
        1000 * target_new_currency_balance,
        new_currency_symbol,
        smart_contracts_dir=smart_contracts_dir,
        bridgebank_address=bridgebank_address,
        solidity_json_path=solidity_json_path)
    sifchain_symbol = ("c" + new_currency["newtoken_symbol"]).lower()

    basic_transfer_request.ethereum_address = source_ethereum_address
    basic_transfer_request.check_wait_blocks = True
    target_rowan_balance = 5 * 10**18
    target_ceth_balance = 10**18
    request, credentials = generate_test_account(
        basic_transfer_request,
        rowan_source_integrationtest_env_transfer_request,
        rowan_source_integrationtest_env_credentials,
        target_ceth_balance=target_ceth_balance,
        target_rowan_balance=target_rowan_balance)

    logging.info(
        f"transfer some of the new currency {new_currency_symbol} to the test sifchain address"
    )
    request.ethereum_symbol = new_currency["newtoken_address"]
    request.sifchain_symbol = sifchain_symbol
    request.amount = target_new_currency_balance
    burn_lock_functions.transfer_ethereum_to_sifchain(request)

    sifaddress = request.sifchain_address
    sifchain_fees = 100000  # Should probably make this a constant
    #from_key = credentials.from_key
    # wait for balance
    test_utilities.wait_for_sifchain_addr_balance(
        sifaddress, "rowan", target_rowan_balance,
        basic_transfer_request.sifnodecli_node)
    test_utilities.wait_for_sifchain_addr_balance(
        sifaddress, sifchain_symbol, target_new_currency_balance,
        basic_transfer_request.sifnodecli_node)

    pools = get_pools(basic_transfer_request.sifnodecli_node)
    basic_transfer_request.sifchain_symbol = sifchain_symbol
    basic_transfer_request.sifchain_address = sifaddress
    current_coin_balance = target_new_currency_balance
    current_rowan_balance = target_rowan_balance

    change_amount = 10**19
    basic_transfer_request.amount = change_amount
    # Fail if amount is greater than user has
    txn = create_pool(basic_transfer_request, credentials)
    assert (txn["code"] == 12)
    current_rowan_balance = current_rowan_balance - sifchain_fees
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "rowan") == current_rowan_balance)

    change_amount = 10**17
    basic_transfer_request.amount = change_amount
    # Fail if amount is less than or equal to minimum
    txn = create_pool(basic_transfer_request, credentials)
    assert (txn["code"] == 7)
    current_rowan_balance = current_rowan_balance - sifchain_fees
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "rowan") == current_rowan_balance)

    change_amount = 10**18
    basic_transfer_request.amount = change_amount
    # Only works the first time, fails later.
    txn = create_pool(basic_transfer_request, credentials)
    assert (txn.get("code", 0) == 0)
    get_pools(basic_transfer_request.sifnodecli_node)
    current_coin_balance = current_coin_balance - change_amount
    current_rowan_balance = current_rowan_balance - change_amount - sifchain_fees
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "rowan") == current_rowan_balance)
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        sifchain_symbol) == current_coin_balance)

    # check for failure if we try to create a pool twice
    txn = create_pool(basic_transfer_request, credentials)
    assert (txn["code"] == 14)
    get_pools(basic_transfer_request.sifnodecli_node)
    current_rowan_balance = current_rowan_balance - sifchain_fees
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "rowan") == current_rowan_balance)
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        sifchain_symbol) == current_coin_balance)

    # ensure we can add liquidity, money gets transferred
    txn = add_pool_liquidity(basic_transfer_request, credentials)
    assert (txn.get("code", 0) == 0)
    get_pools(basic_transfer_request.sifnodecli_node)
    current_coin_balance = current_coin_balance - change_amount
    current_rowan_balance = current_rowan_balance - change_amount - sifchain_fees
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "rowan") == current_rowan_balance)
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        sifchain_symbol) == current_coin_balance)

    # check for failure if we try to remove all liquidity
    txn = remove_pool_liquidity(basic_transfer_request, credentials, 10000)
    assert (txn["code"] == 26)
    get_pools(basic_transfer_request.sifnodecli_node)
    current_rowan_balance = current_rowan_balance - sifchain_fees
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "rowan") == current_rowan_balance)
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        sifchain_symbol) == current_coin_balance)

    # ensure we can remove liquidity, money gets transferred
    txn = remove_pool_liquidity(basic_transfer_request, credentials, 5000)
    assert (txn.get("code", 0) == 0)
    get_pools(basic_transfer_request.sifnodecli_node)
    current_coin_balance = current_coin_balance + change_amount
    current_rowan_balance = current_rowan_balance + change_amount - sifchain_fees
    #assert(test_utilities.get_sifchain_addr_balance(sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance)
    #assert(test_utilities.get_sifchain_addr_balance(sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_ceth_balance)
    # no slippage if pool is perfectly balanced.

    # TODO: compute this precisely?
    slip_pct = 0.01
    balance = test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node, "rowan")
    slip_cost = (slip_pct * current_rowan_balance)
    assert (balance >= current_rowan_balance - slip_cost
            and balance <= current_rowan_balance + slip_cost)
    current_rowan_balance = balance
    balance = test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol)
    slip_cost = (slip_pct * current_coin_balance)
    assert (balance >= current_coin_balance - slip_cost
            and balance <= current_coin_balance + slip_cost)
    current_coin_balance = balance

    # check for failure if we try to add too much liquidity
    basic_transfer_request.amount = 10**19
    txn = add_pool_liquidity(basic_transfer_request, credentials)
    assert (txn["code"] == 25)
    get_pools(basic_transfer_request.sifnodecli_node)
    current_rowan_balance = current_rowan_balance - sifchain_fees
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "rowan") == current_rowan_balance)
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        sifchain_symbol) == current_coin_balance)

    # check for failure if we try to remove too much liquidity, only occurs now if not 100% liquidity provider
    """
    txn = remove_pool_liquidity(basic_transfer_request, credentials, 10000)
    assert(txn["code"] == 3)
    get_pools(basic_transfer_request.sifnodecli_node)
    current_rowan_balance = current_rowan_balance - sifchain_fees
    assert(test_utilities.get_sifchain_addr_balance(sifaddress, basic_transfer_request.sifnodecli_node, "rowan") == current_rowan_balance)
    assert(test_utilities.get_sifchain_addr_balance(sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol) == current_coin_balance)
    """

    # check for failure if we try to swap too much for user
    txn = swap_pool(basic_transfer_request, "rowan", sifchain_symbol,
                    credentials)
    assert (txn["code"] == 27)
    get_pools(basic_transfer_request.sifnodecli_node)
    current_rowan_balance = current_rowan_balance - sifchain_fees
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "rowan") == current_rowan_balance)
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        sifchain_symbol) == current_coin_balance)

    # check for failure if we try to swap too much for pool
    basic_transfer_request.amount = 5 * 10**17
    txn = swap_pool(basic_transfer_request, "rowan", sifchain_symbol,
                    credentials)
    assert (txn["code"] == 31)
    get_pools(basic_transfer_request.sifnodecli_node)
    current_rowan_balance = current_rowan_balance - sifchain_fees
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        "rowan") == current_rowan_balance)
    assert (test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node,
        sifchain_symbol) == current_coin_balance)

    # now try to do a swap that works
    change_amount = 10**15
    basic_transfer_request.amount = change_amount
    txn = swap_pool(basic_transfer_request, "rowan", sifchain_symbol,
                    credentials)
    # TODO: compute this precisely?
    slip_pct = 0.01
    balance = test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node, "rowan")
    assert (balance < current_rowan_balance)
    current_rowan_balance = balance
    balance = test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node, sifchain_symbol)
    assert (balance > current_coin_balance)
    current_coin_balance = balance
Exemple #22
0
def test_rollback_chain(source_ethereum_address):
    new_account_key = get_shell_output("uuidgen")
    credentials = sifchain_cli_credentials_for_test(new_account_key)
    new_account = burn_lock_functions.create_new_sifaddr(
        credentials=credentials, keyname=new_account_key)
    credentials.from_key = new_account["name"]

    # Any amount will work
    amount = 11000

    request = EthereumToSifchainTransferRequest(
        sifchain_address=new_account["address"],
        smart_contracts_dir=get_required_env_var("SMART_CONTRACTS_DIR"),
        ethereum_address=source_ethereum_address,
        ethereum_private_key_env_var="ETHEREUM_PRIVATE_KEY",
        bridgebank_address=get_required_env_var("BRIDGE_BANK_ADDRESS"),
        ethereum_network=(os.environ.get("ETHEREUM_NETWORK") or ""),
        amount=amount)

    logging.info(f"create account with a balance of {request.amount}")
    burn_lock_functions.transfer_ethereum_to_sifchain(request, 50)

    new_addr = new_account["address"]

    snapshot = get_shell_output(
        f"{test_integration_dir}/snapshot_ganache_chain.sh")
    logging.info(f"created new account, took ganache snapshot {snapshot}")
    initial_user_balance = get_sifchain_addr_balance(new_addr, "",
                                                     request.sifchain_symbol)
    logging.info(f"initial_user_balance {initial_user_balance}")

    transfer_1 = send_from_ethereum_to_sifchain(transfer_request=request)
    logging.info(f"transfer started but it will never complete (by design)")

    logging.info("advance less than wait blocks")
    advance_n_ethereum_blocks(n_wait_blocks / 2, request.smart_contracts_dir)

    # the transaction should not have happened on the sifchain side yet
    # since we haven't waited for the right number of blocks.
    # roll back ganache to the snapshot and try another transfer that
    # should succeed.

    logging.info(
        f"apply snapshot {snapshot} - this eliminates transfer_1 (block {transfer_1})"
    )
    get_shell_output(
        f"{test_integration_dir}/apply_ganache_snapshot.sh {snapshot} 2>&1")

    logging.info("advance past block wait")
    advance_n_ethereum_blocks(n_wait_blocks * 2, request.smart_contracts_dir)
    time.sleep(5)

    second_user_balance = get_sifchain_addr_balance(new_addr, "",
                                                    request.sifchain_symbol)
    if second_user_balance == initial_user_balance:
        logging.info(
            f"got expected outcome of no balance change @ {initial_user_balance}"
        )
    else:
        raise Exception(
            f"balance should be the same after applying snapshot and rolling forward n_wait_blocks * 2.  initial_user_balance: {initial_user_balance} second_user_balance: {second_user_balance}"
        )

    request.amount = 10000

    logging.info(f"sending more eth: {request.amount} to {new_addr}")
    burn_lock_functions.transfer_ethereum_to_sifchain(request)

    # We want to know that ebrelayer will never do a second transaction.
    # We can't know that, so just delay a reasonable amount of time.
    logging.info("delay to give ebrelayer time to make a mistake")
    time.sleep(10)

    balance_after_sleep = get_sifchain_addr_balance(new_addr, "",
                                                    request.sifchain_symbol)
    logging.info(
        f"get_sifchain_addr_balance after sleep is {balance_after_sleep} for {new_addr}"
    )

    expected_balance = initial_user_balance + request.amount
    logging.info(f"look for a balance of {expected_balance}")
    wait_for_sifchain_addr_balance(new_addr, request.sifchain_symbol,
                                   expected_balance, "")
def test_transfer_rowan_to_erowan_and_back(
    ropsten_wait_time,
    rowan_source,
    sifnodecli_node,
    source_ethereum_address,
    chain_id,
    smart_contracts_dir,
    basic_transfer_request: EthereumToSifchainTransferRequest,
    ceth_fee,
    bridgetoken_address,
):
    rq: EthereumToSifchainTransferRequest = deepcopy(basic_transfer_request)
    rq.ethereum_address = "0xa584E4Fd44425937649A52846bF95A783564fCda"
    rq.ethereum_symbol = bridgetoken_address
    bx = test_utilities.get_eth_balance(rq)
    logging.info(f"bx is {bx}")
    raise Exception("stop test")
    logging.info(
        f"transfer rowan from {rowan_source} to a newly created account")
    sifaddr, credentials = create_new_sifaddr_and_credentials()
    rowan_transfer_from_source = deepcopy(basic_transfer_request)
    rowan_transfer_from_source.sifchain_address = rowan_source
    rowan_transfer_from_source.sifchain_destination_address = sifaddr
    amt = 20000000
    rowan_transfer_from_source.amount = amt
    rowan_transfer_from_source.sifchain_symbol = "rowan"
    test_utilities.send_from_sifchain_to_sifchain(rowan_transfer_from_source,
                                                  credentials)

    logging.info(f"add ceth to new sif account to pay lock fees")

    eth_transfer: EthereumToSifchainTransferRequest = deepcopy(
        basic_transfer_request)
    eth_transfer.ethereum_address = source_ethereum_address
    eth_transfer.sifchain_address = sifaddr
    eth_transfer.amount = ceth_fee * 2

    logging.info("get balances just to have those commands in the history")
    try:
        test_utilities.get_sifchain_addr_balance(
            sifaddr, basic_transfer_request.sifnodecli_node, "ceth")
    except Exception as e:
        logging.info(f"got exception while checking balance: {e}")
    test_utilities.get_eth_balance(eth_transfer)

    logging.info("execute transfer of eth => ceth to enable fee payment")
    burn_lock_functions.transfer_ethereum_to_sifchain(eth_transfer,
                                                      ropsten_wait_time)

    ethereum_address, _ = test_utilities.create_ethereum_address(
        smart_contracts_dir, ethereum_network)
    logging.info(
        f"lock rowan from {rowan_transfer_from_source.sifchain_destination_address} to {ethereum_address}"
    )

    rowan_lock: EthereumToSifchainTransferRequest = deepcopy(
        rowan_transfer_from_source)
    rowan_lock.sifchain_address = sifaddr
    rowan_lock.ethereum_address = ethereum_address
    burn_lock_functions.transfer_sifchain_to_ethereum(rowan_lock, credentials,
                                                      ropsten_wait_time)

    logging.info(
        f"send erowan back to {sifaddr} from ethereum {ethereum_address}")
    return_request = deepcopy(rowan_lock)
    return_request.amount = amt / 2
    burn_lock_functions.transfer_sifchain_to_ethereum(return_request,
                                                      credentials,
                                                      ropsten_wait_time)
def test_request_faucet_coins(
        basic_transfer_request: EthereumToSifchainTransferRequest,
        source_ethereum_address: str,
        rowan_source_integrationtest_env_credentials: SifchaincliCredentials,
        rowan_source_integrationtest_env_transfer_request:
    EthereumToSifchainTransferRequest, solidity_json_path, operator_address,
        ethereum_network):

    #Generate New Random Currency
    new_currency_symbol = ("a" +
                           get_shell_output("uuidgen").replace("-", ""))[:8]
    target_new_currency_balance = 300000
    new_currency = generate_new_currency(new_currency_symbol,
                                         target_new_currency_balance,
                                         solidity_json_path, operator_address,
                                         ethereum_network)
    sifchain_symbol = ("c" + new_currency["newtoken_symbol"]).lower()

    basic_transfer_request.ethereum_address = source_ethereum_address
    basic_transfer_request.check_wait_blocks = True
    target_rowan_balance = 10**18
    request, credentials = generate_test_account(
        basic_transfer_request,
        rowan_source_integrationtest_env_transfer_request,
        rowan_source_integrationtest_env_credentials,
        target_ceth_balance=100,
        target_rowan_balance=target_rowan_balance)
    #Transfer Newly Generated Currency
    transfer_new_currency(request, sifchain_symbol, new_currency,
                          target_new_currency_balance)

    sifaddress = request.sifchain_address
    logging.info("get balances just to have those commands in the history")
    balance = test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node, "rowan")
    balance_ceth = test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node, "ceth")
    balance_new_currency = test_utilities.get_sifchain_addr_balance(
        sifaddress, request.sifnodecli_node, sifchain_symbol)

    assert balance_new_currency == 300000, "new currency balance is wrong in the test account"
    assert balance_ceth == 100, "new ceth currency balance is wrong in the test account"
    assert balance == 10**18, "new rowan currency balance is wrong in the test account"

    logging.info(f"{sifchain_symbol} balance = {balance_new_currency}")
    logging.info(f"ceth  balance = {balance_ceth}")
    logging.info(f"rowan  balance = {balance}")

    #rowan
    start_faucet_rowan_balance = get_currency_faucet_balance(
        "rowan", get_faucet_balance(basic_transfer_request.sifnodecli_node))
    logging.info(get_faucet_balance(basic_transfer_request.sifnodecli_node))
    basic_transfer_request.amount = 50000
    basic_transfer_request.sifchain_symbol = "rowan"
    basic_transfer_request.sifchain_address = sifaddress
    # add
    txn = add_faucet_coins(basic_transfer_request, credentials)
    assert (txn.get("code", 0) == 0)
    after_add_faucet_rowan_balance = get_currency_faucet_balance(
        "rowan", get_faucet_balance(basic_transfer_request.sifnodecli_node))
    assert (
        start_faucet_rowan_balance + 50000 == after_add_faucet_rowan_balance
    ), "faucet balance in rowan should be higher after adding coins to faucet"
    get_faucet_balance(basic_transfer_request.sifnodecli_node)
    # request
    basic_transfer_request.amount = 25000
    txn = request_faucet_coins(basic_transfer_request, credentials)
    assert (txn.get("code", 0) == 0)
    after_request_faucet_rowan_balance = get_currency_faucet_balance(
        "rowan", get_faucet_balance(basic_transfer_request.sifnodecli_node))
    assert (
        start_faucet_rowan_balance +
        25000 == after_request_faucet_rowan_balance
    ), "current faucet balance in rowan should be equal with the initial faucet balance + 25000"
    get_faucet_balance(basic_transfer_request.sifnodecli_node)
    balance = test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node, "rowan")

    #ceth
    start_faucet_ceth_balance = get_currency_faucet_balance(
        "ceth", get_faucet_balance(basic_transfer_request.sifnodecli_node))
    get_faucet_balance(basic_transfer_request.sifnodecli_node)
    basic_transfer_request.amount = 10
    basic_transfer_request.sifchain_symbol = "ceth"
    basic_transfer_request.sifchain_address = sifaddress
    # add
    txn = add_faucet_coins(basic_transfer_request, credentials)
    assert (txn.get("code", 0) == 0)
    after_add_faucet_ceth_balance = get_currency_faucet_balance(
        "ceth", get_faucet_balance(basic_transfer_request.sifnodecli_node))
    assert (
        start_faucet_ceth_balance + 10 == after_add_faucet_ceth_balance
    ), "faucet balance in ceth should be higher after adding coins to faucet"
    get_faucet_balance(request.sifnodecli_node)
    # request
    basic_transfer_request.amount = 5
    txn = request_faucet_coins(basic_transfer_request, credentials)
    assert (txn.get("code", 0) == 0)
    get_faucet_balance(basic_transfer_request.sifnodecli_node)
    balance_ceth = test_utilities.get_sifchain_addr_balance(
        sifaddress, basic_transfer_request.sifnodecli_node, "ceth")

    #random currency
    start_faucet_random_balance = get_currency_faucet_balance(
        sifchain_symbol, get_faucet_balance(request.sifnodecli_node))
    get_faucet_balance(request.sifnodecli_node)
    request.amount = 20000
    request.sifchain_symbol = sifchain_symbol
    request.sifchain_address = sifaddress
    # add
    txn = add_faucet_coins(request, credentials)
    assert (txn.get("code", 0) == 0)
    after_add_faucet_random_balance = get_currency_faucet_balance(
        sifchain_symbol, get_faucet_balance(request.sifnodecli_node))
    assert (
        start_faucet_random_balance + 20000 == after_add_faucet_random_balance
    ), "faucet balance in random currency should be higher after adding coins to faucet"
    get_faucet_balance(request.sifnodecli_node)
    balance_new_currency = test_utilities.get_sifchain_addr_balance(
        sifaddress, request.sifnodecli_node, sifchain_symbol)

    # request
    request.amount = 1000
    txn = request_faucet_coins(request, credentials)
    assert (txn.get("code", 0) == 0)
    get_faucet_balance(request.sifnodecli_node)
    balance_new_currency = test_utilities.get_sifchain_addr_balance(
        sifaddress, request.sifnodecli_node, sifchain_symbol)

    #get faucet balances
    logging.info(
        "get remaining balances just to have those commands in the history")
    assert balance_new_currency == 281000, "new currency balance is wrong in the test account"
    assert balance_ceth == 95, "new ceth currency balance is wrong in the test account"
    assert balance == 999999999999575000, "new rowan currency balance is wrong in the test account"
    logging.info(f"{sifchain_symbol} balance = {balance_new_currency}")
    logging.info(f"ceth  balance = {balance_ceth}")
    logging.info(f"rowan  balance = {balance}")
    logging.info(get_faucet_balance(request.sifnodecli_node))