Skip to content

Commit

Permalink
Fix Prime balances (#2352)
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser authored Aug 7, 2024
1 parent dd462c2 commit 6b13eda
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 86 deletions.
150 changes: 88 additions & 62 deletions centrifuge-app/src/components/Portfolio/usePortfolio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export function usePortfolio(substrateAddress?: string) {
// })
// return result

const pools = usePools()
const { data: subData } = useSubquery(
`query ($account: String!) {
account(
Expand Down Expand Up @@ -166,6 +167,15 @@ export function usePortfolio(substrateAddress?: string) {
}
}
}
investorPositions {
nodes {
holdingQuantity
poolId
purchasePrice
timestamp
trancheId
}
}
}
}`,
{
Expand All @@ -177,70 +187,86 @@ export function usePortfolio(substrateAddress?: string) {
)

const data = useMemo(() => {
return (
(subData?.account as undefined | {}) &&
(Object.fromEntries(
subData.account.trancheBalances.nodes.map((tranche: any) => {
const decimals = tranche.pool.currency.decimals
const tokenPrice = new Price(tranche.tranche.tokenPrice)
let freeTrancheTokens = new CurrencyBalance(0, decimals)

const claimableCurrency = new CurrencyBalance(tranche.claimableCurrency, decimals)
const claimableTrancheTokens = new TokenBalance(tranche.claimableTrancheTokens, decimals)
const pendingInvestCurrency = new CurrencyBalance(tranche.pendingInvestCurrency, decimals)
const pendingRedeemTrancheTokens = new TokenBalance(tranche.pendingRedeemTrancheTokens, decimals)
const sumClaimedCurrency = new CurrencyBalance(tranche.sumClaimedCurrency, decimals)
const sumClaimedTrancheTokens = new TokenBalance(tranche.sumClaimedTrancheTokens, decimals)

const currencyAmounts = subData.account.currencyBalances.nodes.filter(
(b: any) => b.currency.trancheId && b.currency.trancheId === tranche.trancheId
)
if (currencyAmounts.length) {
freeTrancheTokens = new CurrencyBalance(
currencyAmounts.reduce((acc: BN, cur: any) => acc.add(new BN(cur.amount)), new BN(0)),
decimals
)
}
const trancheBalances: Record<string, { totalTrancheTokens: TokenBalance; tokenPrice: Price }> = {}

const totalTrancheTokens = new CurrencyBalance(
new BN(tranche.claimableTrancheTokens)
.add(new BN(tranche.pendingRedeemTrancheTokens))
.add(freeTrancheTokens),
decimals
)
subData?.account?.investorPositions.nodes.forEach((position: any) => {
const pool = pools?.find((p) => p.id === position.poolId)
const trancheId = position.trancheId.split('-')[1]
const decimals = pool?.currency.decimals ?? 18
const tokenPrice = pool?.tranches.find((t) => trancheId === t.id)?.tokenPrice ?? Price.fromFloat(1)
const balance = new TokenBalance(position.holdingQuantity, decimals)
const existing = trancheBalances[trancheId]
if (existing) {
existing.totalTrancheTokens.iadd(balance)
} else {
trancheBalances[trancheId] = { totalTrancheTokens: balance, tokenPrice }
}
})
// return (
// (subData?.account as undefined | {}) &&
// (Object.fromEntries(
// subData.account.trancheBalances.nodes.map((tranche: any) => {
// const decimals = tranche.pool.currency.decimals
// const tokenPrice = new Price(tranche.tranche.tokenPrice)
// let freeTrancheTokens = new CurrencyBalance(0, decimals)

return [
tranche.trancheId.split('-')[1],
{
claimableCurrency,
claimableTrancheTokens,
pendingInvestCurrency,
pendingRedeemTrancheTokens,
sumClaimedCurrency,
sumClaimedTrancheTokens,
totalTrancheTokens,
freeTrancheTokens,
tokenPrice,
},
]
})
) as Record<
string,
{
claimableCurrency: CurrencyBalance
claimableTrancheTokens: TokenBalance
pendingInvestCurrency: CurrencyBalance
pendingRedeemTrancheTokens: TokenBalance
sumClaimedCurrency: CurrencyBalance
sumClaimedTrancheTokens: TokenBalance
totalTrancheTokens: TokenBalance
freeTrancheTokens: TokenBalance
tokenPrice: Price
// TODO: add reservedTrancheTokens
}
>)
)
}, [subData])
// const claimableCurrency = new CurrencyBalance(tranche.claimableCurrency, decimals)
// const claimableTrancheTokens = new TokenBalance(tranche.claimableTrancheTokens, decimals)
// const pendingInvestCurrency = new CurrencyBalance(tranche.pendingInvestCurrency, decimals)
// const pendingRedeemTrancheTokens = new TokenBalance(tranche.pendingRedeemTrancheTokens, decimals)
// const sumClaimedCurrency = new CurrencyBalance(tranche.sumClaimedCurrency, decimals)
// const sumClaimedTrancheTokens = new TokenBalance(tranche.sumClaimedTrancheTokens, decimals)

// const currencyAmounts = subData.account.currencyBalances.nodes.filter(
// (b: any) => b.currency.trancheId && b.currency.trancheId === tranche.trancheId
// )
// if (currencyAmounts.length) {
// freeTrancheTokens = new CurrencyBalance(
// currencyAmounts.reduce((acc: BN, cur: any) => acc.add(new BN(cur.amount)), new BN(0)),
// decimals
// )
// }

// const totalTrancheTokens = new CurrencyBalance(
// new BN(tranche.claimableTrancheTokens)
// .add(new BN(tranche.pendingRedeemTrancheTokens))
// .add(freeTrancheTokens),
// decimals
// )

// return [
// tranche.trancheId.split('-')[1],
// {
// claimableCurrency,
// claimableTrancheTokens,
// pendingInvestCurrency,
// pendingRedeemTrancheTokens,
// sumClaimedCurrency,
// sumClaimedTrancheTokens,
// totalTrancheTokens,
// freeTrancheTokens,
// tokenPrice,
// },
// ]
// })
// ) satisfies Record<
// string,
// {
// claimableCurrency: CurrencyBalance
// claimableTrancheTokens: TokenBalance
// pendingInvestCurrency: CurrencyBalance
// pendingRedeemTrancheTokens: TokenBalance
// sumClaimedCurrency: CurrencyBalance
// sumClaimedTrancheTokens: TokenBalance
// totalTrancheTokens: TokenBalance
// freeTrancheTokens: TokenBalance
// tokenPrice: Price
// // TODO: add reservedTrancheTokens
// }
// >)
// )
return trancheBalances
}, [subData, pools])

return data
}
Expand Down
71 changes: 47 additions & 24 deletions centrifuge-app/src/pages/Prime/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CurrencyBalance, addressToHex } from '@centrifuge/centrifuge-js'
import { useCentrifugeUtils, useGetNetworkName } from '@centrifuge/centrifuge-react'
import { AnchorButton, Box, IconExternalLink, Shelf, Text, TextWithPlaceholder } from '@centrifuge/fabric'
import { BN } from 'bn.js'
import { Column, DataTable, FilterableTableHeader, SortableTableHeader } from '../../components/DataTable'
import { LayoutBase } from '../../components/LayoutBase'
import { LayoutSection } from '../../components/LayoutBase/LayoutSection'
Expand Down Expand Up @@ -105,6 +104,15 @@ function DaoPortfoliosTable() {
}
}
}
investorPositions {
nodes {
holdingQuantity
poolId
purchasePrice
timestamp
trancheId
}
}
}
}
}`,
Expand All @@ -116,30 +124,45 @@ function DaoPortfoliosTable() {
const mapped: Row[] = daos.map((dao, i) => {
const account = subData?.accounts.nodes.find((n: any) => n.id === dao.centAddress)
const investTxs = account?.investorTransactions.nodes
const trancheBalances = !!account
? Object.fromEntries(
account.trancheBalances.nodes.map((tranche: any) => {
const pool = pools?.find((p) => p.id === tranche.poolId)
const decimals = pool?.currency.decimals ?? 18
const tokenPrice = pool?.tranches.find((t) => tranche.trancheId.endsWith(t.id))?.tokenPrice?.toFloat() ?? 1
let balance = new CurrencyBalance(
new BN(tranche.claimableTrancheTokens).add(new BN(tranche.pendingRedeemTrancheTokens)),
decimals
).toFloat()
const trancheBalances: Record<string, { balance: number; tokenPrice: number }> = {}

const subqueryCurrencies = account?.currencyBalances.nodes.filter(
(b: any) => b.currency.trancheId && b.currency.trancheId === tranche.trancheId
)
if (subqueryCurrencies.length) {
balance += subqueryCurrencies.reduce(
(acc: number, cur: any) => acc + new CurrencyBalance(cur.amount, decimals).toFloat(),
0
)
}
return [tranche.trancheId.split('-')[1], { balance, tokenPrice }]
})
)
: {}
account?.investorPositions.nodes.forEach((position: any) => {
const pool = pools?.find((p) => p.id === position.poolId)
const trancheId = position.trancheId.split('-')[1]
const decimals = pool?.currency.decimals ?? 18
const tokenPrice = pool?.tranches.find((t) => trancheId === t.id)?.tokenPrice?.toFloat() ?? 1
const balance = new CurrencyBalance(position.holdingQuantity, decimals).toFloat()
const existing = trancheBalances[trancheId]
if (existing) {
existing.balance += balance
} else {
trancheBalances[trancheId] = { balance, tokenPrice }
}
})
// const trancheBalances = !!account
// ? Object.fromEntries(
// account.trancheBalances.nodes.map((tranche: any) => {
// const pool = pools?.find((p) => p.id === tranche.poolId)
// const decimals = pool?.currency.decimals ?? 18
// const tokenPrice = pool?.tranches.find((t) => tranche.trancheId.endsWith(t.id))?.tokenPrice?.toFloat() ?? 1
// let balance = new CurrencyBalance(
// new BN(tranche.claimableTrancheTokens).add(new BN(tranche.pendingRedeemTrancheTokens)),
// decimals
// ).toFloat()

// const subqueryCurrencies = account?.currencyBalances.nodes.filter(
// (b: any) => b.currency.trancheId && b.currency.trancheId === tranche.trancheId
// )
// if (subqueryCurrencies.length) {
// balance += subqueryCurrencies.reduce(
// (acc: number, cur: any) => acc + new CurrencyBalance(cur.amount, decimals).toFloat(),
// 0
// )
// }
// return [tranche.trancheId.split('-')[1], { balance, tokenPrice }]
// })
// )
// : {}
const totalValue = Object.values(trancheBalances)?.reduce(
(acc, { balance, tokenPrice }) => acc + balance * tokenPrice,
0
Expand Down

0 comments on commit 6b13eda

Please sign in to comment.