Skip to content

Commit

Permalink
feat: trigger borrow / lend when LST dialog closed (mobile)
Browse files Browse the repository at this point in the history
  • Loading branch information
chambaz committed Oct 11, 2023
1 parent 727deee commit d17be58
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WSOL_MINT } from "@mrgnlabs/mrgn-common";
import { ExtendedBankInfo, ActionType, getCurrentAction } from "@mrgnlabs/marginfi-v2-ui-state";
import { MarginfiAccountWrapper } from "@mrgnlabs/marginfi-client-v2";
import { useMrgnlendStore } from "~/store";
import { borrowOrLend, closeBalance } from "~/utils";
import { borrowOrLend, closeBalance, BorrowOrLendParams } from "~/utils";
import { useAssetItemData } from "~/hooks/useAssetItemData";
import { LSTDialogVariants } from "~/components/common/AssetList";
import { AssetCardStats } from "./AssetCardStats";
Expand All @@ -18,7 +18,7 @@ export const AssetCard: FC<{
isConnected: boolean;
marginfiAccount: MarginfiAccountWrapper | null;
inputRefs?: React.MutableRefObject<Record<string, HTMLInputElement | null>>;
showLSTDialog?: (variant: LSTDialogVariants) => void;
showLSTDialog?: (variant: LSTDialogVariants, callback?: () => void) => void;
}> = ({ bank, nativeSolBalance, isInLendingMode, marginfiAccount, inputRefs, showLSTDialog }) => {
const { rateAP, assetWeight, isBankFilled, isBankHigh, bankCap } = useAssetItemData({ bank, isInLendingMode });
const [mfiClient, fetchMrgnlendState] = useMrgnlendStore((state) => [state.marginfiClient, state.fetchMrgnlendState]);
Expand Down Expand Up @@ -71,11 +71,25 @@ export const AssetCard: FC<{
showLSTDialog
) {
setHasLSTDialogShown((prev) => [...prev, bank.meta.tokenSymbol as LSTDialogVariants]);
showLSTDialog(bank.meta.tokenSymbol as LSTDialogVariants);
showLSTDialog(bank.meta.tokenSymbol as LSTDialogVariants, async () => {
await actionBorrowOrLend(borrowOrLendAmount, {
mfiClient,
currentAction,
bank,
nativeSolBalance,
marginfiAccount,
});
});
return;
}

await borrowOrLend({ mfiClient, currentAction, bank, borrowOrLendAmount, nativeSolBalance, marginfiAccount });
await actionBorrowOrLend(borrowOrLendAmount, {
mfiClient,
currentAction,
bank,
nativeSolBalance,
marginfiAccount,
});

if (
currentAction === ActionType.Withdraw &&
Expand All @@ -87,15 +101,6 @@ export const AssetCard: FC<{
showLSTDialog(bank.meta.tokenSymbol as LSTDialogVariants);
return;
}

// -------- Refresh state
try {
setIsRefreshingStore(true);
await fetchMrgnlendState();
} catch (error: any) {
console.log("Error while reloading state");
console.log(error);
}
},
[
bank,
Expand All @@ -109,6 +114,31 @@ export const AssetCard: FC<{
]
);

const actionBorrowOrLend = useCallback(
async (
borrowOrLendAmount: number,
{
mfiClient,
currentAction,
bank,
nativeSolBalance,
marginfiAccount,
}: Omit<BorrowOrLendParams, "borrowOrLendAmount">
) => {
await borrowOrLend({ mfiClient, currentAction, bank, borrowOrLendAmount, nativeSolBalance, marginfiAccount });

// -------- Refresh state
try {
setIsRefreshingStore(true);
await fetchMrgnlendState();
} catch (error: any) {
console.log("Error while reloading state");
console.log(error);
}
},
[]
);

return (
<div className="bg-[#1A1F22] rounded-xl px-[12px] py-[16px] flex flex-col gap-[16px] max-w-sm min-w-[300px] flex-1">
<AssetCardHeader bank={bank} isInLendingMode={isInLendingMode} rateAP={rateAP} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const MobileAssetsList: FC = () => {
const [isInLendingMode, setIsInLendingMode] = useState(true);
const [isLSTDialogOpen, setIsLSTDialogOpen] = useState(false);
const [lstDialogVariant, setLSTDialogVariant] = useState<LSTDialogVariants | null>(null);
const [lstDialogCallback, setLSTDialogCallback] = useState<(() => void) | null>(null);

const sortBanks = useCallback(
(banks: ExtendedBankInfo[]) => {
Expand Down Expand Up @@ -159,9 +160,12 @@ export const MobileAssetsList: FC = () => {
isConnected={connected}
marginfiAccount={selectedAccount}
inputRefs={inputRefs}
showLSTDialog={(variant: LSTDialogVariants) => {
showLSTDialog={(variant: LSTDialogVariants, onClose?: () => void) => {
setLSTDialogVariant(variant);
setIsLSTDialogOpen(true);
if (onClose) {
setLSTDialogCallback(() => onClose);
}
}}
/>
))}
Expand Down Expand Up @@ -226,6 +230,10 @@ export const MobileAssetsList: FC = () => {
onClose={() => {
setIsLSTDialogOpen(false);
setLSTDialogVariant(null);
if (lstDialogCallback) {
lstDialogCallback();
setLSTDialogCallback(null);
}
}}
/>
</>
Expand Down

3 comments on commit d17be58

@vercel
Copy link

@vercel vercel bot commented on d17be58 Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

marginfi-landing-page – ./apps/marginfi-landing-page

marginfi-landing-page-mrgn.vercel.app
marginfi-landing-page-git-production-mrgn.vercel.app
marginfi-landing-page.vercel.app
www.marginfi.com
marginfi.com

@vercel
Copy link

@vercel vercel bot commented on d17be58 Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

omni – ./apps/omni

omni-git-production-mrgn.vercel.app
omni.marginfi.com
omni-one.vercel.app
omni-mrgn.vercel.app

@vercel
Copy link

@vercel vercel bot commented on d17be58 Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.