Skip to content

Commit

Permalink
feat: birdeye improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders committed Nov 29, 2023
1 parent d0d0203 commit e43e0d0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
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
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 @@ -163,18 +163,22 @@ export async function makeExtendedBankEmission(
extendedBankMetadatas: ExtendedBankMetadata[],
tokenMap: TokenPriceMap,
apiKey?: string
): Promise<[ExtendedBankInfo[], ExtendedBankMetadata[]]> {
): Promise<[ExtendedBankInfo[], ExtendedBankMetadata[], TokenPriceMap]> {
const emissionsMints = Object.keys(tokenMap).map((key) => new PublicKey(key));
let birdeyePrices = 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
): Promise<TokenPriceMap> {
const banksWithEmissions = banks.filter((bank) => !bank.emissionsMint.equals(PublicKey.default));
const emissionsMints = banksWithEmissions.map((bank) => bank.emissionsMint);

Expand All @@ -226,7 +234,7 @@ 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]?.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

0 comments on commit e43e0d0

Please sign in to comment.