"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetaTxMixin = void 0;
const common_1 = require("@bosonprotocol/common");
const _1 = require(".");
const subgraph_1 = require("../offers/subgraph");
const base_core_sdk_1 = require("./../mixins/base-core-sdk");
const __1 = require("..");
class MetaTxMixin extends base_core_sdk_1.BaseCoreSDK {
    // Implementation
    signMetaTx(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTx(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTx(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCreateSeller(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCreateSeller(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCreateSeller(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxUpdateSeller(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxUpdateSeller(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxUpdateSeller(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxOptInToSellerUpdate(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxOptInToSellerUpdate(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxOptInToSellerUpdate(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    signMetaTxUpdateSellerAndOptIn(sellerUpdates) {
        return __awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            let nonce = Date.now();
            const updateMetaTx = yield this.signMetaTxUpdateSeller({
                updateSellerArgs: sellerUpdates,
                nonce,
                theGraphStorage: this._theGraphStorage,
                metadataStorage: this._metadataStorage
            });
            const updateTx = yield this.relayMetaTransaction({
                functionName: updateMetaTx.functionName,
                functionSignature: updateMetaTx.functionSignature,
                sigR: updateMetaTx.r,
                sigS: updateMetaTx.s,
                sigV: updateMetaTx.v,
                nonce
            });
            yield updateTx.wait();
            let seller;
            let count = 200;
            while ((!seller || !seller.pendingSeller) && count-- > 0) {
                yield new Promise((resolve) => setTimeout(resolve, 300));
                seller = yield this["getSellerById"](sellerUpdates.id);
            }
            if (!seller) {
                throw new Error("[signMetaTxUpdateSellerAndOptIn] seller could not be retrieved in time");
            }
            const pendingSellerUpdate = seller.pendingSeller;
            if (!pendingSellerUpdate) {
                throw new Error("[signMetaTxUpdateSellerAndOptIn] seller.pendingSeller could not be retrieved in time");
            }
            // Find all updates that can be opted in by the current account
            const currentAccount = (yield this._web3Lib.getSignerAddress()).toLowerCase();
            const fieldsToUpdate = {
                assistant: currentAccount === ((_a = pendingSellerUpdate.assistant) === null || _a === void 0 ? void 0 : _a.toLowerCase()),
                admin: currentAccount === ((_b = pendingSellerUpdate.admin) === null || _b === void 0 ? void 0 : _b.toLowerCase()),
                authToken: pendingSellerUpdate.authTokenType !== undefined &&
                    pendingSellerUpdate.authTokenType !== null &&
                    pendingSellerUpdate.authTokenType !== common_1.AuthTokenType.NONE
            };
            if (fieldsToUpdate.assistant ||
                fieldsToUpdate.admin ||
                fieldsToUpdate.authToken) {
                nonce = Date.now();
                const optInMetaTx = yield this.signMetaTxOptInToSellerUpdate({
                    optInToSellerUpdateArgs: {
                        id: sellerUpdates.id,
                        fieldsToUpdate: {
                            assistant: currentAccount === pendingSellerUpdate.assistant.toLowerCase(),
                            admin: currentAccount === pendingSellerUpdate.admin.toLowerCase(),
                            authToken: pendingSellerUpdate.authTokenType !== common_1.AuthTokenType.NONE
                        }
                    },
                    nonce
                });
                return this.relayMetaTransaction({
                    functionName: optInMetaTx.functionName,
                    functionSignature: optInMetaTx.functionSignature,
                    sigR: optInMetaTx.r,
                    sigS: optInMetaTx.s,
                    sigV: optInMetaTx.v,
                    nonce
                });
            }
            // If there is nothing to optIn from the current account, then return the response from updateSeller
            return updateTx;
        });
    }
    // Implementation
    signMetaTxCreateOffer(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCreateOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCreateOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCreateOfferBatch(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCreateOfferBatch(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCreateOfferBatch(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCreateGroup(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCreateGroup(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCreateGroup(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxReserveRange(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const offer = yield (0, subgraph_1.getOfferById)(this._subgraphUrl, args.offerId);
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxReserveRange(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { to: args.to === "contract"
                        ? offer.seller.voucherCloneAddress
                        : offer.seller.assistant, returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxReserveRange(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { to: args.to === "contract"
                    ? offer.seller.voucherCloneAddress
                    : offer.seller.assistant, returnTypedDataToSign: false }));
        });
    }
    signMetaTxPreMint(args_1) {
        return __awaiter(this, arguments, void 0, function* (args, overrides = {}) {
            const forwarderAddress = this._contracts.forwarder;
            const batchId = overrides.batchId || 0;
            const offerFromSubgraph = yield (0, subgraph_1.getOfferById)(this._subgraphUrl, args.offerId);
            return _1.handler.signMetaTxPreMint(Object.assign({ web3Lib: this._web3Lib, bosonVoucherAddress: offerFromSubgraph.seller.voucherCloneAddress, chainId: this._chainId, forwarderAddress,
                batchId, forwarderAbi: this._metaTxConfig.forwarderAbi, relayerUrl: this._metaTxConfig.relayerUrl }, args));
        });
    }
    signMetaTxSetApprovalForAll(args_1) {
        return __awaiter(this, arguments, void 0, function* (args, overrides = {}) {
            const sellerAddress = yield this._web3Lib.getSignerAddress();
            const seller = yield __1.accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
            const forwarderAddress = this._contracts.forwarder;
            const batchId = overrides.batchId || 0;
            return _1.handler.signMetaTxSetApprovalForAll(Object.assign({ web3Lib: this._web3Lib, bosonVoucherAddress: seller.voucherCloneAddress, chainId: this._chainId, forwarderAddress,
                batchId, forwarderAbi: this._metaTxConfig.forwarderAbi, relayerUrl: this._metaTxConfig.relayerUrl }, args));
        });
    }
    signMetaTxSetApprovalForAllToContract(args_1) {
        return __awaiter(this, arguments, void 0, function* (args, overrides = {}) {
            const sellerAddress = yield this._web3Lib.getSignerAddress();
            const seller = yield __1.accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
            const forwarderAddress = this._contracts.forwarder;
            const batchId = overrides.batchId || 0;
            return _1.handler.signMetaTxSetApprovalForAllToContract(Object.assign({ web3Lib: this._web3Lib, bosonVoucherAddress: args.bosonVoucherAddress || seller.voucherCloneAddress, chainId: this._chainId, forwarderAddress,
                batchId, forwarderAbi: this._metaTxConfig.forwarderAbi, relayerUrl: this._metaTxConfig.relayerUrl }, args), {
                txGas: overrides.txGas
            });
        });
    }
    signMetaTxCallExternalContract(args_1) {
        return __awaiter(this, arguments, void 0, function* (args, overrides = {}) {
            const sellerAddress = yield this._web3Lib.getSignerAddress();
            const seller = yield __1.accounts.subgraph.getSellerByAddress(this._subgraphUrl, sellerAddress);
            const forwarderAddress = this._contracts.forwarder;
            const batchId = overrides.batchId || 0;
            return _1.handler.signMetaTxCallExternalContract(Object.assign({ web3Lib: this._web3Lib, bosonVoucherAddress: seller.voucherCloneAddress, chainId: this._chainId, forwarderAddress,
                batchId, forwarderAbi: this._metaTxConfig.forwarderAbi, relayerUrl: this._metaTxConfig.relayerUrl }, args), {
                txGas: overrides.txGas
            });
        });
    }
    relayBiconomyMetaTransaction(contractAddress_1, metaTxParams_1) {
        return __awaiter(this, arguments, void 0, function* (contractAddress, metaTxParams, overrides = {}) {
            const { metaTxApiId, metaTxApiKey, metaTxRelayerUrl } = this.assertAndGetMetaTxConfig(Object.assign(Object.assign({}, overrides), { contractAddress }));
            return _1.handler.relayBiconomyMetaTransaction({
                web3LibAdapter: this._web3Lib,
                contractAddress,
                chainId: this._chainId,
                metaTx: {
                    config: {
                        relayerUrl: metaTxRelayerUrl,
                        apiId: metaTxApiId,
                        apiKey: metaTxApiKey
                    },
                    params: {
                        userAddress: overrides.userAddress || (yield this._web3Lib.getSignerAddress()),
                        request: metaTxParams.request,
                        domainSeparator: metaTxParams.domainSeparator,
                        signature: metaTxParams.signature
                    }
                }
            });
        });
    }
    // Implementation
    signMetaTxCreateOfferWithCondition(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCreateOfferWithCondition(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCreateOfferWithCondition(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxVoidOffer(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxVoidOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxVoidOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxVoidOfferBatch(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxVoidOfferBatch(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxVoidOfferBatch(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxExtendOffer(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxExtendOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxExtendOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxExtendOfferBatch(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxExtendOfferBatch(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxExtendOfferBatch(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCompleteExchange(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCompleteExchange(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCompleteExchange(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCompleteExchangeBatch(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCompleteExchangeBatch(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCompleteExchangeBatch(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCommitToOffer(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const offer = yield (0, subgraph_1.getOfferById)(this._subgraphUrl, args.offerId);
            if (offer.condition) {
                // keep compatibility with previous version
                // Using type assertion because TypeScript can't resolve overloads inside the implementation body
                // when the args type comes from a complex Omit<Parameters<...>[0], ...> expression
                const self = this;
                const conditionalArgs = Object.assign(Object.assign({}, args), { tokenId: offer.condition.minTokenId });
                return self.signMetaTxCommitToConditionalOffer(conditionalArgs);
            }
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCommitToOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCommitToOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCommitToConditionalOffer(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCommitToConditionalOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCommitToConditionalOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCommitToBuyerOffer(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCommitToBuyerOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCommitToBuyerOffer(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCreateOfferAndCommit(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCreateOfferAndCommit(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCreateOfferAndCommit(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCommitToOfferAndRedeemVoucher(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCommitToOfferAndRedeemVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCommitToOfferAndRedeemVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCommitToConditionalOfferAndRedeemVoucher(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCommitToConditionalOfferAndRedeemVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCommitToConditionalOfferAndRedeemVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCreateOfferCommitAndRedeem(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCreateOfferCommitAndRedeem(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCreateOfferCommitAndRedeem(Object.assign(Object.assign({ web3Lib: this._web3Lib, theGraphStorage: this._theGraphStorage, metadataStorage: this._metadataStorage, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxCancelVoucher(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxCancelVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxCancelVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxRedeemVoucher(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxRedeemVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxRedeemVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxExpireVoucher(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxExpireVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxExpireVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxRevokeVoucher(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxRevokeVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxRevokeVoucher(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxRetractDispute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxRetractDispute(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxRetractDispute(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxEscalateDispute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxEscalateDispute(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxEscalateDispute(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxRaiseDispute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxRaiseDispute(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxRaiseDispute(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxResolveDispute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxResolveDispute(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxResolveDispute(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxExtendDisputeTimeout(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxExtendDisputeTimeout(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxExtendDisputeTimeout(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxWithdrawFunds(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxWithdrawFunds(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxWithdrawFunds(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    // Implementation
    signMetaTxDepositFunds(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (args.returnTypedDataToSign) {
                return _1.handler.signMetaTxDepositFunds(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: true }));
            }
            return _1.handler.signMetaTxDepositFunds(Object.assign(Object.assign({ web3Lib: this._web3Lib, metaTxHandlerAddress: this._protocolDiamond, chainId: this._chainId }, args), { returnTypedDataToSign: false }));
        });
    }
    /**
     * Relay a meta transaction,
     * @param metaTxParams - Required params for meta transaction.
     * @param overrides - Optional overrides.
     * @returns Transaction response.
     */
    relayMetaTransaction(metaTxParams_1) {
        return __awaiter(this, arguments, void 0, function* (metaTxParams, overrides = {}) {
            const { metaTxApiId, metaTxApiKey, metaTxRelayerUrl, contractAddress } = this.assertAndGetMetaTxConfig(overrides);
            return _1.handler.relayMetaTransaction({
                web3LibAdapter: this._web3Lib,
                contractAddress,
                chainId: this._chainId,
                metaTx: {
                    config: {
                        relayerUrl: metaTxRelayerUrl,
                        apiId: metaTxApiId,
                        apiKey: metaTxApiKey
                    },
                    params: {
                        userAddress: overrides.userAddress || (yield this._web3Lib.getSignerAddress()),
                        functionName: metaTxParams.functionName,
                        functionSignature: metaTxParams.functionSignature,
                        nonce: metaTxParams.nonce,
                        sigR: metaTxParams.sigR,
                        sigS: metaTxParams.sigS,
                        sigV: metaTxParams.sigV,
                        transferAuthorizations: metaTxParams.transferAuthorizations
                    }
                }
            });
        });
    }
    /**
     * Execute a signed meta transaction directly on-chain (no Biconomy relayer).
     * Routes to `executeMetaTransaction` or `executeMetaTransactionWithTokenTransferAuthorization`
     * depending on whether `transferAuthorizations` is provided and non-empty.
     * The wallet backing the SDK pays the gas; pass `overrides.userAddress` when
     * the meta-tx signer differs from that wallet.
     * @param metaTxParams - Required params for meta transaction.
     * @param overrides - Optional overrides.
     * @returns Transaction response.
     */
    executeMetaTransaction(metaTxParams_1) {
        return __awaiter(this, arguments, void 0, function* (metaTxParams, overrides = {}) {
            var _a;
            const userAddress = overrides.userAddress || (yield this._web3Lib.getSignerAddress());
            const contractAddress = overrides.contractAddress || this._protocolDiamond;
            if ((_a = metaTxParams.transferAuthorizations) === null || _a === void 0 ? void 0 : _a.length) {
                return _1.handler.executeMetaTransactionWithTokenTransferAuthorization({
                    web3Lib: this._web3Lib,
                    contractAddress,
                    userAddress,
                    functionName: metaTxParams.functionName,
                    functionSignature: metaTxParams.functionSignature,
                    nonce: metaTxParams.nonce,
                    sigR: metaTxParams.sigR,
                    sigS: metaTxParams.sigS,
                    sigV: metaTxParams.sigV,
                    transferAuthorizations: metaTxParams.transferAuthorizations
                });
            }
            return _1.handler.executeMetaTransaction({
                web3Lib: this._web3Lib,
                contractAddress,
                userAddress,
                functionName: metaTxParams.functionName,
                functionSignature: metaTxParams.functionSignature,
                nonce: metaTxParams.nonce,
                sigR: metaTxParams.sigR,
                sigS: metaTxParams.sigS,
                sigV: metaTxParams.sigV
            });
        });
    }
    /**
     * Returns information of submitted meta transaction.
     * See https://docs.biconomy.io/api/native-meta-tx/get-retried-hashes.
     * @param originalMetaTxHash - Original meta transaction as returned by `coreSDK.relayMetaTransaction`
     * @param overrides - Optional overrides for meta transaction config.
     * @returns - Additional meta transaction information.
     */
    getResubmittedMetaTx(originalMetaTxHash_1) {
        return __awaiter(this, arguments, void 0, function* (originalMetaTxHash, overrides = {}) {
            const { metaTxApiId, metaTxApiKey, metaTxRelayerUrl } = this.assertAndGetMetaTxConfig(overrides);
            return _1.handler.getResubmitted({
                chainId: this._chainId,
                metaTx: {
                    config: {
                        relayerUrl: metaTxRelayerUrl,
                        apiId: metaTxApiId,
                        apiKey: metaTxApiKey
                    },
                    originalHash: originalMetaTxHash
                }
            });
        });
    }
}
exports.MetaTxMixin = MetaTxMixin;
//# sourceMappingURL=mixin.js.map