// SPDX-License-Identifier: Apache-2.0
import { EventLog } from "ethers";
import LogService from "../../../app/service/log/LogService";
import TransactionResponse from "../../../domain/context/transaction/TransactionResponse";
import { TransactionResponseError } from "../../out/error/TransactionResponseError";
import { TransactionResponseAdapter } from "../../out/TransactionResponseAdapter";
export class RPCTransactionResponseAdapter extends TransactionResponseAdapter {
    static async manageResponse(response, network, eventName) {
        LogService.logTrace("Constructing response from:", response);
        try {
            const receipt = await response.wait();
            LogService.logTrace("Receipt:", receipt);
            if (receipt && receipt.logs && eventName) {
                const returnEvent = receipt.logs.filter((e) => e instanceof EventLog && e.eventName === eventName);
                if (returnEvent.length > 0 && returnEvent[0].args) {
                    return new TransactionResponse(receipt.hash, returnEvent[0].args);
                }
            }
            return Promise.resolve(new TransactionResponse(receipt?.hash ?? "", receipt?.status ?? 0));
        }
        catch (error) {
            LogService.logError("Uncaught Exception:", JSON.stringify(error));
            throw new TransactionResponseError({
                message: "",
                network: network,
                name: eventName,
                status: "error",
                transactionId: error?.transactionHash,
                RPC_relay: true,
            });
        }
    }
}
