Skip to content

Commit

Permalink
Update fetching logic to be parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-edge committed Nov 21, 2024
1 parent 8f90806 commit c4c93a1
Showing 1 changed file with 94 additions and 64 deletions.
158 changes: 94 additions & 64 deletions src/components/scenes/Staking/EarnScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,79 +74,106 @@ export const EarnScene = (props: Props) => {

const isFocused = useIsFocused()

function isWalletStakeInfo(value: WalletStakeInfo | null): value is WalletStakeInfo {
return value !== null
}

const refreshStakePositions = async (pluginId: string): Promise<DisplayStakeInfo[]> => {
const isStakingSupported = SPECIAL_CURRENCY_INFO[pluginId]?.isStakingSupported === true && ENV.ENABLE_STAKING
if (!isStakingSupported || STAKE_POLICY_MAP[pluginId] == null) return []

const matchingWallets = wallets.filter((wallet: EdgeCurrencyWallet) => wallet.currencyInfo.pluginId === pluginId)
const updatedDisplayStakeInfos = []
for (const displayStakeInfo of STAKE_POLICY_MAP[pluginId]) {
const { stakePlugin, stakePolicy } = displayStakeInfo

const walletStakePositions = []
for (const wallet of matchingWallets) {
try {
// Determine if a wallet matching this policy has an open position
const stakePosition = await stakePlugin.fetchStakePosition({ stakePolicyId: stakePolicy.stakePolicyId, wallet, account })
const allocations = getPositionAllocations(stakePosition)
const { staked, earned, unstaked } = allocations
const isPositionOpen = [...staked, ...earned, ...unstaked].some(positionAllocation => !zeroString(positionAllocation.nativeAmount))

walletStakePositions.push({ wallet, isPositionOpen, stakePosition })
} catch (e) {
showDevError(e)
}
}
const matchingWallets = wallets.filter(wallet => wallet.currencyInfo.pluginId === pluginId)
const displayStakeInfos = STAKE_POLICY_MAP[pluginId]

// Create a new displayStakeInfo object
updatedDisplayStakeInfos.push({
stakePlugin,
stakePolicy,
walletStakeInfos: walletStakePositions
const updatedDisplayStakeInfos = await Promise.all(
displayStakeInfos.map(async displayStakeInfo => {
const { stakePlugin, stakePolicy } = displayStakeInfo

const walletStakePositionsPromises = matchingWallets.map(async wallet => {
try {
// Determine if a wallet matching this policy has an open position
const stakePosition = await stakePlugin.fetchStakePosition({
stakePolicyId: stakePolicy.stakePolicyId,
wallet,
account
})
const allocations = getPositionAllocations(stakePosition)
const { staked, earned, unstaked } = allocations
const isPositionOpen = [...staked, ...earned, ...unstaked].some(positionAllocation => !zeroString(positionAllocation.nativeAmount))

return { wallet, isPositionOpen, stakePosition }
} catch (e) {
showDevError(e)
return null
}
})

const walletStakePositions = (await Promise.all(walletStakePositionsPromises)).filter(isWalletStakeInfo)

// Create a new displayStakeInfo object
return {
stakePlugin,
stakePolicy,
walletStakeInfos: walletStakePositions
}
})
}
)

return updatedDisplayStakeInfos
}

useAsyncEffect(
async () => {
for (const pluginId of Object.keys(currencyConfigMap)) {
const isStakingSupported = SPECIAL_CURRENCY_INFO[pluginId]?.isStakingSupported === true && ENV.ENABLE_STAKING
if (STAKE_POLICY_MAP[pluginId] != null || !isStakingSupported) continue

// Initialize stake policy
const stakePlugins = await getStakePlugins(pluginId)
STAKE_POLICY_MAP[pluginId] = []

const matchingWallets = wallets.filter((wallet: EdgeCurrencyWallet) => wallet.currencyInfo.pluginId === pluginId)
for (const stakePlugin of stakePlugins) {
const stakePolicies = stakePlugin.getPolicies({ pluginId })

for (const stakePolicy of stakePolicies) {
const walletStakePositions = []
for (const wallet of matchingWallets) {
try {
// Determine if a wallet matching this policy has an open position
const stakePosition = await stakePlugin.fetchStakePosition({ stakePolicyId: stakePolicy.stakePolicyId, wallet, account })
const allocations = getPositionAllocations(stakePosition)
const { staked, earned, unstaked } = allocations
const isPositionOpen = [...staked, ...earned, ...unstaked].some(positionAllocation => !zeroString(positionAllocation.nativeAmount))

walletStakePositions.push({ wallet, isPositionOpen, stakePosition })
} catch (e) {
showDevError(e)
}
}

STAKE_POLICY_MAP[pluginId].push({
stakePlugin,
stakePolicy,
walletStakeInfos: walletStakePositions
const pluginIds = Object.keys(currencyConfigMap)
await Promise.all(
pluginIds.map(async pluginId => {
const isStakingSupported = SPECIAL_CURRENCY_INFO[pluginId]?.isStakingSupported === true && ENV.ENABLE_STAKING
if (STAKE_POLICY_MAP[pluginId] != null || !isStakingSupported) return

const stakePlugins = await getStakePlugins(pluginId)
const matchingWallets = wallets.filter(wallet => wallet.currencyInfo.pluginId === pluginId)

// Initialize stake policy
STAKE_POLICY_MAP[pluginId] = []

await Promise.all(
stakePlugins.map(async stakePlugin => {
const stakePolicies = stakePlugin.getPolicies({ pluginId })

await Promise.all(
stakePolicies.map(async stakePolicy => {
const walletStakePositionsPromises = matchingWallets.map(async wallet => {
try {
const stakePosition = await stakePlugin.fetchStakePosition({
stakePolicyId: stakePolicy.stakePolicyId,
wallet,
account
})
const allocations = getPositionAllocations(stakePosition)
const { staked, earned, unstaked } = allocations
const isPositionOpen = [...staked, ...earned, ...unstaked].some(positionAllocation => !zeroString(positionAllocation.nativeAmount))

return { wallet, isPositionOpen, stakePosition }
} catch (e) {
showDevError(e)
return null
}
})

// Use the type guard function here
const walletStakePositions = (await Promise.all(walletStakePositionsPromises)).filter(isWalletStakeInfo)

STAKE_POLICY_MAP[pluginId].push({
stakePlugin,
stakePolicy,
walletStakeInfos: walletStakePositions
})
})
)
})
}
}
}
)
})
)
setIsLoadingPortfolio(false)
setIsLoadingDiscover(false)
},
Expand All @@ -160,10 +187,13 @@ export const EarnScene = (props: Props) => {
if (isFocused && !isPrevFocused) {
setIsLoadingPortfolio(true)

for (const pluginId of Object.keys(currencyConfigMap)) {
const newDisplayStakeInfos = await refreshStakePositions(pluginId)
STAKE_POLICY_MAP[pluginId] = newDisplayStakeInfos
}
const pluginIds = Object.keys(currencyConfigMap)
await Promise.all(
pluginIds.map(async pluginId => {
const newDisplayStakeInfos = await refreshStakePositions(pluginId)
STAKE_POLICY_MAP[pluginId] = newDisplayStakeInfos
})
)

setIsLoadingPortfolio(false)
}
Expand Down

0 comments on commit c4c93a1

Please sign in to comment.