Skip to content

Commit

Permalink
enable use of fallback oracles in TS client
Browse files Browse the repository at this point in the history
  • Loading branch information
Lou-Kamades committed Dec 5, 2023
1 parent eec6725 commit d7a7492
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/client/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,12 @@ impl MangoGroupContext {
for acc in oracle_accounts {
let bank = banks_by_oracle.get(acc.key()).unwrap();
let state = oracle_state_unchecked(&acc, bank.mint_decimals)?;
if state
if (state
.check_confidence(bank.name(), &bank.oracle_config)
.is_err()
|| state
.check_staleness(bank.name(), &bank.oracle_config, now_slot)
.is_err()
.is_err()) && bank.fallback_oracle != Pubkey::default()
{
fallbacks.push(bank.fallback_oracle);
}
Expand Down
11 changes: 11 additions & 0 deletions ts/client/src/accounts/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class Bank implements BankForHealth {
public maintWeightShiftDurationInv: I80F48;
public maintWeightShiftAssetTarget: I80F48;
public maintWeightShiftLiabTarget: I80F48;
public fallbackOracle: PublicKey;

static from(
publicKey: PublicKey,
Expand Down Expand Up @@ -138,6 +139,7 @@ export class Bank implements BankForHealth {
maintWeightShiftDurationInv: I80F48Dto;
maintWeightShiftAssetTarget: I80F48Dto;
maintWeightShiftLiabTarget: I80F48Dto;
fallbackOracle: PublicKey;
},
): Bank {
return new Bank(
Expand Down Expand Up @@ -197,6 +199,7 @@ export class Bank implements BankForHealth {
obj.maintWeightShiftDurationInv,
obj.maintWeightShiftAssetTarget,
obj.maintWeightShiftLiabTarget,
obj.fallbackOracle
);
}

Expand Down Expand Up @@ -257,6 +260,7 @@ export class Bank implements BankForHealth {
maintWeightShiftDurationInv: I80F48Dto,
maintWeightShiftAssetTarget: I80F48Dto,
maintWeightShiftLiabTarget: I80F48Dto,
public fallbaclOracle: PublicKey,
) {
this.name = utf8.decode(new Uint8Array(name)).split('\x00')[0];
this.oracleConfig = {
Expand Down Expand Up @@ -307,6 +311,8 @@ export class Bank implements BankForHealth {
this.mintDecimals +
'\n oracle - ' +
this.oracle.toBase58() +
'\n fallback oracle - ' +
this.fallbackOracle.toBase58() +
'\n price - ' +
this._price?.toString() +
'\n uiPrice - ' +
Expand Down Expand Up @@ -622,6 +628,7 @@ export class MintInfo {
banks: PublicKey[];
vaults: PublicKey[];
oracle: PublicKey;
fallbackOracle: PublicKey;
registrationTime: BN;
groupInsuranceFund: number;
},
Expand All @@ -634,6 +641,7 @@ export class MintInfo {
obj.banks,
obj.vaults,
obj.oracle,
obj.fallbackOracle,
obj.registrationTime,
obj.groupInsuranceFund == 1,
);
Expand All @@ -647,6 +655,7 @@ export class MintInfo {
public banks: PublicKey[],
public vaults: PublicKey[],
public oracle: PublicKey,
public fallbackOracle: PublicKey,
public registrationTime: BN,
public groupInsuranceFund: boolean,
) {}
Expand All @@ -664,6 +673,8 @@ export class MintInfo {
this.mint.toBase58() +
'\n oracle ' +
this.oracle.toBase58() +
'\n fallback oracle - ' +
this.fallbackOracle.toBase58() +
'\n banks ' +
this.banks
.filter((pk) => pk.toBase58() !== PublicKey.default.toBase58())
Expand Down
41 changes: 41 additions & 0 deletions ts/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ export enum AccountRetriever {
Fixed,
}

export enum FallbackOracleMode {
None,
Fixed,
AllAvailable,
}

export type FallbackOracleOptions = {
mode: FallbackOracleMode,
fixedOracles: PublicKey[]
}

export type IdsSource = 'api' | 'static' | 'get-program-accounts';

export type MangoClientOptions = {
Expand All @@ -114,6 +125,7 @@ export type MangoClientOptions = {
txConfirmationCommitment?: Commitment;
openbookFeesToDao?: boolean;
prependedGlobalAdditionalInstructions?: TransactionInstruction[];
fallbackOracleOptions?: FallbackOracleOptions;
};

export class MangoClient {
Expand All @@ -124,6 +136,10 @@ export class MangoClient {
private txConfirmationCommitment: Commitment;
private openbookFeesToDao: boolean;
private prependedGlobalAdditionalInstructions: TransactionInstruction[] = [];
private fallbackOracleOptions: FallbackOracleOptions = {
mode: FallbackOracleMode.None,
fixedOracles: []
};

constructor(
public program: Program<MangoV4>,
Expand Down Expand Up @@ -4498,6 +4514,16 @@ export class MangoClient {
.map((serumPosition) => serumPosition.openOrders),
);

switch (this.fallbackOracleOptions.mode) {
case FallbackOracleMode.None:
break;
case FallbackOracleMode.Fixed:
healthRemainingAccounts.push(...this.fallbackOracleOptions.fixedOracles)

Check failure on line 4521 in ts/client/src/client.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
case FallbackOracleMode.AllAvailable:

Check failure on line 4522 in ts/client/src/client.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected a 'break' statement before 'case'
healthRemainingAccounts.push(
...mintInfos.filter((mintInfo) => mintInfo.fallbackOracle.toBase58() != PublicKey.default.toBase58()).map(m => m.fallbackOracle))

Check failure on line 4524 in ts/client/src/client.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
}

return healthRemainingAccounts;
}

Expand Down Expand Up @@ -4634,4 +4660,19 @@ export class MangoClient {

return Math.max(1, Math.ceil(medianFee));
}

/**
* Checks all oracles and returns the PublicKeys of any configured fallback oracles if the primary oracle is stale or outside the confidence interval.
* This function assumes that the group has Banks.
*
*/
public async fetchFallbacksForStaleOracles(group: Group): Promise<PublicKey[]> {
await group.reloadBankOraclePrices(this)

Check failure on line 4670 in ts/client/src/client.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
const banks: Bank[][] = Array.from(
group.banksMapByMint,
([, value]) => value,
);
const staleBanks = banks.filter((b) => b[0].isOracleStaleOrUnconfident).map((s) => s[0].fallbackOracle);
return staleBanks

Check failure on line 4676 in ts/client/src/client.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
}
}

0 comments on commit d7a7492

Please sign in to comment.