Skip to content

Commit

Permalink
chore: fix computeDiff
Browse files Browse the repository at this point in the history
  • Loading branch information
Picodes committed Sep 3, 2024
1 parent a84d8d6 commit 57ab217
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/bot/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export const checkOverDistribution: Step = async ({}, report) => {

const { diffCampaigns, diffRecipients, negativeDiffs } = BaseTree.computeDiff(startTree, endTree, campaigns);

// if we are in the time period of unclaimed job
// -> test unclaimed
// -> test successful => discord notif
// -> test unsuccessful => throw
// if not we throw
if (negativeDiffs.length > 0) {
return Result.Error({
code: BotError.NegativeDiff,
Expand Down
35 changes: 28 additions & 7 deletions src/providers/tree/BaseTree.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { BASE_9, Campaign, CampaignParameters, Erc20__factory, Int256, MerklChainId } from '@angleprotocol/sdk';
import { BASE_9, type Campaign, type CampaignParameters, Erc20__factory, Int256, type MerklChainId } from '@angleprotocol/sdk';
import { BigNumber, utils } from 'ethers';
import keccak256 from 'keccak256';
import MerkleTree from 'merkletreejs';

import { MERKL_TREE_OPTIONS } from '../../constants';
import { DiffCampaigns, DiffRecipients } from '../../types';
import type { DiffCampaigns, DiffRecipients } from '../../types';
import { addStrings, gtStrings, subStrings } from '../../utils/addString';
import { displayString } from '../../utils/displayString';
import { getSolidityIndex } from '../../utils/indexing';
import { log } from '../../utils/logger';
import { overridenConsole, overridenConsoleRead } from '../../utils/overridenConsole';
import { provider } from '../../utils/providers';
import { ExpandedLeaf } from './ExpandedLeaf';

Expand All @@ -31,7 +30,7 @@ export class BaseTree {
public sort() {
this.data.sort((a, b) => {
if (a.gte(b)) return 1;
else return -1;
return -1;
});
}

Expand Down Expand Up @@ -98,7 +97,7 @@ export class BaseTree {
// Sort leaves
leaves.sort((a, b) => {
if (a.hashedLeaf > b.hashedLeaf) return 1;
else return -1;
return -1;
});

// Store leaf index in idToAmount
Expand Down Expand Up @@ -293,6 +292,7 @@ export class BaseTree {
/** Check all oldCampaignTypes are still present */
for (const oldCampaignId of oldCampaignIds) {
if (!newCampaignIds.includes(oldCampaignId)) {
// not disputing, needs to dispute otherwise the latter is weird
log.error('computeDiff', `old tree campaign ${oldCampaignId} not found in new tree`);
}
}
Expand All @@ -313,6 +313,7 @@ export class BaseTree {

for (const campaignId of newCampaignIds) {
const campaignInfo = newTree.campaignInfo(campaignId);
const oldCampaignInfo = oldTree.campaignInfo(campaignId);

// TODO @BaptistG
// @dev Compute the total amount per campaign to display it
Expand All @@ -328,21 +329,41 @@ export class BaseTree {
let index = campaignInfo.firstIndex;
while (index <= campaignInfo.lastIndex) {
const newLeaf = newTree.data[index];

const oldIndex = oldTree.findIndex(newLeaf.campaignId, newLeaf.recipient, newLeaf.reason);

const diffLeaf = !oldIndex.found ? newLeaf : newLeaf.sub(oldTree.data[oldIndex.index]);
statsPerCampaign[campaignId].diff = addStrings(statsPerCampaign[campaignId].diff, diffLeaf.amount);
statsPerCampaign[campaignId]['recipients/reasons'] += 1;
statsPerCampaign[campaignId].lastProcessedTimestamp = newLeaf.lastProcessedTimestamp;

// Case to take into account
// check if leaf last processed timestamp < now - YEAR and connect tot he contract check not claimed then it's okay
if (gtStrings('0', diffLeaf.amount)) {
negativeDiffs.push(diffLeaf);
}

diffLeaves.push(diffLeaf);
index++;
}

/** Inverse search for negative amounts */
index = oldCampaignInfo.firstIndex;
if (oldCampaignInfo.lastIndex !== -1) {
while (index <= oldCampaignInfo.lastIndex) {
const oldLeaf = oldTree.data[index];
const newIndex = newTree.findIndex(oldLeaf.campaignId, oldLeaf.recipient, oldLeaf.reason);

if (!newIndex.found) {
const diffLeaf = !newIndex.found ? oldLeaf.invert() : oldLeaf.invert().sub(newTree.data[newIndex.index].invert());
statsPerCampaign[campaignId].diff = addStrings(statsPerCampaign[campaignId].diff, diffLeaf.amount);
statsPerCampaign[campaignId]['recipients/reasons'] += 1;

diffLeaves.push(diffLeaf);
negativeDiffs.push(diffLeaf);
}
index++;
}
}
}

const diffTree = new BaseTree(diffLeaves, newTree.chainId);
Expand Down Expand Up @@ -384,7 +405,7 @@ export class BaseTree {
campaigns[x.campaignId].campaignParameters.decimalsRewardToken
),
token: campaigns[x.campaignId].campaignParameters.symbolRewardToken,
percentage: ((parseFloat(x.amount) * 100) / parseFloat(statsPerCampaign[x.campaignId].diff)).toFixed(6),
percentage: ((Number.parseFloat(x.amount) * 100) / Number.parseFloat(statsPerCampaign[x.campaignId].diff)).toFixed(6),
};
});

Expand Down
4 changes: 4 additions & 0 deletions src/providers/tree/ExpandedLeaf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ export class ExpandedLeaf {
public sub(b: ExpandedLeaf): ExpandedLeaf {
return new ExpandedLeaf({ ...this, amount: BigNumber.from(this.amount).sub(b.amount).toString() });
}

public invert(): ExpandedLeaf {
return new ExpandedLeaf({ ...this, amount: BigNumber.from(this.amount).mul(-1).toString() });
}
}

0 comments on commit 57ab217

Please sign in to comment.