alpha-wallet-ios
alpha-wallet-ios copied to clipboard
Add Swap feature
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)

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

-
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.

Invision prototype
https://invis.io/Z412DM7HGXTF
Screenshots
-
Default view

-
Token selection

-
Settings (you access in the top right icon)

-
Tokens selected, no amount entered

-
Entering amount

-
You can expand the Fees view

-
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.
@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.
@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
Working on Entering amount, Token selection, settings screens
Working on
Entering amount,Token selection,Settingsscreens
Thanks. Yeah, everything else except the confirmation actionsheet.
@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:
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
SwapPairsobject that return a list of "to" tokens given a "from" token. i.e we can useSwapPairsto 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
Re-opening since it's not ship-ready yet.
@oa-s what's left? Should I test more with master?
@hboon needs to test
Already done