Skip to content

Commit

Permalink
Merge pull request #824 from mrgnlabs/j/pyth-push-oracle-update
Browse files Browse the repository at this point in the history
Pyth push oracles
  • Loading branch information
jkbpvsc authored Jul 19, 2024
2 parents 8f81e00 + c151b43 commit 4b76cfb
Show file tree
Hide file tree
Showing 28 changed files with 4,207 additions and 3,556 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ export function calculatePreview({
const poolSize = isLending
? bank.info.state.totalDeposits
: Math.max(
0,
Math.min(bank.info.state.totalDeposits, bank.info.rawBank.config.borrowLimit.toNumber()) -
bank.info.state.totalBorrows
);
0,
Math.min(bank.info.state.totalDeposits, bank.info.rawBank.config.borrowLimit.toNumber()) -
bank.info.state.totalBorrows
);
const bankCap = nativeToUi(
isLending ? bank.info.rawBank.config.depositLimit : bank.info.rawBank.config.borrowLimit,
bank.info.state.mintDecimals
Expand Down Expand Up @@ -181,7 +181,7 @@ export async function simulateAction({
const previewBanks = marginfiClient.banks;
previewBanks.set(
bank.address.toBase58(),
Bank.fromBuffer(bank.address, bankData, marginfiClient.program.idl)
Bank.fromBuffer(bank.address, bankData, marginfiClient.program.idl, marginfiClient.feedIdMap,)
);
const previewClient = new MarginfiClient(
marginfiClient.config,
Expand All @@ -191,7 +191,8 @@ export async function simulateAction({
marginfiClient.group,
marginfiClient.banks,
marginfiClient.oraclePrices,
marginfiClient.mintDatas
marginfiClient.mintDatas,
marginfiClient.feedIdMap,
);
const previewMarginfiAccount = MarginfiAccountWrapper.fromAccountDataRaw(
account.address,
Expand Down Expand Up @@ -232,7 +233,7 @@ export async function simulateAction({
repayWithCollatOptions.repayBank.address,
bank.isActive && isWholePosition(bank, amount),
repayWithCollatOptions.repayBank.isActive &&
isWholePosition(repayWithCollatOptions.repayBank, repayWithCollatOptions.repayAmount),
isWholePosition(repayWithCollatOptions.repayBank, repayWithCollatOptions.repayAmount),
[...setupIxs, swapIx, ...swapcleanupIx],
addressLookupTableAccounts
);
Expand Down Expand Up @@ -360,8 +361,8 @@ function getLiquidationStat(bank: ExtendedBankInfo, isLoading: boolean, simulati
? computeLiquidation / price >= 0.5
? "SUCCESS"
: computeLiquidation / price >= 0.25
? "ALERT"
: "DESTRUCTIVE"
? "ALERT"
: "DESTRUCTIVE"
: undefined;

return {
Expand Down Expand Up @@ -409,10 +410,10 @@ function getPoolSizeStat(bankCap: number, bank: ExtendedBankInfo, isLending: boo
isLending
? bank.info.state.totalDeposits
: Math.max(
0,
Math.min(bank.info.state.totalDeposits, bank.info.rawBank.config.borrowLimit.toNumber()) -
bank.info.state.totalBorrows
)
0,
Math.min(bank.info.state.totalDeposits, bank.info.rawBank.config.borrowLimit.toNumber()) -
bank.info.state.totalBorrows
)
)}

{(isReduceOnly || isBankHigh || isBankFilled) && <IconAlertTriangle size={14} />}
Expand Down Expand Up @@ -443,8 +444,8 @@ function getPoolSizeStat(bankCap: number, bank: ExtendedBankInfo, isLending: boo
{isReduceOnly
? "stSOL is being discontinued."
: `${bank.meta.tokenSymbol} ${isLending ? "deposits" : "borrows"} are at ${percentFormatter.format(
(isLending ? bank.info.state.totalDeposits : bank.info.state.totalBorrows) / bankCap
)} capacity.`}
(isLending ? bank.info.state.totalDeposits : bank.info.state.totalBorrows) / bankCap
)} capacity.`}
</p>
<a href="https://docs.marginfi.com">
<u>Learn more.</u>
Expand Down Expand Up @@ -490,7 +491,10 @@ function getBankTypeStat(bank: ExtendedBankInfo): PreviewStat {
function getOracleStat(bank: ExtendedBankInfo): PreviewStat {
let oracle = "";
switch (bank?.info.rawBank.config.oracleSetup) {
case "PythEma":
case "PythLegacy":
oracle = "Pyth";
break;
case "PythPushOracle":
oracle = "Pyth";
break;
case "SwitchboardV2":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export async function simulateLooping({
]);
if (!mfiAccountData || !bankData) throw new Error("Failed to simulate looping");
const previewBanks = marginfiClient.banks;
previewBanks.set(bank.address.toBase58(), Bank.fromBuffer(bank.address, bankData, marginfiClient.program.idl));
previewBanks.set(bank.address.toBase58(), Bank.fromBuffer(bank.address, bankData, marginfiClient.program.idl, marginfiClient.feedIdMap,));
const previewClient = new MarginfiClient(
marginfiClient.config,
marginfiClient.program,
Expand All @@ -340,7 +340,8 @@ export async function simulateLooping({
marginfiClient.group,
marginfiClient.banks,
marginfiClient.oraclePrices,
marginfiClient.mintDatas
marginfiClient.mintDatas,
marginfiClient.feedIdMap,
);
const previewMarginfiAccount = MarginfiAccountWrapper.fromAccountDataRaw(
account.address,
Expand Down Expand Up @@ -377,7 +378,10 @@ export function generateStats(

let oracle = "";
switch (tokenBank?.info.rawBank.config.oracleSetup) {
case "PythEma":
case "PythLegacy":
oracle = "Pyth";
break;
case "PythPushOracle":
oracle = "Pyth";
break;
case "SwitchboardV2":
Expand Down Expand Up @@ -652,9 +656,8 @@ function canBeLooped(activeGroup: ActiveGroup, loopingObject: LoopingObject, tra

if (wrongSupplied && wrongBorrowed) {
checks.push({
description: `You are already ${
tradeSide === "long" ? "shorting" : "longing"
} this asset, you need to close that position first to start ${tradeSide === "long" ? "longing" : "shorting"}.`,
description: `You are already ${tradeSide === "long" ? "shorting" : "longing"
} this asset, you need to close that position first to start ${tradeSide === "long" ? "longing" : "shorting"}.`,
isEnabled: false,
action: {
type: ActionType.Repay,
Expand All @@ -663,9 +666,8 @@ function canBeLooped(activeGroup: ActiveGroup, loopingObject: LoopingObject, tra
});
} else if (wrongSupplied) {
checks.push({
description: `Before you can ${tradeSide} this asset, you'll need to withdraw your supplied ${
tradeSide === "long" ? activeGroup.usdc.meta.tokenSymbol : activeGroup.token.meta.tokenSymbol
}.`,
description: `Before you can ${tradeSide} this asset, you'll need to withdraw your supplied ${tradeSide === "long" ? activeGroup.usdc.meta.tokenSymbol : activeGroup.token.meta.tokenSymbol
}.`,
isEnabled: true,
action: {
type: ActionType.Withdraw,
Expand All @@ -674,9 +676,8 @@ function canBeLooped(activeGroup: ActiveGroup, loopingObject: LoopingObject, tra
});
} else if (wrongBorrowed) {
checks.push({
description: `Before you can ${tradeSide} this asset, you'll need to repay your borrowed ${
tradeSide === "long" ? activeGroup.token.meta.tokenSymbol : activeGroup.usdc.meta.tokenSymbol
}.`,
description: `Before you can ${tradeSide} this asset, you'll need to repay your borrowed ${tradeSide === "long" ? activeGroup.token.meta.tokenSymbol : activeGroup.usdc.meta.tokenSymbol
}.`,
isEnabled: false,
action: {
type: ActionType.Repay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export const getRateData = (bank: ExtendedBankInfo, isInLendingMode: boolean): R
? emissionsRate
: 0
: emissions == Emissions.Borrowing
? emissionsRate
: 0;
? emissionsRate
: 0;

const rateAPR = interestRate + emissionRate;
const rateAPY = aprToApy(rateAPR);
Expand All @@ -118,7 +118,10 @@ export const getAssetPriceData = (bank: ExtendedBankInfo): AssetPriceData => {

let oracle = "";
switch (bank.info.rawBank.config.oracleSetup) {
case "PythEma":
case "PythLegacy":
oracle = "Pyth";
break;
case "PythPushOracle":
oracle = "Pyth";
break;
case "SwitchboardV2":
Expand Down
6 changes: 4 additions & 2 deletions apps/marginfi-v2-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"lint-fix": "yarn lint --fix"
},
"dependencies": {
"@bonfida/spl-name-service": "^1.1.1",
"@bonfida/spl-name-service": "2.5.4",
"@coral-xyz/borsh": "^0.28.0",
"@dialectlabs/react-sdk-blockchain-solana": "^2.0.3",
"@dialectlabs/react-ui": "^2.0.6",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@jup-ag/api": "^6.0.23",
"@jup-ag/react-hook": "^6.0.0-beta.8",
"@jup-ag/react-hook": "^6.0.0",
"@meso-network/meso-js": "^0.1.2",
"@moongate/moongate-adapter": "^0.4.2",
"@moonpay/moonpay-js": "^0.1.0",
Expand Down Expand Up @@ -48,6 +48,7 @@
"@radix-ui/react-tooltip": "1.0.7",
"@sentry/nextjs": "^7.77.0",
"@socialgouv/matomo-next": "^1.4.0",
"@solana/buffer-layout": "^4.0.1",
"@solana/spl-stake-pool": "^0.6.5",
"@solana/spl-token": "^0.4.6",
"@solana/spl-token-registry": "^0.2.4574",
Expand All @@ -70,6 +71,7 @@
"@web3auth/openlogin-adapter": "^7.0.4",
"@web3auth/solana-provider": "^7.0.4",
"@web3auth/solana-wallet-connector-plugin": "^7.0.4",
"b58": "^4.0.3",
"bignumber.js": "^9.1.1",
"bn.js": "^5.2.1",
"bs58": "^5.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ export function calculatePreview({
const poolSize = isLending
? bank.info.state.totalDeposits
: Math.max(
0,
Math.min(bank.info.state.totalDeposits, bank.info.rawBank.config.borrowLimit.toNumber()) -
bank.info.state.totalBorrows
);
0,
Math.min(bank.info.state.totalDeposits, bank.info.rawBank.config.borrowLimit.toNumber()) -
bank.info.state.totalBorrows
);
const bankCap = nativeToUi(
isLending ? bank.info.rawBank.config.depositLimit : bank.info.rawBank.config.borrowLimit,
bank.info.state.mintDecimals
Expand Down Expand Up @@ -184,7 +184,7 @@ export async function simulateAction({
const previewBanks = marginfiClient.banks;
previewBanks.set(
bank.address.toBase58(),
Bank.fromBuffer(bank.address, bankData, marginfiClient.program.idl)
Bank.fromBuffer(bank.address, bankData, marginfiClient.program.idl, marginfiClient.feedIdMap)
);
const previewClient = new MarginfiClient(
marginfiClient.config,
Expand All @@ -194,7 +194,8 @@ export async function simulateAction({
marginfiClient.group,
marginfiClient.banks,
marginfiClient.oraclePrices,
marginfiClient.mintDatas
marginfiClient.mintDatas,
marginfiClient.feedIdMap,
);
const previewMarginfiAccount = MarginfiAccountWrapper.fromAccountDataRaw(
account.address,
Expand Down Expand Up @@ -231,7 +232,7 @@ export async function simulateAction({
bank.address,
repayWithCollatOptions.depositBank.address,
repayWithCollatOptions.depositBank.isActive &&
isWholePosition(repayWithCollatOptions.depositBank, repayWithCollatOptions.withdrawAmount),
isWholePosition(repayWithCollatOptions.depositBank, repayWithCollatOptions.withdrawAmount),
bank.isActive && isWholePosition(bank, amount),
[swapIx],
swapLUTs
Expand Down Expand Up @@ -361,8 +362,8 @@ function getLiquidationStat(bank: ExtendedBankInfo, isLoading: boolean, simulati
? computeLiquidation / price >= 0.5
? "SUCCESS"
: computeLiquidation / price >= 0.25
? "ALERT"
: "DESTRUCTIVE"
? "ALERT"
: "DESTRUCTIVE"
: undefined;

return {
Expand Down Expand Up @@ -410,10 +411,10 @@ function getPoolSizeStat(bankCap: number, bank: ExtendedBankInfo, isLending: boo
isLending
? bank.info.state.totalDeposits
: Math.max(
0,
Math.min(bank.info.state.totalDeposits, bank.info.rawBank.config.borrowLimit.toNumber()) -
bank.info.state.totalBorrows
)
0,
Math.min(bank.info.state.totalDeposits, bank.info.rawBank.config.borrowLimit.toNumber()) -
bank.info.state.totalBorrows
)
)}

{(isReduceOnly || isBankHigh || isBankFilled) && <IconAlertTriangle size={14} />}
Expand Down Expand Up @@ -444,8 +445,8 @@ function getPoolSizeStat(bankCap: number, bank: ExtendedBankInfo, isLending: boo
{isReduceOnly
? "stSOL is being discontinued."
: `${bank.meta.tokenSymbol} ${isLending ? "deposits" : "borrows"} are at ${percentFormatter.format(
(isLending ? bank.info.state.totalDeposits : bank.info.state.totalBorrows) / bankCap
)} capacity.`}
(isLending ? bank.info.state.totalDeposits : bank.info.state.totalBorrows) / bankCap
)} capacity.`}
</p>
<a href="https://docs.marginfi.com">
<u>Learn more.</u>
Expand Down Expand Up @@ -491,7 +492,10 @@ function getBankTypeStat(bank: ExtendedBankInfo): PreviewStat {
function getOracleStat(bank: ExtendedBankInfo): PreviewStat {
let oracle = "";
switch (bank?.info.rawBank.config.oracleSetup) {
case "PythEma":
case "PythLegacy":
oracle = "Pyth";
break;
case "PythPushOracle":
oracle = "Pyth";
break;
case "SwitchboardV2":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export async function simulateLooping({ marginfiClient, account, bank, loopingTx
]);
if (!mfiAccountData || !bankData) throw new Error("Failed to simulate looping");
const previewBanks = marginfiClient.banks;
previewBanks.set(bank.address.toBase58(), Bank.fromBuffer(bank.address, bankData, marginfiClient.program.idl));
previewBanks.set(bank.address.toBase58(), Bank.fromBuffer(bank.address, bankData, marginfiClient.program.idl, marginfiClient.feedIdMap,));
const previewClient = new MarginfiClient(
marginfiClient.config,
marginfiClient.program,
Expand All @@ -137,7 +137,8 @@ export async function simulateLooping({ marginfiClient, account, bank, loopingTx
marginfiClient.group,
marginfiClient.banks,
marginfiClient.oraclePrices,
marginfiClient.mintDatas
marginfiClient.mintDatas,
marginfiClient.feedIdMap,
);
const previewMarginfiAccount = MarginfiAccountWrapper.fromAccountDataRaw(
account.address,
Expand Down Expand Up @@ -249,8 +250,8 @@ function getLiquidationStat(bank: ExtendedBankInfo, isLoading: boolean, simulati
? computeLiquidation / price >= 0.5
? "SUCCESS"
: computeLiquidation / price >= 0.25
? "ALERT"
: "DESTRUCTIVE"
? "ALERT"
: "DESTRUCTIVE"
: undefined;

return {
Expand Down Expand Up @@ -308,7 +309,10 @@ function getBankTypeStat(bank: ExtendedBankInfo): PreviewStat {
function getOracleStat(bank: ExtendedBankInfo): PreviewStat {
let oracle = "";
switch (bank?.info.rawBank.config.oracleSetup) {
case "PythEma":
case "PythLegacy":
oracle = "Pyth";
break;
case "PythPushOracle":
oracle = "Pyth";
break;
case "SwitchboardV2":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export const getRateData = (bank: ExtendedBankInfo, isInLendingMode: boolean): R
? emissionsRate
: 0
: emissions == Emissions.Borrowing
? emissionsRate
: 0;
? emissionsRate
: 0;

const rateAPR = interestRate + emissionRate;
const rateAPY = aprToApy(rateAPR);
Expand All @@ -119,7 +119,10 @@ export const getAssetPriceData = (bank: ExtendedBankInfo): AssetPriceData => {

let oracle = "";
switch (bank.info.rawBank.config.oracleSetup) {
case "PythEma":
case "PythLegacy":
oracle = "Pyth";
break;
case "PythPushOracle":
oracle = "Pyth";
break;
case "SwitchboardV2":
Expand Down
4 changes: 4 additions & 0 deletions apps/marginfi-v2-ui/src/config/marginfi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let campaignWhitelist: { icon: string; size: number; publicKey: string }[];

const environment = process.env.NEXT_PUBLIC_MARGINFI_ENVIRONMENT;
const groupOverride = process.env.NEXT_PUBLIC_MARGINFI_GROUP_OVERRIDE;
const programOverride = process.env.NEXT_PUBLIC_MARGINFI_PROGRAM_OVERRIDE;

switch (environment) {
case "production":
Expand All @@ -19,6 +20,9 @@ switch (environment) {
if (groupOverride) {
mfiConfig.groupPk = new PublicKey(groupOverride);
}
if (programOverride) {
mfiConfig.programId = new PublicKey(programOverride);
}
campaignWhitelist = [
{
icon: "https://s2.coinmarketcap.com/static/img/coins/64x64/23095.png",
Expand Down
Loading

0 comments on commit 4b76cfb

Please sign in to comment.