Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/centrifuge/apps into asset-…
Browse files Browse the repository at this point in the history
…finance-repay-redesign
  • Loading branch information
hieronx committed Aug 8, 2024
2 parents 17fbc70 + a3cb6f1 commit 00dc506
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 88 deletions.
5 changes: 3 additions & 2 deletions centrifuge-app/src/components/PoolFees/ChargeFeesDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const ChargeFeesDrawer = ({ onClose, isOpen }: ChargeFeesProps) => {
const [updateCharge, setUpdateCharge] = React.useState(false)
const address = useAddress()
const isAllowedToCharge = feeChainData?.destination && addressToHex(feeChainData.destination) === address
const maxFee = formatPercentage(feeChainData?.amounts.percentOfNav.toPercent() || 0)

const { execute: chargeFeeTx, isLoading: isChargeFeeLoading } = useCentrifugeTransaction('Charge fee', (cent) => {
return cent.pools.chargePoolFee
Expand Down Expand Up @@ -146,15 +147,15 @@ export const ChargeFeesDrawer = ({ onClose, isOpen }: ChargeFeesProps) => {
secondaryLabel={`Maximum charge ${formatBalance(
maxCharge || 0,
pool.currency.symbol
)} (${formatPercentage(feeChainData?.amounts.percentOfNav.toString() || 0)} NAV)`}
)} (${maxFee} NAV)`}
onChange={(value) => form.setFieldValue('amount', value)}
/>
)
}}
</Field>
<Box bg="backgroundButtonSecondary" p={1} borderRadius="2px">
<Text variant="body3" color="textSecondary">
Charging of fees will be finalized by the issuer of the pool when executing orders
Fees charged will be paid during the execution of the epoch, if sufficient liquidity is available
</Text>
</Box>
<ButtonGroup variant="small">
Expand Down
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 00dc506

Please sign in to comment.