# How to send ICP from one account to another using motoko?

**URL:** <https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061>\
**Category:** Developers\
**Tags:** Functional-Programming\
**Created:** [August 24, 2022, 6:33am UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061 "2022-08-24T06:33:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Persu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/persu/32/7530_2.png) [@Persu](https://forum.dfinity.org/u/Persu)\
**Post date:** [August 24, 2022, 6:33am UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/1 "2022-08-24T06:33:56Z")

</div>

Hi guys, Just wanted to know how can we transfer ICP from one account to another using motoko. I have tried in my local using the command line like:

```auto
dfx wallet send <wallet id> <amount>

```

How can we achieve this using motoko. Is there any function?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [August 24, 2022, 7:50am UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/2 "2022-08-24T07:50:15Z")

</div>

Please have a look at the [ledger-transfer](https://github.com/dfinity/examples/tree/master/motoko/ledger-transfer) example. It contains the function you asked about.

---

<div class="post-metadata">

**Author:** ![Persu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/persu/32/7530_2.png) [@Persu](https://forum.dfinity.org/u/Persu)\
**Post date:** [August 24, 2022, 4:41pm UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/3 "2022-08-24T16:41:17Z")

</div>

```auto
public func transferFunds(){
    let now = Time.now();
    let res = Ledger.transfer({
      amount = { e8s = 2_000_000_000 };
      created_at_time = ?{ timestamp_nanos = Nat64.fromNat(Int.abs(now)) };
      fee = { e8s = 10_000 };
      from_subaccount = null;
      memo = 42;
      to = "22oak-uehh2-5fc2j-rdhlb-zypwb-hwgyq-kgrfl-tvait-nwqof-hssiv-3qe";
    });
  };  

```

Well I write this function to transfer ICP from the caller account to the other account having principal id = 22oak-uehh2-5fc2j-rdhlb-zypwb-hwgyq-kgrfl-tvait-nwqof-hssiv-3qe… The function execute without any error, but what funny is neither the caller account balance got debited, nor the receiver account got credited.  
What am I doing wrong, and what is the current approach?

---

<div class="post-metadata">

**Author:** ![Seb](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/seb/32/10340_2.png) [@Seb](https://forum.dfinity.org/u/Seb)\
**Post date:** [August 24, 2022, 6:09pm UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/4 "2022-08-24T18:09:46Z")

</div>

You can only send ICPs between **accounts**.  
Do not use the principal as sender or receiver. You need to convert the principal to the main account identifier.  
Take a look a this section : [The ICP Ledger | Internet Computer Home](https://internetcomputer.org/docs/current/developer-docs/functionality/ledger/#accounts)

You can compute the (main) account programmatically or just take a look on ICScan that does it automatically for you.

[https://icscan.io/account/31822f05e22dffe8b6461467ed27efc55c12415b77335078e31868ef307c61b0](https://icscan.io/account/31822f05e22dffe8b6461467ed27efc55c12415b77335078e31868ef307c61b0)

---

<div class="post-metadata">

**Author:** ![Persu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/persu/32/7530_2.png) [@Persu](https://forum.dfinity.org/u/Persu)\
**Post date:** [August 25, 2022, 6:04am UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/5 "2022-08-25T06:04:20Z")

</div>

```auto
public func transferFunds(){
    let now = Time.now();
    var main_account_principal = Principal.fromText("22oak-uehh2-5fc2j-rdhlb-zypwb-hwgyq-kgrfl-tvait-nwqof-hssiv-3qe");
    let res = Ledger.transfer({
      amount = { e8s = 2_000_000_000 };
      created_at_time = ?{ timestamp_nanos = Nat64.fromNat(Int.abs(now)) };
      fee = { e8s = 10_000 };
      from_subaccount = null;
      memo = 42;
      to = Account.accountIdentifier(main_account_principal, Account.defaultSubaccount());
    });
  };  

```

Well I have changed my code, but the problem remain the same, the accounts are not getting updated after the function call.  
Note: I am doing in my local environment.

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [August 25, 2022, 6:12am UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/6 "2022-08-25T06:12:30Z")

</div>

Can you inspect the value of `res`? Maybe you can just return it? It should either contain confirmation that everything worked or it should contain an error of some sort. In my experience with the ledger, the error messages were always good explanations what went wrong.

Wild guess since you’re developing locally: does your source account even have enough tokens to transfer?

---

<div class="post-metadata">

**Author:** ![Persu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/persu/32/7530_2.png) [@Persu](https://forum.dfinity.org/u/Persu)\
**Post date:** [August 25, 2022, 7:05am UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/7 "2022-08-25T07:05:19Z")

</div>

Well well well!!!  
what is the command to check the balance of the account?  
Is it “dfx wallet balance” or “dfx ledger balance”?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [August 25, 2022, 8:44am UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/8 "2022-08-25T08:44:04Z")

</div>

The dfx command to check the ICP balance is `dfx ledger balance <account id>` (maybe with `--network ic`).

To figure out the account identifier, use `dfx ledger account-id` to produce the identifier. Use `dfx ledger account-id --help` to see how to use the command.

`dfx wallet balance` shows you the (cycles) balance of your cycles wallet.

---

<div class="post-metadata">

**Author:** ![Persu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/persu/32/7530_2.png) [@Persu](https://forum.dfinity.org/u/Persu)\
**Post date:** [August 25, 2022, 10:38am UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/9 "2022-08-25T10:38:33Z")

</div>

Referring to this [code](https://github.com/dfinity/examples/tree/master/motoko/ledger-transfer) again for clear understanding!

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/4/4d9142607c0c46e588149344a1cda021fb845fd7.png)

The code runs well. After I hit the command

```auto
dfx canister call ledger_transfer distributeRewards '()'

```

my canister balance get deducted… But can you tell me which account get credited and how can I check that!?

Got this response after the final command…

```auto
dfx canister call test_backend distributeRewards '()'
(opt principal "2vxsx-fae")

```

Thanks!

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [August 25, 2022, 11:29am UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/10 "2022-08-25T11:29:10Z")

</div>

Looking at the function [distributeRewards](https://github.com/dfinity/examples/blob/master/motoko/ledger-transfer/src/ledger_transfer/main.mo#L66-L116):

- first it figures out the ‘most prolific author’ by the number of posts created
- then it checks the most prolific autor
  - if it’s empty, do nothing (no posts created yet, so noone to tranfer tokens to)
  - otherwise: transfer tokens to that person

The transfer happens like this:

```auto
// If there is a winner, transfer 1 Token to the winner.
let res = await Ledger.transfer({
    memo = Nat64.fromNat(maxPosts);
    from_subaccount = null;
    to = Account.accountIdentifier(principal, Account.defaultSubaccount());
    amount = { e8s = 100_000_000 };
    fee = { e8s = 10_000 };
    created_at_time = ?{ timestamp_nanos = Nat64.fromNat(Int.abs(now)) };
 })

```

and it returns the principal of the most prolific author.

Side note: the principal `2vxsx-fae` is the so-called anonymous identity, which everyone is allowed to use. If you do `dfx identity use anonymous` your requests will be sent using that principal.

To figure out the account that got credited, look at the above code block:  
`to = Account.accountIdentifier(principal, Account.defaultSubaccount());`

- principal = `2vxsx-fae` (as returned by the function in the end)
- subaccount = default subaccount = empty string
- ledger account = `dfx ledger account-id --of-principal "2vxsx-fae"` = `1c7a48ba6a562aa9eaa2481a9049cdf0433b9738c992d698c31d8abf89cadc79`. This is where the tokens get sent to.

---

<div class="post-metadata">

**Author:** ![Safik](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/safik/32/953_2.png) [@Safik](https://forum.dfinity.org/u/Safik)\
**Post date:** [November 14, 2022, 7:44am UTC](https://forum.dfinity.org/t/how-to-send-icp-from-one-account-to-another-using-motoko/15061/11 "2022-11-14T07:44:22Z")

</div>

Hi  
My question is closely related  
([Canister financing issues](https://forum.dfinity.org/t/canister-financing-issues/16498))

```auto
 public shared({caller}) func ledger_transfer_icp(
        icp_amount: Nat) : async TransferResult{
        var amount = Nat64.fromNat(icp_amount);
        var res: TransferResult = #Err(#TxCreatedInFuture);
        // let to_ai = Account.principalToSubaccount(Principal.fromText(Const.canister_project_cycles_wallet));
        let to_ = Account.accountIdentifier(
            Principal.fromText("3sq5w-t7zis-qf3wl-vgvih-byre2-ttswt-vzupn-6mnpw-mju7l-jhbfi-hae"), Account.defaultSubaccount());
        try{
            res := await public_ledger.transfer({
                to = to_;
                fee = { e8s = 10_000; };
                memo = 0;
                from_subaccount = null;
                created_at_time = null;
                amount = { e8s = amount - 10_000 };
            });
            return res;
        }
        catch(e){ };
        return res;
    };

```

As a result of the error

Below is the output from the command line

 ![scrin](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/a/a58af40a491d7f8c0865778d1308fb7586f0b395.png)

_ygvtn-qaaaa-aaaan-qa32a-cai - my canister cycles wallet_

_mxjrx-tiaaa-aaaah-aaoxq-cai - my canister where deploy test code_

_mlx7d-nlzwm-jsiyr-txxc2-mlgsf-hafo6-73wnd-du4xx-f2tsd-mjtum-pae - everywhere is conrtroller_

I have replenished the balance wherever possible (on the screen)

I don’t understand where the error is.  
Can I deploy the code in the cycles wallet? (although this is nonsense…)
