How do i notify a canister upon receiving ICP transferred to it

I have a wallet that I’ve created. I want to be able to update users’ transaction history as they send and receive ICP. How do i go about notifying a canister whenever is has received ICP?

I know of three options:

  • Polling. The canister checks occasionally if its balance has changed
  • Some notify_ function on the canister that can be called with a block index so that the canister can go look up the new transaction
  • Let the canister itself do the transaction. We now have icrc2_transfer_from, which allows you to tell a canister that it shall deposit some ICP to itself
2 Likes

The
’notify_’ functions sounds like the best option for me in my case. Is there a specific function signature that the ‘notify_’ function must conform to?

No, that’s just a common pattern, nothing really standardised. Here are two examples of notify_ functions that we have in the NNS.

1 Like

After looking at the link you referenced, I realized that this actually doesn’t resolve my challenge. I intend for a canister, that I control, to be able to know when it has received an incoming transaction and upon knowing that an incoming transaction was made, that canister would then update an internal stable object that is used to keep track of the canister’s transaction history.

As the current solution, I implemented the polling method you described. Every 15 seconds or so, the canister queries the blockchain and makes updates to its transaction history when appropriate. The issue with this method is that it requires a constant timer which consumes many cycles.

I was hoping that there was some sort of system function signature that would allow me to program some logic to fire whenever an incoming transaction is detected; similar to how the heartBeat function allows you program logic thats fired upon each block propogation. I thought that’s what you were referring to when you mentioned the notify_ function earlier.

is there some workaround you have in mind which utilizes the icrc2_transfer_from method to allow me to achieve the desired effect of essentially having a canister “listen” for incoming transactions without relying on the Timer API?

pub / sub would be the pattern that you want, but that’s not a feature that the ICP ledger supports.