-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: decouple getPools function
- Loading branch information
1 parent
f12a609
commit 1a9ca79
Showing
13 changed files
with
327 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...vm/src/clients/api/queries/useGetPools/getPools/formatOutput/formatDistributions/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...vm/src/clients/api/queries/useGetPools/getPools/getIsolatedPoolParticipantCounts/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getIsolatedPoolParticipantsCount } from 'clients/subgraph'; | ||
import type { ChainId } from 'types'; | ||
import type { MarketParticipantsCounts } from '../../types'; | ||
|
||
export const getIsolatedPoolParticipantCounts = async ({ chainId }: { chainId: ChainId }) => { | ||
const isolatedPoolParticipantsCount = await getIsolatedPoolParticipantsCount({ chainId }); | ||
|
||
const isolatedPoolParticipantsCountMap = new Map<string, MarketParticipantsCounts>(); | ||
(isolatedPoolParticipantsCount?.pools || []).forEach(pool => | ||
pool.markets.forEach(market => { | ||
isolatedPoolParticipantsCountMap.set(market.id.toLowerCase(), { | ||
borrowerCount: +market.borrowerCount, | ||
supplierCount: +market.supplierCount, | ||
}); | ||
}), | ||
); | ||
|
||
return { isolatedPoolParticipantsCountMap }; | ||
}; |
53 changes: 53 additions & 0 deletions
53
apps/evm/src/clients/api/queries/useGetPools/getPools/getUserCollateralAddresses/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
type IsolatedPoolComptroller, | ||
type LegacyPoolComptroller, | ||
getIsolatedPoolComptrollerContract, | ||
} from 'libs/contracts'; | ||
import type { Provider } from 'libs/wallet'; | ||
import type { ChainId } from 'types'; | ||
import { isPoolIsolated } from 'utilities'; | ||
import type { ApiPool } from '../getApiPools'; | ||
|
||
export const getUserCollateralAddresses = async ({ | ||
accountAddress, | ||
apiPools, | ||
chainId, | ||
provider, | ||
legacyPoolComptrollerContract, | ||
}: { | ||
accountAddress: string; | ||
apiPools: ApiPool[]; | ||
chainId: ChainId; | ||
provider: Provider; | ||
legacyPoolComptrollerContract?: LegacyPoolComptroller; | ||
}) => { | ||
const getAssetsInPromises: ReturnType<IsolatedPoolComptroller['getAssetsIn']>[] = []; | ||
|
||
apiPools.forEach(pool => { | ||
const isIsolated = isPoolIsolated({ | ||
chainId, | ||
comptrollerAddress: pool.address, | ||
}); | ||
|
||
if (!isIsolated) { | ||
return; | ||
} | ||
|
||
const comptrollerContract = getIsolatedPoolComptrollerContract({ | ||
signerOrProvider: provider, | ||
address: pool.address, | ||
}); | ||
|
||
if (accountAddress) { | ||
getAssetsInPromises.push(comptrollerContract.getAssetsIn(accountAddress)); | ||
} | ||
}); | ||
|
||
if (accountAddress && legacyPoolComptrollerContract) { | ||
getAssetsInPromises.push(legacyPoolComptrollerContract.getAssetsIn(accountAddress)); | ||
} | ||
|
||
const results = await Promise.all(getAssetsInPromises); | ||
|
||
return { userCollateralAddresses: results.flat() }; | ||
}; |
27 changes: 27 additions & 0 deletions
27
apps/evm/src/clients/api/queries/useGetPools/getPools/getUserPrimeApys/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Prime } from 'libs/contracts'; | ||
import { convertAprBipsToApy } from 'utilities'; | ||
import type { PrimeApy } from '../../types'; | ||
|
||
export const getUserPrimeApys = async ({ | ||
primeContract, | ||
accountAddress, | ||
primeVTokenAddresses, | ||
}: { primeContract: Prime; accountAddress: string; primeVTokenAddresses: string[] }) => { | ||
const primeAprs = await Promise.all( | ||
primeVTokenAddresses.map(primeVTokenAddress => | ||
primeContract.calculateAPR(primeVTokenAddress, accountAddress), | ||
), | ||
); | ||
|
||
const userPrimeApyMap = new Map<string, PrimeApy>(); | ||
primeAprs.forEach((primeApr, index) => { | ||
const apys: PrimeApy = { | ||
borrowApy: convertAprBipsToApy({ aprBips: primeApr.borrowAPR.toString() || '0' }), | ||
supplyApy: convertAprBipsToApy({ aprBips: primeApr.supplyAPR.toString() || '0' }), | ||
}; | ||
|
||
userPrimeApyMap.set(primeVTokenAddresses[index], apys); | ||
}); | ||
|
||
return { userPrimeApyMap }; | ||
}; |
96 changes: 96 additions & 0 deletions
96
apps/evm/src/clients/api/queries/useGetPools/getPools/getUserTokenBalances/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { getTokenBalances } from 'clients/api'; | ||
import { NATIVE_TOKEN_ADDRESS } from 'constants/address'; | ||
import type { PoolLens, VenusLens } from 'libs/contracts'; | ||
import type { Provider } from 'libs/wallet'; | ||
import type { ChainId, Token } from 'types'; | ||
import { findTokenByAddress, isPoolIsolated } from 'utilities'; | ||
import type { ApiPool } from '../getApiPools'; | ||
|
||
export const getUserTokenBalances = async ({ | ||
accountAddress, | ||
apiPools, | ||
chainId, | ||
tokens, | ||
provider, | ||
poolLensContract, | ||
venusLensContract, | ||
}: { | ||
accountAddress: string; | ||
apiPools: ApiPool[]; | ||
chainId: ChainId; | ||
tokens: Token[]; | ||
provider: Provider; | ||
poolLensContract: PoolLens; | ||
venusLensContract?: VenusLens; | ||
}) => { | ||
// Extract token records and addresses | ||
const [legacyPoolVTokenAddresses, isolatedPoolsVTokenAddresses, underlyingTokens] = | ||
apiPools.reduce<[string[], string[], Token[]]>( | ||
(acc, pool) => { | ||
const newLegacyPoolVTokenAddresses: string[] = []; | ||
const newIsolatedPoolsVTokenAddresses: string[] = []; | ||
const newUnderlyingTokens: Token[] = []; | ||
const newUnderlyingTokenAddresses: string[] = []; | ||
|
||
pool.markets.forEach(market => { | ||
const isIsolated = isPoolIsolated({ | ||
chainId, | ||
comptrollerAddress: pool.address, | ||
}); | ||
|
||
// VToken addresses are unique | ||
if (isIsolated) { | ||
newIsolatedPoolsVTokenAddresses.push(market.address.toLowerCase()); | ||
} else { | ||
newLegacyPoolVTokenAddresses.push(market.address.toLowerCase()); | ||
} | ||
|
||
const underlyingToken = findTokenByAddress({ | ||
address: market.underlyingAddress || NATIVE_TOKEN_ADDRESS, | ||
tokens, | ||
}); | ||
|
||
if ( | ||
underlyingToken && | ||
!newUnderlyingTokenAddresses.includes(underlyingToken.address.toLowerCase()) | ||
) { | ||
newUnderlyingTokens.push(underlyingToken); | ||
newUnderlyingTokenAddresses.push(underlyingToken.address.toLowerCase()); | ||
} | ||
}); | ||
|
||
return [ | ||
acc[0].concat(newLegacyPoolVTokenAddresses), | ||
acc[1].concat(newIsolatedPoolsVTokenAddresses), | ||
acc[2].concat(newUnderlyingTokens), | ||
]; | ||
}, | ||
[[], [], []], | ||
); | ||
|
||
const [userIsolatedPoolVTokenBalances, userLegacyPoolVTokenBalances, userTokenBalances] = | ||
await Promise.all([ | ||
accountAddress | ||
? poolLensContract.callStatic.vTokenBalancesAll( | ||
isolatedPoolsVTokenAddresses, | ||
accountAddress, | ||
) | ||
: undefined, | ||
accountAddress && venusLensContract | ||
? venusLensContract.callStatic.vTokenBalancesAll(legacyPoolVTokenAddresses, accountAddress) | ||
: undefined, | ||
accountAddress | ||
? getTokenBalances({ | ||
accountAddress, | ||
tokens: underlyingTokens, | ||
provider, | ||
}) | ||
: undefined, | ||
]); | ||
|
||
return { | ||
userIsolatedPoolVTokenBalances, | ||
userLegacyPoolVTokenBalances, | ||
userTokenBalances, | ||
}; | ||
}; |
21 changes: 21 additions & 0 deletions
21
apps/evm/src/clients/api/queries/useGetPools/getPools/getUserVaiBorrowBalance/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import BigNumber from 'bignumber.js'; | ||
import type { VaiController } from 'libs/contracts'; | ||
|
||
export const getUserVaiBorrowBalance = async ({ | ||
accountAddress, | ||
vaiControllerContract, | ||
}: { accountAddress: string; vaiControllerContract?: VaiController }) => { | ||
const [_accrueVaiInterest, vaiRepayAmountMantissa] = await Promise.all([ | ||
// Call (statically) accrueVAIInterest to calculate past accrued interests before fetching all | ||
// interests. Since multicall will batch these requests, the call to accrueVAIInterest and | ||
// getVAIRepayAmount will happen in the same request (thus making the accrual possible) | ||
vaiControllerContract ? vaiControllerContract.callStatic.accrueVAIInterest() : undefined, | ||
vaiControllerContract ? vaiControllerContract.getVAIRepayAmount(accountAddress) : undefined, | ||
]); | ||
|
||
const userVaiBorrowBalanceMantissa = vaiRepayAmountMantissa | ||
? new BigNumber(vaiRepayAmountMantissa.toString()) | ||
: undefined; | ||
|
||
return { userVaiBorrowBalanceMantissa }; | ||
}; |
Oops, something went wrong.