"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dbg = exports.parseOverrideInfoFromDeliveryEvent = exports.parseForwardFailureError = exports.packOverrides = exports.redeliveryInstructionPrintable = exports.vaaKeyPrintable = exports.deliveryInstructionsPrintable = exports.executionInfoToString = exports.parseWormholeRelayerResend = exports.parseEVMExecutionInfoV1 = exports.parseWormholeRelayerSend = exports.createVaaKey = exports.parseWormholeRelayerPayloadType = exports.VaaKeyType = exports.parseRefundStatus = exports.RefundStatus = exports.DeliveryStatus = exports.ExecutionInfoVersion = exports.RelayerPayloadId = void 0;
const ethers_1 = require("ethers");
const utils_1 = require("ethers/lib/utils");
var RelayerPayloadId;
(function (RelayerPayloadId) {
    RelayerPayloadId[RelayerPayloadId["Delivery"] = 1] = "Delivery";
    RelayerPayloadId[RelayerPayloadId["Redelivery"] = 2] = "Redelivery";
})(RelayerPayloadId = exports.RelayerPayloadId || (exports.RelayerPayloadId = {}));
var ExecutionInfoVersion;
(function (ExecutionInfoVersion) {
    ExecutionInfoVersion[ExecutionInfoVersion["EVM_V1"] = 0] = "EVM_V1";
})(ExecutionInfoVersion = exports.ExecutionInfoVersion || (exports.ExecutionInfoVersion = {}));
var DeliveryStatus;
(function (DeliveryStatus) {
    DeliveryStatus["WaitingForVAA"] = "Waiting for VAA";
    DeliveryStatus["PendingDelivery"] = "Pending Delivery";
    DeliveryStatus["DeliverySuccess"] = "Delivery Success";
    DeliveryStatus["ReceiverFailure"] = "Receiver Failure";
    DeliveryStatus["ForwardRequestSuccess"] = "Forward Request Success";
    DeliveryStatus["ForwardRequestFailure"] = "Forward Request Failure";
    DeliveryStatus["ThisShouldNeverHappen"] = "This should never happen. Contact Support.";
    DeliveryStatus["DeliveryDidntHappenWithinRange"] = "Delivery didn't happen within given block range";
})(DeliveryStatus = exports.DeliveryStatus || (exports.DeliveryStatus = {}));
var RefundStatus;
(function (RefundStatus) {
    RefundStatus["RefundSent"] = "Refund Sent";
    RefundStatus["RefundFail"] = "Refund Fail";
    RefundStatus["CrossChainRefundSent"] = "Cross Chain Refund Sent";
    RefundStatus["CrossChainRefundFailProviderNotSupported"] = "Cross Chain Refund Fail - Provider does not support the refund chain";
    RefundStatus["CrossChainRefundFailNotEnough"] = "Cross Chain Refund Fail - Refund too low for cross chain refund";
})(RefundStatus = exports.RefundStatus || (exports.RefundStatus = {}));
function parseRefundStatus(index) {
    return index === 0
        ? RefundStatus.RefundSent
        : index === 1
            ? RefundStatus.RefundFail
            : index === 2
                ? RefundStatus.CrossChainRefundSent
                : index === 3
                    ? RefundStatus.CrossChainRefundFailProviderNotSupported
                    : index === 4
                        ? RefundStatus.CrossChainRefundFailNotEnough
                        : RefundStatus.CrossChainRefundFailProviderNotSupported;
}
exports.parseRefundStatus = parseRefundStatus;
var VaaKeyType;
(function (VaaKeyType) {
    VaaKeyType[VaaKeyType["EMITTER_SEQUENCE"] = 0] = "EMITTER_SEQUENCE";
    VaaKeyType[VaaKeyType["VAAHASH"] = 1] = "VAAHASH";
})(VaaKeyType = exports.VaaKeyType || (exports.VaaKeyType = {}));
function parseWormholeRelayerPayloadType(stringPayload) {
    const payload = typeof stringPayload === "string" ? utils_1.arrayify(stringPayload) : stringPayload;
    if (payload[0] != RelayerPayloadId.Delivery &&
        payload[0] != RelayerPayloadId.Redelivery) {
        throw new Error("Unrecognized payload type " + payload[0]);
    }
    return payload[0];
}
exports.parseWormholeRelayerPayloadType = parseWormholeRelayerPayloadType;
function createVaaKey(chainId, emitterAddress, sequence) {
    return {
        chainId,
        emitterAddress,
        sequence: ethers_1.ethers.BigNumber.from(sequence),
    };
}
exports.createVaaKey = createVaaKey;
function parseWormholeRelayerSend(bytes) {
    let idx = 0;
    const payloadId = bytes.readUInt8(idx);
    if (payloadId !== RelayerPayloadId.Delivery) {
        throw new Error(`Expected Delivery payload type (${RelayerPayloadId.Delivery}), found: ${payloadId}`);
    }
    idx += 1;
    const targetChainId = bytes.readUInt16BE(idx);
    idx += 2;
    const targetAddress = bytes.slice(idx, idx + 32);
    idx += 32;
    let payload;
    [payload, idx] = parsePayload(bytes, idx);
    const requestedReceiverValue = ethers_1.ethers.BigNumber.from(Uint8Array.prototype.subarray.call(bytes, idx, idx + 32));
    idx += 32;
    const extraReceiverValue = ethers_1.ethers.BigNumber.from(Uint8Array.prototype.subarray.call(bytes, idx, idx + 32));
    idx += 32;
    let encodedExecutionInfo;
    [encodedExecutionInfo, idx] = parsePayload(bytes, idx);
    const refundChainId = bytes.readUInt16BE(idx);
    idx += 2;
    const refundAddress = bytes.slice(idx, idx + 32);
    idx += 32;
    const refundDeliveryProvider = bytes.slice(idx, idx + 32);
    idx += 32;
    const sourceDeliveryProvider = bytes.slice(idx, idx + 32);
    idx += 32;
    const senderAddress = bytes.slice(idx, idx + 32);
    idx += 32;
    const numMessages = bytes.readUInt8(idx);
    idx += 1;
    let messages = [];
    for (let i = 0; i < numMessages; ++i) {
        const res = parseVaaKey(bytes, idx);
        idx = res[1];
        messages.push(res[0]);
    }
    return {
        targetChainId,
        targetAddress,
        payload,
        requestedReceiverValue,
        extraReceiverValue,
        encodedExecutionInfo,
        refundChainId,
        refundAddress,
        refundDeliveryProvider,
        sourceDeliveryProvider,
        senderAddress,
        vaaKeys: messages,
    };
}
exports.parseWormholeRelayerSend = parseWormholeRelayerSend;
function parsePayload(bytes, idx) {
    const length = bytes.readUInt32BE(idx);
    idx += 4;
    const payload = bytes.slice(idx, idx + length);
    idx += length;
    return [payload, idx];
}
function parseVaaKey(bytes, idx) {
    const version = bytes.readUInt8(idx);
    idx += 1;
    const chainId = bytes.readUInt16BE(idx);
    idx += 2;
    const emitterAddress = bytes.slice(idx, idx + 32);
    idx += 32;
    const sequence = ethers_1.ethers.BigNumber.from(Uint8Array.prototype.subarray.call(bytes, idx, idx + 8));
    idx += 8;
    return [
        {
            chainId,
            emitterAddress,
            sequence,
        },
        idx,
    ];
}
function parseEVMExecutionInfoV1(bytes, idx) {
    idx += 31;
    const version = bytes.readUInt8(idx);
    idx += 1;
    if (version !== ExecutionInfoVersion.EVM_V1) {
        throw new Error("Unexpected Execution Info version");
    }
    const gasLimit = ethers_1.ethers.BigNumber.from(Uint8Array.prototype.subarray.call(bytes, idx, idx + 32));
    idx += 32;
    const targetChainRefundPerGasUnused = ethers_1.ethers.BigNumber.from(Uint8Array.prototype.subarray.call(bytes, idx, idx + 32));
    idx += 32;
    return [{ gasLimit, targetChainRefundPerGasUnused }, idx];
}
exports.parseEVMExecutionInfoV1 = parseEVMExecutionInfoV1;
function parseWormholeRelayerResend(bytes) {
    let idx = 0;
    const payloadId = bytes.readUInt8(idx);
    if (payloadId !== RelayerPayloadId.Redelivery) {
        throw new Error(`Expected Delivery payload type (${RelayerPayloadId.Redelivery}), found: ${payloadId}`);
    }
    idx += 1;
    const parsedKey = parseVaaKey(bytes, idx);
    const key = parsedKey[0];
    idx = parsedKey[1];
    const targetChainId = bytes.readUInt16BE(idx);
    idx += 2;
    const newRequestedReceiverValue = ethers_1.ethers.BigNumber.from(Uint8Array.prototype.subarray.call(bytes, idx, idx + 32));
    idx += 32;
    let newEncodedExecutionInfo;
    [newEncodedExecutionInfo, idx] = parsePayload(bytes, idx);
    const newSourceDeliveryProvider = bytes.slice(idx, idx + 32);
    idx += 32;
    const newSenderAddress = bytes.slice(idx, idx + 32);
    idx += 32;
    return {
        deliveryVaaKey: key,
        targetChainId,
        newRequestedReceiverValue,
        newEncodedExecutionInfo,
        newSourceDeliveryProvider,
        newSenderAddress,
    };
}
exports.parseWormholeRelayerResend = parseWormholeRelayerResend;
function executionInfoToString(encodedExecutionInfo) {
    const [parsed] = parseEVMExecutionInfoV1(encodedExecutionInfo, 0);
    return `Gas limit: ${parsed.gasLimit}, Target chain refund per unit gas unused: ${parsed.targetChainRefundPerGasUnused}`;
}
exports.executionInfoToString = executionInfoToString;
function deliveryInstructionsPrintable(ix) {
    return {
        targetChainId: ix.targetChainId.toString(),
        targetAddress: ix.targetAddress.toString("hex"),
        payload: ix.payload.toString("base64"),
        requestedReceiverValue: ix.requestedReceiverValue.toString(),
        extraReceiverValue: ix.requestedReceiverValue.toString(),
        encodedExecutionInfo: executionInfoToString(ix.encodedExecutionInfo),
        refundChainId: ix.refundChainId.toString(),
        refundAddress: ix.refundAddress.toString("hex"),
        refundDeliveryProvider: ix.refundDeliveryProvider.toString("hex"),
        sourceDeliveryProvider: ix.sourceDeliveryProvider.toString("hex"),
        senderAddress: ix.senderAddress.toString("hex"),
        vaaKeys: ix.vaaKeys.map(vaaKeyPrintable),
    };
}
exports.deliveryInstructionsPrintable = deliveryInstructionsPrintable;
function vaaKeyPrintable(ix) {
    var _a, _b, _c;
    return {
        chainId: (_a = ix.chainId) === null || _a === void 0 ? void 0 : _a.toString(),
        emitterAddress: (_b = ix.emitterAddress) === null || _b === void 0 ? void 0 : _b.toString("hex"),
        sequence: (_c = ix.sequence) === null || _c === void 0 ? void 0 : _c.toString(),
    };
}
exports.vaaKeyPrintable = vaaKeyPrintable;
function redeliveryInstructionPrintable(ix) {
    return {
        deliveryVaaKey: vaaKeyPrintable(ix.deliveryVaaKey),
        targetChainId: ix.targetChainId.toString(),
        newRequestedReceiverValue: ix.newRequestedReceiverValue.toString(),
        newEncodedExecutionInfo: executionInfoToString(ix.newEncodedExecutionInfo),
        newSourceDeliveryProvider: ix.newSourceDeliveryProvider.toString("hex"),
        newSenderAddress: ix.newSenderAddress.toString("hex"),
    };
}
exports.redeliveryInstructionPrintable = redeliveryInstructionPrintable;
function packOverrides(overrides) {
    const packed = [
        ethers_1.ethers.utils.solidityPack(["uint8"], [1]).substring(2),
        ethers_1.ethers.utils
            .solidityPack(["uint256"], [overrides.newReceiverValue])
            .substring(2),
        ethers_1.ethers.utils
            .solidityPack(["uint32"], [overrides.newExecutionInfo.length])
            .substring(2),
        overrides.newExecutionInfo.toString("hex"),
        overrides.redeliveryHash.toString("hex"), //toString('hex') doesn't add the 0x prefix
    ].join("");
    return "0x" + packed;
}
exports.packOverrides = packOverrides;
function parseForwardFailureError(bytes) {
    let idx = 4;
    idx += 32;
    if (bytes.length <= idx) {
        return `Delivery Provider failed in performing forward`;
    }
    try {
        const amountOfFunds = ethers_1.ethers.BigNumber.from(Uint8Array.prototype.subarray.call(bytes, idx, idx + 32));
        idx += 32;
        const amountOfFundsNeeded = ethers_1.ethers.BigNumber.from(Uint8Array.prototype.subarray.call(bytes, idx, idx + 32));
        return `Not enough funds leftover for forward: Had ${ethers_1.ethers.utils.formatEther(amountOfFunds)} and needed ${ethers_1.ethers.utils.formatEther(amountOfFundsNeeded)}.`;
    }
    catch (err) {
        return `Delivery Provider unexpectedly failed in performing forward`;
    }
}
exports.parseForwardFailureError = parseForwardFailureError;
function parseOverrideInfoFromDeliveryEvent(bytes) {
    let idx = 0;
    const version = bytes.readUInt8(idx);
    idx += 1;
    const newReceiverValue = ethers_1.ethers.BigNumber.from(Uint8Array.prototype.subarray.call(bytes, idx, idx + 32));
    idx += 32;
    let newExecutionInfo;
    [newExecutionInfo, idx] = parsePayload(bytes, idx);
    const redeliveryHash = bytes.slice(idx, idx + 32);
    idx += 32;
    return {
        newReceiverValue,
        newExecutionInfo,
        redeliveryHash,
    };
}
exports.parseOverrideInfoFromDeliveryEvent = parseOverrideInfoFromDeliveryEvent;
/*
 * Helpers
 */
function dbg(x, msg) {
    if (msg) {
        console.log("[DEBUG] " + msg);
    }
    console.log(x);
    return x;
}
exports.dbg = dbg;
