Skip to content

Commit

Permalink
chore: factor dust checking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
losman0s committed Nov 18, 2023
1 parent 08bc03d commit 75f9223
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const AssetRow: FC<{
return bank.userInfo.maxRepay;
}
}, [bank, currentAction]);
const isDust = bank.isActive && uiToNative(bank.position.amount, bank.info.state.mintDecimals).isZero();
const isDust = bank.isActive && bank.position.isDust;
const showCloseBalance = currentAction === ActionType.Withdraw && isDust; // Only case we should show close balance is when we are withdrawing a dust balance, since user receives 0 tokens back (vs repaying a dust balance where the input box will show the smallest unit of the token)
const isActionDisabled = maxAmount === 0 && !showCloseBalance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const UserPositionRow: FC<UserPositionRowProps> = ({ activeBankInfo, marginfiAcc
const maxAmount = activeBankInfo.position.isLending
? activeBankInfo.userInfo.maxWithdraw
: activeBankInfo.userInfo.maxRepay;
const isDust = uiToNative(activeBankInfo.position.amount, activeBankInfo.info.state.mintDecimals).isZero();
const isDust = activeBankInfo.position.isDust;
const showCloseBalance = activeBankInfo.position.isLending && isDust;
const isActionDisabled = maxAmount === 0 && !showCloseBalance;

Expand Down
18 changes: 13 additions & 5 deletions packages/marginfi-v2-ui-state/src/lib/mrgnlend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,20 @@ function makeLendingPosition(
const usdValues = balance.computeUsdValue(bank, oraclePrice, MarginRequirementType.Equity);
const weightedUSDValues = balance.getUsdValueWithPriceBias(bank, oraclePrice, MarginRequirementType.Maintenance);
const isLending = usdValues.liabilities.isZero();

const amount = isLending
? nativeToUi(amounts.assets.toNumber(), bankInfo.mintDecimals)
: nativeToUi(amounts.liabilities.toNumber(), bankInfo.mintDecimals);
const isDust = uiToNative(amount, bankInfo.mintDecimals).isZero();
const weightedUSDValue = isLending ? weightedUSDValues.assets.toNumber() : weightedUSDValues.liabilities.toNumber();
const usdValue = isLending ? usdValues.assets.toNumber() : usdValues.liabilities.toNumber();

return {
amount: isLending
? nativeToUi(amounts.assets.toNumber(), bankInfo.mintDecimals)
: nativeToUi(amounts.liabilities.toNumber(), bankInfo.mintDecimals),
usdValue: isLending ? usdValues.assets.toNumber() : usdValues.liabilities.toNumber(),
weightedUSDValue: isLending ? weightedUSDValues.assets.toNumber() : weightedUSDValues.liabilities.toNumber(),
amount,
usdValue,
weightedUSDValue,
isLending,
isDust,
};
}

Expand Down Expand Up @@ -484,6 +491,7 @@ interface LendingPosition {
amount: number;
usdValue: number;
weightedUSDValue: number;
isDust: boolean;
}

interface BankInfo {
Expand Down

0 comments on commit 75f9223

Please sign in to comment.