Skip to content

Commit

Permalink
Merge pull request #5 from metaDAOproject/fix/finalization
Browse files Browse the repository at this point in the history
fix: > x markets to crank
  • Loading branch information
R-K-H authored Dec 1, 2024
2 parents 6750a70 + e8cd61a commit c540747
Showing 1 changed file with 60 additions and 27 deletions.
87 changes: 60 additions & 27 deletions src/entrypoints/cron/crank-and-finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ const fetchActiveProposalsByVersion = async (version: number) => {
return proposals;
}

const buildAndSendTranxation = async (ixs: any, autocratClient: AutocratClient) => {
let tx = new Transaction();
tx.add(
ComputeBudgetProgram.setComputeUnitLimit({
units: 4_000 * ixs.length,
})
);
tx.add(
ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 1,
})
);
tx.add(...ixs);
try {
logger.log("sending crank txs");
const res = await autocratClient.provider.sendAndConfirm(tx);
logger.log("Cranking done:", res);
} catch (e) {
if (e && e.hasOwnProperty('signature')) {
await sleep(10000);
const txResult = await provider.connection.getSignatureStatuses([e.signature])
logger.log("txResult", txResult);
}
}
}

const run = async () => {

const AUTOCRAT_PROGRAM_ID = new PublicKey(
Expand Down Expand Up @@ -119,34 +145,41 @@ const run = async () => {
amms.push(storedProposal.failAmm);
}
let ixs = [];
for (const amm of amms) {
ixs.push(
await autocratClient.ammClient.crankThatTwapIx(amm).instruction()
);
}
let tx = new Transaction();
tx.add(
ComputeBudgetProgram.setComputeUnitLimit({
units: 4_000 * ixs.length,
})
);
tx.add(
ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 1,
})
);
tx.add(...ixs);
try {
logger.log("sending crank txs");
const res = await autocratClient.provider.sendAndConfirm(tx);
logger.log("Cranking done:", res);
} catch (e) {
if (e && e.hasOwnProperty('signature')) {
await sleep(10000);
const txResult = await provider.connection.getSignatureStatuses([e.signature])
logger.log("txResult", txResult);
// TODO: In here we need to take a mod of however many qe can fit in txn
const loopTimes = Math.ceil(amms.length / 12);
console.log("loopTimes", loopTimes);
try{
for (let i = 0; i < amms.length; i++) {
ixs.push(
await autocratClient.ammClient.crankThatTwapIx(amms[i]).instruction()
);
if(i > 0 && (i % 12) === 0) {
logger.log("cranking 12 amms");
if(ixs.length > 0) {
console.log("sending crank txs", ixs);
await buildAndSendTranxation(ixs, autocratClient);
ixs = [];
}
}
if((i + 1) === amms.length) {
logger.log("sending last crank txs", ixs);
await buildAndSendTranxation(ixs, autocratClient);
ixs = [];
}
}
} catch (e) {
console.log("error", e)
}
// if (amms.length > 12) {
// logger.log("cranking more than 12 amms, skipping");
// return;
// }
// for (const amm of amms) {
// ixs.push(
// await autocratClient.ammClient.crankThatTwapIx(amm).instruction()
// );
// }


// Setup for blockheight check..
let currentBlockHeight: number | null = null;
Expand Down Expand Up @@ -273,7 +306,7 @@ const run = async () => {
}
}
} catch (e) {
logger.errorWithChatBotAlert("failed to crank proposal markets:", e);
logger.errorWithChatBotAlert(`${BACKOFFICE_ENVIRONMENT}: failed to crank proposal markets:`, e);
}
};

Expand Down

0 comments on commit c540747

Please sign in to comment.