Skip to content

Commit

Permalink
Hotfix: finance form validation, subql self hosted, withdraw batch fix (
Browse files Browse the repository at this point in the history
#2436)

* Point subquery to new version

* Fix pending NAV calc

* Fix mistake in subquery urls

* Fix withdraw address validation

* Include withdraw batch only where necessary

* Undo accidental commit

* Revalidate forms when source changes
  • Loading branch information
sophialittlejohn authored Sep 6, 2024
1 parent d25e5e5 commit 699bebd
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 48 deletions.
2 changes: 1 addition & 1 deletion centrifuge-app/.env-config/.env.ff-prod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ REACT_APP_ONBOARDING_API_URL=https://europe-central2-centrifuge-production-x.clo
REACT_APP_PINNING_API_URL=https://europe-central2-centrifuge-production-x.cloudfunctions.net/pinning-api-production
REACT_APP_POOL_CREATION_TYPE=propose
REACT_APP_RELAY_WSS_URL=wss://rpc.polkadot.io
REACT_APP_SUBQUERY_URL=https://api.subquery.network/sq/centrifuge/pools-multichain
REACT_APP_SUBQUERY_URL=https://api.centrifuge.io
REACT_APP_SUBSCAN_URL=https://centrifuge.subscan.io
REACT_APP_TINLAKE_NETWORK=mainnet
REACT_APP_INFURA_KEY=8ed99a9a115349bbbc01dcf3a24edc96
Expand Down
2 changes: 1 addition & 1 deletion centrifuge-app/.env-config/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ REACT_APP_ONBOARDING_API_URL=https://europe-central2-centrifuge-production-x.clo
REACT_APP_PINNING_API_URL=https://europe-central2-centrifuge-production-x.cloudfunctions.net/pinning-api-production
REACT_APP_POOL_CREATION_TYPE=propose
REACT_APP_RELAY_WSS_URL=wss://rpc.polkadot.io
REACT_APP_SUBQUERY_URL=https://api.subquery.network/sq/centrifuge/pools-multichain
REACT_APP_SUBQUERY_URL=https://api.centrifuge.io
REACT_APP_SUBSCAN_URL=https://centrifuge.subscan.io
REACT_APP_TINLAKE_NETWORK=mainnet
REACT_APP_INFURA_KEY=8ed99a9a115349bbbc01dcf3a24edc96
Expand Down
7 changes: 6 additions & 1 deletion centrifuge-app/src/pages/Loan/ExternalFinanceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ export function ExternalFinanceForm({ loan, source }: { loan: ExternalLoan; sour
const maxAvailable =
source === 'reserve' ? pool.reserve.available.toDecimal() : sourceLoan.outstandingDebt.toDecimal()

const withdraw = useWithdraw(loan.poolId, account!, totalFinance)
const withdraw = useWithdraw(loan.poolId, account!, totalFinance, source)

React.useEffect(() => {
financeForm.validateForm()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [source])

if (loan.status === 'Closed' || ('valuationMethod' in loan.pricing && loan.pricing.valuationMethod !== 'oracle')) {
return null
Expand Down
5 changes: 5 additions & 0 deletions centrifuge-app/src/pages/Loan/ExternalRepayForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export function ExternalRepayForm({ loan, destination }: { loan: ExternalLoan; d
validateOnMount: true,
})

React.useEffect(() => {
repayForm.validateForm()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [destination])

const repayFormRef = React.useRef<HTMLFormElement>(null)
useFocusInvalidInput(repayForm, repayFormRef)

Expand Down
58 changes: 38 additions & 20 deletions centrifuge-app/src/pages/Loan/FinanceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,15 @@ function InternalFinanceForm({ loan, source }: { loan: LoanType; source: string
validateOnMount: true,
})

React.useEffect(() => {
financeForm.validateForm()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [source])

const financeFormRef = React.useRef<HTMLFormElement>(null)
useFocusInvalidInput(financeForm, financeFormRef)

const withdraw = useWithdraw(loan.poolId, account!, Dec(financeForm.values.principal || 0))
const withdraw = useWithdraw(loan.poolId, account!, Dec(financeForm.values.principal || 0), source)

if (loan.status === 'Closed') {
return null
Expand Down Expand Up @@ -455,7 +460,7 @@ function Mux({
)
}

export function useWithdraw(poolId: string, borrower: CombinedSubstrateAccount, amount: Decimal) {
export function useWithdraw(poolId: string, borrower: CombinedSubstrateAccount, amount: Decimal, source: string) {
const pool = usePool(poolId)
const isLocalAsset = typeof pool.currency.key !== 'string' && 'LocalAsset' in pool.currency.key
const access = usePoolAccess(poolId)
Expand All @@ -467,13 +472,31 @@ export function useWithdraw(poolId: string, borrower: CombinedSubstrateAccount,
const ao = access.assetOriginators.find((a) => a.address === borrower.actingAddress)
const withdrawAddresses = ao?.transferAllowlist ?? []

if (!isLocalAsset || !withdrawAddresses.length) {
if (!withdrawAddresses.length)
return {
render: () => null,
isValid: true,
getBatch: () => of([]),
}
const sortedBalances = sortBalances(muxBalances?.currencies || [], pool.currency)
const ignoredCurrencies = Object.entries(selectedAddressIndexByCurrency).flatMap(([key, index]) => {
return index === -1 ? [key] : []
})
const { buckets: withdrawAmounts } = muxBalances?.currencies
? divideBetweenCurrencies(amount, sortedBalances, withdrawAddresses, ignoredCurrencies)
: { buckets: [] }

const totalAvailable = withdrawAmounts.reduce((acc, cur) => acc.add(cur.amount), Dec(0))

React.useEffect(() => {
if (withdrawAddresses.length > 0 && sortedBalances.length > 0) {
const initialSelectedAddresses: Record<string, number> = {}
sortedBalances.forEach((balance) => {
const currencyKey = currencyToString(balance.currency.key)
if (!(currencyKey in initialSelectedAddresses)) {
initialSelectedAddresses[currencyKey] = 0
}
})
setSelectedAddressIndexByCurrency(initialSelectedAddresses)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [withdrawAddresses])

if (!isLocalAsset) {
return {
render: () => <WithdrawSelect withdrawAddresses={withdrawAddresses} />,
isValid: true,
Expand All @@ -498,16 +521,6 @@ export function useWithdraw(poolId: string, borrower: CombinedSubstrateAccount,
}
}

const sortedBalances = sortBalances(muxBalances?.currencies || [], pool.currency)
const ignoredCurrencies = Object.entries(selectedAddressIndexByCurrency).flatMap(([key, index]) => {
return index === -1 ? [key] : []
})
const { buckets: withdrawAmounts } = muxBalances?.currencies
? divideBetweenCurrencies(amount, sortedBalances, withdrawAddresses, ignoredCurrencies)
: { buckets: [] }

const totalAvailable = withdrawAmounts.reduce((acc, cur) => acc.add(cur.amount), Dec(0))

return {
render: () => (
<Mux
Expand All @@ -523,8 +536,13 @@ export function useWithdraw(poolId: string, borrower: CombinedSubstrateAccount,
amount={amount}
/>
),
isValid: amount.lte(totalAvailable),
isValid: (_: { values: Pick<FinanceValues, 'withdraw'> }) => {
const withdrawalAddresses = Object.values(selectedAddressIndexByCurrency).filter((index) => index !== -1)
return source === 'reserve' ? amount.lte(totalAvailable) && !!withdrawalAddresses.length : true
},
getBatch: () => {
const withdrawalAddresses = Object.values(selectedAddressIndexByCurrency).filter((index) => index !== -1)
if (!withdrawalAddresses.length) return of([])
return combineLatest(
withdrawAmounts.flatMap((bucket) => {
const index = selectedAddressIndexByCurrency[bucket.currencyKey] ?? 0
Expand Down
5 changes: 5 additions & 0 deletions centrifuge-app/src/pages/Loan/RepayForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ function InternalRepayForm({ loan, destination }: { loan: ActiveLoan | CreatedLo
validateOnMount: true,
})

React.useEffect(() => {
repayForm.validateForm()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [destination])

const repayFormRef = React.useRef<HTMLFormElement>(null)
useFocusInvalidInput(repayForm, repayFormRef)

Expand Down
26 changes: 2 additions & 24 deletions centrifuge-app/src/pages/NavManagement/NavManagementAssetTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { switchMap } from 'rxjs'
import daiLogo from '../../assets/images/dai-logo.svg'
import usdcLogo from '../../assets/images/usdc-logo.svg'
import { ButtonGroup } from '../../components/ButtonGroup'
import { DataCol, DataRow, DataTable } from '../../components/DataTable'
import { DataTable } from '../../components/DataTable'
import { LayoutSection } from '../../components/LayoutBase/LayoutSection'
import { AssetName } from '../../components/LoanList'
import { RouterTextLink } from '../../components/TextLink'
Expand Down Expand Up @@ -371,29 +371,7 @@ export function NavManagementAssetTable({ poolId }: { poolId: string }) {
)
}
>
<DataTable
data={[...reserveRow, ...cashLoans, ...form.values.feed]}
columns={columns}
footer={
<DataRow>
<DataCol align="left">
<Text color="accentPrimary" variant="body2">
Total
</Text>
</DataCol>
<DataCol />
<DataCol />
<DataCol />
<DataCol />
{isEditing && <DataCol />}
<DataCol>
<Text color="accentPrimary" variant="body2">
{formatBalance(newNav, pool.currency.symbol)}
</Text>
</DataCol>
</DataRow>
}
/>
<DataTable data={[...reserveRow, ...cashLoans, ...form.values.feed]} columns={columns} />
</LayoutSection>
</FormikProvider>
</Stack>
Expand Down
6 changes: 5 additions & 1 deletion centrifuge-app/src/pages/NavManagement/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ export function NavOverviewCard({ poolId }: { poolId: string }) {
current={pool.nav.total.toFloat()}
change={changeInValuation ? new CurrencyBalance(changeInValuation, pool.currency.decimals).toFloat() : 0}
pendingFees={pendingFees.toFloat()}
pendingNav={pool.nav.total.toFloat() - pendingFees.toFloat()}
pendingNav={new CurrencyBalance(changeInValuation, pool.currency.decimals)
.toDecimal()
.add(pool.nav.total.toDecimal())
.sub(pendingFees.toDecimal())
.toNumber()}
/>
)
}
Expand Down

0 comments on commit 699bebd

Please sign in to comment.