Skip to content

Commit

Permalink
sbod crank: util to build luts
Browse files Browse the repository at this point in the history
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
  • Loading branch information
microwavedcola1 committed Aug 8, 2024
1 parent 687c50c commit 8455be9
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 107 deletions.
77 changes: 68 additions & 9 deletions ts/client/scripts/maintain-alts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ import {
OPENBOOK_PROGRAM_ID,
} from '../src/constants';
import { buildVersionedTx } from '../src/utils';
import { getOraclesForMangoGroup } from './sb-on-demand-crank-utils';

const { MB_CLUSTER_URL, MB_PAYER3_KEYPAIR, DRY_RUN } = process.env;
const CLUSTER: Cluster = (process.env.CLUSTER as Cluster) || 'mainnet-beta';

async function buildSbOnDemandAccountsForAlts(
connection: Connection,
group: Group,
): Promise<PublicKey[]> {
const userProvider = new AnchorProvider(
connection,
Expand All @@ -47,14 +49,27 @@ async function buildSbOnDemandAccountsForAlts(
const idl = await Anchor30Program.fetchIdl(SB_ON_DEMAND_PID, userProvider);
const sbOnDemandProgram = new Anchor30Program(idl!, userProvider);

// all sbod oracles on mango group
const oracles = getOraclesForMangoGroup(group);
const ais = (
await Promise.all(
chunk(
oracles.map((item) => item.oraclePk),
50,
false,
).map(async (chunk) => await connection.getMultipleAccountsInfo(chunk)),
)
).flat();
const sbodOracles = oracles
.map((o, i) => {
return { oracle: o, ai: ais[i] };
})
.filter((item) => item.ai?.owner.equals(SB_ON_DEMAND_PID));

return await fetchAllLutKeys(
new Queue(sbOnDemandProgram, new PublicKey(ON_DEMAND_MAINNET_QUEUE)),
// TODO replace with oracles which are derived from the group
[
'2A7aqNLy26ZBSMWP2Ekxv926hj16tCA47W1sHWVqaLii', // digitSOL
'AZcoqpWhMJUaKEDUfKsfzCr3Y96gSQwv43KSQ6KpeyQ1', // INF
].map((oracle) => {
return new PullFeed(sbOnDemandProgram, new PublicKey(oracle));
sbodOracles.map((oracle) => {
return new PullFeed(sbOnDemandProgram, oracle.oracle.oraclePk);
}),
);
}
Expand Down Expand Up @@ -178,7 +193,7 @@ async function createANewAlt() {
);
}

async function populateExistingAlts(): Promise<void> {
async function populateExistingAltsWithMangoGroupAccounts(): Promise<void> {
try {
const options = AnchorProvider.defaultOptions();
const connection = new Connection(MB_CLUSTER_URL!, options);
Expand Down Expand Up @@ -387,12 +402,56 @@ async function populateExistingAlts(): Promise<void> {
payer,
'sb on demand oracles',
[altAddress21],
await buildSbOnDemandAccountsForAlts(connection),
await buildSbOnDemandAccountsForAlts(connection, group),
);
} catch (error) {
console.log(error);
}
}

async function populateAltsForSbodOracles(): Promise<void> {
try {
const options = AnchorProvider.defaultOptions();
const connection = new Connection(MB_CLUSTER_URL!, options);
const payer = Keypair.fromSecretKey(
Buffer.from(JSON.parse(fs.readFileSync(MB_PAYER3_KEYPAIR!, 'utf-8'))),
);
const payerWallet = new Wallet(payer);
const userProvider = new AnchorProvider(connection, payerWallet, options);
const client = await MangoClient.connect(
userProvider,
CLUSTER,
MANGO_V4_ID[CLUSTER],
{
idsSource: 'api',
},
);
const group = await client.getGroup(MANGO_V4_MAIN_GROUP);

/// SBOD alts
const altAddress21 = new PublicKey(
'3DdohDpFiXjLNqCzFyU8CP3rtGwkn9VbkxFYGrNiqcyR',
);
await extendTable(
client,
group,
payer,
'sb on demand oracles',
[altAddress21],
await buildSbOnDemandAccountsForAlts(connection, group),
);
// unused
const altAddress22 = new PublicKey(
'7uRdMQCXedJ1SiVtwJJazEjsW9vmYiPZGj2ft99XnVu4',
);
} catch (error) {
console.log(error);
}
}

// uncomment to create a new alt, paste this pubkey in the populate methods, go...
// createANewAlt();
populateExistingAlts();

// run the script to populate existing alts
// populateExistingAltsWithMangoGroupAccounts();
populateAltsForSbodOracles();
35 changes: 19 additions & 16 deletions ts/client/scripts/manageFeeWs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,28 @@ export function manageFeeWebSocket(
ws?.close();
});

ws.addEventListener('message', (event: MessageEvent): void => {
try {
const parsedData = JSON.parse(event.data as string);
const value = parsedData?.params?.result?.value.by_tx[15];
ws.addEventListener(
'message',
(event: { data: any; type: string; target: WebSocket }): void => {
try {
const parsedData = JSON.parse(event.data as string);
const value = parsedData?.params?.result?.value.by_tx[15];

if (value !== undefined && typeof value === 'number') {
recentValues.push(value);
if (recentValues.length > rollingWindowSize) {
recentValues.shift();
}
if (value !== undefined && typeof value === 'number') {
recentValues.push(value);
if (recentValues.length > rollingWindowSize) {
recentValues.shift();
}

const rollingMean = calculateRollingMean(recentValues);
onMeanCalculated(rollingMean);
const rollingMean = calculateRollingMean(recentValues);
onMeanCalculated(rollingMean);
}
} catch (error) {
console.error('Error processing message:', error);
onMeanCalculated(-1);
}
} catch (error) {
console.error('Error processing message:', error);
onMeanCalculated(-1);
}
});
},
);
} catch (error) {
console.error('Error in connectWebSocket:', error);
onMeanCalculated(-1);
Expand Down
59 changes: 59 additions & 0 deletions ts/client/scripts/sb-on-demand-crank-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { PublicKey } from '@solana/web3.js';
import { Group } from '../src/accounts/group';
import { ZERO_I80F48 } from '../src/numbers/I80F48';

/**
* scans mango group for all oracles that need updating
* includes bank oracle, fallback oracle and perp market oracles
*/
export function getOraclesForMangoGroup(
group: Group,
): { oraclePk: PublicKey; name: string }[] {
// oracles for tokens
const oracles1 = Array.from(group.banksMapByName.values())
.filter(
(b) =>
!(
b[0].nativeDeposits().eq(ZERO_I80F48()) &&
b[0].nativeBorrows().eq(ZERO_I80F48()) &&
b[0].reduceOnly == 1
),
)
.map((b) => {
return {
oraclePk: b[0].oracle,

name: b[0].name,
};
});

// oracles for perp markets
const oracles2 = Array.from(group.perpMarketsMapByName.values()).map((pM) => {
return {
oraclePk: pM.oracle,

name: pM.name,
};
});

// fallback oracles for tokens
const oracles3 = Array.from(group.banksMapByName.values())
.filter(
(b) =>
!(
b[0].nativeDeposits().eq(ZERO_I80F48()) &&
b[0].nativeBorrows().eq(ZERO_I80F48()) &&
b[0].reduceOnly == 1
),
)
.map((b) => {
return {
oraclePk: b[0].oracle,

name: b[0].name,
};
})
.filter((item) => !item.oraclePk.equals(PublicKey.default));
const oracles = oracles1.concat(oracles2).concat(oracles3);
return oracles;
}
63 changes: 2 additions & 61 deletions ts/client/scripts/sb-on-demand-crank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import { Group } from '../src/accounts/group';
import { parseSwitchboardOracle } from '../src/accounts/oracle';
import { MangoClient } from '../src/client';
import { MANGO_V4_ID, MANGO_V4_MAIN_GROUP } from '../src/constants';
import { ZERO_I80F48 } from '../src/numbers/I80F48';
import { createComputeBudgetIx } from '../src/utils/rpc';
import { manageFeeWebSocket } from './manageFeeWs';
import { getOraclesForMangoGroup } from './sb-on-demand-crank-utils';

const CLUSTER: Cluster =
(process.env.CLUSTER_OVERRIDE as Cluster) || 'mainnet-beta';
Expand Down Expand Up @@ -310,10 +310,7 @@ async function preparePullIx(
gateway: oracle.gatewayUrl,
};
// TODO use fetchUpdateMany
const [pullIx] = await pullFeed.fetchUpdateIx(
conf,
recentSlothashes,
);
const [pullIx] = await pullFeed.fetchUpdateIx(conf, recentSlothashes);

return pullIx;
}
Expand Down Expand Up @@ -544,62 +541,6 @@ async function setupMango(): Promise<{
return { group, client, connection, user, userProvider };
}

/**
* scans mango group for all oracles that need updating
* includes bank oracle, fallback oracle and perp market oracles
*/
function getOraclesForMangoGroup(
group: Group,
): { oraclePk: PublicKey; name: string }[] {
// oracles for tokens
const oracles1 = Array.from(group.banksMapByName.values())
.filter(
(b) =>
!(
b[0].nativeDeposits().eq(ZERO_I80F48()) &&
b[0].nativeBorrows().eq(ZERO_I80F48()) &&
b[0].reduceOnly == 1
),
)
.map((b) => {
return {
oraclePk: b[0].oracle,

name: b[0].name,
};
});

// oracles for perp markets
const oracles2 = Array.from(group.perpMarketsMapByName.values()).map((pM) => {
return {
oraclePk: pM.oracle,

name: pM.name,
};
});

// fallback oracles for tokens
const oracles3 = Array.from(group.banksMapByName.values())
.filter(
(b) =>
!(
b[0].nativeDeposits().eq(ZERO_I80F48()) &&
b[0].nativeBorrows().eq(ZERO_I80F48()) &&
b[0].reduceOnly == 1
),
)
.map((b) => {
return {
oraclePk: b[0].oracle,

name: b[0].name,
};
})
.filter((item) => !item.oraclePk.equals(PublicKey.default));
const oracles = oracles1.concat(oracles2).concat(oracles3);
return oracles;
}

async function setupSwitchboard(client: MangoClient): Promise<{
sbOnDemandProgram: Anchor30Program<Idl>;
crossbarClient: CrossbarClient;
Expand Down
43 changes: 22 additions & 21 deletions ts/client/scripts/update-risk-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
MANGO_GOVERNANCE_PROGRAM,
MANGO_MINT,
MANGO_REALM_PK,
SB_ON_DEMAND_LST_FALLBACK_ORACLES,
} from './governanceInstructions/constants';
import { createProposal } from './governanceInstructions/createProposal';
import {
Expand Down Expand Up @@ -219,19 +218,30 @@ async function updateTokenParams(): Promise<void> {
bank?.initLiabWeight.toNumber().toFixed(1),
);

const maybeSbOracle = SB_ON_DEMAND_LST_FALLBACK_ORACLES.filter(
(x) =>
x[0].replace('/USD', '').toLocaleUpperCase() ==
bank.name.toLocaleUpperCase(),
);
if (maybeSbOracle.length > 0) {
console.log(` - ${bank.name} ${maybeSbOracle[0][0]}`);
builder.fallbackOracle(PublicKey.default);
change = true;
} else {
return;
// eslint-disable-next-line no-constant-condition
if (true) {
if (bank.uiBorrows() == 0 && bank.reduceOnly == 1) {
console.log(` - ${bank.name}`);
builder.forceWithdraw(true);

// builder.disableAssetLiquidation(true);
change = true;
}
}

// const maybeSbOracle = SB_ON_DEMAND_LST_FALLBACK_ORACLES.filter(
// (x) =>
// x[0].replace('/USD', '').toLocaleUpperCase() ==
// bank.name.toLocaleUpperCase(),
// );
// if (maybeSbOracle.length > 0) {
// console.log(` - ${bank.name} ${maybeSbOracle[0][0]}`);
// builder.fallbackOracle(PublicKey.default);
// change = true;
// } else {
// return;
// }

// const maybeSbOracle = SB_FEEDS_TO_MIGRATE.filter(
// (x) => x.name.replace('/USD', '') === bank.name.toLocaleUpperCase(),
// );
Expand Down Expand Up @@ -300,15 +310,6 @@ async function updateTokenParams(): Promise<void> {
// }
// }

// // eslint-disable-next-line no-constant-condition
// if (true) {
// if (bank.uiBorrows() == 0 && bank.reduceOnly == 1) {
// builder.disableAssetLiquidation(true);
// builder.forceWithdraw(true);
// change = true;
// }
// }

// // eslint-disable-next-line no-constant-condition
// if (true) {
// if (!tier) {
Expand Down

0 comments on commit 8455be9

Please sign in to comment.