fuel_core_e2e_client/tests/
transfers.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::test_context::{
    TestContext,
    BASE_AMOUNT,
};
use libtest_mimic::Failed;
use tokio::time::timeout;

// Alice makes transfer to Bob of `4 * BASE_AMOUNT` native tokens.
pub async fn basic_transfer(ctx: &TestContext) -> Result<(), Failed> {
    // alice makes transfer to bob
    let result = ctx
        .alice
        .transfer(ctx.bob.address, 4 * BASE_AMOUNT, None)
        .await?;
    if !result.success {
        return Err("transfer failed".into())
    }
    // wait until bob sees the transaction
    timeout(
        ctx.config.sync_timeout(),
        ctx.bob.client.await_transaction_commit(&result.tx_id),
    )
    .await??;
    println!("\nThe tx id of the transfer: {}", result.tx_id);

    // bob checks to see if utxo was received
    // we don't check balance in order to avoid brittleness in the case of
    // external activity on these wallets
    let received_transfer = ctx.bob.owns_coin(result.transferred_utxo).await?;
    if !received_transfer {
        return Err("Bob failed to receive transfer".into())
    }

    Ok(())
}

// Alice makes transfer to Bob of `4 * BASE_AMOUNT` native tokens - `basic_transfer`.
// Bob returns `3 * BASE_AMOUNT` tokens back to Alice and pays `BASE_AMOUNT` as a fee.
pub async fn transfer_back(ctx: &TestContext) -> Result<(), Failed> {
    basic_transfer(ctx).await?;

    // bob makes transfer to alice
    let result = ctx
        .bob
        .transfer(ctx.alice.address, 3 * BASE_AMOUNT, None)
        .await?;
    if !result.success {
        return Err("transfer failed".into())
    }
    // wait until alice sees the transaction
    timeout(
        ctx.config.sync_timeout(),
        ctx.alice.client.await_transaction_commit(&result.tx_id),
    )
    .await??;

    // alice checks to see if utxo was received
    // we don't check balance in order to avoid brittleness in the case of
    // external activity on these wallets
    let received_transfer = ctx.alice.owns_coin(result.transferred_utxo).await?;
    if !received_transfer {
        return Err("Alice failed to receive transfer".into())
    }

    Ok(())
}