Skip to content

Commit 83ec187

Browse files
e2e: oneLendingPool determinism
1 parent cb8aee4 commit 83ec187

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ jobs:
140140

141141
- name: Check runtime errors
142142
if: always()
143+
# todo: add `Cannot destructure .. as it is undefined`
143144
run: if grep --ignore-case --extended-regexp "update depth exceeded|many re-renders|unmounted component|non-unique keys|uncontrolled to controlled|properties of undefined" e2e-*.log; then exit 1; fi
144145

145146
- uses: actions/upload-artifact@v4

tests/cypress/support/generators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Address } from '@curvefi/prices-api'
22
import { range, recordValues } from '@curvefi/prices-api/objects.util'
33

4-
export const MAX_USD_VALUE = 40_000_000 // $ 40m 🤑 not higher so we can test the highest TVL
4+
export const MAX_USD_VALUE = 400_000_000
55

66
export const oneFloat = (minOrMax = 1, maxExclusive?: number): number =>
77
maxExclusive === undefined ? Math.random() * minOrMax : minOrMax + Math.random() * (maxExclusive - minOrMax)

tests/cypress/support/helpers/lending-mocks.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@ import { SMALL_POOL_TVL } from '@ui-kit/features/user-profile/store'
77
const LendingChains = ['ethereum', 'fraxtal', 'arbitrum'] as const
88
export type Chain = (typeof LendingChains)[number]
99

10+
// keep the general pool TVL below the special HighTVL row to guarantee ordering in tests
1011
const oneLargeTvl = () => oneFloat(SMALL_POOL_TVL, MAX_USD_VALUE)
1112
const oneSmallTvl = () => oneFloat(SMALL_POOL_TVL)
1213

1314
const oneLendingPool = (
1415
chain: Chain,
15-
{ utilization, tvl = oneLargeTvl() }: { utilization: number; tvl?: number },
16+
{ utilization = oneFloat(0.99), tvl = oneLargeTvl() }: { utilization?: number; tvl?: number },
1617
): GetMarketsResponse['data'][number] => {
1718
const collateral = oneToken(chain)
1819
const borrowed = oneToken(chain)
19-
const collateralBalance = oneFloat()
2020
const borrowedPrice = borrowed.usdPrice ?? onePrice()
2121
const collateralPrice = collateral.usdPrice ?? onePrice()
22-
const totalAssets = onePrice(MAX_USD_VALUE / collateralPrice)
23-
const totalAssetsUsd = totalAssets * collateralPrice
24-
const totalDebtUsd = utilization * totalAssetsUsd
22+
const liquidityUsd = Math.max(0, oneFloat(0, tvl)) // portion of TVL represented by (assets - debt)
23+
const totalAssetsUsd = liquidityUsd / (1 - utilization)
24+
const totalDebtUsd = totalAssetsUsd * utilization
25+
const totalAssets = totalAssetsUsd / collateralPrice
26+
const totalDebt = totalDebtUsd / borrowedPrice
2527
const minBand = oneInt()
2628
const minted = onePrice()
2729
const redeemed = onePrice(minted)
28-
const collateralBalanceUsd = collateralBalance * collateralPrice
29-
const borrowedBalanceUsd = tvl + totalDebtUsd - collateralBalanceUsd - totalAssetsUsd
30-
const borrowedBalance = borrowedBalanceUsd * borrowedPrice
30+
const remainderUsd = Math.max(tvl - liquidityUsd, 0) // portion of TVL not covered by liquidity
31+
const collateralBalanceUsd = remainderUsd === 0 ? 0 : oneFloat(0, remainderUsd)
32+
const borrowedBalanceUsd = remainderUsd - collateralBalanceUsd
33+
const collateralBalance = collateralBalanceUsd / collateralPrice
34+
const borrowedBalance = borrowedBalanceUsd / borrowedPrice
3135
return {
3236
name: [collateral.symbol, borrowed.symbol].join('-'),
3337
controller: oneAddress(),
@@ -46,7 +50,7 @@ const oneLendingPool = (
4650
price_oracle: onePrice(),
4751
amm_price: onePrice(),
4852
base_price: onePrice(),
49-
total_debt: totalDebtUsd / borrowedPrice,
53+
total_debt: totalDebt,
5054
total_assets: totalAssets,
5155
total_debt_usd: totalDebtUsd,
5256
total_assets_usd: totalAssetsUsd,
@@ -89,7 +93,7 @@ function oneLendingVaultResponse(chain: Chain): GetMarketsResponse {
8993
},
9094
{
9195
// largest TVL to test the sorting
92-
...oneLendingPool(chain, { utilization: oneFloat(), tvl: MAX_USD_VALUE }),
96+
...oneLendingPool(chain, { utilization: oneFloat(0.4, 0.8), tvl: MAX_USD_VALUE * 2 }),
9397
address: HighTVLAddress,
9498
vault: HighTVLAddress,
9599
controller: HighTVLAddress,
@@ -102,12 +106,8 @@ function oneLendingVaultResponse(chain: Chain): GetMarketsResponse {
102106
controller: HighUtilizationAddress,
103107
},
104108
{
105-
// 0 TVL (below 10k) to test the slider filter
106-
...oneLendingPool(chain, { utilization: oneFloat() }),
107-
total_assets_usd: 0,
108-
total_debt_usd: 0,
109-
collateral_balance_usd: 0,
110-
borrowed_balance_usd: 0,
109+
// small TVL to test the slider filter
110+
...oneLendingPool(chain, { tvl: SMALL_POOL_TVL / 2 }),
111111
},
112112
] as GetMarketsResponse['data'])
113113
: []),

0 commit comments

Comments
 (0)