Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAN-2818: Remove delta #1099

Open
wants to merge 17 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"bs58": "^5.0.0",
"chart.js": "^4.4.0",
"classnames": "^2.3.2",
"fbonds-core": "0.6.68",
"fbonds-core": "0.6.65-dev2",
"firebase": "^10.1.0",
"idb-keyval": "^6.2.1",
"immer": "9.0.19",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
.offersAmountInput {
max-width: 146px;
}
.deltaInput {
max-width: 116px;
}

.messageContainer {
height: 14px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import styles from './PlaceOfferContent.module.less'
const PlaceOfferContent: FC<PlaceOfferParams> = ({
loanValue,
loansAmount,
deltaValue,
onLoanValueChange,
onLoanAmountChange,
onDeltaValueChange,
onCreateOffer,
onRemoveOffer,
onUpdateOffer,
Expand Down Expand Up @@ -65,16 +63,6 @@ const PlaceOfferContent: FC<PlaceOfferParams> = ({
className={styles.offersAmountInput}
step={1}
/>
<NumericStepInput
label="Decrease by"
onChange={onDeltaValueChange}
value={deltaValue}
disabled={!connected}
className={styles.deltaInput}
tooltipText="Max Offer will decrease by this amount every time a borrower takes your max offer (AKA “delta”)"
postfix={getTokenUnit(tokenType)}
step={inputStepByTokenType}
/>
</div>
<div className={styles.messageContainer}>
{offerErrorMessage && <InputErrorMessage message={offerErrorMessage} />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ export const convertSimpleOfferToMark = (offer: SimpleOffer) => {
return { value: offer.loanValue }
}

export const convertOfferToMark = (offerValue: number, index: number, delta: number) => {
return { value: index === 0 ? offerValue : offerValue - delta * index }
}

export const calcLeftPercentage = (markers: Mark[] | Mark[][], currentIndex: number) => {
const percentage = (currentIndex / (markers.length - 1)) * 100 || 0
return clamp(percentage, MIN_BOUND_PERCENTAGE, MAX_BOUND_PERCENTAGE)
Expand All @@ -26,7 +22,6 @@ export const calcLeftPercentage = (markers: Mark[] | Mark[][], currentIndex: num
export const groupMarks = (markers: Mark[]): Mark[] | Mark[][] => {
const uniqueMarks = uniqBy(markers, (mark) => mark.value)

// //? If all marks have the same value, group them by value. Used for offers without delta
if (uniqueMarks.length === 1) {
return chain(markers)
.groupBy((mark) => mark.value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { core } from '@banx/api/nft'
import { WEEKS_IN_YEAR } from '@banx/constants'
import { calcSyntheticLoanValue } from '@banx/store/nft'
import { calculateMaxLoanValueFromOffer } from '@banx/utils'

interface CalcOfferSizeProps {
initialOffer: core.Offer | undefined
Expand Down Expand Up @@ -46,15 +46,15 @@ export const calcMaxLtv = ({
hasFormChanges: boolean
}) => {
//? Calculate initial LTV based on the best offer in the pool
const bestLoanValue = initialOffer ? calcSyntheticLoanValue(initialOffer) : 0
const bestLoanValue = initialOffer ? calculateMaxLoanValueFromOffer(initialOffer) : 0
const initialCurrentLtv = calcLtv(bestLoanValue, collectionFloor)

//? Calculate initial maximum LTV when the best offer in the pool was created
const initialMaxLoanValue = initialOffer?.validation.loanToValueFilter || 0
const initialMaxLtv = calcLtv(initialMaxLoanValue, collectionFloor)

//? Calculate updated LTV based on the best offer in the pool when form has changes
const updatedBestLoanValue = updatedOffer ? calcSyntheticLoanValue(updatedOffer) : 0
const updatedBestLoanValue = updatedOffer ? calculateMaxLoanValueFromOffer(updatedOffer) : 0
const updatedCurrentLtv = calcLtv(updatedBestLoanValue, collectionFloor) || 0

const currentLtv = initialOffer && !hasFormChanges ? initialCurrentLtv : updatedCurrentLtv
Expand Down
14 changes: 1 addition & 13 deletions src/components/PlaceOfferSection/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ import { ZERO_BN } from '@banx/utils'

type GetUpdatedBondOffer = (props: {
loanValue: number //? lamports
deltaValue: number //? lamports
loansAmount: number //? integer number
syntheticOffer: SyntheticOffer
tokenType: LendingTokenType
}) => core.Offer

export const getUpdatedBondOffer: GetUpdatedBondOffer = ({
loanValue,
deltaValue,
loansAmount,
syntheticOffer,
tokenType,
Expand All @@ -39,7 +37,6 @@ export const getUpdatedBondOffer: GetUpdatedBondOffer = ({
const updatedBondOffer = optimisticUpdateBondOfferBonding({
bondOffer: initializedOffer,
newLoanValue: new BN(loanValue),
newDelta: new BN(deltaValue),
newQuantityOfLoans: new BN(loansAmount),
collateralsPerToken: ZERO_BN,
tokenLendingApr: ZERO_BN,
Expand All @@ -53,9 +50,7 @@ type GetErrorMessage = (props: {
walletBalance: number
escrowBalance: number
offerSize: number
loanValue: number
loansAmount: number
deltaValue: number
hasFormChanges: boolean
tokenType: LendingTokenType
}) => string
Expand All @@ -74,29 +69,24 @@ export const getErrorMessage: GetErrorMessage = ({
walletBalance,
escrowBalance,
offerSize,
loanValue,
loansAmount,
deltaValue,
syntheticOffer,
hasFormChanges,
tokenType,
}) => {
const initialOfferSize = calcOfferSize({
syntheticOffer,
deltaValue: syntheticOffer.deltaValue,
loanValue: syntheticOffer.loanValue,
loansAmount: syntheticOffer.loansAmount,
tokenType,
})

const totalFundsAvailable = initialOfferSize + walletBalance + escrowBalance

const isOfferInvalid = deltaValue && hasFormChanges ? deltaValue * loansAmount > loanValue : false
const isBalanceInsufficient = !!walletBalance && offerSize > totalFundsAvailable
const isEmptyLoansAmount = hasFormChanges && !loansAmount

const errorConditions: Array<[boolean, string]> = [
[isOfferInvalid, ERROR_MESSAGES.INVALID_OFFER],
[isBalanceInsufficient, ERROR_MESSAGES.INSUFFICIENT_BALANCE[tokenType]],
[isEmptyLoansAmount, ERROR_MESSAGES.EMPTY_LOANS_AMOUNT],
]
Expand All @@ -115,7 +105,6 @@ export const checkIsEditMode = (offerPubkey: string) =>
type CalcOfferSize = (props: {
syntheticOffer: SyntheticOffer
loanValue: number //? lamports
deltaValue: number //? lamports
loansAmount: number
tokenType: LendingTokenType
}) => number
Expand All @@ -124,10 +113,9 @@ export const calcOfferSize: CalcOfferSize = ({
syntheticOffer,
loanValue,
loansAmount,
deltaValue,
tokenType,
}) => {
const offerToUpdate = { loanValue, deltaValue, loansAmount, syntheticOffer, tokenType }
const offerToUpdate = { loanValue, loansAmount, syntheticOffer, tokenType }
const updatedBondOffer = getUpdatedBondOffer(offerToUpdate)

const offerSize = updatedBondOffer.fundsSolOrTokenBalance + updatedBondOffer.bidSettlement
Expand Down
34 changes: 5 additions & 29 deletions src/components/PlaceOfferSection/hooks/useOfferFormController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,31 @@ import { getTokenDecimals } from '@banx/utils'
export const useOfferFormController = (syntheticOffer: SyntheticOffer) => {
const { tokenType } = useTokenType()

const {
deltaValue: syntheticDeltaValue,
loanValue: syntheticLoanValue,
loansAmount: syntheticLoansAmount,
} = syntheticOffer
const { loanValue: syntheticLoanValue, loansAmount: syntheticLoansAmount } = syntheticOffer

const decimals = getTokenDecimals(tokenType)

const initialValues = useMemo(() => {
return {
deltaValue: formatNumber(syntheticDeltaValue / decimals),
loanValue: formatNumber(syntheticLoanValue / decimals),
loansAmount: syntheticOffer.isEdit ? String(syntheticLoansAmount) : '1',
}
}, [
decimals,
syntheticDeltaValue,
syntheticLoanValue,
syntheticLoansAmount,
syntheticOffer.isEdit,
])
}, [decimals, syntheticLoanValue, syntheticLoansAmount, syntheticOffer.isEdit])

const [loanValue, setLoanValue] = useState(initialValues.loanValue)
const [loansAmount, setLoansAmount] = useState(initialValues.loansAmount)
const [deltaValue, setDeltaValue] = useState(initialValues.deltaValue)

useEffect(() => {
const { loanValue, loansAmount, deltaValue } = initialValues
const { loanValue, loansAmount } = initialValues

setLoanValue(loanValue)
setLoansAmount(loansAmount)
setDeltaValue(deltaValue)
}, [initialValues])

const onLoanValueChange = useCallback((nextValue: string) => {
setLoanValue(nextValue)
}, [])

const onDeltaValueChange = useCallback((nextValue: string) => {
setDeltaValue(nextValue)
}, [])

const onLoanAmountChange = useCallback((nextValue: string) => {
const sanitizedValue = trimStart(nextValue, '0')
const numericValue = parseFloat(sanitizedValue) || 0
Expand All @@ -62,25 +45,18 @@ export const useOfferFormController = (syntheticOffer: SyntheticOffer) => {
const resetFormValues = () => {
setLoanValue(initialValues.loanValue)
setLoansAmount(initialValues.loansAmount)
setDeltaValue(initialValues.deltaValue)
}

const hasFormChanges = useMemo(() => {
return (
loansAmount !== initialValues.loansAmount ||
loanValue !== initialValues.loanValue ||
deltaValue !== initialValues.deltaValue
)
}, [initialValues, loansAmount, loanValue, deltaValue])
return loansAmount !== initialValues.loansAmount || loanValue !== initialValues.loanValue
}, [initialValues, loansAmount, loanValue])

return {
loanValue,
loansAmount,
deltaValue,

onLoanValueChange,
onLoanAmountChange,
onDeltaValueChange,

hasFormChanges: Boolean(hasFormChanges),
resetFormValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const useOfferTransactions = ({
marketPubkey,
loansAmount,
loanValue,
deltaValue,
optimisticOffer,
updateOrAddOffer,
resetFormValues,
Expand All @@ -46,7 +45,6 @@ export const useOfferTransactions = ({
marketPubkey: string
loansAmount: number
loanValue: number
deltaValue: number
optimisticOffer?: core.Offer
updateOrAddOffer: (offer: core.Offer) => void
resetFormValues: () => void
Expand All @@ -69,7 +67,6 @@ export const useOfferTransactions = ({
marketPubkey,
loansAmount,
loanValue,
deltaValue,
tokenType,
bondFeature: BondFeatures.AutoReceiveAndReceiveNft,
escrowBalance: userVault?.offerLiquidityAmount,
Expand Down Expand Up @@ -126,7 +123,6 @@ export const useOfferTransactions = ({
marketPubkey,
loansAmount,
loanValue,
deltaValue,
tokenType,
},
walletPubkey: wallet?.publicKey?.toBase58(),
Expand All @@ -148,7 +144,6 @@ export const useOfferTransactions = ({
loanValue,
offer: optimisticOffer,
loansAmount,
deltaValue,
tokenType,
escrowBalance: userVault?.offerLiquidityAmount,
depositAmountToVault,
Expand Down Expand Up @@ -201,7 +196,6 @@ export const useOfferTransactions = ({
loanValue,
offer: optimisticOffer,
loansAmount,
deltaValue,
tokenType,
},
walletPubkey: wallet?.publicKey?.toBase58(),
Expand Down
Loading