New ICRC-2 Deposits+Withdrawals Motoko Example Now in the DFINITY Examples Repository

Hello IC Developers!

I’m excited to share a new addition to the DFINITY examples repository - the “ICRC-2 Swap Example.” This example is specially designed to show how to securely handle deposits and withdrawals for ICRC-2 tokens, and show common attacks and remediations.

The ICRC-2 Swap Example is a response to the complex challenges DeFi developers face in managing token deposits and withdrawals. Our focus is on providing a minimalist, easy-to-understand canister example that safely interacts with ICRC-2 tokens. This example is particularly timely and relevant with the ICP ledger canister’s evolution towards ICRC-2.

To avoid confusing new learners, I’ve kept the example as simple as possible. It handles three methods, “deposit”, “swap”, and “withdraw” (with a “balances” method added for debugging). The “swap” is not strictly necessary, but it acts as a simple motivating use-case for the code to show how developers can work with with deposited balances.

The example also includes several test-cases. The purpose of these javascript tests is to show how a potential frontend would use the provided example canister.

Check out the example code at: https://github.com/dfinity/examples/tree/master/motoko/icrc2-swap

(Big shout-out to @Severin for helping get this over-the-line :tada:)

11 Likes