"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.TransactionResponseAdapter = void 0;
const ethers_1 = require("ethers");
const LogService_1 = __importDefault(require("../../app/service/log/LogService"));
const TransactionResponseError_1 = require("./error/TransactionResponseError");
class TransactionResponseAdapter {
    manageResponse() {
        throw new Error("Method not implemented.");
    }
    static decodeFunctionResult(functionName, resultAsBytes, abi, // eslint-disable-line
    network) {
        try {
            const iface = new ethers_1.ethers.Interface(abi);
            if (!iface.getFunction(functionName)) {
                throw new TransactionResponseError_1.TransactionResponseError({
                    message: `Contract function ${functionName} not found in ABI, are you using the right version?`,
                    network: network,
                });
            }
            const resultHex = "0x".concat(Buffer.from(resultAsBytes).toString("hex"));
            const result = iface.decodeFunctionResult(functionName, resultHex);
            const jsonParsedArray = JSON.parse(JSON.stringify(result));
            return jsonParsedArray;
        }
        catch (error) {
            LogService_1.default.logError(error);
            throw new TransactionResponseError_1.TransactionResponseError({
                message: "Could not decode function result",
                network: network,
            });
        }
    }
}
exports.TransactionResponseAdapter = TransactionResponseAdapter;
