Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: approvals failing for non-eth EVM chains #5577

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/contracts/contractManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { AssetId, ChainId } from '@shapeshiftoss/caip'
import { fromAssetId, toAssetId } from '@shapeshiftoss/caip'
import type { EvmChainId } from '@shapeshiftoss/chain-adapters'
import { KnownChainIds } from '@shapeshiftoss/types'
import type { Token } from '@uniswap/sdk'
import { Fetcher } from '@uniswap/sdk'
import assert from 'assert'
import { erc20ABI } from 'contracts/abis/ERC20ABI'
import { FarmingABI } from 'contracts/abis/farmingAbi'
import { IUniswapV2Pair } from 'contracts/abis/IUniswapV2Pair'
Expand All @@ -12,7 +15,7 @@ import memoize from 'lodash/memoize'
import type { Address } from 'viem'
import { getContract } from 'viem'
import { getEthersProvider } from 'lib/ethersProviderSingleton'
import { viemEthMainnetClient } from 'lib/viem-client'
import { viemClientByChainId, viemEthMainnetClient } from 'lib/viem-client'

import {
ETH_FOX_POOL_CONTRACT_ADDRESS,
Expand Down Expand Up @@ -78,20 +81,24 @@ export const getOrCreateContractByAddress = <T extends KnownContractAddress>(
export const getOrCreateContractByType = <T extends ContractType>({
address,
type,
// TODO(gomes): viem client by ChainId
woodenfurniture marked this conversation as resolved.
Show resolved Hide resolved
chainId: _chainId,
chainId,
}: {
address: string | `0x${string}`
type: T
chainId?: ChainId
chainId: ChainId
}): KnownContractByType<T> => {
const definedContract = definedContracts.find(contract => contract.address === address)
if (definedContract && definedContract.contract)
return definedContract.contract as unknown as KnownContractByType<T>

const publicClient = viemClientByChainId[chainId as EvmChainId]
assert(publicClient !== undefined, `no public client found for chainId '${chainId}'`)

const contract = getContract({
abi: CONTRACT_TYPE_TO_ABI[type],
address: address as Address,
publicClient: viemEthMainnetClient,

publicClient,
})
definedContracts.push({
contract,
Expand All @@ -107,6 +114,7 @@ export const fetchUniV2PairData = memoize(async (pairAssetId: AssetId) => {
const pair = getOrCreateContractByType({
address: contractAddress,
type: ContractType.UniV2Pair,
chainId: KnownChainIds.EthereumMainnet,
})
const ethersProvider = getEthersProvider()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AccountId } from '@shapeshiftoss/caip'
import { fromAccountId, fromAssetId, toAssetId } from '@shapeshiftoss/caip'
import { KnownChainIds } from '@shapeshiftoss/types'
import { getConfig } from 'config'
import { getOrCreateContractByType } from 'contracts/contractManager'
import { ContractType } from 'contracts/types'
Expand Down Expand Up @@ -143,6 +144,7 @@ export const Approve: React.FC<ApproveProps> = ({ accountId, onNext }) => {
const contract = getOrCreateContractByType({
address: fromAssetId(assetId).assetReference,
type: ContractType.ERC20,
chainId: KnownChainIds.EthereumMainnet,
woodenfurniture marked this conversation as resolved.
Show resolved Hide resolved
})

const amountToApprove = state.isExactAllowance ? amountCryptoBaseUnit : MAX_ALLOWANCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MaxUint256 } from '@ethersproject/constants'
import type { AccountId, AssetId } from '@shapeshiftoss/caip'
import { ethAssetId, ethChainId, fromAccountId, fromAssetId, toAssetId } from '@shapeshiftoss/caip'
import type { ethereum } from '@shapeshiftoss/chain-adapters'
import { KnownChainIds } from '@shapeshiftoss/types'
import {
UNISWAP_V2_ROUTER_02_CONTRACT_ADDRESS,
WETH_TOKEN_CONTRACT_ADDRESS,
Expand Down Expand Up @@ -98,6 +99,7 @@ export const useUniV2LiquidityPool = ({
: getOrCreateContractByType({
address: asset0ContractAddress,
type: ContractType.ERC20,
chainId: KnownChainIds.EthereumMainnet,
})
}, [asset0ContractAddress, skip])

Expand All @@ -107,6 +109,7 @@ export const useUniV2LiquidityPool = ({
: getOrCreateContractByType({
address: asset1ContractAddress,
type: ContractType.ERC20,
chainId: KnownChainIds.EthereumMainnet,
})
}, [asset1ContractAddress, skip])

Expand All @@ -116,6 +119,7 @@ export const useUniV2LiquidityPool = ({
: getOrCreateContractByType({
address: lpContractAddress,
type: ContractType.UniV2Pair,
chainId: KnownChainIds.EthereumMainnet,
})
}, [lpContractAddress, skip])

Expand Down Expand Up @@ -445,6 +449,7 @@ export const useUniV2LiquidityPool = ({
const contract = getOrCreateContractByType({
address: contractAddress,
type: ContractType.ERC20,
chainId: KnownChainIds.EthereumMainnet,
})

if (!contract) return
Expand Down Expand Up @@ -606,6 +611,7 @@ export const useUniV2LiquidityPool = ({
const contract = getOrCreateContractByType({
address: contractAddress,
type: ContractType.ERC20,
chainId: KnownChainIds.EthereumMainnet,
})

if (!contract) return
Expand Down
8 changes: 6 additions & 2 deletions src/lib/utils/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
import { evmChainIds } from '@shapeshiftoss/chain-adapters'
import type { ETHSignTx, HDWallet } from '@shapeshiftoss/hdwallet-core'
import { supportsETH } from '@shapeshiftoss/hdwallet-core'
import type { KnownChainIds } from '@shapeshiftoss/types'
import { KnownChainIds } from '@shapeshiftoss/types'
import { TxStatus } from '@shapeshiftoss/unchained-client'
import { getTxStatus } from '@shapeshiftoss/unchained-client/dist/evm'
import { getOrCreateContractByType } from 'contracts/contractManager'
Expand Down Expand Up @@ -226,7 +226,11 @@ export const getApproveContractData = ({
spender,
}: GetApproveContractDataArgs): string => {
const address = ethers.utils.getAddress(to)
const contract = getOrCreateContractByType({ address, type: ContractType.ERC20 })
const contract = getOrCreateContractByType({
address,
type: ContractType.ERC20,
chainId: KnownChainIds.EthereumMainnet,
woodenfurniture marked this conversation as resolved.
Show resolved Hide resolved
})
const data = encodeFunctionData({
abi: contract.abi,
functionName: 'approve',
Expand Down
22 changes: 21 additions & 1 deletion src/lib/viem-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { EvmChainId } from '@shapeshiftoss/chain-adapters'
import { KnownChainIds } from '@shapeshiftoss/types'
import { getConfig } from 'config'
import type { PublicClient } from 'viem'
import { createPublicClient, http } from 'viem'
import { arbitrum, avalanche, bsc, gnosis, mainnet, optimism } from 'viem/chains'
import { arbitrum, avalanche, bsc, gnosis, mainnet, optimism, polygon } from 'viem/chains'

export const viemEthMainnetClient = createPublicClient({
chain: mainnet,
Expand Down Expand Up @@ -31,3 +34,20 @@ export const viemGnosisClient = createPublicClient({
chain: gnosis,
transport: http(getConfig().REACT_APP_GNOSIS_NODE_URL),
})

export const viemPolygonClient = createPublicClient({
chain: polygon,
transport: http(getConfig().REACT_APP_POLYGON_NODE_URL),
})

export const viemClientByChainId: Record<EvmChainId, PublicClient> = {
[KnownChainIds.EthereumMainnet]: viemEthMainnetClient,
[KnownChainIds.BnbSmartChainMainnet]: viemBscClient,
[KnownChainIds.AvalancheMainnet]: viemAvalancheClient,
[KnownChainIds.ArbitrumMainnet]: viemArbitrumClient,
[KnownChainIds.GnosisMainnet]: viemGnosisClient,
[KnownChainIds.PolygonMainnet]: viemPolygonClient,
// cast required due to typescript shenanigans
// https://github.com/wagmi-dev/viem/issues/1018
[KnownChainIds.OptimismMainnet]: viemOptimismClient as PublicClient,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethAssetId, fromAssetId, toAssetId } from '@shapeshiftoss/caip'
import type { MarketData } from '@shapeshiftoss/types'
import { KnownChainIds, type MarketData } from '@shapeshiftoss/types'
import type { TokenAmount } from '@uniswap/sdk'
import { WETH_TOKEN_CONTRACT_ADDRESS } from 'contracts/constants'
import { fetchUniV2PairData, getOrCreateContractByType } from 'contracts/contractManager'
Expand Down Expand Up @@ -196,6 +196,7 @@ export const uniV2LpOpportunitiesMetadataResolver = async ({
const uniV2LPContract = getOrCreateContractByType({
address: contractAddress,
type: ContractType.UniV2Pair,
chainId: KnownChainIds.EthereumMainnet,
})
const apy = bnOrZero(apr).div(100).toString()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AssetId } from '@shapeshiftoss/caip'
import { fromAssetId } from '@shapeshiftoss/caip'
import { KnownChainIds } from '@shapeshiftoss/types'
import type { TokenAmount } from '@uniswap/sdk'
import type { IUniswapV2Pair } from 'contracts/abis/IUniswapV2Pair'
import { getOrCreateContractByType } from 'contracts/contractManager'
Expand Down Expand Up @@ -108,6 +109,7 @@ export const calculateAPRFromToken0 = memoize(
const pair = getOrCreateContractByType({
address: contractAddress,
type: ContractType.UniV2Pair,
chainId: KnownChainIds.EthereumMainnet,
})

const token0Volume24Hr = await getToken0Volume24Hr({
Expand Down
Loading