"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventNameToContractNameMap = exports.ConfigContractName = exports.ContractFileName = exports.ContractName = void 0;
exports.initContract = initContract;
exports.getAbi = getAbi;
exports.isKnownOrderId = isKnownOrderId;
const tslib_1 = require("tslib");
const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const logger_1 = require("./logger");
const ethers_1 = require("ethers");
const types_1 = require("../types");
var ContractName;
(function (ContractName) {
    ContractName["AttestationBatchMaker"] = "AttestationBatchMaker";
    ContractName["AttestationBatchMakerV2"] = "AttestationBatchMakerV2";
    ContractName["AttestationBatchMakerV3"] = "AttestationBatchMakerV3";
    ContractName["AttestationDLQ"] = "AttestationDLQFactory";
    ContractName["AttestationsVerifierProofs"] = "AttestationsVerifierProofs";
    ContractName["avpBatchSubmitter"] = "avpBatchSubmitter";
    ContractName["ClaimerGMP"] = "ClaimerGMP";
    ContractName["ClaimerGMPV2"] = "ClaimerGMPV2";
    ContractName["AttestersPortal"] = "AttestersPortal";
    ContractName["BRN"] = "BRN";
    ContractName["BRN2TRN"] = "BRN2TRN";
    ContractName["BRNBalanceGetter"] = "BRNBalanceGetter";
    ContractName["BRNRewards"] = "BRNRewards";
    ContractName["BiddingBook"] = "BiddingBook";
    ContractName["PendingBook"] = "PendingBook";
    ContractName["ConfirmationBook"] = "ConfirmationBook";
    ContractName["ERC20Mock"] = "ERC20Mock";
    ContractName["EscrowGMP"] = "EscrowGMP";
    ContractName["EscrowOrder"] = "EscrowOrder";
    ContractName["EscrowOrderBook"] = "EscrowOrderBook";
    ContractName["OpenMarketPricer"] = "OpenMarketPricer";
    ContractName["OrderBook"] = "OrderBook";
    ContractName["RemoteOrder"] = "RemoteOrder";
    ContractName["RemoteOrderV2"] = "RemoteOrderV2";
    ContractName["ParachainRemoteOrder"] = "ParachainRemoteOrder";
    ContractName["PayloadGetter"] = "PayloadGetter";
    ContractName["BonusesL3"] = "BonusesL3";
    ContractName["SegmentBook"] = "SegmentBook";
    ContractName["SingleAttestationBook"] = "SingleAttestationBook";
    ContractName["SingleAttestationBookV2"] = "SingleAttestationBookV2";
    ContractName["SingleAttestationBookV3"] = "SingleAttestationBookV3";
    ContractName["SingleAttestationBookV4"] = "SingleAttestationBookV4";
    ContractName["LongRangeAttestationsBook"] = "LongRangeAttestationsBook";
    ContractName["PendingAttestationsBook"] = "PendingAttestationsBook";
    ContractName["TRN"] = "TRN";
    ContractName["TRNInflation"] = "TRNInflation";
    ContractName["TRNInflationRewards"] = "TRNInflationRewards";
    ContractName["XDNS"] = "XDNS";
    ContractName["t3BTC"] = "t3BTC";
    ContractName["t3DOT"] = "t3DOT";
    ContractName["t3SOL"] = "t3SOL";
    ContractName["t3USD"] = "t3USD";
    ContractName["t3TIA"] = "t3TIA";
    ContractName["t3ETH"] = "t3ETH";
    ContractName["t3SEI"] = "t3SEI";
    ContractName["t3MON"] = "t3MON";
    ContractName["tERC20"] = "tERC20";
})(ContractName || (exports.ContractName = ContractName = {}));
exports.ContractFileName = {
    AttestationBatchMaker: 'attestationBatchMaker',
    AttestationBatchMakerV2: 'attestationBatchMakerV2',
    AttestationBatchMakerV3: 'attestationBatchMakerV3',
    SingleAttestationBookV4: 'singleAttestationBookV4',
    LongRangeAttestationsBook: 'longRangeAttestationsBook',
    AttestationDLQFactory: 'attestationDLQ',
    AttestationsVerifierProofs: 'attestationsVerifierProofs',
    avpBatchSubmitter: 'avpBatchSubmitter',
    ClaimerGMP: 'claimerGMP',
    BonusesL3: 'bonusesL3',
    ClaimerGMPV2: 'claimerGMPV2',
    ParachainRemoteOrder: 'parachainRemoteOrder',
    AttestersPortal: 'attestersPortal',
    BRN2TRN: 'BRN2TRN',
    BRN: 'BRN',
    BRNRewards: 'BRNRewards',
    BRNBalanceGetter: 'BRNBalanceGetter',
    BiddingBook: 'biddingBook',
    ConfirmationBook: 'confirmationBook',
    ERC20Mock: 'ERC20Mock',
    EscrowGMP: 'escrowGMP',
    EscrowOrder: 'escrowOrder',
    EscrowOrderBook: 'escrowOrderBook',
    OpenMarketPricer: 'openMarketPricer',
    OrderBook: 'orderBook',
    PendingBook: 'pendingBook',
    RemoteOrder: 'remoteOrder',
    RemoteOrderV2: 'remoteOrderV2',
    SegmentBook: 'segmentBook',
    SingleAttestationBook: 'singleAttestationBook',
    SingleAttestationBookV2: 'singleAttestationBookV2',
    SingleAttestationBookV3: 'singleAttestationBookV3',
    PendingAttestationsBook: 'pendingAttestationsBook',
    PayloadGetter: 'payloadGetter',
    TRN: 'TRN',
    TRNInflation: 'TRNInflation',
    TRNInflationRewards: 'TRNInflationRewards',
    XDNS: 'XDNS',
    t3BTC: 't3BTC',
    t3DOT: 't3DOT',
    t3SOL: 't3SOL',
    t3USD: 't3USD',
    t3TIA: 't3TIA',
    t3ETH: 't3ETH',
    t3MON: 't3MON',
    t3SEI: 't3SEI',
    tERC20: 'tERC20',
};
var ConfigContractName;
(function (ConfigContractName) {
    ConfigContractName["BRN"] = "BRN";
    ConfigContractName["BRN2TRN"] = "BRN2TRN";
    ConfigContractName["BRNRewards"] = "BRNRewards";
    ConfigContractName["BRNBalanceGetter"] = "BRNBalanceGetter";
    ConfigContractName["DistributionAttempt"] = "DistributionAttempt";
    ConfigContractName["ERC20Mock"] = "ERC20Mock";
    ConfigContractName["TRN"] = "TRN";
    ConfigContractName["TRNInflation"] = "TRNInflation";
    ConfigContractName["TRNInflationRewards"] = "TRNInflationRewards";
    ConfigContractName["XDNS"] = "XDNS";
    ConfigContractName["attestationBatchMaker"] = "attestationBatchMaker";
    ConfigContractName["avpBatchSubmitter"] = "avpBatchSubmitter";
    ConfigContractName["attestationBatchMakerV2"] = "attestationBatchMakerV2";
    ConfigContractName["attestationBatchMakerV3"] = "attestationBatchMakerV3";
    ConfigContractName["attestationDLQ"] = "attestationDLQ";
    ConfigContractName["attesters"] = "attesters";
    ConfigContractName["attestersPortal"] = "attestersPortal";
    ConfigContractName["pendingBook"] = "pendingBook";
    ConfigContractName["biddingBook"] = "biddingBook";
    ConfigContractName["claimerGMP"] = "claimerGMP";
    ConfigContractName["claimerGMPV2"] = "claimerGMPV2";
    ConfigContractName["confirmationBook"] = "confirmationBook";
    ConfigContractName["escrowGMP"] = "escrowGMP";
    ConfigContractName["escrowOrder"] = "escrowOrder";
    ConfigContractName["escrowOrderBook"] = "escrowOrderBook";
    ConfigContractName["localExchange"] = "localExchange";
    ConfigContractName["bonusesL3"] = "bonusesL3";
    ConfigContractName["openMarketPricer"] = "openMarketPricer";
    ConfigContractName["orderBook"] = "orderBook";
    ConfigContractName["payloadGetter"] = "payloadGetter";
    ConfigContractName["remoteOrder"] = "remoteOrder";
    ConfigContractName["remoteOrderV2"] = "remoteOrderV2";
    ConfigContractName["segmentBook"] = "segmentBook";
    ConfigContractName["singleAttestationBook"] = "singleAttestationBook";
    ConfigContractName["singleAttestationBookV2"] = "singleAttestationBookV2";
    ConfigContractName["singleAttestationBookV3"] = "singleAttestationBookV3";
    ConfigContractName["pendingAttestationsBook"] = "pendingAttestationsBook";
    ConfigContractName["singleAttestationBookV4"] = "singleAttestationBookV4";
    ConfigContractName["longRangeAttestationsBook"] = "longRangeAttestationsBook";
    ConfigContractName["t3BTC"] = "t3BTC";
    ConfigContractName["t3DOT"] = "t3DOT";
    ConfigContractName["t3SOL"] = "t3SOL";
    ConfigContractName["t3USD"] = "t3USD";
    ConfigContractName["tERC20"] = "tERC20";
    ConfigContractName["t3ETH"] = "t3ETH";
    ConfigContractName["t3TIA"] = "t3TIA";
    ConfigContractName["t3MON"] = "t3MON";
    ConfigContractName["t3SEI"] = "t3SEI";
})(ConfigContractName || (exports.ConfigContractName = ConfigContractName = {}));
exports.EventNameToContractNameMap = {
    [types_1.EventName.OrderCreated]: ConfigContractName.remoteOrder,
    [types_1.EventName.RemoteOrderCreated]: ConfigContractName.orderBook,
    [types_1.EventName.EscrowOrderCreated]: ConfigContractName.escrowOrder,
    [types_1.EventName.L3EscrowOrderCreated]: ConfigContractName.escrowOrderBook,
    [types_1.EventName.BidReceived]: ConfigContractName.biddingBook,
    [types_1.EventName.BRNDistributed]: ConfigContractName.BRNRewards,
    [types_1.EventName.DistributionAttempt]: ConfigContractName.DistributionAttempt,
    [types_1.EventName.BatchError]: ConfigContractName.attestationBatchMaker,
    [types_1.EventName.TRNInflationDistributed]: ConfigContractName.biddingBook,
    [types_1.EventName.BRNRewardsDistributed]: ConfigContractName.biddingBook,
    [types_1.EventName.InsuranceClaimed]: ConfigContractName.biddingBook,
    [types_1.EventName.OrderAttested]: ConfigContractName.singleAttestationBook,
    [types_1.EventName.Confirmation]: ConfigContractName.remoteOrder,
    [types_1.EventName.ConfirmationBatch]: ConfigContractName.avpBatchSubmitter,
    [types_1.EventName.ConfirmationAttest]: ConfigContractName.attesters,
    [types_1.EventName.TransferCommitApplied]: ConfigContractName.attesters,
    [types_1.EventName.BatchApplied]: ConfigContractName.attesters,
    [types_1.EventName.TransferRevertApplied]: ConfigContractName.attesters,
    [types_1.EventName.Claimed]: ConfigContractName.remoteOrder,
    [types_1.EventName.ClaimedRefund]: ConfigContractName.remoteOrder,
    [types_1.EventName.ClaimedV1]: ConfigContractName.avpBatchSubmitter,
    [types_1.EventName.ClaimedRefundV1]: ConfigContractName.avpBatchSubmitter,
    [types_1.EventName.AlreadyApplied]: ConfigContractName.attesters,
    [types_1.EventName.OutbidReceived]: ConfigContractName.biddingBook,
    [types_1.EventName.InsuranceSet]: ConfigContractName.biddingBook,
    [types_1.EventName.NonRefundable]: ConfigContractName.avpBatchSubmitter,
    [types_1.EventName.NonClaimable]: ConfigContractName.avpBatchSubmitter,
    [types_1.EventName.GMPExpectedPayloadNotMatched]: ConfigContractName.escrowGMP,
    [types_1.EventName.OrderAttestationDuplicatedOrAboveLimit]: ConfigContractName.confirmationBook,
    [types_1.EventName.BatchConstructed]: ConfigContractName.singleAttestationBook,
    [types_1.EventName.AddPendingAttestationToUnknownOrder]: ConfigContractName.longRangeAttestationsBook,
    [types_1.EventName.GMPMismatch]: ConfigContractName.longRangeAttestationsBook,
    [types_1.EventName.ExecutorMismatch]: ConfigContractName.singleAttestationBookV4,
    [types_1.EventName.NewOrderAttested]: ConfigContractName.longRangeAttestationsBook,
    [types_1.EventName.RevertOrderApproved]: ConfigContractName.payloadGetter,
    [types_1.EventName.NewPayloadMemo]: ConfigContractName.payloadGetter,
    [types_1.EventName.RevertOrderRevertWithCommitPrevented]: ConfigContractName.payloadGetter,
    [types_1.EventName.SegmentReceived]: ConfigContractName.segmentBook,
    [types_1.EventName.NewBatchAttested]: ConfigContractName.segmentBook,
    [types_1.EventName.ClaimedBatch]: ConfigContractName.avpBatchSubmitter,
    [types_1.EventName.OrderGasConsumed]: ConfigContractName.remoteOrder,
    [types_1.EventName.ExecuteGasConsumed]: ConfigContractName.remoteOrder,
    [types_1.EventName.ExecutedBatchGasConsumed]: ConfigContractName.avpBatchSubmitter,
};
/**
 * Initializes a contract instance
 *
 * @param {ContractName}  contractName
 * @param {string}  address
 * @param {Wallet | CustomJsonRpcProvider}  signer
 *
 * @returns {Contract}
 */
function initContract(contractName, address, signer) {
    const fileName = exports.ContractFileName[contractName];
    const abi = getAbi(fileName, contractName);
    return new ethers_1.Contract(address, abi, signer);
}
/**
 * Load the ABI of a contract, parse it as JSON and return.
 * Note: file of a contract can have different case than a contract
 *
 * @param {string}  fileName  The Solidity contract file name
 * @param {string}  contractName  The ABI file name
 *
 * @returns {ContractInterface}
 */
function getAbi(fileName, contractName) {
    // eslint-disable-next-line unicorn/prefer-module
    const rootDir = node_path_1.default.resolve(__dirname, '..', '..');
    const abiFilePath = node_path_1.default.join(rootDir, 'artifacts', 'contracts', `${fileName}.sol`, `${contractName}.json`);
    logger_1.logger.trace({ abiFilePath }, `📖 Loading ABI for ${contractName}`);
    try {
        const contractJSON = JSON.parse(node_fs_1.default.readFileSync(abiFilePath, 'utf8'));
        return contractJSON.abi;
    }
    catch {
        logger_1.logger.error({ abiFilePath }, `❌ Failed to load ABI for ${contractName}`);
        throw new Error(`Failed to load ABI for ${contractName} from ${abiFilePath}`);
    }
}
async function isKnownOrderId(orderId, remoteOrderContract) {
    let isKnown = false;
    try {
        isKnown = await remoteOrderContract?.isKnownId(orderId);
        return isKnown;
    }
    catch (error) {
        logger_1.logger.error({ error }, 'Error while checking if order is known');
        return false;
    }
}
//# sourceMappingURL=contract.js.map