I am trying to understand how private transactions can work with applications within the Aztec protocol.
For example, if you have an AMM implemented on the L2 that have an ETH-USDC pool, can a user execute a private swap transaction with this pool?
Here is how I imagine the transaction execution would work. The user sends the ETH to be swapped to the AMM as a private TX. The AMM then exposes the ETH mount to execute the swap. The resulting USDC will be sent as a private balance to the user’s address.
Is this flow possible?
Hello @Fouda, yes the flow you described is possible and quite easy to do. You would use the unshielding and shielding flow for that. Here is how it would work:
- you choose a random secret,
- you compute a hash of that secret (secret_hash)
- you call a private function swap(token_in, token_out, secret_hash, amount)
- This swap function spends/nullifies a note (or multiple notes) containing your private eth balance,
- then a pubic function would get called by the private function. The function would be something like swap_public(token_in, token_out, secret_hash, amount). This function would do the actual swap on AMM and then it would shield the resulting funds (USDC) by creating a TransparentNote with the secret hash. See this function for a real-world example of shielding.
- User would call redeem_shield(secret) function, the function would check whether hash(secret) matches secret hash and if yes, it would create a new note representing the usdc balance. Example is here.
Let me know if something is not clear. Feel free to also check the e2e_token.test.ts in aztec-packages repository to see how the flow is triggered from typescript.
Thanks @Jan for the detailed answer and even mentioning the functions.