Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: v0 - reallocation check #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/bot/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ALERTING_DELAY } from '../constants/alertingDelay';
import { BaseTree } from '../providers/tree';
import { BotError, MerklReport, Resolver, Result, Step, StepResult } from '../types/bot';
import { gtStrings } from '../utils/addString';
import { wasReallocated } from '../utils/leafDiff';
import { fetchCampaigns, fetchLeaves } from '../utils/merklAPI';
import { DisputeContext } from './context';
import { approveDisputeStake, createSigner, disputeTree } from './dispute';
Expand Down Expand Up @@ -116,11 +117,16 @@ export const checkOverDistribution: Step = async ({}, report) => {
// -> test unsuccessful => throw
// if not we throw
if (negativeDiffs.length > 0) {
return Result.Error({
code: BotError.NegativeDiff,
reason: negativeDiffs.join('\n'),
report: { ...report, diffCampaigns, diffRecipients },
});
const allReallocated = (await Promise.all(negativeDiffs.map(async (leaf) => await wasReallocated(chainId, leaf)))).reduce(
(a, b) => a && b
);
if (!allReallocated) {
return Result.Error({
code: BotError.NegativeDiff,
reason: negativeDiffs.join('\n'),
report: { ...report, diffCampaigns, diffRecipients },
});
}
}

const overDistributed = [];
Expand Down
2 changes: 1 addition & 1 deletion src/providers/tree/BaseTree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BASE_9, type Campaign, type CampaignParameters, Erc20__factory, Int256, type MerklChainId } from '@angleprotocol/sdk';
import { type Campaign, type CampaignParameters, type MerklChainId, BASE_9, Erc20__factory, Int256 } from '@angleprotocol/sdk';
import { BigNumber, utils } from 'ethers';
import keccak256 from 'keccak256';
import MerkleTree from 'merkletreejs';
Expand Down
18 changes: 18 additions & 0 deletions src/utils/leafDiff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DistributionCreator__factory, Distributor__factory, registry } from '@angleprotocol/sdk';
import { BigNumber, ethers } from 'ethers';

import { httpProvider } from '../providers';
import { ExpandedLeaf } from '../providers/tree';

export const wasReallocated = async (chainId: number, leaf: ExpandedLeaf) => {
const provider = httpProvider(chainId);
const distributionCreatorAddress = registry(chainId).Merkl.DistributionCreator;
const distributorAddress = registry(chainId).Merkl.Distributor;
const creator = DistributionCreator__factory.connect(distributionCreatorAddress, provider);
const distributor = Distributor__factory.connect(distributorAddress, provider);

const to = await creator.campaignReallocation(leaf.campaignId, leaf.recipient);
const claimed = await distributor.claimed(leaf.campaignId, leaf.recipient);

return to !== ethers.constants.AddressZero && claimed.amount.eq(BigNumber.from('0'));
};
Loading