Skip to content

Commit

Permalink
Merge pull request #940 from mrgnlabs/feature/collect-reward-refactor
Browse files Browse the repository at this point in the history
feat(mfi-v2-ui): collect reward refactor & position transfer
  • Loading branch information
k0beLeenders authored Oct 31, 2024
2 parents 5cfb2c7 + 77f9f43 commit 7f50330
Show file tree
Hide file tree
Showing 77 changed files with 1,639 additions and 873 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useRouter } from "next/router";
import { ActionType, ActiveBankInfo, ExtendedBankInfo } from "@mrgnlabs/marginfi-v2-ui-state";
import { numeralFormatter } from "@mrgnlabs/mrgn-common";
import {
ActionMethod,
ActionMessageType,
calculateLoopingParams,
handleSimulationError,
LoopingObject,
Expand Down Expand Up @@ -56,7 +56,7 @@ export const TradingBox = ({ activeGroup, side = "long" }: TradingBoxProps) => {
const [leverage, setLeverage] = React.useState(0);
const [isLoading, setIsLoading] = React.useState<boolean>(false);
const [Stats, setStats] = React.useState<React.JSX.Element>(<></>);
const [additionalChecks, setAdditionalChecks] = React.useState<ActionMethod>();
const [additionalChecks, setAdditionalChecks] = React.useState<ActionMessageType>();

const debouncedLeverage = useDebounce(leverage, 1000);
const debouncedAmount = useDebounce(amount, 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
usdFormatter,
} from "@mrgnlabs/mrgn-common";
import {
ActionMethod,
ActionMessageType,
DYNAMIC_SIMULATION_ERRORS,
loopingBuilder,
LoopingObject,
Expand Down Expand Up @@ -375,8 +375,8 @@ export function checkLoopingActionAvailable({
activeGroup,
loopingObject,
tradeSide,
}: CheckActionAvailableProps): ActionMethod[] {
let checks: ActionMethod[] = [];
}: CheckActionAvailableProps): ActionMessageType[] {
let checks: ActionMessageType[] = [];

const requiredCheck = getRequiredCheck(connected, activeGroup, loopingObject);
if (requiredCheck) return [requiredCheck];
Expand All @@ -402,7 +402,7 @@ function getRequiredCheck(
connected: boolean,
activeGroup: GroupData | null,
loopingObject: LoopingObject | null
): ActionMethod | null {
): ActionMessageType | null {
if (!connected) {
return { isEnabled: false };
}
Expand All @@ -416,8 +416,8 @@ function getRequiredCheck(
return null;
}

function getGeneralChecks(amount: string): ActionMethod[] {
let checks: ActionMethod[] = [];
function getGeneralChecks(amount: string): ActionMessageType[] {
let checks: ActionMessageType[] = [];

try {
if (Number(amount) === 0) {
Expand All @@ -430,8 +430,8 @@ function getGeneralChecks(amount: string): ActionMethod[] {
}
}

function canBeLooped(activeGroup: GroupData, loopingObject: LoopingObject, tradeSide: TradeSide): ActionMethod[] {
let checks: ActionMethod[] = [];
function canBeLooped(activeGroup: GroupData, loopingObject: LoopingObject, tradeSide: TradeSide): ActionMessageType[] {
let checks: ActionMessageType[] = [];
const isUsdcBankPaused =
activeGroup.pool.quoteTokens[0].info.rawBank.config.operationalState === OperationalState.Paused;
const isTokenBankPaused = activeGroup.pool.token.info.rawBank.config.operationalState === OperationalState.Paused;
Expand Down
4 changes: 2 additions & 2 deletions apps/marginfi-v2-trading/src/store/actionBoxStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
LstType,
RepayType,
YbxType,
ActionMethod,
ActionMessageType,
calculateMaxRepayableCollateral,
calculateRepayCollateralParams,
DYNAMIC_SIMULATION_ERRORS,
Expand Down Expand Up @@ -40,7 +40,7 @@ interface ActionBoxState {
feedCrankTxs: VersionedTransaction[];
};

errorMessage: ActionMethod | null;
errorMessage: ActionMessageType | null;
isLoading: boolean;

// Actions
Expand Down
20 changes: 12 additions & 8 deletions apps/marginfi-v2-trading/src/utils/actionBoxUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
ActionMethod,
ActionMethodType,
ActionMessageType,
ActionMessageUIType,
canBeBorrowed,
canBeLent,
canBeRepaid,
Expand All @@ -13,7 +13,7 @@ import { MarginfiAccountWrapper } from "@mrgnlabs/marginfi-client-v2";
import { createJupiterApiClient, QuoteGetRequest, QuoteResponse } from "@jup-ag/api";
import { PublicKey } from "@solana/web3.js";

export function getColorForActionMethodType(type?: ActionMethodType) {
export function getColorForActionMessageUIType(type?: ActionMessageUIType) {
if (type === "INFO") {
return "info";
} else if (type === "WARNING") {
Expand Down Expand Up @@ -53,8 +53,8 @@ export function checkActionAvailable({
blacklistRoutes,
repayMode,
repayCollatQuote,
}: CheckActionAvailableProps): ActionMethod[] {
let checks: ActionMethod[] = [];
}: CheckActionAvailableProps): ActionMessageType[] {
let checks: ActionMessageType[] = [];

const requiredCheck = getRequiredCheck(connected, selectedBank);
if (requiredCheck) return [requiredCheck];
Expand Down Expand Up @@ -97,7 +97,7 @@ export function checkActionAvailable({
return checks;
}

function getRequiredCheck(connected: boolean, selectedBank: ExtendedBankInfo | null): ActionMethod | null {
function getRequiredCheck(connected: boolean, selectedBank: ExtendedBankInfo | null): ActionMessageType | null {
if (!connected) {
return { isEnabled: false };
}
Expand All @@ -108,8 +108,12 @@ function getRequiredCheck(connected: boolean, selectedBank: ExtendedBankInfo | n
return null;
}

function getGeneralChecks(amount: number = 0, repayAmount: number = 0, showCloseBalance?: boolean): ActionMethod[] {
let checks: ActionMethod[] = [];
function getGeneralChecks(
amount: number = 0,
repayAmount: number = 0,
showCloseBalance?: boolean
): ActionMessageType[] {
let checks: ActionMessageType[] = [];
if (showCloseBalance) {
checks.push({ actionMethod: "INFO", description: "Close lending balance.", isEnabled: true });
}
Expand Down
4 changes: 2 additions & 2 deletions apps/marginfi-v2-trading/src/utils/tradingActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { BankConfigOpt, MarginfiAccountWrapper, MarginfiClient } from "@mrgnlabs
import {
calculateLoopingTransaction,
LoopingObject,
ActionMethod,
ActionMessageType,
calculateBorrowLendPositionParams,
getMaybeSquadsOptions,
ToastStep,
Expand Down Expand Up @@ -313,7 +313,7 @@ export async function calculateClosePositions({
feedCrankTxs: VersionedTransaction[];
quote?: QuoteResponse;
}
| ActionMethod
| ActionMessageType
> {
// user is borrowing and depositing
if (borrowBank && depositBanks.length === 1) {
Expand Down
54 changes: 4 additions & 50 deletions apps/marginfi-v2-ui/src/components/common/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
import { FC, useEffect, useMemo, useState } from "react";
import { FC } from "react";

import Link from "next/link";
import Image from "next/image";
import { useRouter } from "next/router";

import { PublicKey } from "@solana/web3.js";
import { IconBell, IconBrandTelegram, IconSettings } from "@tabler/icons-react";

import { collectRewardsBatch, capture, cn } from "@mrgnlabs/mrgn-utils";
import { Settings, Wallet } from "@mrgnlabs/mrgn-ui";

import { useMrgnlendStore, useUiStore, useUserProfileStore } from "~/store";
import { useFirebaseAccount } from "~/hooks/useFirebaseAccount";
import { useWallet } from "~/components/wallet-v2/hooks/use-wallet.hook";

import { useConnection } from "~/hooks/use-connection";
import { useIsMobile } from "~/hooks/use-is-mobile";

import { EMISSION_MINT_INFO_MAP } from "~/components/desktop/AssetList/components";
import { Popover, PopoverContent, PopoverTrigger } from "~/components/ui/popover";
import { Button } from "~/components/ui/button";
import { IconMrgn } from "~/components/ui/icons";
import { cn, capture } from "@mrgnlabs/mrgn-utils";
import { Settings, Wallet } from "~/components";

// @todo implement second pretty navbar row
export const Navbar: FC = () => {
useFirebaseAccount();

const { connection } = useConnection();
const isMobile = useIsMobile();
const { wallet } = useWallet();
const router = useRouter();
const [
initialized,
Expand Down Expand Up @@ -64,26 +57,6 @@ export const Navbar: FC = () => {

const [userPointsData] = useUserProfileStore((state) => [state.userPointsData]);

// const [lipAccount, setLipAccount] = useState<LipAccount | null>(null);

const bankAddressesWithEmissions: PublicKey[] = useMemo(() => {
if (!selectedAccount) return [];
return [...EMISSION_MINT_INFO_MAP.keys()]
.map((bankMintSymbol) => {
const uxdBankInfo = extendedBankInfos?.find((b) => b.isActive && b.meta.tokenSymbol === bankMintSymbol);
return uxdBankInfo?.address;
})
.filter((address) => address !== undefined) as PublicKey[];
}, [selectedAccount, extendedBankInfos]);

// useEffect(() => {
// (async function () {
// if (!mfiClient || !lipClient || !walletAddress) return;
// const lipAccount = await LipAccount.fetch(walletAddress, lipClient, mfiClient);
// setLipAccount(lipAccount);
// })();
// }, [lipClient, mfiClient, walletAddress]);

return (
<header className="h-[64px] mb-4 md:mb-8 lg:mb-14">
<nav className={cn("fixed w-full top-0 h-[64px] z-50 bg-background", isOraclesStale && "top-16 md:top-10")}>
Expand Down Expand Up @@ -156,25 +129,6 @@ export const Navbar: FC = () => {
</div>
{initialized && (
<div className="h-full w-1/2 flex justify-end items-center z-10 gap-4 lg:gap-4 text-[#868E95]">
<div
className={`whitespace-nowrap inline-flex mr-4 md: mr-0 ${
bankAddressesWithEmissions.length > 0 ? "cursor-pointer hover:text-[#AAA]" : "cursor-not-allowed"
}`}
onClick={async () => {
if (!wallet || !selectedAccount || bankAddressesWithEmissions.length === 0) return;
const priorityFee = 0; // code has been removed on new collect rewards so temporary placeholder
await collectRewardsBatch(selectedAccount, bankAddressesWithEmissions, priorityFee);
}}
>
{!isMobile && "collect"} rewards
{bankAddressesWithEmissions.length > 0 && (
<span className="relative flex h-1 w-1">
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-[#DCE85D] opacity-75"></span>
<span className="relative inline-flex rounded-full h-1 w-1 bg-[#DCE85DAA]"></span>
</span>
)}
</div>

<Popover>
<PopoverTrigger asChild>
<Button variant="ghost" size="icon" className="h-10 w-10 shrink-0">
Expand Down
Loading

0 comments on commit 7f50330

Please sign in to comment.