Move.

Post

Share your knowledge.

Ramirez.
Feb 13, 2025
Discussion

Taking a fee from a coin in a DEX swap operation

I've been struggling with implementing a contract for a week that applies a fee to a coin used in a DEX swap. It seems I can't read the coin's SUI amount from the output due to it being consumed in the transaction. How do I take a fee atomically if functions like swap_a2b don't return a coin?

  • Move CLI
  • Move Bug
0
2
Share
Comments
.

Answers

2
1 Luca.
Feb 13 2025, 11:08

If you consume the coin for a transaction, it can't be used again. If you're using it for a gas payment, it's a one-time use. When it comes to DEX swaps, once you pass a coin like outCoinA in a function swap, it gets consumed, and subsequent calls won't recognize it unless returned explicitly. The main issue could be that swap_a2b doesn't return anything, meaning you can't directly take a fee after a swap operation. A workaround could be to take ownership of the coin to be swapped and, after executing the swap, subtract the fee before transferring the result back to the user.

0
Comments
.
CarlkawIy.
Feb 13 2025, 11:10

Wrapping the swap functionality in a smart contract can solve this problem. By executing the swap within the contract, you can manage ownership and distribution to handle fees atomically. The contract would execute the swap, handle the fee subtraction, and then distribute the remaining amount back to the user. Here's an approach in the PTB context: 1. split the coin into a separate coinIn, 2. pass this coin to the smart contract for the swap, fee handling, and return to user step. You don't necessarily need to transfer the coin object explicitly before step 2 if the smart contract takes coinIn by value.

0
Comments
.

Do you know the answer?

Please log in and share it.

We use cookies to ensure you get the best experience on our website.
More info