Skip to content

Commit

Permalink
Fix calculateTokensPerCollateral function
Browse files Browse the repository at this point in the history
  • Loading branch information
sablevsky committed Dec 9, 2024
1 parent a05b88b commit cd800a2
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const Offer: FC<OfferProps> = ({ offer, collateral, collateralPrice = 0 }) => {
const tokensPerCollateralBN = calculateTokensPerCollateral(
collateralsPerToken,
collateralDecimals,
marketTokenDecimals,
)

const offerValue = formatTokensPerCollateralToStr(tokensPerCollateralBN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const useOfferFormController = (
const collateralsPerToken = calculateTokensPerCollateral(
syntheticCollateralsPerToken,
collateralsDecimals,
Math.log10(getTokenDecimals(tokenType)),
)

const offerSize = calculateOfferSize(syntheticOfferSize, decimals)
Expand All @@ -43,7 +44,7 @@ export const useOfferFormController = (
offerSize: offerSize ? String(offerSize) : '0',
apr: syntheticApr ? String(syntheticApr) : DEFAULT_APR_PERCENT.toString(),
}
}, [decimals, market, syntheticApr, syntheticCollateralsPerToken, syntheticOfferSize])
}, [decimals, market, syntheticApr, syntheticCollateralsPerToken, syntheticOfferSize, tokenType])

const [collateralsPerToken, setLoanValue] = useState(initialValues.collateralsPerToken)
const [offerSize, setOfferSize] = useState(initialValues.offerSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const PositionAdditionalInfo: FC<{ loan: TokenLoan }> = ({ loan }) => {
const { connection } = useConnection()
const { rate: conversionRate } = useCollateralConversionRate(
new web3.PublicKey(loan.collateral.mint),
loan.bondTradeTransaction.lendingToken,
connection,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export const BorrowCell: FC<BorrowCellProps> = ({ loan, offer, tokenType }) => {
const borrowValue = adjustTokenAmountWithUpfrontFee(loanDebt, marketUpfrontFee)

const tokensPerCollateral = formatTokensPerCollateralToStr(
calculateTokensPerCollateral(offer.validation.collateralsPerToken, loan.collateral.decimals),
calculateTokensPerCollateral(
offer.validation.collateralsPerToken,
loan.collateral.decimals,
Math.log10(getTokenDecimals(tokenType)),
),
)

const ltvPercent = calculateLtvPercent({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const MarketAdditionalInfo: FC<MarketAdditionalInfoProps> = ({ offerPreview, isO
calculateTokensPerCollateral(
offerPreview.bondOffer.validation.collateralsPerToken,
collateral.decimals,
marketTokenDecimals,
),
)

Expand Down
13 changes: 8 additions & 5 deletions src/utils/core/tokenOffers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,25 @@ export const isBondOfferV3Closed = (offer: BondOfferV3) => {
* Calculates the number of tokens per collateral unit.
* @param {BN} collateralsPerToken - The amount of collateral per token.
* @param {number} collateralDecimals - The number of decimal places used by the collateral token.
* @param {number} lendingTokenDecimals - The number of decimal places used by the lending token.
* @returns {BN} -The result is scaled by 1e9 to maintain precision in calculations involving small fractional values
*/

export const calculateTokensPerCollateral = (
collateralsPerToken: BN,
collateralDecimals: number,
lendingTokenDecimals: number,
): BN => {
const PRECISION_ADJUSTMENT = 9
const PRECISION_ADJUSTMENT = 3

if (!collateralsPerToken || collateralsPerToken.eq(ZERO_BN)) {
return ZERO_BN
}

const adjustedScale = collateralDecimals + PRECISION_ADJUSTMENT
const scaledValue = new BN(10).pow(new BN(adjustedScale))
const tokensPerCollateral = scaledValue.div(collateralsPerToken)
const adjustedScale = collateralDecimals + lendingTokenDecimals + PRECISION_ADJUSTMENT
const scaledValue = new BN(10).pow(new BN(adjustedScale)) // 1e15
const tokensPerCollateral = scaledValue
.div(collateralsPerToken)
.div(new BN(10 ** PRECISION_ADJUSTMENT))

return tokensPerCollateral
}
Expand Down

0 comments on commit cd800a2

Please sign in to comment.