Error "failed transfer to address." when trying to make a transfer using a script

Hi guys
I have a problem

I’m trying to make a transfer using the script

script;
use std::token::transfer_to_address;
fn main(amount: u64, asset: ContractId, predicate_address: Address) {
    transfer_to_address(amount, asset, predicate_address);
}

And I run it using this test

use std::str::FromStr;

use fuels::{prelude::*, tx::ContractId};

abigen!(Script(
    name = "Script",
    abi = "out/debug/script_transfer-abi.json"
));
#[tokio::test]
async fn main() {
    let mint_amout = 1_000_000_000;

    let wallets_config = WalletsConfig::new(Some(2), Some(1), Some(mint_amout));
    let wallets = launch_custom_provider_and_get_wallets(wallets_config, None, None).await;

    let alice = wallets[0].clone();
    let alice_balance = alice.get_asset_balance(&BASE_ASSET_ID).await.unwrap();
    let bob = wallets[1].clone();
    let bob_balance = alice.get_asset_balance(&BASE_ASSET_ID).await.unwrap();

    assert_eq!(alice_balance, mint_amout);
    assert_eq!(bob_balance, mint_amout);

    let amount0 = mint_amout / 2;
    let contract_id = ContractId::from_str(BASE_ASSET_ID.to_string().as_str()).unwrap();
    let instance = Script::new(alice.clone(), "out/debug/script_transfer.bin");
    instance
        .main(amount0, contract_id, Address::from(bob.address()))
        .call()
        .await
        .unwrap();

    assert_eq!(
        alice.get_asset_balance(&BASE_ASSET_ID).await.unwrap(),
        bob_balance + amount0
    );
}

But I’m get this error

running 1 test
test main ... FAILED

failures:

---- main stdout ----
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: RevertTransactionError { reason: "failed transfer to address.", revert_id: 18446744073709486081, receipts: [Revert { id: 0000000000000000000000000000000000000000000000000000000000000000, ra: 18446744073709486081, pc: 10544, is: 10344 }, ScriptResult { result: Revert, gas_used: 125 }] }', tests/harness.rs:32:10
stack backtrace:
   0: rust_begin_unwind
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/panicking.rs:64:14
   2: core::result::unwrap_failed
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/result.rs:1790:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/result.rs:1112:23
   4: integration_tests::main::{{closure}}
             at ./tests/harness.rs:28:5
   5: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/future/future.rs:125:9
   6: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/future/future.rs:125:9
   7: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::{{closure}}
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:541:57
   8: tokio::runtime::coop::with_budget
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/coop.rs:107:5
   9: tokio::runtime::coop::budget
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/coop.rs:73:5
  10: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:541:25
  11: tokio::runtime::scheduler::current_thread::Context::enter
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:350:19
  12: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:540:36
  13: tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:615:57
  14: tokio::macros::scoped_tls::ScopedKey<T>::set
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/macros/scoped_tls.rs:61:9
  15: tokio::runtime::scheduler::current_thread::CoreGuard::enter
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:615:27
  16: tokio::runtime::scheduler::current_thread::CoreGuard::block_on
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:530:19
  17: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:154:24
  18: tokio::runtime::runtime::Runtime::block_on
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/runtime.rs:302:47
  19: integration_tests::main
             at ./tests/harness.rs:34:5
  20: integration_tests::main::{{closure}}
             at ./tests/harness.rs:11:17
  21: core::ops::function::FnOnce::call_once
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/ops/function.rs:250:5
  22: core::ops::function::FnOnce::call_once
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Here is a source

You can try to run it using those commands

git clone git@github.com:chlenc/sway-script-transfer.git
cd sway-script-transfer
forc build
cargo test 
1 Like

Can you replace the script call with this so that we can see the receipts:

let receipts = instance
        .main(amount0, contract_id, Address::from(bob.address()))
        .call()
        .await;

dbg!(&receipts);

receipts.unwrap();

Some notes:

  • The main function’s third argument is named predicate_address but the test passes a wallet address
  • You can refer to these lines to see how OTC swap predicate builds a custom transaction to spend / recover the predicate . I can elaborate further once we see the receipts :slight_smile:
1 Like

So, with this code

use std::str::FromStr;

use fuels::{prelude::*, tx::ContractId};

abigen!(Script(
    name = "Script",
    abi = "out/debug/script_transfer-abi.json"
));
#[tokio::test]
async fn main() {
    let mint_amout = 1_000_000_000;

    let wallets_config = WalletsConfig::new(Some(2), Some(1), Some(mint_amout));
    let wallets = launch_custom_provider_and_get_wallets(wallets_config, None, None).await;

    let alice = wallets[0].clone();
    let alice_balance = alice.get_asset_balance(&BASE_ASSET_ID).await.unwrap();
    let bob = wallets[1].clone();
    let bob_balance = alice.get_asset_balance(&BASE_ASSET_ID).await.unwrap();

    assert_eq!(alice_balance, mint_amout);
    assert_eq!(bob_balance, mint_amout);

    let amount0 = mint_amout / 2;
    let contract_id = ContractId::from_str(BASE_ASSET_ID.to_string().as_str()).unwrap();
    let instance = Script::new(alice.clone(), "out/debug/script_transfer.bin");
    let receipts = instance
        .main(amount0, contract_id, Address::from(bob.address()))
        .call()
        .await;

    dbg!(&receipts);

    receipts.unwrap();

    assert_eq!(
        alice.get_asset_balance(&BASE_ASSET_ID).await.unwrap(),
        bob_balance + amount0
    );
}

I have output like that


running 1 test
test main ... FAILED

failures:

---- main stdout ----
[tests/harness.rs:32] &receipts = Err(
    RevertTransactionError {
        reason: "failed transfer to address.",
        revert_id: 18446744073709486081,
        receipts: [
            Revert {
                id: 0x0000000000000000000000000000000000000000000000000000000000000000,
                ra: 18446744073709486081,
                pc: 10544,
                is: 10344,
            },
            ScriptResult {
                result: Revert,
                gas_used: 125,
            },
        ],
    },
)
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: RevertTransactionError { reason: "failed transfer to address.", revert_id: 18446744073709486081, receipts: [Revert { id: 0000000000000000000000000000000000000000000000000000000000000000, ra: 18446744073709486081, pc: 10544, is: 10344 }, ScriptResult { result: Revert, gas_used: 125 }] }', tests/harness.rs:34:14
stack backtrace:
   0: rust_begin_unwind
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/panicking.rs:64:14
   2: core::result::unwrap_failed
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/result.rs:1790:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/result.rs:1112:23
   4: integration_tests::main::{{closure}}
             at ./tests/harness.rs:34:5
   5: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/future/future.rs:125:9
   6: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/future/future.rs:125:9
   7: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::{{closure}}
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:541:57
   8: tokio::runtime::coop::with_budget
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/coop.rs:107:5
   9: tokio::runtime::coop::budget
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/coop.rs:73:5
  10: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:541:25
  11: tokio::runtime::scheduler::current_thread::Context::enter
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:350:19
  12: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:540:36
  13: tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:615:57
  14: tokio::macros::scoped_tls::ScopedKey<T>::set
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/macros/scoped_tls.rs:61:9
  15: tokio::runtime::scheduler::current_thread::CoreGuard::enter
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:615:27
  16: tokio::runtime::scheduler::current_thread::CoreGuard::block_on
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:530:19
  17: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/scheduler/current_thread.rs:154:24
  18: tokio::runtime::runtime::Runtime::block_on
             at /Users/alexey/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.27.0/src/runtime/runtime.rs:302:47
  19: integration_tests::main
             at ./tests/harness.rs:36:5
  20: integration_tests::main::{{closure}}
             at ./tests/harness.rs:10:17
  21: core::ops::function::FnOnce::call_once
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/ops/function.rs:250:5
  22: core::ops::function::FnOnce::call_once
             at /rustc/065852def0903296da33a9eaf557f230bcf3a61a/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.


failures:
    main

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.28s

error: test failed, to rerun pass `-p script_transfer --test integration_tests`

P.S.
I have fixed the name of the variable in the script

1 Like

Is the issue just that you’re not providing UTXOs to the script?

When I’ve written scripts, I’ve had to manually collect and include UTXOs with the call (following the AMM example).

1 Like

Can you share the amm example please?
Or explain how to provide utxo to the script?

The problem was in the outputs

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.