0

Can someone explain how to swap, usdt to usdc, eth to usdt, eth to usdc etc. Without any third-party (uniswap, 1inch etc). In language java/kotlin. For mobile wallet an application

1 Answer 1

0

Tokens are smart contract living on the blockchain. e.g : Matic contract: https://etherscan.io/token/0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0#code

Swaping tokenA for tokenB imply :

  • interacting with each of these token's smart contract through their functions (transfer, transferFrom, approve, ...)
  • calculating an exchange rate between both tokens (multiple way to do so: k=x*y, oracle, twap, ...)
  • Having access to token A and B (either holding them itself, or having approval from other addresses) in order to execute the requested swap.

Usually token swap is done on-chain by a smart-contract. This is what will make the swap atomic: only one tx executing the whole flow of operation, if something fails whole tx is reverted and nothing happened. This make the operation trustless.

But to answer your question, if you would do it off-chain:

  1. you need the user to send you X tokenA (you can make him sign an approve tx, then execute a transferFrom of the approved value to your app onchain address)
  2. then calculate exchange rate off-chain
  3. Finally sending him Y tokenB using transfer.

This would require at least 2 different tx. And user cannot be sure that tx n°2 (tokenB transfer) will be executed (call could revert)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.