"use strict";
// SPDX-License-Identifier: Apache-2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClearingOperations = void 0;
const ethers_1 = require("ethers");
const asset_tokenization_contracts_1 = require("@hashgraph/asset-tokenization-contracts");
const Constants_1 = require("../../../../core/Constants");
const Clearing_1 = require("../../../../domain/context/security/Clearing");
const LogService_1 = __importDefault(require("../../../../app/service/log/LogService"));
class ClearingOperations {
    executor;
    constructor(executor) {
        this.executor = executor;
    }
    async activateClearing(security, securityId) {
        LogService_1.default.logTrace(`Activate Clearing to address ${security.toString()}`);
        return this.executor.executeContractCall(securityId.toString(), new ethers_1.ethers.Interface(["function activateClearing()"]), "activateClearing", [], Constants_1.GAS.ACTIVATE_CLEARING);
    }
    async deactivateClearing(security, securityId) {
        LogService_1.default.logTrace(`Deactivate Clearing to address ${security.toString()}`);
        return this.executor.executeContractCall(securityId.toString(), new ethers_1.ethers.Interface(["function deactivateClearing()"]), "deactivateClearing", [], Constants_1.GAS.DEACTIVATE_CLEARING);
    }
    async clearingTransferByPartition(security, partitionId, amount, targetId, expirationDate, securityId) {
        LogService_1.default.logTrace(`Clearing Transfer By Partition to address ${security.toString()}`);
        const clearingOperation = {
            partition: partitionId,
            expirationTimestamp: expirationDate.toBigInt(),
            data: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingTransferFacet__factory.createInterface(), "clearingTransferByPartition", [clearingOperation, amount.toBigInt(), targetId.toString()], Constants_1.GAS.CLEARING_TRANSFER_BY_PARTITION);
    }
    async clearingTransferFromByPartition(security, partitionId, amount, sourceId, targetId, expirationDate, securityId) {
        LogService_1.default.logTrace(`Clearing Transfer From By Partition to address ${security.toString()}`);
        const clearingOperationFrom = {
            clearingOperation: {
                partition: partitionId,
                expirationTimestamp: expirationDate.toBigInt(),
                data: "0x",
            },
            from: sourceId.toString(),
            operatorData: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingTransferFacet__factory.createInterface(), "clearingTransferFromByPartition", [clearingOperationFrom, amount.toBigInt(), targetId.toString()], Constants_1.GAS.CLEARING_TRANSFER_FROM_BY_PARTITION);
    }
    async protectedClearingTransferByPartition(security, partitionId, amount, sourceId, targetId, expirationDate, deadline, nonce, signature, securityId) {
        LogService_1.default.logTrace(`Protected Clearing Transfer By Partition to address ${security.toString()}`);
        const protectedClearingOperation = {
            clearingOperation: {
                partition: partitionId,
                expirationTimestamp: expirationDate.toBigInt(),
                data: "0x",
            },
            from: sourceId.toString(),
            deadline: deadline.toBigInt(),
            nonce: nonce.toBigInt(),
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingTransferFacet__factory.createInterface(), "protectedClearingTransferByPartition", [protectedClearingOperation, amount.toBigInt(), targetId.toString(), signature], Constants_1.GAS.PROTECTED_CLEARING_TRANSFER_BY_PARTITION);
    }
    async approveClearingOperationByPartition(security, partitionId, targetId, clearingId, clearingOperationType, securityId) {
        LogService_1.default.logTrace(`Approve Clearing Operation By Partition to address ${security.toString()}`);
        const clearingOperationIdentifier = {
            partition: partitionId,
            tokenHolder: targetId.toString(),
            clearingOperationType: Clearing_1.CastClearingOperationType.toNumber(clearingOperationType),
            clearingId: clearingId,
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingActionsFacet__factory.createInterface(), "approveClearingOperationByPartition", [clearingOperationIdentifier], Constants_1.GAS.APPROVE_CLEARING_TRANSFER_BY_PARTITION);
    }
    async cancelClearingOperationByPartition(security, partitionId, targetId, clearingId, clearingOperationType, securityId) {
        LogService_1.default.logTrace(`Cancel Clearing Operation By Partition to address ${security.toString()}`);
        const clearingOperationIdentifier = {
            partition: partitionId,
            tokenHolder: targetId.toString(),
            clearingOperationType: Clearing_1.CastClearingOperationType.toNumber(clearingOperationType),
            clearingId: clearingId,
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingActionsFacet__factory.createInterface(), "cancelClearingOperationByPartition", [clearingOperationIdentifier], Constants_1.GAS.CANCEL_CLEARING_TRANSFER_BY_PARTITION);
    }
    async reclaimClearingOperationByPartition(security, partitionId, targetId, clearingId, clearingOperationType, securityId) {
        LogService_1.default.logTrace(`Reclaim Clearing Operation By Partition to address ${security.toString()}`);
        const clearingOperationIdentifier = {
            partition: partitionId,
            tokenHolder: targetId.toString(),
            clearingOperationType: Clearing_1.CastClearingOperationType.toNumber(clearingOperationType),
            clearingId: clearingId,
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingActionsFacet__factory.createInterface(), "reclaimClearingOperationByPartition", [clearingOperationIdentifier], Constants_1.GAS.RECLAIM_CLEARING_TRANSFER_BY_PARTITION);
    }
    async clearingRedeemByPartition(security, partitionId, amount, expirationDate, securityId) {
        LogService_1.default.logTrace(`Clearing Redeem By Partition to address ${security.toString()}`);
        const clearingOperation = {
            partition: partitionId,
            expirationTimestamp: expirationDate.toBigInt(),
            data: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingRedeemFacet__factory.createInterface(), "clearingRedeemByPartition", [clearingOperation, amount.toBigInt()], Constants_1.GAS.CLEARING_REDEEM_BY_PARTITION);
    }
    async clearingRedeemFromByPartition(security, partitionId, amount, sourceId, expirationDate, securityId) {
        LogService_1.default.logTrace(`Clearing Redeem From By Partition to address ${security.toString()}`);
        const clearingOperationFrom = {
            clearingOperation: {
                partition: partitionId,
                expirationTimestamp: expirationDate.toBigInt(),
                data: "0x",
            },
            from: sourceId.toString(),
            operatorData: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingRedeemFacet__factory.createInterface(), "clearingRedeemFromByPartition", [clearingOperationFrom, amount.toBigInt()], Constants_1.GAS.CLEARING_REDEEM_FROM_BY_PARTITION);
    }
    async protectedClearingRedeemByPartition(security, partitionId, amount, sourceId, expirationDate, deadline, nonce, signature, securityId) {
        LogService_1.default.logTrace(`Protected Clearing Redeem By Partition to address ${security.toString()}`);
        const protectedClearingOperation = {
            clearingOperation: {
                partition: partitionId,
                expirationTimestamp: expirationDate.toBigInt(),
                data: "0x",
            },
            from: sourceId.toString(),
            deadline: deadline.toBigInt(),
            nonce: nonce.toBigInt(),
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingRedeemFacet__factory.createInterface(), "protectedClearingRedeemByPartition", [protectedClearingOperation, amount.toBigInt(), signature], Constants_1.GAS.PROTECTED_CLEARING_REDEEM_BY_PARTITION);
    }
    async clearingCreateHoldByPartition(security, partitionId, escrowId, amount, targetId, clearingExpirationDate, holdExpirationDate, securityId) {
        LogService_1.default.logTrace(`Clearing Create Hold By Partition to address ${security.toString()}`);
        const clearingOperation = {
            partition: partitionId,
            expirationTimestamp: clearingExpirationDate.toBigInt(),
            data: "0x",
        };
        const hold = {
            amount: amount.toBigInt(),
            expirationTimestamp: holdExpirationDate.toBigInt(),
            escrow: escrowId.toString(),
            to: targetId.toString(),
            data: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingHoldCreationFacet__factory.createInterface(), "clearingCreateHoldByPartition", [clearingOperation, hold], Constants_1.GAS.CLEARING_CREATE_HOLD_BY_PARTITION);
    }
    async clearingCreateHoldFromByPartition(security, partitionId, escrowId, amount, sourceId, targetId, clearingExpirationDate, holdExpirationDate, securityId) {
        LogService_1.default.logTrace(`Clearing Create Hold From By Partition to address ${security.toString()}`);
        const clearingOperationFrom = {
            clearingOperation: {
                partition: partitionId,
                expirationTimestamp: clearingExpirationDate.toBigInt(),
                data: "0x",
            },
            from: sourceId.toString(),
            operatorData: "0x",
        };
        const hold = {
            amount: amount.toBigInt(),
            expirationTimestamp: holdExpirationDate.toBigInt(),
            escrow: escrowId.toString(),
            to: targetId.toString(),
            data: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingHoldCreationFacet__factory.createInterface(), "clearingCreateHoldFromByPartition", [clearingOperationFrom, hold], Constants_1.GAS.CLEARING_CREATE_HOLD_FROM_BY_PARTITION);
    }
    async protectedClearingCreateHoldByPartition(security, partitionId, amount, escrowId, sourceId, targetId, clearingExpirationDate, holdExpirationDate, deadline, nonce, signature, securityId) {
        LogService_1.default.logTrace(`Protected Clearing Create Hold By Partition to address ${security.toString()}`);
        const protectedClearingOperation = {
            clearingOperation: {
                partition: partitionId,
                expirationTimestamp: clearingExpirationDate.toBigInt(),
                data: "0x",
            },
            from: sourceId.toString(),
            deadline: deadline.toBigInt(),
            nonce: nonce.toBigInt(),
        };
        const hold = {
            amount: amount.toBigInt(),
            expirationTimestamp: holdExpirationDate.toBigInt(),
            escrow: escrowId.toString(),
            to: targetId.toString(),
            data: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingHoldCreationFacet__factory.createInterface(), "protectedClearingCreateHoldByPartition", [protectedClearingOperation, hold, signature], Constants_1.GAS.PROTECTED_CLEARING_CREATE_HOLD_BY_PARTITION);
    }
    async operatorClearingCreateHoldByPartition(security, partitionId, escrowId, amount, sourceId, targetId, clearingExpirationDate, holdExpirationDate, securityId) {
        LogService_1.default.logTrace(`Operator Clearing Create Hold By Partition to address ${security.toString()}`);
        const clearingOperationFrom = {
            clearingOperation: {
                partition: partitionId,
                expirationTimestamp: clearingExpirationDate.toBigInt(),
                data: "0x",
            },
            from: sourceId.toString(),
            operatorData: "0x",
        };
        const hold = {
            amount: amount.toBigInt(),
            expirationTimestamp: holdExpirationDate.toBigInt(),
            escrow: escrowId.toString(),
            to: targetId.toString(),
            data: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingHoldCreationFacet__factory.createInterface(), "operatorClearingCreateHoldByPartition", [clearingOperationFrom, hold], Constants_1.GAS.OPERATOR_CLEARING_CREATE_HOLD_BY_PARTITION);
    }
    async operatorClearingRedeemByPartition(security, partitionId, amount, sourceId, expirationDate, securityId) {
        LogService_1.default.logTrace(`Operator Clearing Redeem By Partition to address ${security.toString()}`);
        const clearingOperationFrom = {
            clearingOperation: {
                partition: partitionId,
                expirationTimestamp: expirationDate.toBigInt(),
                data: "0x",
            },
            from: sourceId.toString(),
            operatorData: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingRedeemFacet__factory.createInterface(), "operatorClearingRedeemByPartition", [clearingOperationFrom, amount.toBigInt()], Constants_1.GAS.OPERATOR_CLEARING_REDEEM_BY_PARTITION);
    }
    async operatorClearingTransferByPartition(security, partitionId, amount, sourceId, targetId, expirationDate, securityId) {
        LogService_1.default.logTrace(`Operator Clearing Transfer By Partition to address ${security.toString()}`);
        const clearingOperationFrom = {
            clearingOperation: {
                partition: partitionId,
                expirationTimestamp: expirationDate.toBigInt(),
                data: "0x",
            },
            from: sourceId.toString(),
            operatorData: "0x",
        };
        return this.executor.executeContractCall(securityId.toString(), asset_tokenization_contracts_1.ClearingTransferFacet__factory.createInterface(), "operatorClearingTransferByPartition", [clearingOperationFrom, amount.toBigInt(), targetId.toString()], Constants_1.GAS.OPERATOR_CLEARING_TRANSFER_BY_PARTITION);
    }
}
exports.ClearingOperations = ClearingOperations;
