ckBTC Transfer from Canister After Approval

There is an example of how to use the approve / transfer_from flow with a canister here.

3 Likes

Okay, I have updated the code to print errors like this:

case (#Err(error)) {
                 let errorMessage = switch (error) {
                     case (#BadFee(_)) { "Bad fee" };
                     case (#InsufficientFunds(_)) { "Insufficient funds" };
                     case (#GenericError(_)) { "Generic error" };
                     case (#TemporarilyUnavailable) { "Temporarily unavailable" };
                     case (#BadBurn(_)) { "Bad burn" };
                     case (#Duplicate(_)) { "Duplicate" };
                     case (#CreatedInFuture(_)) { "Created in future" };
                     case (#TooOld) { "Too old" };
                 };
                 Debug.print(errorMessage);
                 return #err(errorMessage);
             };

When trying to execute this on the frontend, I received the following error:

Call was rejected: Request ID: 4af89411d85f9ae4fe2b44aa3626539433a66d02c36a631cef3b895075d5722f Reject code:  Reject text: Canister bza44-ciaaa-aaaan-qlvna-cai trapped explicitly: pattern failed```

[Canister Id](https://a4gq6-oaaaa-aaaab-qaa4q-cai.raw.icp0.io/?id=bza44-ciaaa-aaaan-qlvna-cai)

This will deduct ICRC1 tokens from the canister, correct? Not from the user’s wallet.

No, the canister has been approved to transfer funds from the users wallet.

2 Likes

@cryptoschindler

I encountered an error while deploying the canister using the code you provided.

expression of type
  Account__1 = {owner : Principal; subaccount : ?[Nat8]}
cannot produce expected type
  {owner : Principal; subaccount : ?Subaccount}

so i change to ?Subaccount

GIthub

2 Likes

I provided a fix here, can you try again and report back?

1 Like

Thank you for the quick fix. I will try it and get back to you.

zsh: command not found: dfx

Initially, everything was working fine. However, since I upgraded dfx, whenever I use the dfx command or anything related to it, I receive an error message.

Maybe you need to install dfx via dfxvm? Did you follow the steps outlined here?

I followed these steps:

First, I executed the command:

sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"

Then, I checked the installation by typing:

dfx --version

This worked until I closed the terminal. When I reopened the terminal and tried to check the version again with:

dfx --version

I received an error:

zsh: command not found: dfx

Please follow the steps outlined in Using the dfx version manager

1 Like

Hello @cryptoschindler

The dfx CLI error was solved; the problem was with the PATH. I have successfully deployed the above code, and the transfer one is working fine.

I have some questions related to that:

  1. If someone approves, can anyone transfer from the canister? How can this be secured?

  2. Is there any API so I can fetch the transactions of a particular canister?

I want to run my own canister code locally in the NNS. For running it locally, I have to follow this guide:

GitHub

  1. For good understanding of approve and transfer_from functions : ICRC-1/standards/ICRC-2/README.md at main · dfinity/ICRC-1 · GitHub
  2. You can check get_transactions/get_blocks functions in ledger canister of any icrc-2 std token to fetch transactions.
2 Likes

Thanks for the links. I’ll check them out!

@h1teshtr1path1, I’ve gone through the docs; it’s the same as the ERC-20 approve method that I know. My concern is that if I give the approve to the canister, then it can take the approved amount (x) from that user’s wallet. In the example canister, there is a transfer function that sends ICRC-1 tokens to any address once it gets the approval. I was referring to that. If you input your address and x amount and call the transfer function, it will transfer to that address. In the canister, when you call the transfer function, it will not pop up the wallet because it will use cycles for that. So, is there anything I can put, like an owner cap type, so that x address can withdraw this amount?