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

feat(mfi-v2-ui): birdeye improvement #386

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions apps/marginfi-v2-ui/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { PageHeader } from "~/components/common/PageHeader";

import { IconAlertTriangle } from "~/components/ui/icons";
import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger } from "~/components/ui/select";
import { showErrorToast } from "~/utils/toastUtils";

const DesktopAccountSummary = dynamic(
async () => (await import("~/components/desktop/DesktopAccountSummary")).DesktopAccountSummary,
Expand All @@ -41,15 +42,23 @@ const Home = () => {
setIsRefreshingStore,
marginfiAccounts,
selectedAccount,
emissionTokenMap,
] = useMrgnlendStore((state) => [
state.fetchMrgnlendState,
state.initialized,
state.isRefreshingStore,
state.setIsRefreshingStore,
state.marginfiAccounts,
state.selectedAccount,
state.emissionTokenMap,
]);

React.useEffect(() => {
if (emissionTokenMap === null) {
showErrorToast("Failed to fetch prices, emission APY may be incorrect.");
}
}, [emissionTokenMap]);

React.useEffect(() => {
const fetchData = () => {
setIsRefreshingStore(true);
Expand Down
22 changes: 16 additions & 6 deletions packages/marginfi-v2-ui-state/src/lib/mrgnlend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,22 @@ export async function makeExtendedBankEmission(
extendedBankMetadatas: ExtendedBankMetadata[],
tokenMap: TokenPriceMap,
apiKey?: string
): Promise<[ExtendedBankInfo[], ExtendedBankMetadata[]]> {
): Promise<[ExtendedBankInfo[], ExtendedBankMetadata[], TokenPriceMap | null]> {
const emissionsMints = Object.keys(tokenMap).map((key) => new PublicKey(key));
let birdeyePrices = emissionsMints.map(() => new BigNumber(0));
let birdeyePrices: null | BigNumber[] = emissionsMints.map(() => new BigNumber(0));

try {
birdeyePrices = await fetchBirdeyePrices(emissionsMints, apiKey);
} catch (err) {
console.log("Failed to fetch emissions prices from Birdeye", err);
birdeyePrices = null;
}

emissionsMints.map((mint, idx) => {
tokenMap[mint.toBase58()] = { ...tokenMap[mint.toBase58()], price: birdeyePrices[idx] };
tokenMap[mint.toBase58()] = {
...tokenMap[mint.toBase58()],
price: birdeyePrices ? birdeyePrices[idx] : new BigNumber(0),
};
});

const updatedBanks = banks.map((bank) => {
Expand Down Expand Up @@ -214,10 +218,14 @@ export async function makeExtendedBankEmission(
return b.info.state.totalDeposits * b.info.state.price - a.info.state.totalDeposits * a.info.state.price;
});

return [sortedExtendedBankInfos, sortedExtendedBankMetadatas];
return [sortedExtendedBankInfos, sortedExtendedBankMetadatas, birdeyePrices ? tokenMap : null];
}

export async function makeEmissionsPriceMap(banks: Bank[], connection: Connection): Promise<TokenPriceMap> {
export async function makeEmissionsPriceMap(
banks: Bank[],
connection: Connection,
emissionTokenMap: TokenPriceMap | null
): Promise<TokenPriceMap> {
const banksWithEmissions = banks.filter((bank) => !bank.emissionsMint.equals(PublicKey.default));
const emissionsMints = banksWithEmissions.map((bank) => bank.emissionsMint);

Expand All @@ -226,7 +234,9 @@ export async function makeEmissionsPriceMap(banks: Bank[], connection: Connectio
const mint = mintAis.map((ai) => MintLayout.decode(ai!.data));
const emissionsPrices = banksWithEmissions.map((bank, i) => ({
mint: bank.emissionsMint,
price: new BigNumber(0),
price: emissionTokenMap
? emissionTokenMap[bank.emissionsMint.toBase58()]?.price ?? new BigNumber(0)
: new BigNumber(0),
decimals: mint[0].decimals,
}));

Expand Down
14 changes: 7 additions & 7 deletions packages/marginfi-v2-ui-state/src/store/mrgnlendStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ExtendedBankMetadata,
makeExtendedBankMetadata,
makeExtendedBankEmission,
TokenPriceMap,
} from "../lib";
import { getPointsSummary } from "../lib/points";
import { create, StateCreator } from "zustand";
Expand Down Expand Up @@ -49,6 +50,7 @@ interface MrgnlendState {
selectedAccount: MarginfiAccountWrapper | null;
nativeSolBalance: number;
accountSummary: AccountSummary;
emissionTokenMap: TokenPriceMap | null;
birdEyeApiKey: string;

// Actions
Expand Down Expand Up @@ -103,6 +105,7 @@ const stateCreator: StateCreator<MrgnlendState, [], []> = (set, get) => ({
nativeSolBalance: 0,
accountSummary: DEFAULT_ACCOUNT_SUMMARY,
birdEyeApiKey: "",
emissionTokenMap: {},

// Actions
fetchMrgnlendState: async (args?: {
Expand Down Expand Up @@ -132,7 +135,7 @@ const stateCreator: StateCreator<MrgnlendState, [], []> = (set, get) => ({
const banks = [...marginfiClient.banks.values()];

const birdEyeApiKey = args?.birdEyeApiKey ?? get().birdEyeApiKey;
const priceMap = await makeEmissionsPriceMap(banks, connection);
const priceMap = await makeEmissionsPriceMap(banks, connection, get().emissionTokenMap);

let nativeSolBalance: number = 0;
let tokenAccountMap: TokenAccountMap;
Expand Down Expand Up @@ -283,16 +286,13 @@ const stateCreator: StateCreator<MrgnlendState, [], []> = (set, get) => ({
protocolStats: { deposits, borrows, tvl: deposits - borrows, pointsTotal: pointSummary.points_total },
});

const [sortedExtendedBankEmission, sortedExtendedBankMetadatasEmission] = await makeExtendedBankEmission(
sortedExtendedBankInfos,
sortedExtendedBankMetadatas,
priceMap,
birdEyeApiKey
);
const [sortedExtendedBankEmission, sortedExtendedBankMetadatasEmission, emissionTokenMap] =
await makeExtendedBankEmission(sortedExtendedBankInfos, sortedExtendedBankMetadatas, priceMap, birdEyeApiKey);

set({
extendedBankInfos: sortedExtendedBankEmission,
extendedBankMetadatas: sortedExtendedBankMetadatasEmission,
emissionTokenMap: emissionTokenMap,
});
} catch (err) {
console.error("error refreshing state: ", err);
Expand Down