"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRefundsFromBundle = getRefundsFromBundle;
exports.prettyPrintV3SpokePoolEvents = prettyPrintV3SpokePoolEvents;
exports.getEndBlockBuffers = getEndBlockBuffers;
exports._buildPoolRebalanceRoot = _buildPoolRebalanceRoot;
exports._buildHistoricalPoolRebalanceRoot = _buildHistoricalPoolRebalanceRoot;
exports._buildOptimisticPoolRebalanceRoot = _buildOptimisticPoolRebalanceRoot;
exports._getMarginalRunningBalances = _getMarginalRunningBalances;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importDefault(require("assert"));
var utils_1 = require("../../../utils");
var PoolRebalanceUtils_1 = require("./PoolRebalanceUtils");
var MerkleTreeUtils_1 = require("./MerkleTreeUtils");
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;
            if (refunds === undefined) {
                return;
            }
            var refundsShallowCopy = tslib_1.__assign({}, refunds);
            if (combinedRefunds[repaymentChainId][l2TokenAddress] === undefined) {
                combinedRefunds[repaymentChainId][l2TokenAddress] = refundsShallowCopy;
            }
            else {
                combinedRefunds[repaymentChainId][l2TokenAddress] = tslib_1.__assign(tslib_1.__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 : utils_1.bnZero);
                }
            });
        });
    });
    return combinedRefunds;
}
function prettyPrintV3SpokePoolEvents(bundleDepositsV3, bundleFillsV3, bundleSlowFillsV3, expiredDepositsToRefundV3, unexecutableSlowFills) {
    return {
        bundleDepositsV3: (0, utils_1.count2DDictionaryValues)(bundleDepositsV3),
        bundleFillsV3: (0, utils_1.count3DDictionaryValues)(bundleFillsV3, "fills"),
        bundleSlowFillsV3: (0, utils_1.count2DDictionaryValues)(bundleSlowFillsV3),
        expiredDepositsToRefundV3: (0, utils_1.count2DDictionaryValues)(expiredDepositsToRefundV3),
        unexecutableSlowFills: (0, utils_1.count2DDictionaryValues)(unexecutableSlowFills),
    };
}
function getEndBlockBuffers(chainIdListForBundleEvaluationBlockNumbers, blockRangeEndBlockBuffer) {
    return chainIdListForBundleEvaluationBlockNumbers.map(function (chainId) { var _a; return (_a = blockRangeEndBlockBuffer[chainId]) !== null && _a !== void 0 ? _a : 0; });
}
function _buildPoolRebalanceRoot(latestMainnetBlock, mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients, maxL1TokenCountOverride) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (!(clients.hubPoolClient.hasPendingProposal() &&
                        clients.hubPoolClient.getPendingRootBundle().bundleEvaluationBlockNumbers[0] < mainnetBundleEndBlock)) return [3, 2];
                    return [4, _buildOptimisticPoolRebalanceRoot(latestMainnetBlock, mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients, maxL1TokenCountOverride)];
                case 1: return [2, _a.sent()];
                case 2: return [2, _buildHistoricalPoolRebalanceRoot(latestMainnetBlock, mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients, maxL1TokenCountOverride)];
            }
        });
    });
}
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;
    (0, PoolRebalanceUtils_1.addLastRunningBalance)(latestMainnetBlock, runningBalances, clients.hubPoolClient);
    var leaves = (0, PoolRebalanceUtils_1.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: (0, MerkleTreeUtils_1.buildPoolRebalanceLeafTree)(leaves),
    };
}
function _buildOptimisticPoolRebalanceRoot(latestMainnetBlock, mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients, maxL1TokenCountOverride) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var _a, runningBalances, realizedLpFees, chainWithRefundsOnly, mostRecentProposedRootBundle, blockRangesForChains, pendingRootBundleData, _b, leaves, tree, poolRebalanceLeaves;
        return tslib_1.__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 = (0, utils_1.getImpliedBundleBlockRanges)(clients.hubPoolClient, clients.configStoreClient, mostRecentProposedRootBundle);
                    return [4, 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;
                    (0, assert_1.default)(tree.getHexRoot() === mostRecentProposedRootBundle.poolRebalanceRoot);
                    Object.keys(runningBalances).forEach(function (_repaymentChainId) {
                        Object.keys(runningBalances[Number(_repaymentChainId)]).forEach(function (_l1TokenAddress) {
                            var repaymentChainId = Number(_repaymentChainId);
                            var l1TokenAddress = utils_1.EvmAddress.from(_l1TokenAddress);
                            var pendingPoolRebalanceLeaf = leaves.find(function (leaf) { return leaf.chainId === repaymentChainId && leaf.l1Tokens.some(function (l1Token) { return l1Token.eq(l1TokenAddress); }); });
                            if ((0, utils_1.isDefined)(pendingPoolRebalanceLeaf)) {
                                var pendingLeafTokenIdx = pendingPoolRebalanceLeaf.l1Tokens.findIndex(function (l1Token) {
                                    return l1Token.eq(l1TokenAddress);
                                });
                                (0, assert_1.default)(pendingLeafTokenIdx !== -1);
                                var pendingRunningBalanceAmount = pendingPoolRebalanceLeaf.runningBalances[pendingLeafTokenIdx];
                                if (!pendingRunningBalanceAmount.eq(utils_1.bnZero)) {
                                    (0, PoolRebalanceUtils_1.updateRunningBalance)(runningBalances, repaymentChainId, _l1TokenAddress, pendingRunningBalanceAmount);
                                }
                            }
                            else {
                                var lastExecutedBundleRunningBalance = clients.hubPoolClient.getRunningBalanceBeforeBlockForChain(latestMainnetBlock, repaymentChainId, l1TokenAddress).runningBalance;
                                if (!lastExecutedBundleRunningBalance.eq(utils_1.bnZero)) {
                                    (0, PoolRebalanceUtils_1.updateRunningBalance)(runningBalances, repaymentChainId, _l1TokenAddress, lastExecutedBundleRunningBalance);
                                }
                            }
                        });
                    });
                    poolRebalanceLeaves = (0, PoolRebalanceUtils_1.constructPoolRebalanceLeaves)(mainnetBundleEndBlock, runningBalances, realizedLpFees, Array.from(chainWithRefundsOnly).filter(function (chainId) { return !Object.keys(runningBalances).includes(chainId.toString()); }), clients.configStoreClient, maxL1TokenCountOverride);
                    return [2, {
                            runningBalances: runningBalances,
                            realizedLpFees: realizedLpFees,
                            leaves: poolRebalanceLeaves,
                            tree: (0, MerkleTreeUtils_1.buildPoolRebalanceLeafTree)(poolRebalanceLeaves),
                        }];
            }
        });
    });
}
function _getMarginalRunningBalances(mainnetBundleEndBlock, bundleV3Deposits, bundleFillsV3, bundleSlowFillsV3, unexecutableSlowFills, expiredDepositsToRefundV3, clients) {
    var runningBalances = {};
    var realizedLpFees = {};
    var chainWithRefundsOnly = new Set();
    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;
            var l1Token = clients.hubPoolClient.getL1TokenForL2TokenAtBlock((0, utils_1.toAddressType)(l2TokenAddress, repaymentChainId), repaymentChainId, mainnetBundleEndBlock);
            if (!l1Token) {
                chainWithRefundsOnly.add(repaymentChainId);
                return;
            }
            var l1TokenAddr = l1Token.toNative();
            (0, assert_1.default)(l1Token.isEVM(), "Expected an EVM address: ".concat(l1TokenAddr));
            (0, PoolRebalanceUtils_1.updateRunningBalance)(runningBalances, repaymentChainId, l1TokenAddr, totalRefundAmount);
            (0, PoolRebalanceUtils_1.updateRunningBalance)(realizedLpFees, repaymentChainId, l1TokenAddr, totalRealizedLpFee);
        });
    });
    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((0, utils_1.toAddressType)(outputToken, destinationChainId), destinationChainId, mainnetBundleEndBlock);
                (0, assert_1.default)((0, utils_1.isDefined)(l1TokenCounterpart), "getRefundInformationFromFill: l1TokenCounterpart is undefined");
                var lpFee = deposit.lpFeePct.mul(deposit.inputAmount).div(utils_1.fixedPointAdjustment);
                (0, PoolRebalanceUtils_1.updateRunningBalance)(runningBalances, destinationChainId, l1TokenCounterpart.toEvmAddress(), deposit.inputAmount.sub(lpFee));
            });
        });
    });
    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((0, utils_1.toAddressType)(outputToken, destinationChainId), destinationChainId, mainnetBundleEndBlock);
                (0, assert_1.default)((0, utils_1.isDefined)(l1TokenCounterpart), "getRefundInformationFromFill: l1TokenCounterpart is undefined");
                var lpFee = deposit.lpFeePct.mul(deposit.inputAmount).div(utils_1.fixedPointAdjustment);
                (0, PoolRebalanceUtils_1.updateRunningBalance)(runningBalances, destinationChainId, l1TokenCounterpart.toEvmAddress(), lpFee.sub(deposit.inputAmount));
            });
        });
    });
    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 (!clients.hubPoolClient.l2TokenHasPoolRebalanceRoute(deposit.inputToken, deposit.originChainId, mainnetBundleEndBlock)) {
                    return;
                }
                (0, PoolRebalanceUtils_1.updateRunningBalanceForDeposit)(runningBalances, clients.hubPoolClient, deposit, deposit.inputAmount.mul(-1), mainnetBundleEndBlock);
            });
        });
    });
    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) {
                var l1TokenCounterpart = clients.hubPoolClient.getL1TokenForL2TokenAtBlock((0, utils_1.toAddressType)(inputToken, originChainId), originChainId, mainnetBundleEndBlock);
                if (!l1TokenCounterpart) {
                    chainWithRefundsOnly.add(deposit.originChainId);
                    return;
                }
                (0, PoolRebalanceUtils_1.updateRunningBalance)(runningBalances, originChainId, l1TokenCounterpart.toEvmAddress(), deposit.inputAmount);
            });
        });
    });
    return { runningBalances: runningBalances, chainWithRefundsOnly: chainWithRefundsOnly, realizedLpFees: realizedLpFees };
}
//# sourceMappingURL=DataworkerUtils.js.map