Is it possible to transfer an asset from Alice to Bob using a script?

Hi guys
Is it possible to transfer an asset from Alice to Bob using a script Alice calls?
For example, Alice has 1ETH and wants to send 0.5ETH to Bob
She can do it like that

alice
   .transfer(bob.address(), 500_000_000, BASE_ASSET_ID, TxParameters::default())
   .await
   .unwrap();

But I want to make a transfer using a script.
So, I wrote that script:

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

and trying to run it using that 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; //1 eth

    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()))
        .tx_params(TxParameters::default().set_gas_price(1))
        .call()
        .await;

    dbg!(&receipts);

    receipts.unwrap();

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

But have this output

running 1 test
test main ... FAILED

failures:

---- main stdout ----
[tests/harness.rs:33] &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:35: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:35: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:37: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.30s

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

Here is a source

You can try to run it using those commands

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

Please help

3 Likes

The problem was in outputs

3 Likes

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