Generating token swapping paths
In this section, we will dive into the implementation of generating swapping paths. Similar to the liquidity provisioning page, AddLiquidity.js
, which we discussed in Chapter 6, Implementing a Liquidity Management Frontend with Web3 we use the TokenSelectModal
component for token selection. Once both tokens are selected, we will find all available paths from one token to another token. By reading this section, you will learn how to write the code to build the graph with the token pair information of a DEX, and how to find the swapping paths once after the tokens are selected.
Building the graph for token pairs
In order to find all available paths from spending tokens to receiving tokens, we can use the data structure of a bidirectional graph to represent all the token pairs of the DEX. The addresses of supported tokens of the DEX are the graph nodes, and all the token pairs (the smart contract instance of TokenPair
that represents the paired tokens...