added correct resolving of account positions (#270)

This commit is contained in:
Bob van der Helm 2023-06-26 15:13:21 +02:00 committed by GitHub
parent 415da05b8d
commit 9007e31707
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 11 deletions

View File

@ -1,13 +1,24 @@
import { getCreditManagerQueryClient } from 'api/cosmwasm-client' import { getCreditManagerQueryClient } from 'api/cosmwasm-client'
import { BNCoin } from 'types/classes/BNCoin'
import { Positions } from 'types/generated/mars-credit-manager/MarsCreditManager.types' import { Positions } from 'types/generated/mars-credit-manager/MarsCreditManager.types'
export default async function getAccount(accountId: string): Promise<Positions> { export default async function getAccount(accountId: string): Promise<Account> {
const creditManagerQueryClient = await getCreditManagerQueryClient() const creditManagerQueryClient = await getCreditManagerQueryClient()
const account = creditManagerQueryClient.positions({ accountId }) const accountPosition: Positions = await creditManagerQueryClient.positions({ accountId })
if (account) { if (accountPosition) {
return account const debts = accountPosition.debts.map((debt) => new BNCoin(debt))
const lends = accountPosition.lends.map((lend) => new BNCoin(lend))
const deposits = accountPosition.deposits.map((deposit) => new BNCoin(deposit))
return {
id: accountPosition.account_id,
debts,
deposits,
lends,
vaults: accountPosition.vaults,
}
} }
return new Promise((_, reject) => reject('No account found')) return new Promise((_, reject) => reject('No account found'))

View File

@ -5,7 +5,7 @@ export default async function getAccountDebts(accountId: string): Promise<BNCoin
const account = await getAccount(accountId) const account = await getAccount(accountId)
if (account) { if (account) {
return account.debts.map((coin) => new BNCoin(coin)) return account.debts
} }
return new Promise((_, reject) => reject('Account not found')) return new Promise((_, reject) => reject('Account not found'))

View File

@ -5,7 +5,7 @@ export default async function getAccountDeposits(accountId: string): Promise<BNC
const account = await getAccount(accountId) const account = await getAccount(accountId)
if (account) { if (account) {
return account.deposits.map((coin) => new BNCoin(coin)) return account.deposits
} }
return new Promise((_, reject) => reject('Account not found')) return new Promise((_, reject) => reject('Account not found'))

View File

@ -62,11 +62,12 @@ export default function ConnectedButton() {
useEffect(() => { useEffect(() => {
if (!walletBalances || walletBalances.length === 0) return if (!walletBalances || walletBalances.length === 0) return
setWalletAmount( const newAmount = BigNumber(
BigNumber( walletBalances?.find((coin: Coin) => coin.denom === baseAsset.denom)?.amount ?? 0,
walletBalances?.find((coin: Coin) => coin.denom === baseAsset.denom)?.amount ?? 0, ).div(10 ** baseAsset.decimals)
).div(10 ** baseAsset.decimals),
) if (walletAmount.isEqualTo(newAmount)) return
setWalletAmount(newAmount)
const assetDenoms = marketAssets.map((asset) => asset.denom) const assetDenoms = marketAssets.map((asset) => asset.denom)
const balances = walletBalances.filter((coin) => assetDenoms.includes(coin.denom)) const balances = walletBalances.filter((coin) => assetDenoms.includes(coin.denom))