Skip to content

Commit

Permalink
fix: uni-v2 DeFi opportunities resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre committed Feb 19, 2024
1 parent 566db58 commit dc47e9c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/contracts/contractManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ethers } from 'ethers'
import memoize from 'lodash/memoize'
import type { Address } from 'viem'
import { getContract } from 'viem'
import { getEthersProvider } from 'lib/ethersProviderSingleton'
import { getEthersProvider, getEthersV5Provider } from 'lib/ethersProviderSingleton'
import { viemClientByChainId, viemEthMainnetClient } from 'lib/viem-client'

import {
Expand Down Expand Up @@ -119,7 +119,7 @@ export const fetchUniV2PairData = memoize(async (pairAssetId: AssetId) => {
type: ContractType.UniV2Pair,
chainId: KnownChainIds.EthereumMainnet,
})
const ethersProvider = getEthersProvider() as Provider
const ethersV5Provider = getEthersV5Provider()

const token0Address = await pair.read.token0()
const token1Address = await pair.read.token1()
Expand All @@ -142,19 +142,13 @@ export const fetchUniV2PairData = memoize(async (pairAssetId: AssetId) => {
const token0: Token = await Fetcher.fetchTokenData(
Number(asset0EvmChainId),
asset0Address,
// @ts-ignore TODO(gomes): FIXME, this is 99% likely to fail because ethers v6 now uses BigInt instead of BigNumber
// We may want to patch @uniswap/sdk while https://github.com/Uniswap/web3-react/issues/812 is open
ethersProvider,
ethersV5Provider,
)
const token1: Token = await Fetcher.fetchTokenData(
Number(asset1EvmChainId),
asset1Address,
// @ts-ignore TODO(gomes): FIXME, this is 99% likely to fail because ethers v6 now uses BigInt instead of BigNumber
// We may want to patch @uniswap/sdk while https://github.com/Uniswap/web3-react/issues/812 is open
ethersProvider,
ethersV5Provider,
)

// @ts-ignore TODO(gomes): FIXME, this is 99% likely to fail because ethers v6 now uses BigInt instead of BigNumber
// We may want to patch @uniswap/sdk while https://github.com/Uniswap/web3-react/issues/812 is open
return Fetcher.fetchPairData(token0, token1, ethersProvider)
return Fetcher.fetchPairData(token0, token1, ethersV5Provider)
})
15 changes: 15 additions & 0 deletions src/lib/ethersProviderSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { EvmChainId } from '@shapeshiftoss/chain-adapters'
import { KnownChainIds } from '@shapeshiftoss/types'
import { getConfig } from 'config'
import { JsonRpcProvider } from 'ethers'
import { ethers as ethersV5 } from 'ethers5'

import { assertUnreachable } from './utils'

Expand Down Expand Up @@ -30,6 +31,7 @@ export const rpcUrlByChainId = (chainId: EvmChainId): string => {
}

const ethersProviders: Map<ChainId, JsonRpcProvider> = new Map()
const ethersV5Providers: Map<ChainId, ethersV5.providers.StaticJsonRpcProvider> = new Map()

export const getEthersProvider = (
chainId: EvmChainId = KnownChainIds.EthereumMainnet,
Expand All @@ -42,3 +44,16 @@ export const getEthersProvider = (
return ethersProviders.get(chainId)!
}
}

// For backwards-compatibility for libraries still stuck in v5
export const getEthersV5Provider = (
chainId: EvmChainId = KnownChainIds.EthereumMainnet,
): ethersV5.providers.JsonRpcProvider => {
if (!ethersProviders.has(chainId)) {
const provider = new ethersV5.providers.StaticJsonRpcProvider(rpcUrlByChainId(chainId))
ethersV5Providers.set(chainId, provider)
return provider
} else {
return ethersV5Providers.get(chainId)!
}
}

0 comments on commit dc47e9c

Please sign in to comment.