Fetching Data
Looking for a quickstart?
While the SDK is fully self-contained, there are two cases where it needs on-chain data to function. This guide will detail both of these cases, and offer some strategies that you can use to fetch this data.
#
Case 1: TokensUnsurprisingly, the SDK needs some notion of an ERC-20 token to be able to function. This immediately raises the question of where data about tokens comes from.
As an example, let's try to represent DAI in a format the SDK can work with. To do so, we need at least 3 pieces of data: a chainId, a token address, and how many decimals the token has. We also may be interested in the symbol and/or name of the token.
#
Identifying DataThe first two pieces of data โ chainId and token address โ must be provided by us. Thinking about it, this makes sense, as there's really no other way to unambiguously identify a token.
So, in the case of DAI, we know that the chainId is 57
(we're on syscoin mainnet), and the token address is 0xeFAeeE334F0Fd1712f9a8cc375f427D9Cdd40d73
. Note that it's very important to externally verify token addresses. Don't use addresses from sources you don't trust!
#
Required DataThe next piece of data we need is decimals.
#
Provided by the UserOne option here is to simply pass in the correct value, which we may know is 18
. At this point, we're ready to represent DAI as a Token:
If we don't know or don't want to hardcode the value, we could look it up ourselves via any method of retrieving on-chain data in a function that looks something like:
#
Fetched by the SDKIf we don't want to provide or look up the value ourselves, we can ask the SDK to look it up for us with Fetcher.fetchTokenData
By default, this method will use the default provider defined by ethers.js. If you're already using ethers.js in your application, you may pass in your provider as a 3rd argument. If you're using another library, you'll have to fetch the data separately.
#
Optional DataFinally, we can talk about symbol and name. Because these fields aren't used anywhere in the SDK itself, they're optional, and can be provided if you want to use them in your application. However, the SDK will not fetch them for you, so you'll have to provide them:
or:
#
Case 2: PairsNow that we've explored how to define a token, let's talk about pairs. To read more about what Pegasys pairs are, see Pair
As an example, let's try to represent the DAI-WSYS pair.
#
Identifying DataEach pair consists of two tokens (see previous section). Note that WSYS used by the router is exported by the SDK.
#
Required DataThe data we need is the reserves of the pair. To read more about reserves, see getReserves.
#
Provided by the UserOne option here is to simply pass in values which we've fetched ourselves to create a Pair:
Note that these values can change as frequently as every block, and should be kept up-to-date.
#
Fetched by the SDKIf we don't want to look up the value ourselves, we can ask the SDK to look them up for us with Fetcher.fetchTokenData:
By default, this method will use the default provider defined by ethers.js. If you're already using ethers.js in your application, you may pass in your provider as a 3rd argument. If you're using another library, you'll have to fetch the data separately.
Note that these values can change as frequently as every block, and should be kept up-to-date.