alpha-wallet-ios icon indicating copy to clipboard operation
alpha-wallet-ios copied to clipboard

Add Swap feature

Open colourfreak opened this issue 3 years ago • 9 comments

How to access Swap feature?

  • For now, we let users to access the swap feature from the token menu (we will keep Send and Swap as main actions) ETH-Short

  • The second option will be in the dotted menu at the top Wallet-Menu

  • When we will test it positively (I know how bad it sounds nowadays), we can transform the tab bar so it has one more button int the centre. Wallet-New Wallet-Actions

Invision prototype

https://invis.io/Z412DM7HGXTF

Screenshots

  • Default view Swap-Empty

  • Token selection Swap-Select-Token

  • Settings (you access in the top right icon) Swap-Settings

  • Tokens selected, no amount entered Swap-Default

  • Entering amount Swap-Entered

  • You can expand the Fees view Swap-Expanded

  • Confirmation Swap-Confirmation

Zeplin:

https://zpl.io/7wkvBnW

Working on:

Few more states, when the price has been updated, no search results when selecting a token, switching to networks etc.

colourfreak avatar Feb 15 '22 10:02 colourfreak

@oa-s if you haven't, don't work on the actionsheet confirmation screen yet. I might add it on my side. i.e. the last screenshot above.

hboon avatar Mar 14 '22 06:03 hboon

@oa-s if you haven't, don't work on the actionsheet confirmation screen yet. I might add it on my side. i.e. the last screenshot above.

got it

oa-s avatar Mar 14 '22 07:03 oa-s

Working on Entering amount, Token selection, settings screens

oa-s avatar Mar 14 '22 07:03 oa-s

Working on Entering amount, Token selection, Settings screens

oa-s avatar Mar 14 '22 07:03 oa-s

Thanks. Yeah, everything else except the confirmation actionsheet.

hboon avatar Mar 14 '22 07:03 hboon

@oa-s The backend code is available in the https://github.com/AlphaWallet/alpha-wallet-ios/tree/swap branch. Would you help to integrate it into the UI you are working on and add missing pieces?

The li.fi API supports both swap (same-chain) and bridging (cross-chain). We only implement swaps at the moment, so fromChain and toChain are always the same.

SwapViewController is only for development and should be removed. SwapViewController and TokenSwapCoordinator contains the driver code for swapping currently. For example displaying a list of quotes for the user to try from isn't implemented. Meta information and options like slippage isn't implemented too. We can skip them first. I haven't made the code fit into TokenActionsProvider yet. See if you want to leave it to a future PR.

There are actual UI integration and bits and pieces left to do; as usual, look out for the //hhhs and NSLog("xxxs.

To start the swap process, tap on the blockie button in Wallet tab to launch SwapViewController:

Screenshot 2022-04-06 at 1 24 55 PM

The 3 buttons correspond to the 3 endpoints li.fi provides:

  • "Fetch Chains" — get supported chains. Use this to limit which chains the user can choose in the swap UI
  • "Fetch Tokens" — get supported swap "pairs" for selected chain. This is parsed into a SwapPairs object that return a list of "to" tokens given a "from" token. i.e we can use SwapPairs to popular to the list of "to" tokens based on user's selection
  • "Swap" — fetch the transaction (in li.fi terminology the "quote") that will perform the swap functionality. This will sometimes require an ERC20 approval. Look for SwapViewController.swap() which fetches the transaction, checks if an ERC20 is required (prompt if necessary) and then prompts the user to sign the swap transaction.

Fetch chains

curl --url 'https://li.quest/v1/chains' --header 'Accept: application/json' --header 'Content-Type: application/json' | json_pp

Fetch Tokens

curl --url 'https://li.quest/v1/connections?fromChain=1&toChain=1' --header 'Accept: application/json' --header 'Content-Type: application/json' | json_pp

Swap

curl --request GET --url 'https://li.quest/v1/quote?fromChain=137&toChain=137&fromToken=0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619&toToken=0x0000000000000000000000000000000000000000&fromAddress=0xbbce83173d5c1D122AE64856b4Af0D5AE07Fa362&fromAmount=1000000000000000&order=BEST_VALUE&slippage=0.05' --header 'Accept: application/json' | json_pp

The quote API returns 503 sometimes. Just drop or retry.

If the from amount is too small, this error might be returned:

"message": "Unable to find a quote for the requested transfer."

Docs

The API docs are at https://apidocs.li.finance/reference/welcome-to-the-lifinance-api

The JavaScript SDK examples are easy to read to understand how to use the APIs https://apidocs.li.finance/reference/how-to-transfer-tokens

Others

Remember to use Polygon where possible. Cheaper to try

hboon avatar Apr 06 '22 05:04 hboon

Re-opening since it's not ship-ready yet.

hboon avatar Jun 06 '22 04:06 hboon

@oa-s what's left? Should I test more with master?

hboon avatar Jun 06 '22 04:06 hboon

@hboon needs to test

hboon avatar Jun 28 '22 03:06 hboon

Already done

hboon avatar Mar 28 '23 03:03 hboon