Is there a way to "withdraw" ICP held by a canister to wallet(s)

Hi there! :slight_smile:

Since people say that it’s impossible to have a method inside of a canister that acts similar to ERC721 withdraw methods that are widely implemented in ETH contracts which allow withdrawing of native ETH from them to either the caller’s wallet or a specified wallet address, I am wondering is there any way of replicating this kind of functionality with canisters right now on ICP, or are we stuck at always instantly sending out whatever amount of ICP that we receive from someone in the canister out to the specified account address(es)?

1 Like

I’m not sure what you mean. Since canisters can have ledger accounts you can implement this pattern.

1 Like

Nevermind, I think I got confused :slight_smile: I stumbled upon the “ledger_transfer” example in official examples repository that shows how to transfer ICP from that canister to any other account

1 Like

I had success with the following code:

#[update(name = "send_balance")]
#[candid_method(update, rename = "send_balance")]
async fn send_balance() -> Result<BlockIndex, String> {
    let transfer_args = ic_ledger_types::TransferArgs {
        memo: Memo(0),
        amount: Tokens::from_e8s(100000) - Tokens::from_e8s(10_000),
        fee: Tokens::from_e8s(10_000),
        from_subaccount: Some(DEFAULT_SUBACCOUNT),
        to: AccountIdentifier::new(
            &Principal::from_str("your-principal-here")
                .unwrap(),
            &DEFAULT_SUBACCOUNT,
        ),
        created_at_time: None,
    };

    ic_ledger_types::transfer(MAINNET_LEDGER_CANISTER_ID, transfer_args)
        .await
        .map_err(|e| format!("failed to call ledger: {:?}", e))?
        .map_err(|e| format!("ledger transfer error {:?}", e))
}
2 Likes

Yeah, thank you :slight_smile: I only use Motoko and this post was mostly made due to some confusion I had at the time of making it, but it looks like it’s pretty easy to do and there’s an example of how to do it in the “ledger_transfer” repository of the official “dfinity examples” repo on Github.