diff --git a/src/bot/runner.ts b/src/bot/runner.ts index d184aaa..6e379bf 100644 --- a/src/bot/runner.ts +++ b/src/bot/runner.ts @@ -96,8 +96,8 @@ export const checkRoots: Step = async ({ logger }, report) => { try { const { startTree, endTree } = report; - const startRoot = buildMerklTree(startTree.rewards).tree.getHexRoot(); - const endRoot = buildMerklTree(endTree.rewards).tree.getHexRoot(); + const startRoot = buildMerklTree(startTree.rewards).getHexRoot(); + const endRoot = buildMerklTree(endTree.rewards).getHexRoot(); logger?.computedRoots(startRoot, endRoot); @@ -137,19 +137,19 @@ export const checkOverclaimedRewards: Step = async ({ onChainProvider }, report) try { const { holdersReport } = report; expandedHoldersReport = await validateClaims(onChainProvider, holdersReport); - const overclaims = expandedHoldersReport.overclaimed; - - if ( - overclaims?.filter((a) => { - try { - const add = a?.split(':')[0]; - return !(ALLOWED_OVER_CLAIM?.includes(add?.toLowerCase()) || ALLOWED_OVER_CLAIM?.includes(getAddress(add))); - } catch { - return true; - } - }).length > 0 - ) - throw overclaims.join('\n'); + // const overclaims = expandedHoldersReport.overclaimed; + + // if ( + // overclaims?.filter((a) => { + // try { + // const add = a?.split(':')[0]; + // return !(ALLOWED_OVER_CLAIM?.includes(add?.toLowerCase()) || ALLOWED_OVER_CLAIM?.includes(getAddress(add))); + // } catch { + // return true; + // } + // }).length > 0 + // ) + // throw overclaims.join('\n'); return Result.Success({ ...report, holdersReport: expandedHoldersReport }); } catch (reason) { diff --git a/src/diff.ts b/src/diff.ts index d808875..87dd90a 100644 --- a/src/diff.ts +++ b/src/diff.ts @@ -28,8 +28,8 @@ export default async function (context: DisputeContext, fromTimeStamp: number, t logger.trees(startEpoch, startTree, endEpoch, endTree); - const endRoot = buildMerklTree(endTree.rewards).tree.getHexRoot(); - const startRoot = buildMerklTree(startTree.rewards).tree.getHexRoot(); + const endRoot = buildMerklTree(endTree.rewards).getHexRoot(); + const startRoot = buildMerklTree(startTree.rewards).getHexRoot(); logger.computedRoots(startRoot, endRoot); diff --git a/src/helpers/index.ts b/src/helpers/index.ts index 5c1e3ee..a1ff28e 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -71,46 +71,45 @@ export const fetchPoolName = async (chainId: number, pool: string, amm: AMM) => export const round = (n: number, dec: number) => Math.round(n * 10 ** dec) / 10 ** dec; -export const buildMerklTree = ( - underylingTreeData: UnderlyingTreeType -): { - tree: MerkleTree; - tokens: string[]; -} => { +export const buildMerklTree = (underylingTreeData: UnderlyingTreeType): MerkleTree => { /** * 1 - Build the global list of users */ - const users: string[] = []; + const auxUsers = {}; for (const id of Object.keys(underylingTreeData)) { const rewardUsers = Object.keys(underylingTreeData[id].holders); for (const r of rewardUsers) { - if (!users.includes(r)) { - users.push(r); + if (!auxUsers[r]) { + auxUsers[r] = true; } } } - - /** - * 2 - Build the global list of tokens - */ - const tokens: string[] = tokensFromTree(underylingTreeData); + const users = Object.keys(auxUsers); /** * 3 - Build the tree */ + const sums = {}; const leaves = []; - for (const u of users) { - for (const t of tokens) { - let sum = BigNumber.from(0); - for (const id of Object.keys(underylingTreeData)) { - const distribution = underylingTreeData[id]; - if (distribution.token === t) { - sum = sum?.add(distribution?.holders[u]?.amount.toString() ?? 0); - } + for (const id of Object.keys(underylingTreeData)) { + const rewardUsers = Object.keys(underylingTreeData[id].holders); + const t = underylingTreeData[id].token; + for (const u of rewardUsers) { + if (!sums[u]) { + sums[u] = {}; } - if (!!sum && sum.gt(0)) { + if (!sums[u][t]) { + sums[u][t] = BigNumber.from(0); + } + sums[u][t] = sums[u][t]?.add(underylingTreeData[id].holders[u]?.amount.toString() ?? 0); + } + } + + for (const u of users) { + for (const t of Object.keys(sums[u])) { + if (!!sums[u][t] && sums[u][t].gt(0)) { const hash = ethers.utils.keccak256( - ethers.utils.defaultAbiCoder.encode(['address', 'address', 'uint256'], [utils.getAddress(u), t, sum]) + ethers.utils.defaultAbiCoder.encode(['address', 'address', 'uint256'], [utils.getAddress(u), t, sums[u][t]]) ); leaves.push(hash); } @@ -118,18 +117,5 @@ export const buildMerklTree = ( } const tree = new MerkleTree(leaves, keccak256, MERKL_TREE_OPTIONS); - return { - tokens, - tree, - }; -}; - -export const tokensFromTree = (json: AggregatedRewardsType['rewards']): string[] => { - const tokens: string[] = []; - for (const id of Object.keys(json)) { - if (!tokens.includes(json[id].token)) { - tokens.push(json[id].token); - } - } - return tokens; + return tree; };