mars-v2-frontend/src/api/vaults/getVaultConfigs.ts
Yusuf Seyrek b24bbb3376
New architecture (#196)
* feat: move app to pages features

* feat: route changes

* Use React Router, Remove SSR

* Fix account menu

* Remove app folder

* remove old useParams

* Moved pages back to pages and refactor names

* add layout to route

* clean up

* create hooks for api fetching

* fix refetch of all data on tx complete

* formatting

* fix: fixed the wallet-connector race condition

* remove cosmjs/stargate (#202)

* remove cosmjs/stargate

* add Yusuf as code-orwner

* Singleton client (#203)

* remove cosmjs/stargate

* add Yusuf as code-orwner

* create signleton client and refactor vaults api

* update client name, add apollo apr env

* Setup validate-env and remove checking from apis

* uncomment vaults

* Resolve comments

* fix: html templating, add checks for hydration&window object, reduce bundle size (#204)

* fix: tests

* Fix routing and wallet client (#205)

* Add header to router (as layout)

* Refactor Wallet component

* Remove server fallback packages webpack

* add missing dependency for useeffect

---------

Co-authored-by: Bob van der Helm <34470358+bobthebuidlr@users.noreply.github.com>
Co-authored-by: Linkie Link <linkielink.dev@gmail.com>
2023-05-16 12:39:52 +02:00

65 lines
1.8 KiB
TypeScript

import { getClient } from 'api/cosmwasm-client'
import { ENV, IS_TESTNET } from 'constants/env'
import { TESTNET_VAULTS, VAULTS } from 'constants/vaults'
import {
ArrayOfVaultInfoResponse,
VaultBaseForString,
} from 'types/generated/mars-credit-manager/MarsCreditManager.types'
export default async function getVaultConfigs(): Promise<VaultConfig[]> {
const vaultInfos: VaultInfo[] = await getVaultInfos([])
const vaults = IS_TESTNET ? TESTNET_VAULTS : VAULTS
return vaults.map((vaultMetaData) => {
const vaultConfig = vaultInfos.find((vaultInfo) => vaultInfo.address === vaultMetaData.address)
return {
...vaultMetaData,
...vaultConfig,
} as VaultConfig
})
}
const getVaultInfos = async (
vaultInfos: VaultInfo[],
startAfter?: VaultBaseForString,
): Promise<VaultInfo[]> => {
if (!ENV.ADDRESS_CREDIT_MANAGER) return []
const client = await getClient()
try {
const batch: ArrayOfVaultInfoResponse = await client.queryContractSmart(
ENV.ADDRESS_CREDIT_MANAGER,
{
vaults_info: { limit: 4, start_after: startAfter },
},
)
const batchProcessed = batch?.map((vaultInfo) => {
return {
address: vaultInfo.vault.address,
cap: {
denom: vaultInfo.config.deposit_cap.denom,
used: Number(vaultInfo.utilization.amount),
max: Number(vaultInfo.config.deposit_cap.amount),
},
ltv: {
max: Number(vaultInfo.config.max_ltv),
liq: Number(vaultInfo.config.liquidation_threshold),
},
} as VaultConfig
})
vaultInfos.push(...batchProcessed)
if (batch.length === 4) {
return await getVaultInfos(vaultInfos, {
address: batchProcessed[batchProcessed.length - 1].address,
} as VaultBaseForString)
}
return vaultInfos
} catch {
return vaultInfos
}
}