Skip to content

Commit

Permalink
Merge pull request #366 from curvefi/task/use-lend-api
Browse files Browse the repository at this point in the history
Update API usage from `lending-api`
  • Loading branch information
ignromanov authored Oct 8, 2024
2 parents c770cc8 + 3e57d04 commit 60b968f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 61 deletions.
2 changes: 1 addition & 1 deletion apps/lend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"analyze": "ANALYZE=true next build"
},
"dependencies": {
"@curvefi/lending-api": "^2.2.1",
"@curvefi/lending-api": "^2.3.3",
"@lingui/detect-locale": "^4.6.0",
"@lingui/react": "^4.6.0",
"@supercharge/promise-pool": "^2.3.2",
Expand Down
101 changes: 51 additions & 50 deletions apps/lend/src/lib/apiLending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { INVALID_ADDRESS } from '@/constants'
import { fulfilledValue, getErrorMessage, log } from '@/utils/helpers'
import { BN, shortenAccount } from '@/ui/utils'
import networks from '@/networks'
import { USE_API } from '@/shared/config'

export const helpers = {
initApi: async (chainId: ChainId, wallet: Wallet | null) => {
Expand All @@ -30,7 +31,7 @@ export const helpers = {
getIsUserCloseToLiquidation: (
userFirstBand: number,
userLiquidationBand: number | null,
oraclePriceBand: number | null | undefined
oraclePriceBand: number | null | undefined,
) => {
if (typeof userLiquidationBand !== null && typeof oraclePriceBand !== 'number') {
return false
Expand All @@ -48,7 +49,7 @@ export const helpers = {
const resp = { marketList: [] as string[], error: '' }
try {
log('fetchMarkets', api.chainId)
await api.oneWayfactory.fetchMarkets()
await api.oneWayfactory.fetchMarkets(USE_API)
resp.marketList = api.oneWayfactory.getMarketList()
return resp
} catch (error) {
Expand Down Expand Up @@ -120,7 +121,7 @@ export const helpers = {
},
waitForTransactions: async (hashes: string[], provider: Provider) => {
const { results, errors } = await PromisePool.for(hashes).process(
async (hash) => await provider.waitForTransaction(hash)
async (hash) => await provider.waitForTransaction(hash),
)
if (Array.isArray(errors) && errors.length > 0) {
throw errors
Expand Down Expand Up @@ -176,7 +177,7 @@ const market = {
_sortBands(bandsBalances),
liquidationBand,
owmData,
true
true,
)

results[owm.id] = {
Expand Down Expand Up @@ -206,7 +207,7 @@ const market = {
results[owm.id] = { borrowed: '', collateral: '', error }
})
.process(async ({ owm }) => {
const resp = await owm.stats.ammBalances(useMultiCall)
const resp = await owm.stats.ammBalances(useMultiCall, USE_API)
results[owm.id] = { ...resp, error: '' }
})

Expand All @@ -224,7 +225,7 @@ const market = {
results[owm.id] = { cap: '', available: '', error }
})
.process(async ({ owm }) => {
const resp = await owm.stats.capAndAvailable(useMultiCall)
const resp = await owm.stats.capAndAvailable(useMultiCall, USE_API)
results[owm.id] = { ...resp, error: '' }
})

Expand All @@ -242,7 +243,7 @@ const market = {
results[owm.id] = { totalDebt: '', error }
})
.process(async ({ owm }) => {
const totalDebt = await owm.stats.totalDebt(useMultiCall)
const totalDebt = await owm.stats.totalDebt(useMultiCall, USE_API)
results[owm.id] = { totalDebt, error: '' }
})

Expand Down Expand Up @@ -291,7 +292,7 @@ const market = {
results[owm.id] = { rates: null, error }
})
.process(async ({ owm }) => {
const rates = await owm.stats.rates(useMultiCall)
const rates = await owm.stats.rates(useMultiCall, USE_API)
results[owm.id] = { rates, error: '' }
})

Expand Down Expand Up @@ -390,7 +391,7 @@ const market = {
const { collateral_token, borrowed_token } = owm

const [ammBalance, collateralUsdRate, borrowedUsdRate] = await Promise.all([
owm.stats.ammBalances(useMultiCall),
owm.stats.ammBalances(useMultiCall, USE_API),
api.getUsdRate(collateral_token.address),
api.getUsdRate(borrowed_token.address),
])
Expand Down Expand Up @@ -516,13 +517,13 @@ const user = {
const isCloseToLiquidation = helpers.getIsUserCloseToLiquidation(
reversedUserBands[0],
liquidationBand,
oraclePriceBand
oraclePriceBand,
)
const parsedBandsBalances = await _fetchChartBandBalancesData(
_sortBands(bandsBalances),
liquidationBand,
owmData,
false
false,
)

results[userActiveKey] = {
Expand Down Expand Up @@ -596,7 +597,7 @@ const loanCreate = {
userCollateral: string,
userBorrowed: string,
n: number,
isLeverage: boolean
isLeverage: boolean,
) => {
userCollateral = userCollateral || '0'
userBorrowed = userBorrowed || '0'
Expand Down Expand Up @@ -655,7 +656,7 @@ const loanCreate = {
userBorrowed: string,
debt: string,
n: number,
maxSlippage: string
maxSlippage: string,
) => {
userCollateral = userCollateral || '0'
userBorrowed = userBorrowed || '0'
Expand Down Expand Up @@ -711,7 +712,7 @@ const loanCreate = {
userCollateral: string,
userBorrowed: string,
debt: string,
isLeverage: boolean
isLeverage: boolean,
) => {
totalCollateral = totalCollateral || '0'
userCollateral = userCollateral || '0'
Expand Down Expand Up @@ -799,7 +800,7 @@ const loanCreate = {
debt: string,
n: number,
maxSlippage: string,
isLeverage: boolean
isLeverage: boolean,
) => {
userCollateral = userCollateral || '0'
userBorrowed = userBorrowed || '0'
Expand All @@ -816,8 +817,8 @@ const loanCreate = {
? await owm.leverage.estimateGas.createLoan(userCollateral, userBorrowed, debt, n, +maxSlippage)
: await owm.estimateGas.createLoan(userCollateral, debt, n)
: isLeverage
? await owm.leverage.estimateGas.createLoanApprove(userCollateral, userBorrowed)
: await owm.estimateGas.createLoanApprove(userCollateral)
? await owm.leverage.estimateGas.createLoanApprove(userCollateral, userBorrowed)
: await owm.estimateGas.createLoanApprove(userCollateral)
return resp
} catch (error) {
console.error(error)
Expand All @@ -831,7 +832,7 @@ const loanCreate = {
{ owm }: OWMData,
userCollateral: string,
userBorrowed: string,
isLeverage: boolean
isLeverage: boolean,
) => {
userCollateral = userCollateral || '0'
userBorrowed = userBorrowed || '0'
Expand All @@ -851,7 +852,7 @@ const loanCreate = {
debt: string,
n: number,
maxSlippage: string,
isLeverage: boolean
isLeverage: boolean,
) => {
userCollateral = userCollateral || '0'
userBorrowed = userBorrowed || '0'
Expand Down Expand Up @@ -906,7 +907,7 @@ const loanBorrowMore = {
{ signerAddress }: Api,
{ owm }: OWMData,
userCollateral: string,
debt: string
debt: string,
) => {
userCollateral = userCollateral || '0'
debt = debt || '0'
Expand Down Expand Up @@ -946,7 +947,7 @@ const loanBorrowMore = {
userCollateral: string,
userBorrowed: string,
debt: string,
slippage: string
slippage: string,
) => {
userCollateral = userCollateral || '0'
userBorrowed = userBorrowed || '0'
Expand Down Expand Up @@ -1007,7 +1008,7 @@ const loanBorrowMore = {
userBorrowed: string,
debt: string,
maxSlippage: string,
isLeverage: boolean
isLeverage: boolean,
) => {
userCollateral = userCollateral || '0'
userBorrowed = userBorrowed || '0'
Expand All @@ -1024,8 +1025,8 @@ const loanBorrowMore = {
? await owm.leverage.estimateGas.borrowMore(userCollateral, userBorrowed, debt, +maxSlippage)
: await owm.estimateGas.borrowMore(userCollateral, debt)
: isLeverage
? await owm.leverage.estimateGas.borrowMoreApprove(userCollateral, userBorrowed)
: await owm.estimateGas.borrowMoreApprove(userCollateral)
? await owm.leverage.estimateGas.borrowMoreApprove(userCollateral, userBorrowed)
: await owm.estimateGas.borrowMoreApprove(userCollateral)
return resp
} catch (error) {
console.error(error)
Expand All @@ -1043,7 +1044,7 @@ const loanBorrowMore = {
{ owm }: OWMData,
userCollateral: string,
userBorrowed: string,
isLeverage: boolean
isLeverage: boolean,
) => {
userCollateral = userCollateral || '0'
userBorrowed = userBorrowed || '0'
Expand All @@ -1062,7 +1063,7 @@ const loanBorrowMore = {
userBorrowed: string,
debt: string,
maxSlippage: string,
isLeverage: boolean
isLeverage: boolean,
) => {
userCollateral = userCollateral || '0'
userBorrowed = userBorrowed || '0'
Expand All @@ -1081,7 +1082,7 @@ const loanRepay = {
{ owm }: OWMData,
stateCollateral: string,
userCollateral: string,
userBorrowed: string
userBorrowed: string,
) => {
stateCollateral = stateCollateral || '0'
userCollateral = userCollateral || '0'
Expand All @@ -1104,7 +1105,7 @@ const loanRepay = {
{ owm }: OWMData,
userBorrowed: string,
isFullRepay: boolean,
userStateDebt: string
userStateDebt: string,
) => {
log('detailInfo', userBorrowed)
let resp: { activeKey: string; resp: DetailInfoResp | null; error: string } = { activeKey, resp: null, error: '' }
Expand Down Expand Up @@ -1143,7 +1144,7 @@ const loanRepay = {
userCollateral: string,
userBorrowed: string,
maxSlippage: string,
userStateDebt: string
userStateDebt: string,
) => {
stateCollateral = stateCollateral || '0'
userCollateral = userCollateral || '0'
Expand Down Expand Up @@ -1228,7 +1229,7 @@ const loanRepay = {
userBorrowed: string,
isFullRepay: boolean,
maxSlippage: string,
isLeverage: boolean
isLeverage: boolean,
) => {
stateCollateral = stateCollateral || '0'
userCollateral = userCollateral || '0'
Expand All @@ -1239,27 +1240,27 @@ const loanRepay = {
userCollateral,
userBorrowed,
isFullRepay,
maxSlippage
maxSlippage,
)
let resp = { activeKey, isApproved: false, estimatedGas: null as EstimatedGas, error: '' }

try {
resp.isApproved = isLeverage
? await owm.leverage.repayIsApproved(userCollateral, userBorrowed)
: isFullRepay
? await owm.fullRepayIsApproved()
: await owm.repayIsApproved(userBorrowed)
? await owm.fullRepayIsApproved()
: await owm.repayIsApproved(userBorrowed)
resp.estimatedGas = isLeverage
? resp.isApproved
? await owm.leverage.estimateGas.repay(stateCollateral, userCollateral, userBorrowed, +maxSlippage)
: await owm.leverage.estimateGas.repayApprove(userCollateral, userBorrowed)
: resp.isApproved
? isFullRepay
? await owm.estimateGas.fullRepay()
: await owm.estimateGas.repay(userBorrowed)
: isFullRepay
? await owm.estimateGas.fullRepayApprove()
: await owm.estimateGas.repayApprove(userBorrowed)
? isFullRepay
? await owm.estimateGas.fullRepay()
: await owm.estimateGas.repay(userBorrowed)
: isFullRepay
? await owm.estimateGas.fullRepayApprove()
: await owm.estimateGas.repayApprove(userBorrowed)
return resp
} catch (error) {
console.error(error)
Expand All @@ -1275,7 +1276,7 @@ const loanRepay = {
userCollateral: string,
userBorrowed: string,
isFullRepay: boolean,
isLeverage: boolean
isLeverage: boolean,
) => {
stateCollateral = stateCollateral || '0'
userCollateral = userCollateral || '0'
Expand All @@ -1285,8 +1286,8 @@ const loanRepay = {
isLeverage
? await owm.leverage.repayApprove(userCollateral, userBorrowed)
: isFullRepay
? await owm.fullRepayApprove()
: await owm.repayApprove(userBorrowed)
? await owm.fullRepayApprove()
: await owm.repayApprove(userBorrowed)
return await approve(activeKey, fn, provider)
},
repay: async (
Expand All @@ -1298,7 +1299,7 @@ const loanRepay = {
userBorrowed: string,
isFullRepay: boolean,
maxSlippage: string,
isLeverage: boolean
isLeverage: boolean,
) => {
stateCollateral = stateCollateral || '0'
userCollateral = userCollateral || '0'
Expand All @@ -1308,8 +1309,8 @@ const loanRepay = {
isLeverage
? await owm.leverage.repay(stateCollateral, userCollateral, userBorrowed, +maxSlippage)
: isFullRepay
? await owm.fullRepay()
: await owm.repay(userBorrowed)
? await owm.fullRepay()
: await owm.repay(userBorrowed)
return await submit(activeKey, fn, provider)
},
}
Expand Down Expand Up @@ -1375,7 +1376,7 @@ const loanCollateralAdd = {
{ signerAddress }: Api,
{ owm }: OWMData,
collateral: string,
address?: string
address?: string,
) => {
log('detailInfo', collateral)
let resp: { activeKey: string; resp: DetailInfoResp | null; error: string } = { activeKey, resp: null, error: '' }
Expand Down Expand Up @@ -1453,7 +1454,7 @@ const loanCollateralRemove = {
{ signerAddress }: Api,
{ owm }: OWMData,
collateral: string,
address?: string
address?: string,
) => {
log('loanCollateralRemoveHealthPricesBands', owm.collateral_token.symbol, collateral)
let resp: { activeKey: string; resp: DetailInfoResp | null; error: string } = { activeKey, resp: null, error: '' }
Expand Down Expand Up @@ -1767,7 +1768,7 @@ const vaultWithdraw = {
{ owm }: OWMData,
isFullWithdraw: boolean,
amount: string,
vaultShares: string
vaultShares: string,
) => {
log('vaultWithdraw', owm.id, amount, 'isFullWithdraw', isFullWithdraw, 'vaultShares', vaultShares)

Expand Down Expand Up @@ -2006,7 +2007,7 @@ async function _fetchChartBandBalancesData(
{ bandsBalances, bandsBalancesArr }: { bandsBalances: BandsBalances; bandsBalancesArr: BandsBalancesArr },
liquidationBand: number | null,
{ owm }: OWMData,
isMarket: boolean
isMarket: boolean,
) {
// filter out bands that doesn't have borrowed or collaterals
const ns = isMarket
Expand Down Expand Up @@ -2093,7 +2094,7 @@ function _getPriceImpactResp(priceImpactResp: PromiseSettledResult<string | unde

function _detailInfoRespErrorMessage(
futureRatesResp: PromiseSettledResult<{ borrowApr: string; lendApr: string; borrowApy: string; lendApy: string }>,
bandsResp: PromiseSettledResult<[number, number]>
bandsResp: PromiseSettledResult<[number, number]>,
) {
let errorMessage = ''

Expand Down
1 change: 1 addition & 0 deletions apps/lend/src/shared/config/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const USE_API = true as const
1 change: 1 addition & 0 deletions apps/lend/src/shared/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './api'
Loading

0 comments on commit 60b968f

Please sign in to comment.