import { __assign, __awaiter, __generator } from "tslib";
// Create a combined `refunds` object containing refunds for V2 + V3 fills
import assert from "assert";
import { bnZero, fixedPointAdjustment, count2DDictionaryValues, count3DDictionaryValues, toAddressType, getImpliedBundleBlockRanges, isDefined, EvmAddress, } from "../../../utils";
import { addLastRunningBalance, constructPoolRebalanceLeaves, updateRunningBalance, updateRunningBalanceForDeposit, } from "./PoolRebalanceUtils";
import { buildPoolRebalanceLeafTree } from "./MerkleTreeUtils";
// and expired deposits.
export function getRefundsFromBundle(bundleFillsV3, expiredDepositsToRefundV3) {
    var combinedRefunds = {};
    Object.entries(bundleFillsV3).forEach(function (_a) {
        var _b;
        var repaymentChainId = _a[0], fillsForChain = _a[1];
        (_b = combinedRefunds[repaymentChainId]) !== null && _b !== void 0 ? _b : (combinedRefunds[repaymentChainId] = {});
        Object.entries(fillsForChain).forEach(function (_a) {
            var l2TokenAddress = _a[0], refunds = _a[1].refunds;
            // refunds can be undefined if these fills were all slow fill executions.
            if (refunds === undefined) {
                return;
            }
            // @dev use shallow copy so that modifying combinedRefunds doesn't modify the original refunds object.
            var refundsShallowCopy = __assign({}, refunds);
            if (combinedRefunds[repaymentChainId][l2TokenAddress] === undefined) {
                combinedRefunds[repaymentChainId][l2TokenAddress] = refundsShallowCopy;
            }
            else {
                // Each refunds object should have a unique refund address so we can add new ones to the
                // existing dictionary.
                combinedRefunds[repaymentChainId][l2TokenAddress] = __assign(__assign({}, combinedRefunds[repaymentChainId][l2TokenAddress]), refundsShallowCopy);
            }
        });
    });
    Object.entries(expiredDepositsToRefundV3).forEach(function (_a) {
        var _b;
        var originChainId = _a[0], depositsForChain = _a[1];
        (_b = combinedRefunds[originChainId]) !== null && _b !== void 0 ? _b : (combinedRefunds[originChainId] = {});
        Object.entries(depositsForChain).forEach(function (_a) {
            var l2TokenAddress = _a[0], deposits = _a[1];
            deposits.forEach(function (deposit) {
                var _a;
                if (combinedRefunds[originChainId][l2TokenAddress] === undefined) {
                    combinedRefunds[originChainId][l2TokenAddress] = (_a = {}, _a[deposit.depositor.toBytes32()] = deposit.inputAmount, _a);
                }
                else {
                    var existingRefundAmount = combinedRefunds[originChainId][l2TokenAddress][deposit.depositor.toBytes32()];
                    combinedRefunds[originChainId][l2TokenAddress][deposit.depositor.toBytes32()] = deposit.inputAmount.add(existingRefundAmount !== null && existingRefundAmount !== void 0 ? existingRefundAmount : bnZero);
                }
            });
        });
    });
    return combinedRefunds;
}
export function prettyPrintV3SpokePoolEvents(bundleDepositsV3, bundleFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills) {
    return {
        bundleDepositsV3: count2DDictionaryValues(bundleDepositsV3),
        bundleFillsV3: count3DDictionaryValues(bundleFillsV3, "fills"),
        bundleSlowFillsV3: count2DDictionaryValues(bundleSlowFillsV3),
        expiredDepositsToRefundV3: count2DDictionaryValues(expiredDepositsToRefundV3),
        unexecutableSlowFills: count2DDictionaryValues(unexecutableSlowFills),
    };
}
export function getEndBlockBuffers(chainIdListForBundleEvaluationBlockNumbers, blockRangeEndBlockBuffer) {
    // These buffers can be configured by the bot runner. They have two use cases:
    // 1) Validate the end blocks specified in the pending root bundle. If the end block is greater than the latest
    // block for its chain, then we should dispute the bundle because we can't look up events in the future for that
    // chain. However, there are some cases where the proposer's node for that chain is returning a higher HEAD block
    // than the bot-runner is seeing, so we can use this buffer to allow the proposer some margin of error. If
    // the bundle end block is less than HEAD but within this buffer, then we won't dispute and we'll just exit
    // early from this function.
    // 2) Subtract from the latest block in a new root bundle proposal. This can be used to reduce the chance that
    // bot runs using different providers see different contract state close to the HEAD block for a chain.
    // Reducing the latest block that we query also gives partially filled deposits slightly more buffer for relayers
    // to fully fill the deposit and reduces the chance that the data worker includes a slow fill payment that gets
    // filled during the challenge period.
    return chainIdListForBundleEvaluationBlockNumbers.map(function (chainId) { var _a; return (_a = blockRangeEndBlockBuffer[chainId]) !== null && _a !== void 0 ? _a : 0; });
}
/*
 * @notice Constructs a new pool rebalance root given the input bundle data.
 * @dev It is assumed that the input bundle data corresponds to the block ranges of the input mainnetBundleEndBlock.
 * If the mainnetBundleEndBlock does not correspond to any historical or pending root bundle, then the output pool rebalance
 * root will be constructed under the assumption that the pending root bundle is valid and passes liveness.
 * @param latestMainnetBlock The latest mainnet block number.
 * @param mainnetBundleEndBlock The end block number of the block range corresponding to the bundle data.
 * @param bundleV3Deposits Deposit bundle data for the implied block range given by the mainnetBundleEndBlock.
 * @param bundleFillsV3 Fill bundle data.
 * @param bundleSlowFillsV3 Slow fill bundle data.
 * @param unexecutableSlowFills Expired slow fill bundle data.
 * @param expiredDepositsToRefundV3 Expired deposit bundle data.
 * @param clients Clients required to construct a new pool rebalance root.
 * @maxL1TokenCountOverride Optional parameter to cap the number of tokens in a single pool rebalance leaf.
 */
export function _buildPoolRebalanceRoot(latestMainnetBlock, mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients, maxL1TokenCountOverride) {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (!(clients.hubPoolClient.hasPendingProposal() &&
                        clients.hubPoolClient.getPendingRootBundle().bundleEvaluationBlockNumbers[0] < mainnetBundleEndBlock)) return [3 /*break*/, 2];
                    return [4 /*yield*/, _buildOptimisticPoolRebalanceRoot(latestMainnetBlock, mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients, maxL1TokenCountOverride)];
                case 1: return [2 /*return*/, _a.sent()];
                case 2: 
                // Otherwise, we can synchronously reconstruct a historical pool rebalance root from the input data.
                return [2 /*return*/, _buildHistoricalPoolRebalanceRoot(latestMainnetBlock, mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients, maxL1TokenCountOverride)];
            }
        });
    });
}
/*
 * @notice Constructs a new pool rebalance root given historical bundle data.
 * @param latestMainnetBlock The latest mainnet block number.
 * @param mainnetBundleEndBlock The end block number of the block range corresponding to the bundle data.
 * @param bundleV3Deposits Deposit bundle data for the implied block range given by the mainnetBundleEndBlock.
 * @param bundleFillsV3 Fill bundle data.
 * @param bundleSlowFillsV3 Slow fill bundle data.
 * @param unexecutableSlowFills Expired slow fill bundle data.
 * @param expiredDepositsToRefundV3 Expired deposit bundle data.
 * @param clients Clients required to construct a new pool rebalance root.
 * @maxL1TokenCountOverride Optional parameter to cap the number of tokens in a single pool rebalance leaf.
 */
export function _buildHistoricalPoolRebalanceRoot(latestMainnetBlock, mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients, maxL1TokenCountOverride) {
    var _a = _getMarginalRunningBalances(mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients), runningBalances = _a.runningBalances, realizedLpFees = _a.realizedLpFees, chainWithRefundsOnly = _a.chainWithRefundsOnly;
    addLastRunningBalance(latestMainnetBlock, runningBalances, clients.hubPoolClient);
    var leaves = constructPoolRebalanceLeaves(mainnetBundleEndBlock, runningBalances, realizedLpFees, Array.from(chainWithRefundsOnly).filter(function (chainId) { return !Object.keys(runningBalances).includes(chainId.toString()); }), clients.configStoreClient, maxL1TokenCountOverride);
    return {
        runningBalances: runningBalances,
        realizedLpFees: realizedLpFees,
        leaves: leaves,
        tree: buildPoolRebalanceLeafTree(leaves),
    };
}
/*
 * @notice Constructs a new pool rebalance root given the input bundle data. This function assumes there is a pending root bundle which will eventually clear liveness.
 * @param latestMainnetBlock The latest mainnet block number.
 * @param mainnetBundleEndBlock The end block number of the block range corresponding to the bundle data.
 * @param bundleV3Deposits Deposit bundle data for the implied block range given by the mainnetBundleEndBlock.
 * @param bundleFillsV3 Fill bundle data.
 * @param bundleSlowFillsV3 Slow fill bundle data.
 * @param unexecutableSlowFills Expired slow fill bundle data.
 * @param expiredDepositsToRefundV3 Expired deposit bundle data.
 * @param clients Clients required to construct a new pool rebalance root.
 * @maxL1TokenCountOverride Optional parameter to cap the number of tokens in a single pool rebalance leaf.
 */
export function _buildOptimisticPoolRebalanceRoot(latestMainnetBlock, mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients, maxL1TokenCountOverride) {
    return __awaiter(this, void 0, void 0, function () {
        var _a, runningBalances, realizedLpFees, chainWithRefundsOnly, mostRecentProposedRootBundle, blockRangesForChains, pendingRootBundleData, _b, leaves, tree, poolRebalanceLeaves;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    _a = _getMarginalRunningBalances(mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients), runningBalances = _a.runningBalances, realizedLpFees = _a.realizedLpFees, chainWithRefundsOnly = _a.chainWithRefundsOnly;
                    mostRecentProposedRootBundle = clients.hubPoolClient.getLatestProposedRootBundle();
                    blockRangesForChains = getImpliedBundleBlockRanges(clients.hubPoolClient, clients.configStoreClient, mostRecentProposedRootBundle);
                    return [4 /*yield*/, clients.bundleDataClient.loadData(blockRangesForChains, clients.spokePoolClients, true)];
                case 1:
                    pendingRootBundleData = _c.sent();
                    _b = _buildHistoricalPoolRebalanceRoot(latestMainnetBlock, blockRangesForChains[0][1], pendingRootBundleData.bundleDepositsV3, pendingRootBundleData.bundleFillsV3, pendingRootBundleData.bundleSlowFillsV3, pendingRootBundleData.unexecutableSlowFills, pendingRootBundleData.expiredDepositsToRefundV3, clients, maxL1TokenCountOverride), leaves = _b.leaves, tree = _b.tree;
                    // Assert that the rebuilt pool rebalance root matches the pending root bundle's value. If it does not, then we likely misconstructed the pending root bundle and should throw.
                    assert(tree.getHexRoot() === mostRecentProposedRootBundle.poolRebalanceRoot);
                    // Only add marginal pending running balances if there is already an entry in `runningBalances`. If there is no entry in `runningBalances`, then
                    // The running balance for this entry was unchanged since the last root bundle.
                    Object.keys(runningBalances).forEach(function (_repaymentChainId) {
                        Object.keys(runningBalances[Number(_repaymentChainId)]).forEach(function (_l1TokenAddress) {
                            var repaymentChainId = Number(_repaymentChainId);
                            var l1TokenAddress = EvmAddress.from(_l1TokenAddress);
                            var pendingPoolRebalanceLeaf = leaves.find(function (leaf) { return leaf.chainId === repaymentChainId && leaf.l1Tokens.some(function (l1Token) { return l1Token.eq(l1TokenAddress); }); });
                            // If the pending pool rebalance root has running balances defined, then add it to `runningBalances`.
                            if (isDefined(pendingPoolRebalanceLeaf)) {
                                var pendingLeafTokenIdx = pendingPoolRebalanceLeaf.l1Tokens.findIndex(function (l1Token) {
                                    return l1Token.eq(l1TokenAddress);
                                });
                                assert(pendingLeafTokenIdx !== -1);
                                var pendingRunningBalanceAmount = pendingPoolRebalanceLeaf.runningBalances[pendingLeafTokenIdx];
                                if (!pendingRunningBalanceAmount.eq(bnZero)) {
                                    updateRunningBalance(runningBalances, repaymentChainId, _l1TokenAddress, pendingRunningBalanceAmount);
                                }
                            }
                            else {
                                // Otherwise, add the last running balance for this token.
                                var lastExecutedBundleRunningBalance = clients.hubPoolClient.getRunningBalanceBeforeBlockForChain(latestMainnetBlock, repaymentChainId, l1TokenAddress).runningBalance;
                                if (!lastExecutedBundleRunningBalance.eq(bnZero)) {
                                    updateRunningBalance(runningBalances, repaymentChainId, _l1TokenAddress, lastExecutedBundleRunningBalance);
                                }
                            }
                        });
                    });
                    poolRebalanceLeaves = constructPoolRebalanceLeaves(mainnetBundleEndBlock, runningBalances, realizedLpFees, Array.from(chainWithRefundsOnly).filter(function (chainId) { return !Object.keys(runningBalances).includes(chainId.toString()); }), clients.configStoreClient, maxL1TokenCountOverride);
                    return [2 /*return*/, {
                            runningBalances: runningBalances,
                            realizedLpFees: realizedLpFees,
                            leaves: poolRebalanceLeaves,
                            tree: buildPoolRebalanceLeafTree(poolRebalanceLeaves),
                        }];
            }
        });
    });
}
/*
 * @notice Gets the running balance amounts derived from the input bundle data.
 * @param mainnetBundleEndBlock The end block number of the block range corresponding to the bundle data.
 * @param bundleV3Deposits Deposit bundle data for the implied block range given by the mainnetBundleEndBlock.
 * @param bundleFillsV3 Fill bundle data.
 * @param bundleSlowFillsV3 Slow fill bundle data.
 * @param unexecutableSlowFills Expired slow fill bundle data.
 * @param expiredDepositsToRefundV3 Expired deposit bundle data.
 * @param clients Clients required to construct a new pool rebalance root.
 */
export function _getMarginalRunningBalances(mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients) {
    // Running balances are the amount of tokens that we need to send to each SpokePool to pay for all instant and
    // slow relay refunds. They are decreased by the amount of funds already held by the SpokePool. Balances are keyed
    // by the SpokePool's network and L1 token equivalent of the L2 token to refund.
    // Realized LP fees are keyed the same as running balances and represent the amount of LP fees that should be paid
    // to LP's for each running balance.
    // For each FilledV3Relay group, identified by { repaymentChainId, L1TokenAddress }, initialize a "running balance"
    // to the total refund amount for that group.
    var runningBalances = {};
    var realizedLpFees = {};
    // Keep track of any chains that we want to create a PoolRebalanceLeaf (and send a root bundle to) but don't have
    // any running balances.
    var chainWithRefundsOnly = new Set();
    /**
     * REFUNDS FOR FAST FILLS
     */
    // Add running balances and lp fees for v3 relayer refunds using BundleDataClient.bundleFillsV3. Refunds
    // should be equal to inputAmount - lpFees so that relayers get to keep the relayer fee. Add the refund amount
    // to the running balance for the repayment chain.
    Object.entries(bundleFillsV3).forEach(function (_a) {
        var _repaymentChainId = _a[0], fillsForChain = _a[1];
        var repaymentChainId = Number(_repaymentChainId);
        Object.entries(fillsForChain).forEach(function (_a) {
            var l2TokenAddress = _a[0], _b = _a[1], totalRealizedLpFee = _b.realizedLpFees, totalRefundAmount = _b.totalRefundAmount;
            // If the repayment token and repayment chain ID do not map to a PoolRebalanceRoute graph, then
            // there are no relevant L1 running balances.
            var l1Token = clients.hubPoolClient.getL1TokenForL2TokenAtBlock(toAddressType(l2TokenAddress, repaymentChainId), repaymentChainId, mainnetBundleEndBlock);
            if (!l1Token) {
                chainWithRefundsOnly.add(repaymentChainId);
                return;
            }
            var l1TokenAddr = l1Token.toNative();
            assert(l1Token.isEVM(), "Expected an EVM address: ".concat(l1TokenAddr));
            updateRunningBalance(runningBalances, repaymentChainId, l1TokenAddr, totalRefundAmount);
            updateRunningBalance(realizedLpFees, repaymentChainId, l1TokenAddr, totalRealizedLpFee);
        });
    });
    /**
     * PAYMENTS SLOW FILLS
     */
    // Add running balances and lp fees for v3 slow fills using BundleDataClient.bundleSlowFillsV3.
    // Slow fills should still increment bundleLpFees and updatedOutputAmount should be equal to inputAmount - lpFees.
    // Increment the updatedOutputAmount to the destination chain.
    Object.entries(bundleSlowFillsV3).forEach(function (_a) {
        var _destinationChainId = _a[0], depositsForChain = _a[1];
        var destinationChainId = Number(_destinationChainId);
        Object.entries(depositsForChain).forEach(function (_a) {
            var outputToken = _a[0], deposits = _a[1];
            deposits.forEach(function (deposit) {
                var l1TokenCounterpart = clients.hubPoolClient.getL1TokenForL2TokenAtBlock(toAddressType(outputToken, destinationChainId), destinationChainId, mainnetBundleEndBlock);
                assert(isDefined(l1TokenCounterpart), "getRefundInformationFromFill: l1TokenCounterpart is undefined");
                var lpFee = deposit.lpFeePct.mul(deposit.inputAmount).div(fixedPointAdjustment);
                updateRunningBalance(runningBalances, destinationChainId, l1TokenCounterpart.toEvmAddress(), deposit.inputAmount.sub(lpFee));
                // Slow fill LP fees are accounted for when the slow fill executes and a V3FilledRelay is emitted. i.e. when
                // the slow fill execution is included in bundleFillsV3.
            });
        });
    });
    /**
     * EXCESSES FROM UNEXECUTABLE SLOW FILLS
     */
    // Subtract destination chain running balances for BundleDataClient.unexecutableSlowFills.
    // These are all slow fills that are impossible to execute and therefore the amount to return would be
    // the updatedOutputAmount = inputAmount - lpFees.
    Object.entries(unexecutableSlowFills).forEach(function (_a) {
        var _destinationChainId = _a[0], slowFilledDepositsForChain = _a[1];
        var destinationChainId = Number(_destinationChainId);
        Object.entries(slowFilledDepositsForChain).forEach(function (_a) {
            var outputToken = _a[0], slowFilledDeposits = _a[1];
            slowFilledDeposits.forEach(function (deposit) {
                var l1TokenCounterpart = clients.hubPoolClient.getL1TokenForL2TokenAtBlock(toAddressType(outputToken, destinationChainId), destinationChainId, mainnetBundleEndBlock);
                assert(isDefined(l1TokenCounterpart), "getRefundInformationFromFill: l1TokenCounterpart is undefined");
                var lpFee = deposit.lpFeePct.mul(deposit.inputAmount).div(fixedPointAdjustment);
                updateRunningBalance(runningBalances, destinationChainId, l1TokenCounterpart.toEvmAddress(), lpFee.sub(deposit.inputAmount));
                // Slow fills don't add to lpFees, only when the slow fill is executed and a V3FilledRelay is emitted, so
                // we don't need to subtract it here. Moreover, the HubPoole expects bundleLpFees to be > 0.
            });
        });
    });
    /**
     * DEPOSITS
     */
    // Handle v3Deposits. These decrement running balances from the origin chain equal to the inputAmount.
    // There should not be early deposits in v3.
    Object.entries(bundleV3Deposits).forEach(function (_a) {
        var depositsForChain = _a[1];
        Object.entries(depositsForChain).forEach(function (_a) {
            var deposits = _a[1];
            deposits.forEach(function (deposit) {
                // If the repayment token and repayment chain ID do not map to a PoolRebalanceRoute graph, then
                // there are no relevant L1 running balances.
                if (!clients.hubPoolClient.l2TokenHasPoolRebalanceRoute(deposit.inputToken, deposit.originChainId, mainnetBundleEndBlock)) {
                    return;
                }
                updateRunningBalanceForDeposit(runningBalances, clients.hubPoolClient, deposit, deposit.inputAmount.mul(-1), mainnetBundleEndBlock);
            });
        });
    });
    /**
     * REFUNDS FOR EXPIRED DEPOSITS
     */
    // Add origin chain running balance for expired v3 deposits. These should refund the inputAmount.
    Object.entries(expiredDepositsToRefundV3).forEach(function (_a) {
        var _originChainId = _a[0], depositsForChain = _a[1];
        var originChainId = Number(_originChainId);
        Object.entries(depositsForChain).forEach(function (_a) {
            var inputToken = _a[0], deposits = _a[1];
            deposits.forEach(function (deposit) {
                // If the repayment token and repayment chain ID do not map to a PoolRebalanceRoute graph, then
                // there are no relevant L1 running balances.
                var l1TokenCounterpart = clients.hubPoolClient.getL1TokenForL2TokenAtBlock(toAddressType(inputToken, originChainId), originChainId, mainnetBundleEndBlock);
                if (!l1TokenCounterpart) {
                    chainWithRefundsOnly.add(deposit.originChainId);
                    return;
                }
                updateRunningBalance(runningBalances, originChainId, l1TokenCounterpart.toEvmAddress(), deposit.inputAmount);
            });
        });
    });
    return { runningBalances: runningBalances, chainWithRefundsOnly: chainWithRefundsOnly, realizedLpFees: realizedLpFees };
}
//# sourceMappingURL=DataworkerUtils.js.map