"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderStatus = exports.GMPByteRaw = void 0;
exports.rawOrderToDBOrder = rawOrderToDBOrder;
exports.orderToOrderL3 = orderToOrderL3;
exports.dbOrderToRawOrder = dbOrderToRawOrder;
exports.createNewEmptyOrder = createNewEmptyOrder;
exports.checkMissingOrderFields = checkMissingOrderFields;
exports.generateId = generateId;
const ethers_1 = require("ethers");
const helpers_1 = require("../utils/helpers");
const enums_1 = require("../pricer/enums");
function rawOrderToDBOrder(order) {
    return {
        ...order,
        amount: order.amount.toString(),
        insurance: order.insurance.toString(),
        maxReward: order.maxReward.toString(),
        profit: order.profit.toString(),
        confirmedAmount: order.confirmedAmount ? order.confirmedAmount.toString() : undefined,
        executor: order.executor.toLowerCase(),
        sourceAccount: order.sourceAccount.toLowerCase(),
        targetAccount: order.targetAccount.toLowerCase(),
    };
}
function orderToOrderL3(order) {
    return {
        destination: (0, helpers_1.encodeNetworkId)(order.destination),
        source: (0, helpers_1.encodeNetworkId)(order.source),
        asset: order.destinationAssetCode || enums_1.assetNamePriceProviderToAssetNumberT3rn.eth,
        targetAccount: ethers_1.ethers.utils.hexZeroPad(order.targetAccount, 32),
        amount: order.amount,
        rewardAsset: order.destinationAsset,
        maxReward: order.maxReward,
        nonce: order.nonce,
        sourceAccount: order.sourceAccount,
        orderTimestamp: order.orderTimestamp,
    };
}
function dbOrderToRawOrder(order) {
    return {
        ...order,
        assetNative: order.destinationAsset === ethers_1.ethers.constants.AddressZero && order.sourceAsset === ethers_1.ethers.constants.AddressZero,
        amount: ethers_1.BigNumber.from(order.amount),
        insurance: ethers_1.BigNumber.from(order.insurance),
        maxReward: ethers_1.BigNumber.from(order.maxReward),
        profit: ethers_1.BigNumber.from(order.profit),
        confirmedAmount: order.confirmedAmount ? ethers_1.BigNumber.from(order.confirmedAmount) : undefined,
        executor: order.executor !== '' ? ethers_1.ethers.utils.getAddress(order.executor) : order.executor,
        sourceAccount: ethers_1.ethers.utils.getAddress(order.sourceAccount),
        targetAccount: ethers_1.ethers.utils.getAddress(order.targetAccount),
    };
}
function createNewEmptyOrder() {
    return {
        amountUSD: 0,
        attestationTimestamp: 0,
        batchHash: undefined,
        bidTimestamp: 0,
        bidTxHash: undefined,
        claimTimestamp: 0,
        claimTxHash: undefined,
        confirmationGasFee: '',
        confirmationGasUSD: 0,
        confirmationTimestamp: 0,
        confirmationTxHash: undefined,
        destinationAsset: '',
        insuranceTxHash: undefined,
        insuranceUSD: 0,
        maxRewardUSD: 0,
        orderCreatedGasFee: '',
        orderCreatedGasUSD: 0,
        refundAmount: '0',
        orderTimestampTtl: 0,
        partitionKey: '',
        profitUSD: 0,
        refundTimestamp: 0,
        refundTxHash: undefined,
        verifiedExecution: undefined,
        verifiedExecutionL3: undefined,
        id: '',
        source: 'zero',
        destination: 'zero',
        sourceAsset: '',
        targetAccount: '',
        amount: ethers_1.BigNumber.from(0),
        insurance: ethers_1.BigNumber.from(0),
        maxReward: ethers_1.BigNumber.from(0),
        nonce: 0,
        sourceAccount: '',
        orderTimestamp: 0,
        txHash: '',
        profit: ethers_1.BigNumber.from(0),
        executor: '',
        status: OrderStatus.Placed,
        assetNative: true,
        confirmedAmount: undefined,
        claimGasUSD: 0,
        sourceGasFeesUSD: 0,
        destinationGasFeesUSD: 0,
        protocolFeeInUSD: 0,
        executorFeeUSD: 0,
        refundAmountUSD: 0,
        refundGasUSD: 0,
        totalFeesUSD: 0,
    };
}
function checkMissingOrderFields(order) {
    const requiredOrderFields = Object.keys(createNewEmptyOrder());
    return requiredOrderFields.filter((key) => !Object.prototype.hasOwnProperty.call(order, key));
}
var GMPByteRaw;
(function (GMPByteRaw) {
    GMPByteRaw["commit"] = "0x00";
    GMPByteRaw["revert"] = "0x01";
    GMPByteRaw["commitOpenMarket"] = "0x05";
})(GMPByteRaw || (exports.GMPByteRaw = GMPByteRaw = {}));
var OrderStatus;
(function (OrderStatus) {
    OrderStatus["Placed"] = "Placed";
    OrderStatus["Pending"] = "Pending";
    OrderStatus["Bid"] = "Bid";
    OrderStatus["Executed"] = "Executed";
    OrderStatus["Expired"] = "Expired";
    OrderStatus["MissingExecutor"] = "MissingExecutor";
    OrderStatus["MissingConfirmation"] = "MissingConfirmation";
    OrderStatus["MissingOrderTimestamp"] = "MissingOrderTimestamp";
    OrderStatus["MissingOrderMaxReward"] = "MissingOrderMaxReward";
    OrderStatus["AttestedL3"] = "AttestedL3";
    OrderStatus["AttestedUnsupportedNetwork"] = "AttestedUnsupportedNetwork";
    OrderStatus["PlacedUnsupportedNetwork"] = "PlacedUnsupportedNetwork";
    OrderStatus["Attested"] = "Attested";
    OrderStatus["Claimed"] = "Claimed";
    OrderStatus["ClaimedInsurance"] = "ClaimedInsurance";
    OrderStatus["ProcessInsurance"] = "ProcessInsurance";
    OrderStatus["ConfirmedRefund"] = "ConfirmedRefund";
    OrderStatus["PendingRefund"] = "PendingRefund";
    OrderStatus["RefundAttested"] = "RefundAttested";
    OrderStatus["RefundZeroAddress"] = "RefundZeroAddress";
    OrderStatus["PendingRefundWaiting"] = "PendingRefundWaiting";
    OrderStatus["InvalidPendingRefund"] = "InvalidPendingRefund";
    OrderStatus["InvalidAttestation"] = "InvalidAttestation";
    OrderStatus["NonClaimable"] = "NonClaimable";
    OrderStatus["InvalidMissingL2"] = "InvalidMissingL2";
    OrderStatus["ClaimedRefund"] = "ClaimedRefund";
    OrderStatus["GMPUnmoved"] = "GMPUnmoved";
    OrderStatus["GMPReAttest"] = "GMPReAttest";
    OrderStatus["GMPMismatch"] = "GMPMismatch";
    OrderStatus["UnknownPossibleMigration"] = "UnknownPossibleMigration";
    OrderStatus["Broken"] = "Broken";
    OrderStatus["UnRefundable"] = "UnRefundable";
    OrderStatus["ToRepatriate"] = "ToRepatriate";
    OrderStatus["Repatriated"] = "Repatriated";
    OrderStatus["Duplicated"] = "Duplicated";
    OrderStatus["PendingIngress"] = "PendingIngress";
    OrderStatus["ResolvedIngress"] = "ResolvedIngress";
    OrderStatus["PendingEgress"] = "PendingEgress";
    OrderStatus["ResolvedEgress"] = "ResolvedEgress";
    OrderStatus["AppliedEgress"] = "AppliedEgress";
})(OrderStatus || (exports.OrderStatus = OrderStatus = {}));
function generateId(addr, nonce) {
    const xtx_id = ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.defaultAbiCoder.encode(['address', 'uint32', 'bytes4'], [addr, nonce, '0x00000000']));
    return ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.defaultAbiCoder.encode(['bytes32', 'bytes32'], [xtx_id, '0x0000000000000000000000000000000000000000000000000000000000000000']));
}
//# sourceMappingURL=types.js.map