|
1 | | -import type { AssetId, ChainId } from '@shapeshiftoss/caip' |
| 1 | +import type { AssetId } from '@shapeshiftoss/caip' |
2 | 2 | import { fromAssetId, fromChainId } from '@shapeshiftoss/caip' |
3 | | -import type { PartialRecord } from '@shapeshiftoss/types' |
4 | | -import { KnownChainIds } from '@shapeshiftoss/types' |
5 | 3 | import { Pair, Token, TokenAmount } from '@uniswap/sdk' |
6 | | -import assert from 'assert' |
7 | 4 | import memoize from 'lodash/memoize' |
8 | | -import type { Address } from 'viem' |
| 5 | +import type { Address, GetContractReturnType, PublicClient } from 'viem' |
9 | 6 | import { erc20Abi, getAddress, getContract } from 'viem' |
10 | 7 |
|
11 | | -import { CONTRACT_ADDRESS_TO_ABI, CONTRACT_TYPE_TO_ABI } from './constants' |
12 | | -import type { |
13 | | - KnownContract, |
14 | | - KnownContractAbiByAddress, |
15 | | - KnownContractAddress, |
16 | | - KnownContractByAddress, |
17 | | - KnownContractByType, |
18 | | -} from './types' |
19 | | -import { ContractType } from './types' |
20 | | -import { viemClientByChainId, viemEthMainnetClient } from './viemClient' |
| 8 | +import { EVERGREEN_FARMING_ABI, FARMING_ABI, I_UNISWAP_V2_PAIR_ABI } from './abis' |
| 9 | +import { ETH_FOX_POOL_CONTRACT, ETH_FOX_STAKING_EVERGREEN_CONTRACT } from './constants' |
| 10 | +import type { FoxEthStakingContractAbi, FoxEthStakingContractAddress } from './types' |
| 11 | +import { assertGetViemClient, viemEthMainnetClient } from './viemClient' |
21 | 12 |
|
22 | | -const definedContracts: PartialRecord<Address, KnownContract> = {} |
23 | | - |
24 | | -export const getOrCreateContractByAddress: <A extends KnownContractAddress>( |
25 | | - address: A, |
26 | | -) => KnownContractByAddress<A> = <A extends KnownContractAddress>( |
27 | | - address: A, |
28 | | -): KnownContractByAddress<A> => { |
29 | | - const checksumAddress = getAddress(address) as A |
30 | | - const definedContract = definedContracts[checksumAddress] |
31 | | - if (definedContract !== undefined) { |
32 | | - return definedContract as unknown as KnownContractByAddress<A> |
33 | | - } |
34 | | - const contractAbi: KnownContractAbiByAddress<A> = CONTRACT_ADDRESS_TO_ABI[address] |
35 | | - |
36 | | - const contract = getContract({ |
37 | | - abi: contractAbi, |
38 | | - address: checksumAddress, |
| 13 | +export const getFoxEthStakingContract = ( |
| 14 | + address: FoxEthStakingContractAddress, |
| 15 | +): GetContractReturnType<FoxEthStakingContractAbi, PublicClient, Address> => |
| 16 | + getContract({ |
| 17 | + abi: address === ETH_FOX_STAKING_EVERGREEN_CONTRACT ? EVERGREEN_FARMING_ABI : FARMING_ABI, |
| 18 | + address: getAddress(address), |
39 | 19 | client: viemEthMainnetClient, |
40 | 20 | }) |
41 | | - definedContracts[checksumAddress] = contract as unknown as KnownContract |
42 | | - return contract as KnownContractByAddress<A> |
43 | | -} |
44 | 21 |
|
45 | | -export const getOrCreateContractByType: <A extends KnownContractAddress, T extends ContractType>({ |
46 | | - address, |
47 | | - type, |
48 | | - chainId, |
49 | | -}: { |
50 | | - address: string | Address |
51 | | - type: T |
52 | | - chainId: ChainId |
53 | | -}) => KnownContractByType<A, T> = <A extends KnownContractAddress, T extends ContractType>({ |
54 | | - address, |
55 | | - type, |
56 | | - chainId, |
57 | | -}: { |
58 | | - address: string | Address |
59 | | - type: T |
60 | | - chainId: ChainId |
61 | | -}): KnownContractByType<A, T> => { |
62 | | - const checksumAddress = getAddress(address) as A |
63 | | - const definedContract = definedContracts[checksumAddress] |
64 | | - if (definedContract !== undefined) { |
65 | | - return definedContract as unknown as KnownContractByType<A, T> |
66 | | - } |
67 | | - |
68 | | - const publicClient = viemClientByChainId[chainId] |
69 | | - assert(publicClient !== undefined, `no public client found for chainId '${chainId}'`) |
70 | | - |
71 | | - const contract = getContract({ |
72 | | - abi: CONTRACT_TYPE_TO_ABI[type], |
73 | | - address: checksumAddress, |
74 | | - client: publicClient, |
| 22 | +export const getEthFoxPoolContract = (): GetContractReturnType< |
| 23 | + typeof I_UNISWAP_V2_PAIR_ABI, |
| 24 | + PublicClient, |
| 25 | + Address |
| 26 | +> => |
| 27 | + getContract({ |
| 28 | + abi: I_UNISWAP_V2_PAIR_ABI, |
| 29 | + address: ETH_FOX_POOL_CONTRACT, |
| 30 | + client: viemEthMainnetClient, |
75 | 31 | }) |
76 | | - definedContracts[checksumAddress] = contract as unknown as KnownContract |
77 | | - return contract as unknown as KnownContractByType<A, T> |
78 | | -} |
79 | 32 |
|
80 | 33 | const getUniV2PairTokens = memoize(async (pairAssetId: AssetId) => { |
81 | 34 | const { assetReference, chainId } = fromAssetId(pairAssetId) |
82 | | - const pair = getOrCreateContractByType({ |
| 35 | + const client = assertGetViemClient(chainId) |
| 36 | + const pair = getContract({ |
| 37 | + abi: I_UNISWAP_V2_PAIR_ABI, |
83 | 38 | address: getAddress(assetReference), |
84 | | - type: ContractType.UniV2Pair, |
85 | | - chainId: KnownChainIds.EthereumMainnet, |
| 39 | + client, |
86 | 40 | }) |
87 | 41 |
|
88 | 42 | const [token0Address, token1Address] = await Promise.all([pair.read.token0(), pair.read.token1()]) |
89 | 43 |
|
90 | 44 | const [decimals0, decimals1] = await Promise.all( |
91 | 45 | [token0Address, token1Address].map(address => |
92 | | - viemEthMainnetClient.readContract({ address, abi: erc20Abi, functionName: 'decimals' }), |
| 46 | + client.readContract({ address, abi: erc20Abi, functionName: 'decimals' }), |
93 | 47 | ), |
94 | 48 | ) |
95 | 49 |
|
|
0 commit comments