Confusion of sdk and v2-sdk
Installing @uniswap/sdk returns a package from "https://github.com/Uniswap/uniswap-sdk.git" - but that URL forwards to this repository, which holds a v1.0.7 version. (not 3.0.3)
Is it now recommended to install @uniswap/v2-sdk ?
Then the docs on uniswap.org (which are also linked in the Readme of this repository) still talk about the old version and should be updated.
Also: I don't find the Fetcher source code (is it not part of the v2-sdk anymore?)
I'm here trying to figure out the same questions @TeNNoX !
@TeNNoX It looks like they redid the versioning and renamed this repository about a month back. You can see the old naming here with the v3.0.3, the name is @uniswap/sdk, and the Fetcher class still exists:
https://github.com/Uniswap/uniswap-v2-sdk/tree/a88048e9c4198a5bdaea00883ca00c8c8e582605
About a month back, the name changed to @uniswap/uniswap-v2-sdk, redid the numbering to start at 1.0.0, and the Fetcher class disappeared. It seems that npm hasn't been updated since these changes.
So it seems that the answer is yes, we should be using @uniswap/v2-sdk instead, and they might need some help updating the documentation on the site.
Having same issue.
The module @uniswap/uniswap-sdk gives me a dependency error, and I can't access the fetcher in @uniswap/uniswap-v2-sdk.
I have managed to follow some of their docs by importing dependencies such as ChainId from @uniswap/sdk-core, but this is a mess.
This is the best I could do using as much sdk-v2 code as possible. But the execution price retrieved is very wrong.
const { Token, WETH9, CurrencyAmount, TradeType } = require('@uniswap/sdk-core')
const { Route, Trade, Pair } = require('@uniswap/v2-sdk')
const { Fetcher } = require('@uniswap/sdk')
async function v2 () {
const CHAIN_ID = 1
const daiAddress = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
const tokenA = WETH9[CHAIN_ID]
const tokenB = new Token(CHAIN_ID, daiAddress, 18, 'DAI', 'DAI')
const legacyPair = await Fetcher.fetchPairData(tokenA, tokenB)
const reserveA = CurrencyAmount.fromFractionalAmount(tokenA, legacyPair.reserveOf(tokenA).numerator, legacyPair.reserveOf(tokenA).denominator)
const reserveB = CurrencyAmount.fromFractionalAmount(tokenB, legacyPair.reserveOf(tokenB).numerator, legacyPair.reserveOf(tokenB).denominator)
const pair = new Pair(reserveA, reserveB)
const route = new Route([pair], tokenA, tokenB)
const tokenAmount = CurrencyAmount.fromRawAmount(tokenA, '1000000000000000000')
const trade = new Trade(route, tokenAmount, TradeType.EXACT_INPUT)
console.log('execution price', trade.executionPrice.toSignificant(6))
console.log('inverse execution price', trade.executionPrice.invert().toSignificant(6))
}
Someone can spot what may be wrong?
Using just @uniswa/sdk package works fine though.
In the end, I let go of the Fetcher and called the pair contract directly for the reserves. See gist: https://gist.github.com/monokh/5dc494b9fb7887ac02d6898b6458648c
@uniswa/sdk is v1 @uniswa/sdk-v2 is v2 @uniswa/sdk-v3 is v3
The npm package @uniswap/sdk works for what is in the Uniswap v2 SDK documentation. But I'm not really sure what source is used for that. It seems that it is github.com/Uniswap/[email protected], as most things were removed in https://github.com/Uniswap/v2-sdk/commit/52266e31526481a3701bd8b3fc9876785f944d33, just before Uniswap/[email protected]
Well since the Fetcher class isn't exported anymore, here's a function to get Pair:
const getChainProvider = () => {
return (new ethers.providers.JsonRpcProvider(process.env.RPC));
}
const getPair = async (tokenA: Token, tokenB: Token) => {
const pairAddress = Pair.getAddress(tokenA, tokenB)
const contract = new ethers.Contract(pairAddress, [
'function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast)',
'function token0() external view returns (address)',
'function token1() external view returns (address)'
], getChainProvider());
const reserves = await contract.getReserves()
const token0Address = await contract.token0()
const token1Address = await contract.token1()
const token0 = [tokenA, tokenB].find(token => token.address === token0Address)
const token1 = [tokenA, tokenB].find(token => token.address === token1Address)
const pair = new Pair(
CurrencyAmount.fromRawAmount(token0, reserves.reserve0.toString()),
CurrencyAmount.fromRawAmount(token1, reserves.reserve1.toString())
)
return pair;
}
Source: https://gist.github.com/monokh/5dc494b9fb7887ac02d6898b6458648c