Skip to content

Commit

Permalink
some cleanup, make sure to skip canary tiers if we know it's not prof…
Browse files Browse the repository at this point in the history
…itable yet, and to skip everything if we know an upper tier (ie. not canary) is not profitable
  • Loading branch information
chuckbergeron committed Jul 5, 2023
1 parent 0fba417 commit d288cab
Showing 1 changed file with 47 additions and 21 deletions.
68 changes: 47 additions & 21 deletions packages/library/src/claimerProfitablePrizeTxs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function executeClaimerProfitablePrizeTxs(
const marketRate = getContract('MarketRate', chainId, readProvider, contracts, contractsVersion);

if (!claimer) {
throw new Error('Claimer: Contract Unavailable');
throw new Error('Contract Unavailable');
}

// #1. Get context about the prize pool prize token, etc
Expand Down Expand Up @@ -100,19 +100,29 @@ export async function executeClaimerProfitablePrizeTxs(
// console.log('unclaimedClaimsGrouped');
// console.log(unclaimedClaimsGrouped);

let canaryTierNotProfitable = false;
for (let vaultTier of Object.entries(unclaimedClaimsGrouped)) {
const [key, value] = vaultTier;
const [vault, tier] = key.split(',');
const groupedClaims = value;

printSpacer();
printSpacer();
printSpacer();
printAsterisks();

console.log(chalk.blueBright(`Processing ...`));
console.log(chalk.blueBright(`Vault: ${vault}`));
console.log(chalk.blueBright(`Tier: ${Number(tier) + 1}`));
console.log(chalk.blueBright(`Tier: ${tierWords(context, Number(tier))}`));

if (isCanary(context, Number(tier)) && canaryTierNotProfitable) {
console.log(
chalk.redBright(`Tier #${Number(tier)} (Canary tier) not profitable, skipping ...`),
);
continue;
}

// #5. Decide if profitable or not
printAsterisks();
console.log(chalk.blue(`5a. Calculating # of profitable claims ...`));

const claimPrizesParams = await calculateProfit(
Expand All @@ -131,7 +141,9 @@ export async function executeClaimerProfitablePrizeTxs(
// It's profitable if there is at least 1 claim to claim
// #6. Populate transaction
if (claimPrizesParams.winners.length > 0) {
console.log(chalk.green('Claimer: Execute Claim Transaction'));
console.log(
chalk.green(`Execute Claim Transaction for Tier #${tierWords(context, Number(tier))}`),
);
printSpacer();

const populatedTx = await claimer.populateTransaction.claimPrizes(
Expand All @@ -155,13 +167,32 @@ export async function executeClaimerProfitablePrizeTxs(
} else {
console.log(
chalk.yellow(
`Claimer: Not profitable to claim for Draw #${context.drawId}, Tier: ${Number(tier) + 1}`,
`Claimer: Not profitable to claim for Draw #${context.drawId}, Tier: ${tierWords(
context,
Number(tier),
)}`,
),
);

if (isCanary(context, Number(tier))) {
canaryTierNotProfitable = true;
} else {
console.log(
chalk.redBright(
`Claimer: Not profitable to claim any more tiers yet for Draw #${context.drawId}`,
),
);

break;
}
}
}
}

const isCanary = (context: ClaimPrizeContext, tier: number): boolean => {
return context.tiers.rangeArray.length - 1 === tier;
};

/**
* Figures out how much gas is required to run the contract function
*
Expand Down Expand Up @@ -259,12 +290,22 @@ const calculateProfit = async (
console.log(chalk.yellow(`Submitting transaction to claim ${claimCount} prize(s):`));
logClaims(claimsSlice);
} else {
console.log(chalk.yellow(`Claiming tier #${tier + 1} is currently not profitable:`));
console.log(
chalk.yellow(`Claiming tier #${tierWords(context, tier)} currently not profitable.`),
);
}

return claimPrizesParams;
};

const tierWords = (context: ClaimPrizeContext, tier: number) => {
const tiersArray = context.tiers.rangeArray;

const canaryWords = tiersArray.length - 1 === tier ? ' (Canary tier)' : '';

return `${tier + 1}${canaryWords}`;
};

const logClaims = (claims: Claim[]) => {
printSpacer();
claims.forEach((claim) =>
Expand Down Expand Up @@ -384,22 +425,8 @@ const getGasCost = async (
feeRecipient: string,
gasTokenMarketRateUsd: number,
) => {
printAsterisks();
printSpacer();
// console.log('claims');
// console.log(claims);
// printAsterisks();
// printSpacer();
// 1. Gas cost for 1 claim:
let claimsSlice = claims.slice(0, 1);
// printSpacer();
// console.log('claimsSlice');
// console.log(claimsSlice);
printSpacer();

let claimPrizesParams = buildParams(vault, tier, claimsSlice, feeRecipient);
// console.log('claimPrizesParams');
// console.log(claimPrizesParams);

let estimatedGasLimitForOne = await getEstimatedGasLimit(claimer, claimPrizesParams);
if (!estimatedGasLimitForOne || estimatedGasLimitForOne.eq(0)) {
Expand Down Expand Up @@ -501,7 +528,6 @@ const getClaimInfo = async (

const nextClaimFees = await claimer.computeTotalFees(tier, numClaims);
printSpacer();
console.log(chalk.bgRed('nextClaimFees'));

// COSTS USD
const claimCostUsd = gasCost.gasCostPerClaimUsd * numClaims;
Expand Down

0 comments on commit d288cab

Please sign in to comment.