"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.RPCTransactionResponseAdapter = void 0;
const ethers_1 = require("ethers");
const LogService_1 = __importDefault(require("../../../app/service/log/LogService"));
const TransactionResponse_1 = __importDefault(require("../../../domain/context/transaction/TransactionResponse"));
const TransactionResponseError_1 = require("../../out/error/TransactionResponseError");
const TransactionResponseAdapter_1 = require("../../out/TransactionResponseAdapter");
class RPCTransactionResponseAdapter extends TransactionResponseAdapter_1.TransactionResponseAdapter {
    static async manageResponse(response, network, eventName) {
        LogService_1.default.logTrace("Constructing response from:", response);
        try {
            const receipt = await response.wait();
            LogService_1.default.logTrace("Receipt:", receipt);
            if (receipt && receipt.logs && eventName) {
                const returnEvent = receipt.logs.filter((e) => e instanceof ethers_1.EventLog && e.eventName === eventName);
                if (returnEvent.length > 0 && returnEvent[0].args) {
                    return new TransactionResponse_1.default(receipt.hash, returnEvent[0].args);
                }
            }
            return Promise.resolve(new TransactionResponse_1.default(receipt?.hash ?? "", receipt?.status ?? 0));
        }
        catch (error) {
            LogService_1.default.logError("Uncaught Exception:", JSON.stringify(error));
            throw new TransactionResponseError_1.TransactionResponseError({
                message: "",
                network: network,
                name: eventName,
                status: "error",
                transactionId: error?.transactionHash,
                RPC_relay: true,
            });
        }
    }
}
exports.RPCTransactionResponseAdapter = RPCTransactionResponseAdapter;
