From 1df5260db7dbe4a4584468d9621c64b52c3a7339 Mon Sep 17 00:00:00 2001 From: gomes <17035424+gomesalexandre@users.noreply.github.com> Date: Wed, 6 Dec 2023 09:47:35 +0100 Subject: [PATCH] feat: remove foxy rebase history (#5777) --- .env.app | 1 - .env.dev | 1 - .env.develop | 1 - .env.e2e | 1 - .env.private | 1 - .../headers/csps/chains/ethereum.ts | 1 - src/config.ts | 1 - src/context/AppProvider/AppContext.tsx | 15 -- .../useBalanceChartData.test.ts | 10 +- .../useBalanceChartData.ts | 37 +---- src/lib/address/unstoppable-domains.ts | 4 +- src/lib/investor/investor-foxy/api/api.ts | 129 +----------------- .../investor/investor-foxy/api/foxy-types.ts | 12 -- src/lib/investor/investor-foxy/foxycli.ts | 2 +- src/state/apis/foxy/foxyApiSingleton.ts | 8 +- src/state/migrations/index.ts | 1 + src/state/slices/txHistorySlice/selectors.ts | 48 +------ .../txHistorySlice/txHistorySlice.test.ts | 15 +- .../slices/txHistorySlice/txHistorySlice.ts | 113 +-------------- src/test/mocks/store.ts | 5 - 20 files changed, 21 insertions(+), 385 deletions(-) diff --git a/.env.app b/.env.app index f09a41863b0..9d0b29fa44b 100644 --- a/.env.app +++ b/.env.app @@ -36,7 +36,6 @@ REACT_APP_UNCHAINED_THORCHAIN_WS_URL=wss://api.thorchain.shapeshift.com # nodes REACT_APP_ETHEREUM_NODE_URL=https://daemon.ethereum.shapeshift.com -REACT_APP_ETHEREUM_INFURA_URL=https://mainnet.infura.io/v3/6e2f28ff4f5340fdb0db5da3baec0af2 REACT_APP_AVALANCHE_NODE_URL=https://daemon.avalanche.shapeshift.com/ext/bc/C/rpc REACT_APP_OPTIMISM_NODE_URL=https://daemon.optimism.shapeshift.com REACT_APP_BNBSMARTCHAIN_NODE_URL=https://daemon.bnbsmartchain.shapeshift.com diff --git a/.env.dev b/.env.dev index d12f9b107de..6a8b974beda 100644 --- a/.env.dev +++ b/.env.dev @@ -40,7 +40,6 @@ REACT_APP_UNCHAINED_THORCHAIN_WS_URL=wss://dev-api.thorchain.shapeshift.com # nodes REACT_APP_ETHEREUM_NODE_URL=https://dev-daemon.ethereum.shapeshift.com -REACT_APP_ETHEREUM_INFURA_URL=https://mainnet.infura.io/v3/fb05c87983c4431baafd4600fd33de7e REACT_APP_AVALANCHE_NODE_URL=https://dev-daemon.avalanche.shapeshift.com/ext/bc/C/rpc REACT_APP_OPTIMISM_NODE_URL=https://dev-daemon.optimism.shapeshift.com REACT_APP_BNBSMARTCHAIN_NODE_URL=https://dev-daemon.bnbsmartchain.shapeshift.com diff --git a/.env.develop b/.env.develop index 367e8286be1..d11b97bcb7b 100644 --- a/.env.develop +++ b/.env.develop @@ -38,7 +38,6 @@ REACT_APP_UNCHAINED_THORCHAIN_WS_URL=wss://dev-api.thorchain.shapeshift.com # nodes REACT_APP_ETHEREUM_NODE_URL=https://dev-daemon.ethereum.shapeshift.com -REACT_APP_ETHEREUM_INFURA_URL=https://mainnet.infura.io/v3/d28923bef53e4b26b21a094ae38d32b3 REACT_APP_AVALANCHE_NODE_URL=https://dev-daemon.avalanche.shapeshift.com/ext/bc/C/rpc REACT_APP_OPTIMISM_NODE_URL=https://dev-daemon.optimism.shapeshift.com REACT_APP_BNBSMARTCHAIN_NODE_URL=https://dev-daemon.bnbsmartchain.shapeshift.com diff --git a/.env.e2e b/.env.e2e index aa10ce3a9c9..417fb7771e8 100644 --- a/.env.e2e +++ b/.env.e2e @@ -33,7 +33,6 @@ REACT_APP_UNCHAINED_THORCHAIN_WS_URL=wss://dev-api.thorchain.shapeshift.com # nodes REACT_APP_ETHEREUM_NODE_URL=http://localhost:8080 -REACT_APP_ETHEREUM_INFURA_URL=https://mainnet.infura.io/v3/d28923bef53e4b26b21a094ae38d32b3 REACT_APP_AVALANCHE_NODE_URL=https://dev-daemon.avalanche.shapeshift.com/ext/bc/C/rpc REACT_APP_OPTIMISM_NODE_URL=https://dev-daemon.optimism.shapeshift.com REACT_APP_BNBSMARTCHAIN_NODE_URL=https://dev-daemon.bnbsmartchain.shapeshift.com diff --git a/.env.private b/.env.private index d56c904b7ec..eb29f4eb91e 100644 --- a/.env.private +++ b/.env.private @@ -34,7 +34,6 @@ REACT_APP_UNCHAINED_THORCHAIN_WS_URL=wss://api.thorchain.shapeshift.com # nodes REACT_APP_ETHEREUM_NODE_URL=https://daemon.ethereum.shapeshift.com -REACT_APP_ETHEREUM_INFURA_URL=https://mainnet.infura.io/v3/6e2f28ff4f5340fdb0db5da3baec0af2 REACT_APP_AVALANCHE_NODE_URL=https://daemon.avalanche.shapeshift.com/ext/bc/C/rpc REACT_APP_OPTIMISM_NODE_URL=https://daemon.optimism.shapeshift.com REACT_APP_BNBSMARTCHAIN_NODE_URL=https://daemon.bnbsmartchain.shapeshift.com diff --git a/react-app-rewired/headers/csps/chains/ethereum.ts b/react-app-rewired/headers/csps/chains/ethereum.ts index a1c0d21965a..e8a38e2fd99 100644 --- a/react-app-rewired/headers/csps/chains/ethereum.ts +++ b/react-app-rewired/headers/csps/chains/ethereum.ts @@ -6,6 +6,5 @@ export const csp: Csp = { process.env.REACT_APP_UNCHAINED_ETHEREUM_HTTP_URL!, process.env.REACT_APP_UNCHAINED_ETHEREUM_WS_URL!, process.env.REACT_APP_ALCHEMY_POLYGON_URL!, - process.env.REACT_APP_ETHEREUM_INFURA_URL!, ], } diff --git a/src/config.ts b/src/config.ts index f51454404d2..884af09df07 100644 --- a/src/config.ts +++ b/src/config.ts @@ -42,7 +42,6 @@ const validators = { REACT_APP_UNCHAINED_THORCHAIN_WS_URL: url(), REACT_APP_THORCHAIN_NODE_URL: url(), REACT_APP_ETHEREUM_NODE_URL: url(), - REACT_APP_ETHEREUM_INFURA_URL: url(), REACT_APP_AVALANCHE_NODE_URL: url(), REACT_APP_OPTIMISM_NODE_URL: url(), REACT_APP_BNBSMARTCHAIN_NODE_URL: url(), diff --git a/src/context/AppProvider/AppContext.tsx b/src/context/AppProvider/AppContext.tsx index 90926e6af44..9b744f17af3 100644 --- a/src/context/AppProvider/AppContext.tsx +++ b/src/context/AppProvider/AppContext.tsx @@ -179,8 +179,6 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { if (!requestedAccountIds.length) return if (portfolioLoadingStatus === 'loading') return - const { getFoxyRebaseHistoryByAccountId } = txHistoryApi.endpoints - dispatch(nftApi.endpoints.getNftUserTokens.initiate({ accountIds: requestedAccountIds })) dispatch(zapper.endpoints.getZapperAppsBalancesOutput.initiate()) @@ -213,19 +211,6 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => { await fetchAllOpportunitiesMetadataByChainId(chainId) await fetchAllOpportunitiesUserDataByAccountId(accountId) })() - - /** - * fetch all rebase history for foxy - * - * foxy rebase history is most closely linked to transactions. - * unfortunately, we have to call this for a specific asset here - * because we need it for the dashboard balance chart - * - * if you're reading this and are about to add another rebase token here, - * stop, and make a getRebaseHistoryByAccountId that takes - * an accountId and assetId[] in the txHistoryApi - */ - dispatch(getFoxyRebaseHistoryByAccountId.initiate({ accountId, portfolioAssetIds })) break default: } diff --git a/src/hooks/useBalanceChartData/useBalanceChartData.test.ts b/src/hooks/useBalanceChartData/useBalanceChartData.test.ts index 1b782023e6f..ed4f9af2dae 100644 --- a/src/hooks/useBalanceChartData/useBalanceChartData.test.ts +++ b/src/hooks/useBalanceChartData/useBalanceChartData.test.ts @@ -6,7 +6,6 @@ import { ethereum, fox } from 'test/mocks/assets' import { ethereumTransactions, FOXSend } from 'test/mocks/txs' import type { Asset } from 'lib/asset-service' import { bn } from 'lib/bignumber/bignumber' -import type { RebaseHistory } from 'lib/investor/investor-foxy' import type { PriceHistoryData } from 'state/slices/marketDataSlice/types' import type { Bucket } from './useBalanceChartData' @@ -59,9 +58,8 @@ describe('bucketTxs', () => { const buckets = makeBuckets({ assetIds, balances, timeframe }) const txs = [FOXSend] - const rebases: RebaseHistory[] = [] - const bucketedTxs = bucketEvents(txs, rebases, buckets) + const bucketedTxs = bucketEvents(txs, buckets) const totalTxs = bucketedTxs.reduce((acc, bucket: Bucket) => acc + bucket.txs.length, 0) @@ -104,8 +102,7 @@ describe('calculateBucketPrices', () => { [foxAssetId]: fox, } - const rebases: RebaseHistory[] = [] - const buckets = bucketEvents(txs, rebases, emptyBuckets) + const buckets = bucketEvents(txs, emptyBuckets) const calculatedBuckets = calculateBucketPrices({ assetIds, @@ -137,8 +134,7 @@ describe('calculateBucketPrices', () => { [ethAssetId]: ethereum, } const emptyBuckets = makeBuckets({ assetIds, balances, timeframe }) - const rebases: RebaseHistory[] = [] - const buckets = bucketEvents(txs, rebases, emptyBuckets) + const buckets = bucketEvents(txs, emptyBuckets) const calculatedBuckets = calculateBucketPrices({ assetIds, diff --git a/src/hooks/useBalanceChartData/useBalanceChartData.ts b/src/hooks/useBalanceChartData/useBalanceChartData.ts index ea406015b6d..b26506abf72 100644 --- a/src/hooks/useBalanceChartData/useBalanceChartData.ts +++ b/src/hooks/useBalanceChartData/useBalanceChartData.ts @@ -15,7 +15,6 @@ import { useEffect, useMemo, useState } from 'react' import { useFetchPriceHistories } from 'hooks/useFetchPriceHistories/useFetchPriceHistories' import { bnOrZero } from 'lib/bignumber/bignumber' import { priceAtDate } from 'lib/charts' -import type { RebaseHistory } from 'lib/investor/investor-foxy' import type { SupportedFiatCurrencies } from 'lib/market-service' import type { AssetsById } from 'state/slices/assetsSlice/assetsSlice' import type { PriceHistoryData } from 'state/slices/marketDataSlice/types' @@ -25,7 +24,6 @@ import { selectBalanceChartCryptoBalancesByAccountIdAboveThreshold, selectCryptoPriceHistoryTimeframe, selectFiatPriceHistoryTimeframe, - selectRebasesByFilter, selectSelectedCurrency, selectTxsByFilter, selectWalletId, @@ -48,7 +46,6 @@ export type Bucket = { end: dayjs.Dayjs balance: BucketBalance txs: Tx[] - rebases: RebaseHistory[] } type BucketMeta = { @@ -116,32 +113,24 @@ export const makeBuckets: MakeBuckets = args => { const end = now.subtract(idx * duration, unit) const start = end.subtract(duration, unit).add(1, 'second') const txs: Tx[] = [] - const rebases: RebaseHistory[] = [] const balance = { crypto: assetBalances, fiat: zeroAssetBalances, } - return { start, end, txs, rebases, balance } + return { start, end, txs, balance } }) .reverse() return { buckets, meta } } -export const bucketEvents = ( - txs: Tx[], - rebases: RebaseHistory[], - bucketsAndMeta: MakeBucketsReturn, -): Bucket[] => { +export const bucketEvents = (txs: Tx[], bucketsAndMeta: MakeBucketsReturn): Bucket[] => { const { buckets, meta } = bucketsAndMeta const start = head(buckets)!.start const end = last(buckets)!.end - // both txs and rebase events have the same blockTime property which is all we need - const txAndRebaseEvents = [...txs, ...rebases] - // events are potentially a lot longer than buckets, iterate the long list once - return txAndRebaseEvents.reduce((acc, event) => { + return txs.reduce((acc, event) => { const eventDayJs = dayjs(event.blockTime * 1000) // unchained uses seconds const eventOutsideDomain = eventDayJs.isBefore(start) || eventDayJs.isAfter(end) if (eventOutsideDomain) return acc @@ -155,9 +144,8 @@ export const bucketEvents = ( return acc } - const isTx = (event: Tx | RebaseHistory): event is Tx => !!(event as Tx)?.txid // add to the correct bucket - isTx(event) ? acc[bucketIndex].txs.push(event) : acc[bucketIndex].rebases.push(event) + acc[bucketIndex].txs.push(event) return acc }, buckets) @@ -229,7 +217,7 @@ export const calculateBucketPrices: CalculateBucketPrices = args => { // we iterate from latest to oldest for (let i = buckets.length - 1; i >= 0; i--) { const bucket = buckets[i] - const { rebases, txs } = bucket + const { txs } = bucket // copy the balance back from the most recent bucket const currentBalance = buckets[i + 1]?.balance ?? startingBucket.balance @@ -275,14 +263,6 @@ export const calculateBucketPrices: CalculateBucketPrices = args => { }) }) - rebases.forEach(rebase => { - const { assetId, balanceDiff } = rebase - if (!assetIds.includes(assetId)) return - // UP ONLY - rebase events can only go up, we don't have to consider the case adjusting balances down - // we're going backwards, so a rebase means we had less before - bucket.balance.crypto[assetId] = bnOrZero(bucket.balance.crypto[assetId]).minus(balanceDiff) - }) - bucket.balance.fiat = fiatBalanceAtBucket({ bucket, cryptoPriceHistoryData, @@ -386,10 +366,6 @@ export const useBalanceChartData: UseBalanceChartData = args => { const txFilter = useMemo(() => ({ assetId, accountId }), [assetId, accountId]) const txs = useAppSelector(state => selectTxsByFilter(state, txFilter)) - // rebasing token balances can be adjusted by rebase events rather than txs - // and we need to account for this in charts - const rebases = useAppSelector(state => selectRebasesByFilter(state, txFilter)) - const selectedCurrency = useAppSelector(selectSelectedCurrency) // kick off requests for all the price histories we need @@ -418,7 +394,7 @@ export const useBalanceChartData: UseBalanceChartData = args => { timeframe, }) // put each tx into a bucket for the chart - const buckets = bucketEvents(txs, rebases, emptyBuckets) + const buckets = bucketEvents(txs, emptyBuckets) // iterate each bucket, updating crypto balances and fiat prices per bucket const calculatedBuckets = calculateBucketPrices({ @@ -445,7 +421,6 @@ export const useBalanceChartData: UseBalanceChartData = args => { balances, setBalanceChartData, walletId, - rebases, selectedCurrency, ]) diff --git a/src/lib/address/unstoppable-domains.ts b/src/lib/address/unstoppable-domains.ts index 00b1a334c54..ba557f5306e 100644 --- a/src/lib/address/unstoppable-domains.ts +++ b/src/lib/address/unstoppable-domains.ts @@ -10,7 +10,7 @@ import type { let _resolution: Resolution | undefined const getResolution = (): Resolution => { - const infuraProviderUrl = getConfig().REACT_APP_ETHEREUM_NODE_URL + const ethereumProviderUrl = getConfig().REACT_APP_ETHEREUM_NODE_URL const polygonProviderUrl = getConfig().REACT_APP_ALCHEMY_POLYGON_URL if (!polygonProviderUrl) @@ -21,7 +21,7 @@ const getResolution = (): Resolution => { sourceConfig: { uns: { locations: { - Layer1: { url: infuraProviderUrl, network: 'mainnet' }, + Layer1: { url: ethereumProviderUrl, network: 'mainnet' }, Layer2: { url: polygonProviderUrl, network: 'polygon-mainnet', diff --git a/src/lib/investor/investor-foxy/api/api.ts b/src/lib/investor/investor-foxy/api/api.ts index 5205a63a638..b1ecc7f0d75 100644 --- a/src/lib/investor/investor-foxy/api/api.ts +++ b/src/lib/investor/investor-foxy/api/api.ts @@ -1,5 +1,5 @@ import type { ChainReference } from '@shapeshiftoss/caip' -import { CHAIN_NAMESPACE, CHAIN_REFERENCE, toAssetId } from '@shapeshiftoss/caip' +import { CHAIN_REFERENCE } from '@shapeshiftoss/caip' import type { EvmBaseAdapter, EvmChainId, @@ -36,8 +36,6 @@ import type { FoxyAddressesType, FoxyOpportunityInputData, GetTokeRewardAmount, - RebaseEvent, - RebaseHistory, SignAndBroadcastTx, StakingContract, TokeClaimIpfs, @@ -1088,129 +1086,4 @@ export class FoxyApi { throw new Error(`Failed to get information from Tokemak ipfs ${e}`) } } - - async getRebaseHistory(input: BalanceInput) { - const { tokenContractAddress, userAddress } = input - this.verifyAddresses([tokenContractAddress]) - - const foxyContract = new ethers.Contract(tokenContractAddress, foxyAbi, this.provider) - const fromBlock = 14381454 // genesis rebase - - const rebaseEvents = await (async () => { - try { - const filter = foxyContract.filters.LogRebase() - const events = await foxyContract.queryFilter(filter, fromBlock, 'latest') - const filteredEvents = events.filter( - rebase => rebase.args?.rebase && !rebase.args.rebase.isZero(), - ) - return filteredEvents - } catch (e) { - console.error(e, 'failed to get rebase events') - return undefined - } - })() - - if (!rebaseEvents) return [] - - const transferEvents = await (async () => { - try { - const filter = foxyContract.filters.Transfer() - const events = await foxyContract.queryFilter(filter, fromBlock, 'latest') - return events - } catch (e) { - console.error(e, 'failed to get transfer events') - return undefined - } - })() - - const events: RebaseEvent[] = rebaseEvents.map(rebaseEvent => { - const { blockNumber, args: { epoch } = { epoch: '' } } = rebaseEvent - return { - blockNumber, - epoch, - } - }) - - const chainNamespace = CHAIN_NAMESPACE.Evm - const chainReference = CHAIN_REFERENCE.EthereumMainnet - const assetNamespace = 'erc20' - const assetReference = tokenContractAddress - // foxy assetId - const assetId = toAssetId({ chainNamespace, chainReference, assetNamespace, assetReference }) - - const results = await Promise.allSettled( - events.map(async event => { - const { preRebaseBalance, postRebaseBalance } = await (async () => { - try { - // check transfer events to see if a user triggered a rebase through unstake or stake - const unstakedTransferInfo = transferEvents?.filter( - e => - e.blockNumber === event.blockNumber && e.args?.from.toLowerCase() === userAddress, - ) - const unstakedTransferAmount = unstakedTransferInfo?.[0]?.args?.value ?? 0 - const stakedTransferInfo = transferEvents?.filter( - e => e.blockNumber === event.blockNumber && e.args?.to.toLowerCase() === userAddress, - ) - const stakedTransferAmount = stakedTransferInfo?.[0]?.args?.value ?? 0 - - const postRebaseBalanceResult = await foxyContract.balanceOf(userAddress, { - blockTag: event.blockNumber, - }) - const unadjustedPreRebaseBalance = await foxyContract.balanceOf(userAddress, { - blockTag: event.blockNumber - 1, - }) - - // unstake events can trigger rebases, if they do, adjust the amount to not include that unstake's transfer amount - const preRebaseBalanceResult = bnOrZero(unadjustedPreRebaseBalance.toString()) - .minus(unstakedTransferAmount.toString()) - .plus(stakedTransferAmount.toString()) - .toString() - - return { - preRebaseBalance: preRebaseBalanceResult, - postRebaseBalance: postRebaseBalanceResult.toString() as string, - } - } catch (e) { - console.error(e, 'failed to get balance of address') - return { - preRebaseBalance: bn(0).toString(), - postRebaseBalance: bn(0).toString(), - } - } - })() - - const blockTime = await (async () => { - try { - const block = await this.provider.getBlock(event.blockNumber) - return bnOrZero(block.timestamp).toNumber() - } catch (e) { - console.error(e, 'failed to get timestamp of block') - return 0 - } - })() - - return { assetId, preRebaseBalance, postRebaseBalance, blockTime } - }), - ) - - const actualResults = results.reduce((acc, cur) => { - if (cur.status === 'rejected') { - console.error('getFoxyRebaseHistory: balanceOf call failed - charts will be wrong') - return acc - } - if (cur.value.preRebaseBalance === '0') return acc // don't return rebase history with 0 balance diff - - const balanceDiff = bnOrZero(cur.value.postRebaseBalance) - .minus(cur.value.preRebaseBalance) - .toString() - - acc.push({ - balanceDiff, - ...cur.value, - }) - return acc - }, []) - - return actualResults - } } diff --git a/src/lib/investor/investor-foxy/api/foxy-types.ts b/src/lib/investor/investor-foxy/api/foxy-types.ts index ce166d5ef06..409af3b3be5 100644 --- a/src/lib/investor/investor-foxy/api/foxy-types.ts +++ b/src/lib/investor/investor-foxy/api/foxy-types.ts @@ -1,4 +1,3 @@ -import type { AssetId } from '@shapeshiftoss/caip' import type { FeeDataEstimate } from '@shapeshiftoss/chain-adapters' import type { ETHWallet } from '@shapeshiftoss/hdwallet-core' import type { BIP44Params, KnownChainIds, WithdrawType } from '@shapeshiftoss/types' @@ -136,17 +135,6 @@ export type Recipient = { amount: string } -export type RebaseEvent = { - epoch: string - blockNumber: number -} - -export type RebaseHistory = { - assetId: AssetId - balanceDiff: string - blockTime: number -} - export type StakingContract = { stakingContract: Contract } diff --git a/src/lib/investor/investor-foxy/foxycli.ts b/src/lib/investor/investor-foxy/foxycli.ts index a721e145cd6..542395d1544 100644 --- a/src/lib/investor/investor-foxy/foxycli.ts +++ b/src/lib/investor/investor-foxy/foxycli.ts @@ -40,7 +40,7 @@ const main = async (): Promise => { }), ), }, - rpcUrl: 'https://mainnet.infura.io/v3/d734c7eebcdf400185d7eb67322a7e57', + rpcUrl: 'https://dev-daemon.ethereum.shapeshift.com', }) // using 0 value array since only one contract subset exists diff --git a/src/state/apis/foxy/foxyApiSingleton.ts b/src/state/apis/foxy/foxyApiSingleton.ts index 112592bbc2e..9b2ae5962ba 100644 --- a/src/state/apis/foxy/foxyApiSingleton.ts +++ b/src/state/apis/foxy/foxyApiSingleton.ts @@ -9,13 +9,7 @@ let _foxyApi: FoxyApi | undefined = undefined // we need to be able to access this outside react export const getFoxyApi = (): FoxyApi => { - // Infura requests are origin restricted upstream to *.shapeshift.com - // Using our own node locally allows FOXy development, though the balances aren't guaranteed to be accurate - // since our archival node isn't fully synced yet - const isLocalhost = window.location.hostname === 'localhost' - const RPC_PROVIDER_ENV = isLocalhost - ? 'REACT_APP_ETHEREUM_NODE_URL' - : 'REACT_APP_ETHEREUM_INFURA_URL' + const RPC_PROVIDER_ENV = 'REACT_APP_ETHEREUM_NODE_URL' if (_foxyApi) return _foxyApi diff --git a/src/state/migrations/index.ts b/src/state/migrations/index.ts index a2e25a073bb..ba014a23b8a 100644 --- a/src/state/migrations/index.ts +++ b/src/state/migrations/index.ts @@ -22,4 +22,5 @@ export const migrations = { 14: clearTxHistory, 15: clearAssets, 16: clearOpportunities, + 17: clearTxHistory, } diff --git a/src/state/slices/txHistorySlice/selectors.ts b/src/state/slices/txHistorySlice/selectors.ts index ae25d6967d6..b733152a2c0 100644 --- a/src/state/slices/txHistorySlice/selectors.ts +++ b/src/state/slices/txHistorySlice/selectors.ts @@ -9,7 +9,6 @@ import values from 'lodash/values' import { matchSorter } from 'match-sorter' import createCachedSelector from 're-reselect' import { createSelector } from 'reselect' -import type { RebaseHistory } from 'lib/investor/investor-foxy' import { isSome } from 'lib/utils' import type { ReduxState } from 'state/reducer' import { createDeepEqualOutputSelector } from 'state/selector-utils' @@ -25,13 +24,7 @@ import { selectAssets } from '../assetsSlice/selectors' import { selectWalletAccountIds } from '../common-selectors' import type { AccountMetadata } from '../portfolioSlice/portfolioSliceCommon' import { selectPortfolioAccountMetadata } from '../portfolioSlice/selectors' -import type { - RebaseId, - RebaseIdsByAccountIdAssetId, - Tx, - TxId, - TxIdsByAccountIdAssetId, -} from './txHistorySlice' +import type { Tx, TxId, TxIdsByAccountIdAssetId } from './txHistorySlice' export const selectTxs = createDeepEqualOutputSelector( (state: ReduxState) => state.txHistory.txs.byId, @@ -42,12 +35,6 @@ export const selectTxIds = createDeepEqualOutputSelector( ids => ids, ) -const selectRebasesById = (state: ReduxState) => state.txHistory.rebases.byId -export const selectRebaseIds = createDeepEqualOutputSelector( - (state: ReduxState) => state.txHistory.rebases.ids, - ids => ids, -) - const selectTxIdParam = createCachedSelector( (_state: ReduxState, txId: string) => txId, txId => txId, @@ -101,13 +88,6 @@ const selectWalletTxsByAccountIdAssetId = createSelector( pickBy(txsByAccountIdAssetId, (_, accountId) => accountIds.includes(accountId)), ) -const selectWalletRebasesByAccountIdAssetId = createSelector( - selectWalletAccountIds, - (state: ReduxState) => state.txHistory.rebases.byAccountIdAssetId, - (accountIds, rebasesByAccountIdAssetId): RebaseIdsByAccountIdAssetId => - pickBy(rebasesByAccountIdAssetId, (_, accountId) => accountIds.includes(accountId)), -) - export const selectTxIdsByFilter = createDeepEqualOutputSelector( selectTxIds, selectTxs, @@ -190,32 +170,6 @@ export const selectTxStatusById = createCachedSelector( (tx): Tx['status'] | undefined => tx?.status, )((_state: ReduxState, txId: TxId) => txId ?? 'undefined') -export const selectRebaseIdsByFilter = createDeepEqualOutputSelector( - selectRebaseIds, - selectWalletRebasesByAccountIdAssetId, - selectAccountIdParamFromFilter, - selectAssetIdParamFromFilter, - (rebaseIds, data, accountIdFilter, assetIdFilter): RebaseId[] => { - // filter by accountIdFilter, if it exists, otherwise data for all accountIds - const filtered = pickBy(data, (_, accountId) => - accountIdFilter ? accountId === accountIdFilter : true, - ) - const flattened = values(filtered) - .flatMap(byAssetId => (assetIdFilter ? byAssetId?.[assetIdFilter] : values(byAssetId).flat())) - .filter(isSome) - const uniqueIds = uniq(flattened) - const sortedIds = uniqueIds.sort((a, b) => rebaseIds.indexOf(a) - rebaseIds.indexOf(b)) - return sortedIds - }, -) - -export const selectRebasesByFilter = createSelector( - selectRebasesById, - selectRebaseIdsByFilter, - (rebasesById, rebaseIds): RebaseHistory[] => - rebaseIds.map(rebaseId => rebasesById[rebaseId]).filter(isSome), -) - /** * to be able to add an account for a chain, we want to ensure there is some tx history * on the current highest accountNumber accountIds diff --git a/src/state/slices/txHistorySlice/txHistorySlice.test.ts b/src/state/slices/txHistorySlice/txHistorySlice.test.ts index 77654c4a0b0..2b9340eef66 100644 --- a/src/state/slices/txHistorySlice/txHistorySlice.test.ts +++ b/src/state/slices/txHistorySlice/txHistorySlice.test.ts @@ -7,7 +7,7 @@ import { BtcSend, ethereumTransactions, EthReceive, EthSend } from 'test/mocks/t import { store } from 'state/store' import { selectLastNTxIds } from './selectors' -import type { RebasesState, TxHistory, TxsState } from './txHistorySlice' +import type { TxHistory, TxsState } from './txHistorySlice' import { txHistory } from './txHistorySlice' import { serializeTxIndex } from './utils' @@ -25,11 +25,6 @@ describe('txHistorySlice', () => { byAccountIdAssetId: {}, ids: [], }, - rebases: { - byAccountIdAssetId: {}, - ids: [], - byId: {}, - }, }) }) @@ -211,13 +206,8 @@ describe('txHistorySlice', () => { byAccountIdAssetId: {}, ids: ['a', 'b'], } - const rebases: RebasesState = { - byAccountIdAssetId: {}, - ids: [], - byId: {}, - } - const txHistory: TxHistory = { txs, rebases } + const txHistory: TxHistory = { txs } const state = { ...mockStore, @@ -231,7 +221,6 @@ describe('txHistorySlice', () => { // this array will always change on every new tx ids: ['a', 'b', 'c'], }, - rebases, } // redux will replace the array on update diff --git a/src/state/slices/txHistorySlice/txHistorySlice.ts b/src/state/slices/txHistorySlice/txHistorySlice.ts index 3a7191b5cbd..78d83ae5221 100644 --- a/src/state/slices/txHistorySlice/txHistorySlice.ts +++ b/src/state/slices/txHistorySlice/txHistorySlice.ts @@ -1,26 +1,15 @@ import { createSlice } from '@reduxjs/toolkit' import { createApi } from '@reduxjs/toolkit/dist/query/react' import type { AccountId, AssetId } from '@shapeshiftoss/caip' -import { - ASSET_NAMESPACE, - ethChainId, - fromAccountId, - gnosisChainId, - isNft, - polygonChainId, - toAssetId, -} from '@shapeshiftoss/caip' +import { fromAccountId, gnosisChainId, isNft, polygonChainId } from '@shapeshiftoss/caip' import type { Transaction } from '@shapeshiftoss/chain-adapters' import type { UtxoAccountType } from '@shapeshiftoss/types' import orderBy from 'lodash/orderBy' import { PURGE } from 'redux-persist' import { getChainAdapterManager } from 'context/PluginProvider/chainAdapterSingleton' -import type { RebaseHistory } from 'lib/investor/investor-foxy' -import { foxyAddresses } from 'lib/investor/investor-foxy' import type { PartialRecord } from 'lib/utils' -import { deepUpsertArray, isSome } from 'lib/utils' +import { deepUpsertArray } from 'lib/utils' import { BASE_RTK_CREATE_API_CONFIG } from 'state/apis/const' -import { getFoxyApi } from 'state/apis/foxy/foxyApiSingleton' import { BLACKLISTED_COLLECTION_IDS, isSpammyNftText, @@ -29,7 +18,7 @@ import { import type { State } from 'state/apis/types' import type { Nominal } from 'types/common' -import { getRelatedAssetIds, serializeTxIndex, UNIQUE_TX_ID_DELIMITER } from './utils' +import { getRelatedAssetIds, serializeTxIndex } from './utils' export type TxId = Nominal export type Tx = Transaction & { accountType?: UtxoAccountType } @@ -65,27 +54,14 @@ type TransactionsByAccountId = Record export type TxIdsByAssetId = PartialRecord export type TxIdsByAccountIdAssetId = PartialRecord -export type RebaseId = Nominal -type RebaseById = PartialRecord - -type RebaseIdsByAssetId = PartialRecord -export type RebaseIdsByAccountIdAssetId = PartialRecord - export type TxsState = { byId: TxHistoryById byAccountIdAssetId: TxIdsByAccountIdAssetId ids: TxId[] } -export type RebasesState = { - byAccountIdAssetId: RebaseIdsByAccountIdAssetId - ids: RebaseId[] - byId: RebaseById -} - export type TxHistory = { txs: TxsState - rebases: RebasesState } export type TxMessage = { payload: { message: Tx; accountId: AccountId } } @@ -99,11 +75,6 @@ export const initialState: TxHistory = { byId: {}, ids: [], // sorted, newest first }, - rebases: { - byAccountIdAssetId: {}, - ids: [], - byId: {}, - }, } const checkIsSpam = (tx: Tx): boolean => { @@ -154,48 +125,6 @@ const updateOrInsertTx = (txHistory: TxHistory, tx: Tx, accountId: AccountId) => ) } -type UpdateOrInsertRebase = (txState: TxHistory, data: RebaseHistoryPayload['payload']) => void - -const updateOrInsertRebase: UpdateOrInsertRebase = (txState, payload) => { - const { accountId, assetId } = payload - const { rebases } = txState - payload.data.forEach(rebase => { - const rebaseId = makeRebaseId({ accountId, assetId, rebase }) - const isNew = !txState.rebases.byId[rebaseId] - - rebases.byId[rebaseId] = rebase - - if (isNew) { - const orderedRebases = orderBy(rebases.byId, 'blockTime', ['desc']).filter(isSome) - const index = orderedRebases.findIndex( - rebase => makeRebaseId({ accountId, assetId, rebase }) === rebaseId, - ) - rebases.ids.splice(index, 0, rebaseId) - } - - deepUpsertArray(rebases.byAccountIdAssetId, accountId, assetId, rebaseId) - }) -} - -type MakeRebaseIdArgs = { - accountId: AccountId - assetId: AssetId - rebase: RebaseHistory -} - -type MakeRebaseId = (args: MakeRebaseIdArgs) => string - -const makeRebaseId: MakeRebaseId = ({ accountId, assetId, rebase }) => - [accountId, assetId, rebase.blockTime].join(UNIQUE_TX_ID_DELIMITER) - -type RebaseHistoryPayload = { - payload: { - accountId: AccountId - assetId: AssetId - data: RebaseHistory[] - } -} - export const txHistory = createSlice({ name: 'txHistory', initialState, @@ -212,50 +141,14 @@ export const txHistory = createSlice({ } } }, - upsertRebaseHistory: (txState, { payload }: RebaseHistoryPayload) => - updateOrInsertRebase(txState, payload), }, extraReducers: builder => builder.addCase(PURGE, () => initialState), }) -type RebaseTxHistoryArgs = { - accountId: AccountId - portfolioAssetIds: AssetId[] -} - export const txHistoryApi = createApi({ ...BASE_RTK_CREATE_API_CONFIG, reducerPath: 'txHistoryApi', endpoints: build => ({ - getFoxyRebaseHistoryByAccountId: build.query({ - queryFn: ({ accountId, portfolioAssetIds }, { dispatch }) => { - const { chainId, account: userAddress } = fromAccountId(accountId) - // foxy is only on eth mainnet, and [] is a valid return type and won't upsert anything - if (chainId !== ethChainId) return { data: [] } - // foxy contract address, note not assetIds - const foxyTokenContractAddress = (() => { - const contractAddress = foxyAddresses[0].foxy.toLowerCase() - if (portfolioAssetIds.some(id => id.includes(contractAddress))) return contractAddress - })() - - // don't do anything below if we don't have FOXy as a portfolio AssetId - if (!foxyTokenContractAddress) return { data: [] } - - // setup foxy api - const foxyApi = getFoxyApi() - - ;(async () => { - const rebaseHistoryArgs = { userAddress, tokenContractAddress: foxyTokenContractAddress } - const data = await foxyApi.getRebaseHistory(rebaseHistoryArgs) - const assetReference = foxyTokenContractAddress - const assetNamespace = ASSET_NAMESPACE.erc20 - const assetId = toAssetId({ chainId, assetNamespace, assetReference }) - const upsertPayload = { accountId, assetId, data } - if (data.length) dispatch(txHistory.actions.upsertRebaseHistory(upsertPayload)) - })() - return { data: [] } - }, - }), getAllTxHistory: build.query({ queryFn: async (accountIds, { dispatch, getState }) => { const results: TransactionsByAccountId = {} diff --git a/src/test/mocks/store.ts b/src/test/mocks/store.ts index 2bf2d019626..e2d39897686 100644 --- a/src/test/mocks/store.ts +++ b/src/test/mocks/store.ts @@ -136,11 +136,6 @@ export const mockStore: ReduxState = { byAccountIdAssetId: {}, ids: [], }, - rebases: { - byAccountIdAssetId: {}, - ids: [], - byId: {}, - }, }, opportunities: { lp: {