# Transferring funds from wallet to wallet

**URL:** <https://forum.fuel.network/t/transferring-funds-from-wallet-to-wallet/8236>\
**Category:** TypeScript SDK\
**Created:** [January 22, 2025, 5:06pm UTC](https://forum.fuel.network/t/transferring-funds-from-wallet-to-wallet/8236 "2025-01-22T17:06:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antik21](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/antik21/32/4010_2.png) [@Antik21](https://forum.fuel.network/u/Antik21)\
**Post date:** [January 22, 2025, 5:06pm UTC](https://forum.fuel.network/t/transferring-funds-from-wallet-to-wallet/8236/1 "2025-01-22T17:06:24Z")

</div>

Good day,

I implemented a function to send a specified amount of funds from one wallet to another.  
I added the calculation of fees and gas. Everything worked well when I sent the entire wallet balance.

However, when I sent only 50%, I encountered a situation where the remaining funds in the sender’s wallet disappeared.  
**Transaction via my code**

> **[Fuel Ignition is Live!](https://app.fuel.network/tx/0xcb8250cfc05c6d18eb6f438f120edff1291d83792bceafd5d8e3f009948ed020/standard)**
>
> Fuel Ignition is a high-performance Ethereum layer-2 rollup powered by the FuelVM; built for home verification and scalable for all.

> **[Fuel Ignition is Live!](https://app.fuel.network/tx/0xcb69b234f6bd07e1d2b7f8486ff96cfd6942461dec3d133caf986a7c1cc65fcb/standard)**
>
> Fuel Ignition is a high-performance Ethereum layer-2 rollup powered by the FuelVM; built for home verification and scalable for all.

After that, I compared the transaction generated by my method with the one created by the Fuel wallet.  
I discovered that my transaction lacks the `CHANGE` block, which contains the remaining wallet balance.  
**Transaction via wallet**

> **[Fuel Ignition is Live!](https://app.fuel.network/tx/0x41406702bb687281c51d638334051b0e0daa409ffa00b9288db01afcb63ec1a5/standard)**
>
> Fuel Ignition is a high-performance Ethereum layer-2 rollup powered by the FuelVM; built for home verification and scalable for all.

I would like to ask for your assistance:

1. What is the mistake in my code?

```auto

export async function transferWithFeeAdjustment(
    wallet: WalletUnlocked,
    destination: AddressLike,
    assetId: BytesLike,
    amountToSend: BN
): Promise<void> {
    const txRequest = new ScriptTransactionRequest();

    const resources = await wallet.getResourcesToSpend([
        { amount: amountToSend, assetId },
    ]);
    txRequest.addResources(resources);

    txRequest.addCoinOutput(destination, amountToSend, assetId);

    const chainInfo = wallet.provider.getChain();
    const minGas = txRequest.calculateMinGas(chainInfo);
    const maxGas = txRequest.calculateMaxGas(chainInfo, minGas);

    txRequest.maxFee = maxGas.mul(1.1);

    const txCost = await wallet.getTransactionCost(txRequest);

    const transactionFee = txCost.maxGas
    txRequest.gasLimit = transactionFee;

    if (amountToSend.lte(transactionFee)) {
        throw new Error(
            "Insufficient balance to cover transaction fee. Please add more funds."
        );
    }

    const adjustedAmount = amountToSend.sub(transactionFee);
    if (adjustedAmount.lte(new BN(0))) {
        throw new Error(
            "Insufficient amount after deducting transaction fee. Unable to proceed."
        );
    }

    txRequest.outputs = [];
    txRequest.addCoinOutput(destination, adjustedAmount, assetId);

    const response = await wallet.sendTransaction(txRequest);

     await retry(async () => {
        return await response.wait();
    }, 10);

    return Promise.resolve()
}

```

1. I found documentation on sending funds using a Predicate. Would it be better for me to use this method instead?  
[Fuel TS SDK | Fuel Docs](https://docs.fuel.network/docs/fuels-ts/predicates/send-and-spend-funds-from-predicates/#predicate-validation-failure)

@p.s @naz3eh

---

<div class="post-metadata">

**Author:** ![p.s](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/p.s/32/2491_2.png) [@p.s](https://forum.fuel.network/u/p.s)\
**Post date:** [January 22, 2025, 5:19pm UTC](https://forum.fuel.network/t/transferring-funds-from-wallet-to-wallet/8236/2 "2025-01-22T17:19:13Z")

</div>

Hey @Antik21 👋

The following line will be your issue, as within the `addResources` method, we also add the associated change to the output.

```ts
txRequest.outputs = [];

```

* * *

I’d suggest to use the [`transfer`](https://docs.fuel.network/docs/fuels-ts/wallets/wallet-transferring/#example) or `createTransfer` function:

```ts
const response = await wallet.transfer(
  destination,
  amountToSend,
  assetId
);

```

This will return any unused coins from the transfer back the sender 🙂

---

<div class="post-metadata">

**Author:** ![Antik21](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/antik21/32/4010_2.png) [@Antik21](https://forum.fuel.network/u/Antik21)\
**Post date:** [January 22, 2025, 5:40pm UTC](https://forum.fuel.network/t/transferring-funds-from-wallet-to-wallet/8236/3 "2025-01-22T17:40:18Z")

</div>

You are right!  
Thk you!

Now i use

```auto
const response = await wallet.transfer(
        destination,
        adjustedAmount,
        assetId
    );

```

Istead of

```auto
 txRequest.outputs = [];
 txRequest.addCoinOutput(destination, adjustedAmount, assetId);
  const response = await wallet.sendTransaction(txRequest);

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/5/57d5a345cc15a64b636e0d56e042857f8a0e80b1.png) [@system](https://forum.fuel.network/u/system)\
**Post date:** [February 12, 2025, 1:40pm UTC](https://forum.fuel.network/t/transferring-funds-from-wallet-to-wallet/8236/4 "2025-02-12T13:40:42Z")

</div>

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