Skip to content

Commit

Permalink
feat: remove foxy rebase history (#5777)
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre authored Dec 6, 2023
1 parent fd098f5 commit 1df5260
Show file tree
Hide file tree
Showing 20 changed files with 21 additions and 385 deletions.
1 change: 0 additions & 1 deletion .env.app
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion .env.develop
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion .env.e2e
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion .env.private
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion react-app-rewired/headers/csps/chains/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
],
}
1 change: 0 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
15 changes: 0 additions & 15 deletions src/context/AppProvider/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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:
}
Expand Down
10 changes: 3 additions & 7 deletions src/hooks/useBalanceChartData/useBalanceChartData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<number>((acc, bucket: Bucket) => acc + bucket.txs.length, 0)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 6 additions & 31 deletions src/hooks/useBalanceChartData/useBalanceChartData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -25,7 +24,6 @@ import {
selectBalanceChartCryptoBalancesByAccountIdAboveThreshold,
selectCryptoPriceHistoryTimeframe,
selectFiatPriceHistoryTimeframe,
selectRebasesByFilter,
selectSelectedCurrency,
selectTxsByFilter,
selectWalletId,
Expand All @@ -48,7 +46,6 @@ export type Bucket = {
end: dayjs.Dayjs
balance: BucketBalance
txs: Tx[]
rebases: RebaseHistory[]
}

type BucketMeta = {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand All @@ -445,7 +421,6 @@ export const useBalanceChartData: UseBalanceChartData = args => {
balances,
setBalanceChartData,
walletId,
rebases,
selectedCurrency,
])

Expand Down
4 changes: 2 additions & 2 deletions src/lib/address/unstoppable-domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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',
Expand Down
Loading

0 comments on commit 1df5260

Please sign in to comment.