using System.Numerics;
using Managing.Tools.ABI.OrderBook.ContractDefinition;
using Nethereum.Contracts.ContractHandlers;
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Web3;

namespace Managing.Tools.ABI.OrderBook
{
    public class OrderBookService
    {
        public static Task<TransactionReceipt> DeployContractAndWaitForReceiptAsync(Web3 web3,
            OrderBookDeployment orderBookDeployment, CancellationTokenSource cancellationTokenSource = null)
        {
            return web3.Eth.GetContractDeploymentHandler<OrderBookDeployment>()
                .SendRequestAndWaitForReceiptAsync(orderBookDeployment, cancellationTokenSource);
        }

        public static Task<string> DeployContractAsync(Web3 web3, OrderBookDeployment orderBookDeployment)
        {
            return web3.Eth.GetContractDeploymentHandler<OrderBookDeployment>().SendRequestAsync(orderBookDeployment);
        }

        public static async Task<OrderBookService> DeployContractAndGetServiceAsync(Web3 web3,
            OrderBookDeployment orderBookDeployment, CancellationTokenSource cancellationTokenSource = null)
        {
            var receipt =
                await DeployContractAndWaitForReceiptAsync(web3, orderBookDeployment, cancellationTokenSource);
            return new OrderBookService(web3, receipt.ContractAddress);
        }

        protected IWeb3 Web3 { get; }

        public ContractHandler ContractHandler { get; }

        public OrderBookService(Web3 web3, string contractAddress)
        {
            Web3 = web3;
            ContractHandler = web3.Eth.GetContractHandler(contractAddress);
        }

        public OrderBookService(IWeb3 web3, string contractAddress)
        {
            Web3 = web3;
            ContractHandler = web3.Eth.GetContractHandler(contractAddress);
        }

        public Task<BigInteger> PricePrecisionQueryAsync(PricePrecisionFunction pricePrecisionFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<PricePrecisionFunction, BigInteger>(pricePrecisionFunction,
                blockParameter);
        }


        public Task<BigInteger> PricePrecisionQueryAsync(BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<PricePrecisionFunction, BigInteger>(null, blockParameter);
        }

        public Task<BigInteger> UsdgPrecisionQueryAsync(UsdgPrecisionFunction usdgPrecisionFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<UsdgPrecisionFunction, BigInteger>(usdgPrecisionFunction, blockParameter);
        }


        public Task<BigInteger> UsdgPrecisionQueryAsync(BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<UsdgPrecisionFunction, BigInteger>(null, blockParameter);
        }

        public Task<string> CancelDecreaseOrderRequestAsync(CancelDecreaseOrderFunction cancelDecreaseOrderFunction)
        {
            return ContractHandler.SendRequestAsync(cancelDecreaseOrderFunction);
        }

        public Task<TransactionReceipt> CancelDecreaseOrderRequestAndWaitForReceiptAsync(
            CancelDecreaseOrderFunction cancelDecreaseOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(cancelDecreaseOrderFunction, cancellationToken);
        }

        public Task<string> CancelDecreaseOrderRequestAsync(BigInteger orderIndex)
        {
            var cancelDecreaseOrderFunction = new CancelDecreaseOrderFunction();
            cancelDecreaseOrderFunction.OrderIndex = orderIndex;

            return ContractHandler.SendRequestAsync(cancelDecreaseOrderFunction);
        }

        public Task<TransactionReceipt> CancelDecreaseOrderRequestAndWaitForReceiptAsync(BigInteger orderIndex,
            CancellationTokenSource cancellationToken = null)
        {
            var cancelDecreaseOrderFunction = new CancelDecreaseOrderFunction();
            cancelDecreaseOrderFunction.OrderIndex = orderIndex;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(cancelDecreaseOrderFunction, cancellationToken);
        }

        public Task<string> CancelIncreaseOrderRequestAsync(CancelIncreaseOrderFunction cancelIncreaseOrderFunction)
        {
            return ContractHandler.SendRequestAsync(cancelIncreaseOrderFunction);
        }

        public Task<TransactionReceipt> CancelIncreaseOrderRequestAndWaitForReceiptAsync(
            CancelIncreaseOrderFunction cancelIncreaseOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(cancelIncreaseOrderFunction, cancellationToken);
        }

        public Task<string> CancelIncreaseOrderRequestAsync(BigInteger orderIndex)
        {
            var cancelIncreaseOrderFunction = new CancelIncreaseOrderFunction();
            cancelIncreaseOrderFunction.OrderIndex = orderIndex;

            return ContractHandler.SendRequestAsync(cancelIncreaseOrderFunction);
        }

        public Task<TransactionReceipt> CancelIncreaseOrderRequestAndWaitForReceiptAsync(BigInteger orderIndex,
            CancellationTokenSource cancellationToken = null)
        {
            var cancelIncreaseOrderFunction = new CancelIncreaseOrderFunction();
            cancelIncreaseOrderFunction.OrderIndex = orderIndex;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(cancelIncreaseOrderFunction, cancellationToken);
        }

        public Task<string> CancelMultipleRequestAsync(CancelMultipleFunction cancelMultipleFunction)
        {
            return ContractHandler.SendRequestAsync(cancelMultipleFunction);
        }

        public Task<TransactionReceipt> CancelMultipleRequestAndWaitForReceiptAsync(
            CancelMultipleFunction cancelMultipleFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(cancelMultipleFunction, cancellationToken);
        }

        public Task<string> CancelMultipleRequestAsync(List<BigInteger> swapOrderIndexes,
            List<BigInteger> increaseOrderIndexes, List<BigInteger> decreaseOrderIndexes)
        {
            var cancelMultipleFunction = new CancelMultipleFunction();
            cancelMultipleFunction.SwapOrderIndexes = swapOrderIndexes;
            cancelMultipleFunction.IncreaseOrderIndexes = increaseOrderIndexes;
            cancelMultipleFunction.DecreaseOrderIndexes = decreaseOrderIndexes;

            return ContractHandler.SendRequestAsync(cancelMultipleFunction);
        }

        public Task<TransactionReceipt> CancelMultipleRequestAndWaitForReceiptAsync(List<BigInteger> swapOrderIndexes,
            List<BigInteger> increaseOrderIndexes, List<BigInteger> decreaseOrderIndexes,
            CancellationTokenSource cancellationToken = null)
        {
            var cancelMultipleFunction = new CancelMultipleFunction();
            cancelMultipleFunction.SwapOrderIndexes = swapOrderIndexes;
            cancelMultipleFunction.IncreaseOrderIndexes = increaseOrderIndexes;
            cancelMultipleFunction.DecreaseOrderIndexes = decreaseOrderIndexes;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(cancelMultipleFunction, cancellationToken);
        }

        public Task<string> CancelSwapOrderRequestAsync(CancelSwapOrderFunction cancelSwapOrderFunction)
        {
            return ContractHandler.SendRequestAsync(cancelSwapOrderFunction);
        }

        public Task<TransactionReceipt> CancelSwapOrderRequestAndWaitForReceiptAsync(
            CancelSwapOrderFunction cancelSwapOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(cancelSwapOrderFunction, cancellationToken);
        }

        public Task<string> CancelSwapOrderRequestAsync(BigInteger orderIndex)
        {
            var cancelSwapOrderFunction = new CancelSwapOrderFunction();
            cancelSwapOrderFunction.OrderIndex = orderIndex;

            return ContractHandler.SendRequestAsync(cancelSwapOrderFunction);
        }

        public Task<TransactionReceipt> CancelSwapOrderRequestAndWaitForReceiptAsync(BigInteger orderIndex,
            CancellationTokenSource cancellationToken = null)
        {
            var cancelSwapOrderFunction = new CancelSwapOrderFunction();
            cancelSwapOrderFunction.OrderIndex = orderIndex;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(cancelSwapOrderFunction, cancellationToken);
        }

        public Task<string> CreateDecreaseOrderRequestAsync(CreateDecreaseOrderFunction createDecreaseOrderFunction)
        {
            return ContractHandler.SendRequestAsync(createDecreaseOrderFunction);
        }

        public Task<TransactionReceipt> CreateDecreaseOrderRequestAndWaitForReceiptAsync(
            CreateDecreaseOrderFunction createDecreaseOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(createDecreaseOrderFunction, cancellationToken);
        }

        public Task<string> CreateDecreaseOrderRequestAsync(string indexToken, BigInteger sizeDelta,
            string collateralToken, BigInteger collateralDelta, bool isLong, BigInteger triggerPrice,
            bool triggerAboveThreshold)
        {
            var createDecreaseOrderFunction = new CreateDecreaseOrderFunction();
            createDecreaseOrderFunction.IndexToken = indexToken;
            createDecreaseOrderFunction.SizeDelta = sizeDelta;
            createDecreaseOrderFunction.CollateralToken = collateralToken;
            createDecreaseOrderFunction.CollateralDelta = collateralDelta;
            createDecreaseOrderFunction.IsLong = isLong;
            createDecreaseOrderFunction.TriggerPrice = triggerPrice;
            createDecreaseOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;

            return ContractHandler.SendRequestAsync(createDecreaseOrderFunction);
        }

        public Task<TransactionReceipt> CreateDecreaseOrderRequestAndWaitForReceiptAsync(string indexToken,
            BigInteger sizeDelta, string collateralToken, BigInteger collateralDelta, bool isLong,
            BigInteger triggerPrice, bool triggerAboveThreshold, CancellationTokenSource cancellationToken = null)
        {
            var createDecreaseOrderFunction = new CreateDecreaseOrderFunction();
            createDecreaseOrderFunction.IndexToken = indexToken;
            createDecreaseOrderFunction.SizeDelta = sizeDelta;
            createDecreaseOrderFunction.CollateralToken = collateralToken;
            createDecreaseOrderFunction.CollateralDelta = collateralDelta;
            createDecreaseOrderFunction.IsLong = isLong;
            createDecreaseOrderFunction.TriggerPrice = triggerPrice;
            createDecreaseOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(createDecreaseOrderFunction, cancellationToken);
        }

        public Task<string> CreateIncreaseOrderRequestAsync(CreateIncreaseOrderFunction createIncreaseOrderFunction)
        {
            return ContractHandler.SendRequestAsync(createIncreaseOrderFunction);
        }

        public Task<TransactionReceipt> CreateIncreaseOrderRequestAndWaitForReceiptAsync(
            CreateIncreaseOrderFunction createIncreaseOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(createIncreaseOrderFunction, cancellationToken);
        }

        public Task<string> CreateIncreaseOrderRequestAsync(List<string> path, BigInteger amountIn, string indexToken,
            BigInteger minOut, BigInteger sizeDelta, string collateralToken, bool isLong, BigInteger triggerPrice,
            bool triggerAboveThreshold, BigInteger executionFee, bool shouldWrap)
        {
            var createIncreaseOrderFunction = new CreateIncreaseOrderFunction();
            createIncreaseOrderFunction.Path = path;
            createIncreaseOrderFunction.AmountIn = amountIn;
            createIncreaseOrderFunction.IndexToken = indexToken;
            createIncreaseOrderFunction.MinOut = minOut;
            createIncreaseOrderFunction.SizeDelta = sizeDelta;
            createIncreaseOrderFunction.CollateralToken = collateralToken;
            createIncreaseOrderFunction.IsLong = isLong;
            createIncreaseOrderFunction.TriggerPrice = triggerPrice;
            createIncreaseOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;
            createIncreaseOrderFunction.ExecutionFee = executionFee;
            createIncreaseOrderFunction.ShouldWrap = shouldWrap;

            return ContractHandler.SendRequestAsync(createIncreaseOrderFunction);
        }

        public Task<TransactionReceipt> CreateIncreaseOrderRequestAndWaitForReceiptAsync(List<string> path,
            BigInteger amountIn, string indexToken, BigInteger minOut, BigInteger sizeDelta, string collateralToken,
            bool isLong, BigInteger triggerPrice, bool triggerAboveThreshold, BigInteger executionFee, bool shouldWrap,
            CancellationTokenSource cancellationToken = null)
        {
            var createIncreaseOrderFunction = new CreateIncreaseOrderFunction();
            createIncreaseOrderFunction.Path = path;
            createIncreaseOrderFunction.AmountIn = amountIn;
            createIncreaseOrderFunction.IndexToken = indexToken;
            createIncreaseOrderFunction.MinOut = minOut;
            createIncreaseOrderFunction.SizeDelta = sizeDelta;
            createIncreaseOrderFunction.CollateralToken = collateralToken;
            createIncreaseOrderFunction.IsLong = isLong;
            createIncreaseOrderFunction.TriggerPrice = triggerPrice;
            createIncreaseOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;
            createIncreaseOrderFunction.ExecutionFee = executionFee;
            createIncreaseOrderFunction.ShouldWrap = shouldWrap;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(createIncreaseOrderFunction, cancellationToken);
        }

        public Task<string> CreateSwapOrderRequestAsync(CreateSwapOrderFunction createSwapOrderFunction)
        {
            return ContractHandler.SendRequestAsync(createSwapOrderFunction);
        }

        public Task<TransactionReceipt> CreateSwapOrderRequestAndWaitForReceiptAsync(
            CreateSwapOrderFunction createSwapOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(createSwapOrderFunction, cancellationToken);
        }

        public Task<string> CreateSwapOrderRequestAsync(List<string> path, BigInteger amountIn, BigInteger minOut,
            BigInteger triggerRatio, bool triggerAboveThreshold, BigInteger executionFee, bool shouldWrap,
            bool shouldUnwrap)
        {
            var createSwapOrderFunction = new CreateSwapOrderFunction();
            createSwapOrderFunction.Path = path;
            createSwapOrderFunction.AmountIn = amountIn;
            createSwapOrderFunction.MinOut = minOut;
            createSwapOrderFunction.TriggerRatio = triggerRatio;
            createSwapOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;
            createSwapOrderFunction.ExecutionFee = executionFee;
            createSwapOrderFunction.ShouldWrap = shouldWrap;
            createSwapOrderFunction.ShouldUnwrap = shouldUnwrap;

            return ContractHandler.SendRequestAsync(createSwapOrderFunction);
        }

        public Task<TransactionReceipt> CreateSwapOrderRequestAndWaitForReceiptAsync(List<string> path,
            BigInteger amountIn, BigInteger minOut, BigInteger triggerRatio, bool triggerAboveThreshold,
            BigInteger executionFee, bool shouldWrap, bool shouldUnwrap,
            CancellationTokenSource cancellationToken = null)
        {
            var createSwapOrderFunction = new CreateSwapOrderFunction();
            createSwapOrderFunction.Path = path;
            createSwapOrderFunction.AmountIn = amountIn;
            createSwapOrderFunction.MinOut = minOut;
            createSwapOrderFunction.TriggerRatio = triggerRatio;
            createSwapOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;
            createSwapOrderFunction.ExecutionFee = executionFee;
            createSwapOrderFunction.ShouldWrap = shouldWrap;
            createSwapOrderFunction.ShouldUnwrap = shouldUnwrap;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(createSwapOrderFunction, cancellationToken);
        }

        public Task<DecreaseOrdersOutputDTO> DecreaseOrdersQueryAsync(DecreaseOrdersFunction decreaseOrdersFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryDeserializingToObjectAsync<DecreaseOrdersFunction, DecreaseOrdersOutputDTO>(
                decreaseOrdersFunction, blockParameter);
        }

        public Task<DecreaseOrdersOutputDTO> DecreaseOrdersQueryAsync(string returnValue1, BigInteger returnValue2,
            BlockParameter blockParameter = null)
        {
            var decreaseOrdersFunction = new DecreaseOrdersFunction();
            decreaseOrdersFunction.ReturnValue1 = returnValue1;
            decreaseOrdersFunction.ReturnValue2 = returnValue2;

            return ContractHandler.QueryDeserializingToObjectAsync<DecreaseOrdersFunction, DecreaseOrdersOutputDTO>(
                decreaseOrdersFunction, blockParameter);
        }

        public Task<BigInteger> DecreaseOrdersIndexQueryAsync(DecreaseOrdersIndexFunction decreaseOrdersIndexFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<DecreaseOrdersIndexFunction, BigInteger>(decreaseOrdersIndexFunction,
                blockParameter);
        }


        public Task<BigInteger> DecreaseOrdersIndexQueryAsync(string returnValue1, BlockParameter blockParameter = null)
        {
            var decreaseOrdersIndexFunction = new DecreaseOrdersIndexFunction();
            decreaseOrdersIndexFunction.ReturnValue1 = returnValue1;

            return ContractHandler.QueryAsync<DecreaseOrdersIndexFunction, BigInteger>(decreaseOrdersIndexFunction,
                blockParameter);
        }

        public Task<string> ExecuteDecreaseOrderRequestAsync(ExecuteDecreaseOrderFunction executeDecreaseOrderFunction)
        {
            return ContractHandler.SendRequestAsync(executeDecreaseOrderFunction);
        }

        public Task<TransactionReceipt> ExecuteDecreaseOrderRequestAndWaitForReceiptAsync(
            ExecuteDecreaseOrderFunction executeDecreaseOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(executeDecreaseOrderFunction, cancellationToken);
        }

        public Task<string> ExecuteDecreaseOrderRequestAsync(string address, BigInteger orderIndex, string feeReceiver)
        {
            var executeDecreaseOrderFunction = new ExecuteDecreaseOrderFunction();
            executeDecreaseOrderFunction.Address = address;
            executeDecreaseOrderFunction.OrderIndex = orderIndex;
            executeDecreaseOrderFunction.FeeReceiver = feeReceiver;

            return ContractHandler.SendRequestAsync(executeDecreaseOrderFunction);
        }

        public Task<TransactionReceipt> ExecuteDecreaseOrderRequestAndWaitForReceiptAsync(string address,
            BigInteger orderIndex, string feeReceiver, CancellationTokenSource cancellationToken = null)
        {
            var executeDecreaseOrderFunction = new ExecuteDecreaseOrderFunction();
            executeDecreaseOrderFunction.Address = address;
            executeDecreaseOrderFunction.OrderIndex = orderIndex;
            executeDecreaseOrderFunction.FeeReceiver = feeReceiver;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(executeDecreaseOrderFunction, cancellationToken);
        }

        public Task<string> ExecuteIncreaseOrderRequestAsync(ExecuteIncreaseOrderFunction executeIncreaseOrderFunction)
        {
            return ContractHandler.SendRequestAsync(executeIncreaseOrderFunction);
        }

        public Task<TransactionReceipt> ExecuteIncreaseOrderRequestAndWaitForReceiptAsync(
            ExecuteIncreaseOrderFunction executeIncreaseOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(executeIncreaseOrderFunction, cancellationToken);
        }

        public Task<string> ExecuteIncreaseOrderRequestAsync(string address, BigInteger orderIndex, string feeReceiver)
        {
            var executeIncreaseOrderFunction = new ExecuteIncreaseOrderFunction();
            executeIncreaseOrderFunction.Address = address;
            executeIncreaseOrderFunction.OrderIndex = orderIndex;
            executeIncreaseOrderFunction.FeeReceiver = feeReceiver;

            return ContractHandler.SendRequestAsync(executeIncreaseOrderFunction);
        }

        public Task<TransactionReceipt> ExecuteIncreaseOrderRequestAndWaitForReceiptAsync(string address,
            BigInteger orderIndex, string feeReceiver, CancellationTokenSource cancellationToken = null)
        {
            var executeIncreaseOrderFunction = new ExecuteIncreaseOrderFunction();
            executeIncreaseOrderFunction.Address = address;
            executeIncreaseOrderFunction.OrderIndex = orderIndex;
            executeIncreaseOrderFunction.FeeReceiver = feeReceiver;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(executeIncreaseOrderFunction, cancellationToken);
        }

        public Task<string> ExecuteSwapOrderRequestAsync(ExecuteSwapOrderFunction executeSwapOrderFunction)
        {
            return ContractHandler.SendRequestAsync(executeSwapOrderFunction);
        }

        public Task<TransactionReceipt> ExecuteSwapOrderRequestAndWaitForReceiptAsync(
            ExecuteSwapOrderFunction executeSwapOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(executeSwapOrderFunction, cancellationToken);
        }

        public Task<string> ExecuteSwapOrderRequestAsync(string account, BigInteger orderIndex, string feeReceiver)
        {
            var executeSwapOrderFunction = new ExecuteSwapOrderFunction();
            executeSwapOrderFunction.Account = account;
            executeSwapOrderFunction.OrderIndex = orderIndex;
            executeSwapOrderFunction.FeeReceiver = feeReceiver;

            return ContractHandler.SendRequestAsync(executeSwapOrderFunction);
        }

        public Task<TransactionReceipt> ExecuteSwapOrderRequestAndWaitForReceiptAsync(string account,
            BigInteger orderIndex, string feeReceiver, CancellationTokenSource cancellationToken = null)
        {
            var executeSwapOrderFunction = new ExecuteSwapOrderFunction();
            executeSwapOrderFunction.Account = account;
            executeSwapOrderFunction.OrderIndex = orderIndex;
            executeSwapOrderFunction.FeeReceiver = feeReceiver;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(executeSwapOrderFunction, cancellationToken);
        }

        public Task<GetDecreaseOrderOutputDTO> GetDecreaseOrderQueryAsync(
            GetDecreaseOrderFunction getDecreaseOrderFunction, BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryDeserializingToObjectAsync<GetDecreaseOrderFunction, GetDecreaseOrderOutputDTO>(
                getDecreaseOrderFunction, blockParameter);
        }

        public Task<GetDecreaseOrderOutputDTO> GetDecreaseOrderQueryAsync(string account, BigInteger orderIndex,
            BlockParameter blockParameter = null)
        {
            var getDecreaseOrderFunction = new GetDecreaseOrderFunction();
            getDecreaseOrderFunction.Account = account;
            getDecreaseOrderFunction.OrderIndex = orderIndex;

            return ContractHandler.QueryDeserializingToObjectAsync<GetDecreaseOrderFunction, GetDecreaseOrderOutputDTO>(
                getDecreaseOrderFunction, blockParameter);
        }

        public Task<GetIncreaseOrderOutputDTO> GetIncreaseOrderQueryAsync(
            GetIncreaseOrderFunction getIncreaseOrderFunction, BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryDeserializingToObjectAsync<GetIncreaseOrderFunction, GetIncreaseOrderOutputDTO>(
                getIncreaseOrderFunction, blockParameter);
        }

        public Task<GetIncreaseOrderOutputDTO> GetIncreaseOrderQueryAsync(string account, BigInteger orderIndex,
            BlockParameter blockParameter = null)
        {
            var getIncreaseOrderFunction = new GetIncreaseOrderFunction();
            getIncreaseOrderFunction.Account = account;
            getIncreaseOrderFunction.OrderIndex = orderIndex;

            return ContractHandler.QueryDeserializingToObjectAsync<GetIncreaseOrderFunction, GetIncreaseOrderOutputDTO>(
                getIncreaseOrderFunction, blockParameter);
        }

        public Task<GetSwapOrderOutputDTO> GetSwapOrderQueryAsync(GetSwapOrderFunction getSwapOrderFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryDeserializingToObjectAsync<GetSwapOrderFunction, GetSwapOrderOutputDTO>(
                getSwapOrderFunction, blockParameter);
        }

        public Task<GetSwapOrderOutputDTO> GetSwapOrderQueryAsync(string account, BigInteger orderIndex,
            BlockParameter blockParameter = null)
        {
            var getSwapOrderFunction = new GetSwapOrderFunction();
            getSwapOrderFunction.Account = account;
            getSwapOrderFunction.OrderIndex = orderIndex;

            return ContractHandler.QueryDeserializingToObjectAsync<GetSwapOrderFunction, GetSwapOrderOutputDTO>(
                getSwapOrderFunction, blockParameter);
        }

        public Task<BigInteger> GetUsdgMinPriceQueryAsync(GetUsdgMinPriceFunction getUsdgMinPriceFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<GetUsdgMinPriceFunction, BigInteger>(getUsdgMinPriceFunction,
                blockParameter);
        }


        public Task<BigInteger> GetUsdgMinPriceQueryAsync(string otherToken, BlockParameter blockParameter = null)
        {
            var getUsdgMinPriceFunction = new GetUsdgMinPriceFunction();
            getUsdgMinPriceFunction.OtherToken = otherToken;

            return ContractHandler.QueryAsync<GetUsdgMinPriceFunction, BigInteger>(getUsdgMinPriceFunction,
                blockParameter);
        }

        public Task<string> GovQueryAsync(GovFunction govFunction, BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<GovFunction, string>(govFunction, blockParameter);
        }


        public Task<string> GovQueryAsync(BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<GovFunction, string>(null, blockParameter);
        }

        public Task<IncreaseOrdersOutputDTO> IncreaseOrdersQueryAsync(IncreaseOrdersFunction increaseOrdersFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryDeserializingToObjectAsync<IncreaseOrdersFunction, IncreaseOrdersOutputDTO>(
                increaseOrdersFunction, blockParameter);
        }

        public Task<IncreaseOrdersOutputDTO> IncreaseOrdersQueryAsync(string returnValue1, BigInteger returnValue2,
            BlockParameter blockParameter = null)
        {
            var increaseOrdersFunction = new IncreaseOrdersFunction();
            increaseOrdersFunction.ReturnValue1 = returnValue1;
            increaseOrdersFunction.ReturnValue2 = returnValue2;

            return ContractHandler.QueryDeserializingToObjectAsync<IncreaseOrdersFunction, IncreaseOrdersOutputDTO>(
                increaseOrdersFunction, blockParameter);
        }

        public Task<BigInteger> IncreaseOrdersIndexQueryAsync(IncreaseOrdersIndexFunction increaseOrdersIndexFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<IncreaseOrdersIndexFunction, BigInteger>(increaseOrdersIndexFunction,
                blockParameter);
        }


        public Task<BigInteger> IncreaseOrdersIndexQueryAsync(string returnValue1, BlockParameter blockParameter = null)
        {
            var increaseOrdersIndexFunction = new IncreaseOrdersIndexFunction();
            increaseOrdersIndexFunction.ReturnValue1 = returnValue1;

            return ContractHandler.QueryAsync<IncreaseOrdersIndexFunction, BigInteger>(increaseOrdersIndexFunction,
                blockParameter);
        }

        public Task<string> InitializeRequestAsync(InitializeFunction initializeFunction)
        {
            return ContractHandler.SendRequestAsync(initializeFunction);
        }

        public Task<TransactionReceipt> InitializeRequestAndWaitForReceiptAsync(InitializeFunction initializeFunction,
            CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(initializeFunction, cancellationToken);
        }

        public Task<string> InitializeRequestAsync(string router, string vault, string weth, string usdg,
            BigInteger minExecutionFee, BigInteger minPurchaseTokenAmountUsd)
        {
            var initializeFunction = new InitializeFunction();
            initializeFunction.Router = router;
            initializeFunction.Vault = vault;
            initializeFunction.Weth = weth;
            initializeFunction.Usdg = usdg;
            initializeFunction.MinExecutionFee = minExecutionFee;
            initializeFunction.MinPurchaseTokenAmountUsd = minPurchaseTokenAmountUsd;

            return ContractHandler.SendRequestAsync(initializeFunction);
        }

        public Task<TransactionReceipt> InitializeRequestAndWaitForReceiptAsync(string router, string vault,
            string weth, string usdg, BigInteger minExecutionFee, BigInteger minPurchaseTokenAmountUsd,
            CancellationTokenSource cancellationToken = null)
        {
            var initializeFunction = new InitializeFunction();
            initializeFunction.Router = router;
            initializeFunction.Vault = vault;
            initializeFunction.Weth = weth;
            initializeFunction.Usdg = usdg;
            initializeFunction.MinExecutionFee = minExecutionFee;
            initializeFunction.MinPurchaseTokenAmountUsd = minPurchaseTokenAmountUsd;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(initializeFunction, cancellationToken);
        }

        public Task<bool> IsInitializedQueryAsync(IsInitializedFunction isInitializedFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<IsInitializedFunction, bool>(isInitializedFunction, blockParameter);
        }


        public Task<bool> IsInitializedQueryAsync(BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<IsInitializedFunction, bool>(null, blockParameter);
        }

        public Task<BigInteger> MinExecutionFeeQueryAsync(MinExecutionFeeFunction minExecutionFeeFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<MinExecutionFeeFunction, BigInteger>(minExecutionFeeFunction,
                blockParameter);
        }


        public Task<BigInteger> MinExecutionFeeQueryAsync(BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<MinExecutionFeeFunction, BigInteger>(null, blockParameter);
        }

        public Task<BigInteger> MinPurchaseTokenAmountUsdQueryAsync(
            MinPurchaseTokenAmountUsdFunction minPurchaseTokenAmountUsdFunction, BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<MinPurchaseTokenAmountUsdFunction, BigInteger>(
                minPurchaseTokenAmountUsdFunction, blockParameter);
        }


        public Task<BigInteger> MinPurchaseTokenAmountUsdQueryAsync(BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<MinPurchaseTokenAmountUsdFunction, BigInteger>(null, blockParameter);
        }

        public Task<string> RouterQueryAsync(RouterFunction routerFunction, BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<RouterFunction, string>(routerFunction, blockParameter);
        }


        public Task<string> RouterQueryAsync(BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<RouterFunction, string>(null, blockParameter);
        }

        public Task<string> SetGovRequestAsync(SetGovFunction setGovFunction)
        {
            return ContractHandler.SendRequestAsync(setGovFunction);
        }

        public Task<TransactionReceipt> SetGovRequestAndWaitForReceiptAsync(SetGovFunction setGovFunction,
            CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(setGovFunction, cancellationToken);
        }

        public Task<string> SetGovRequestAsync(string gov)
        {
            var setGovFunction = new SetGovFunction();
            setGovFunction.Gov = gov;

            return ContractHandler.SendRequestAsync(setGovFunction);
        }

        public Task<TransactionReceipt> SetGovRequestAndWaitForReceiptAsync(string gov,
            CancellationTokenSource cancellationToken = null)
        {
            var setGovFunction = new SetGovFunction();
            setGovFunction.Gov = gov;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(setGovFunction, cancellationToken);
        }

        public Task<string> SetMinExecutionFeeRequestAsync(SetMinExecutionFeeFunction setMinExecutionFeeFunction)
        {
            return ContractHandler.SendRequestAsync(setMinExecutionFeeFunction);
        }

        public Task<TransactionReceipt> SetMinExecutionFeeRequestAndWaitForReceiptAsync(
            SetMinExecutionFeeFunction setMinExecutionFeeFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(setMinExecutionFeeFunction, cancellationToken);
        }

        public Task<string> SetMinExecutionFeeRequestAsync(BigInteger minExecutionFee)
        {
            var setMinExecutionFeeFunction = new SetMinExecutionFeeFunction();
            setMinExecutionFeeFunction.MinExecutionFee = minExecutionFee;

            return ContractHandler.SendRequestAsync(setMinExecutionFeeFunction);
        }

        public Task<TransactionReceipt> SetMinExecutionFeeRequestAndWaitForReceiptAsync(BigInteger minExecutionFee,
            CancellationTokenSource cancellationToken = null)
        {
            var setMinExecutionFeeFunction = new SetMinExecutionFeeFunction();
            setMinExecutionFeeFunction.MinExecutionFee = minExecutionFee;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(setMinExecutionFeeFunction, cancellationToken);
        }

        public Task<string> SetMinPurchaseTokenAmountUsdRequestAsync(
            SetMinPurchaseTokenAmountUsdFunction setMinPurchaseTokenAmountUsdFunction)
        {
            return ContractHandler.SendRequestAsync(setMinPurchaseTokenAmountUsdFunction);
        }

        public Task<TransactionReceipt> SetMinPurchaseTokenAmountUsdRequestAndWaitForReceiptAsync(
            SetMinPurchaseTokenAmountUsdFunction setMinPurchaseTokenAmountUsdFunction,
            CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(setMinPurchaseTokenAmountUsdFunction,
                cancellationToken);
        }

        public Task<string> SetMinPurchaseTokenAmountUsdRequestAsync(BigInteger minPurchaseTokenAmountUsd)
        {
            var setMinPurchaseTokenAmountUsdFunction = new SetMinPurchaseTokenAmountUsdFunction();
            setMinPurchaseTokenAmountUsdFunction.MinPurchaseTokenAmountUsd = minPurchaseTokenAmountUsd;

            return ContractHandler.SendRequestAsync(setMinPurchaseTokenAmountUsdFunction);
        }

        public Task<TransactionReceipt> SetMinPurchaseTokenAmountUsdRequestAndWaitForReceiptAsync(
            BigInteger minPurchaseTokenAmountUsd, CancellationTokenSource cancellationToken = null)
        {
            var setMinPurchaseTokenAmountUsdFunction = new SetMinPurchaseTokenAmountUsdFunction();
            setMinPurchaseTokenAmountUsdFunction.MinPurchaseTokenAmountUsd = minPurchaseTokenAmountUsd;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(setMinPurchaseTokenAmountUsdFunction,
                cancellationToken);
        }

        public Task<SwapOrdersOutputDTO> SwapOrdersQueryAsync(SwapOrdersFunction swapOrdersFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryDeserializingToObjectAsync<SwapOrdersFunction, SwapOrdersOutputDTO>(
                swapOrdersFunction, blockParameter);
        }

        public Task<SwapOrdersOutputDTO> SwapOrdersQueryAsync(string returnValue1, BigInteger returnValue2,
            BlockParameter blockParameter = null)
        {
            var swapOrdersFunction = new SwapOrdersFunction();
            swapOrdersFunction.ReturnValue1 = returnValue1;
            swapOrdersFunction.ReturnValue2 = returnValue2;

            return ContractHandler.QueryDeserializingToObjectAsync<SwapOrdersFunction, SwapOrdersOutputDTO>(
                swapOrdersFunction, blockParameter);
        }

        public Task<BigInteger> SwapOrdersIndexQueryAsync(SwapOrdersIndexFunction swapOrdersIndexFunction,
            BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<SwapOrdersIndexFunction, BigInteger>(swapOrdersIndexFunction,
                blockParameter);
        }


        public Task<BigInteger> SwapOrdersIndexQueryAsync(string returnValue1, BlockParameter blockParameter = null)
        {
            var swapOrdersIndexFunction = new SwapOrdersIndexFunction();
            swapOrdersIndexFunction.ReturnValue1 = returnValue1;

            return ContractHandler.QueryAsync<SwapOrdersIndexFunction, BigInteger>(swapOrdersIndexFunction,
                blockParameter);
        }

        public Task<string> UpdateDecreaseOrderRequestAsync(UpdateDecreaseOrderFunction updateDecreaseOrderFunction)
        {
            return ContractHandler.SendRequestAsync(updateDecreaseOrderFunction);
        }

        public Task<TransactionReceipt> UpdateDecreaseOrderRequestAndWaitForReceiptAsync(
            UpdateDecreaseOrderFunction updateDecreaseOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(updateDecreaseOrderFunction, cancellationToken);
        }

        public Task<string> UpdateDecreaseOrderRequestAsync(BigInteger orderIndex, BigInteger collateralDelta,
            BigInteger sizeDelta, BigInteger triggerPrice, bool triggerAboveThreshold)
        {
            var updateDecreaseOrderFunction = new UpdateDecreaseOrderFunction();
            updateDecreaseOrderFunction.OrderIndex = orderIndex;
            updateDecreaseOrderFunction.CollateralDelta = collateralDelta;
            updateDecreaseOrderFunction.SizeDelta = sizeDelta;
            updateDecreaseOrderFunction.TriggerPrice = triggerPrice;
            updateDecreaseOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;

            return ContractHandler.SendRequestAsync(updateDecreaseOrderFunction);
        }

        public Task<TransactionReceipt> UpdateDecreaseOrderRequestAndWaitForReceiptAsync(BigInteger orderIndex,
            BigInteger collateralDelta, BigInteger sizeDelta, BigInteger triggerPrice, bool triggerAboveThreshold,
            CancellationTokenSource cancellationToken = null)
        {
            var updateDecreaseOrderFunction = new UpdateDecreaseOrderFunction();
            updateDecreaseOrderFunction.OrderIndex = orderIndex;
            updateDecreaseOrderFunction.CollateralDelta = collateralDelta;
            updateDecreaseOrderFunction.SizeDelta = sizeDelta;
            updateDecreaseOrderFunction.TriggerPrice = triggerPrice;
            updateDecreaseOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(updateDecreaseOrderFunction, cancellationToken);
        }

        public Task<string> UpdateIncreaseOrderRequestAsync(UpdateIncreaseOrderFunction updateIncreaseOrderFunction)
        {
            return ContractHandler.SendRequestAsync(updateIncreaseOrderFunction);
        }

        public Task<TransactionReceipt> UpdateIncreaseOrderRequestAndWaitForReceiptAsync(
            UpdateIncreaseOrderFunction updateIncreaseOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(updateIncreaseOrderFunction, cancellationToken);
        }

        public Task<string> UpdateIncreaseOrderRequestAsync(BigInteger orderIndex, BigInteger sizeDelta,
            BigInteger triggerPrice, bool triggerAboveThreshold)
        {
            var updateIncreaseOrderFunction = new UpdateIncreaseOrderFunction();
            updateIncreaseOrderFunction.OrderIndex = orderIndex;
            updateIncreaseOrderFunction.SizeDelta = sizeDelta;
            updateIncreaseOrderFunction.TriggerPrice = triggerPrice;
            updateIncreaseOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;

            return ContractHandler.SendRequestAsync(updateIncreaseOrderFunction);
        }

        public Task<TransactionReceipt> UpdateIncreaseOrderRequestAndWaitForReceiptAsync(BigInteger orderIndex,
            BigInteger sizeDelta, BigInteger triggerPrice, bool triggerAboveThreshold,
            CancellationTokenSource cancellationToken = null)
        {
            var updateIncreaseOrderFunction = new UpdateIncreaseOrderFunction();
            updateIncreaseOrderFunction.OrderIndex = orderIndex;
            updateIncreaseOrderFunction.SizeDelta = sizeDelta;
            updateIncreaseOrderFunction.TriggerPrice = triggerPrice;
            updateIncreaseOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(updateIncreaseOrderFunction, cancellationToken);
        }

        public Task<string> UpdateSwapOrderRequestAsync(UpdateSwapOrderFunction updateSwapOrderFunction)
        {
            return ContractHandler.SendRequestAsync(updateSwapOrderFunction);
        }

        public Task<TransactionReceipt> UpdateSwapOrderRequestAndWaitForReceiptAsync(
            UpdateSwapOrderFunction updateSwapOrderFunction, CancellationTokenSource cancellationToken = null)
        {
            return ContractHandler.SendRequestAndWaitForReceiptAsync(updateSwapOrderFunction, cancellationToken);
        }

        public Task<string> UpdateSwapOrderRequestAsync(BigInteger orderIndex, BigInteger minOut,
            BigInteger triggerRatio, bool triggerAboveThreshold)
        {
            var updateSwapOrderFunction = new UpdateSwapOrderFunction();
            updateSwapOrderFunction.OrderIndex = orderIndex;
            updateSwapOrderFunction.MinOut = minOut;
            updateSwapOrderFunction.TriggerRatio = triggerRatio;
            updateSwapOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;

            return ContractHandler.SendRequestAsync(updateSwapOrderFunction);
        }

        public Task<TransactionReceipt> UpdateSwapOrderRequestAndWaitForReceiptAsync(BigInteger orderIndex,
            BigInteger minOut, BigInteger triggerRatio, bool triggerAboveThreshold,
            CancellationTokenSource cancellationToken = null)
        {
            var updateSwapOrderFunction = new UpdateSwapOrderFunction();
            updateSwapOrderFunction.OrderIndex = orderIndex;
            updateSwapOrderFunction.MinOut = minOut;
            updateSwapOrderFunction.TriggerRatio = triggerRatio;
            updateSwapOrderFunction.TriggerAboveThreshold = triggerAboveThreshold;

            return ContractHandler.SendRequestAndWaitForReceiptAsync(updateSwapOrderFunction, cancellationToken);
        }

        public Task<string> UsdgQueryAsync(UsdgFunction usdgFunction, BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<UsdgFunction, string>(usdgFunction, blockParameter);
        }


        public Task<string> UsdgQueryAsync(BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<UsdgFunction, string>(null, blockParameter);
        }

        public Task<ValidatePositionOrderPriceOutputDTO> ValidatePositionOrderPriceQueryAsync(
            ValidatePositionOrderPriceFunction validatePositionOrderPriceFunction, BlockParameter blockParameter = null)
        {
            return ContractHandler
                .QueryDeserializingToObjectAsync<ValidatePositionOrderPriceFunction,
                    ValidatePositionOrderPriceOutputDTO>(validatePositionOrderPriceFunction, blockParameter);
        }

        public Task<ValidatePositionOrderPriceOutputDTO> ValidatePositionOrderPriceQueryAsync(
            bool triggerAboveThreshold, BigInteger triggerPrice, string indexToken, bool maximizePrice, bool raise,
            BlockParameter blockParameter = null)
        {
            var validatePositionOrderPriceFunction = new ValidatePositionOrderPriceFunction();
            validatePositionOrderPriceFunction.TriggerAboveThreshold = triggerAboveThreshold;
            validatePositionOrderPriceFunction.TriggerPrice = triggerPrice;
            validatePositionOrderPriceFunction.IndexToken = indexToken;
            validatePositionOrderPriceFunction.MaximizePrice = maximizePrice;
            validatePositionOrderPriceFunction.Raise = raise;

            return ContractHandler
                .QueryDeserializingToObjectAsync<ValidatePositionOrderPriceFunction,
                    ValidatePositionOrderPriceOutputDTO>(validatePositionOrderPriceFunction, blockParameter);
        }

        public Task<bool> ValidateSwapOrderPriceWithTriggerAboveThresholdQueryAsync(
            ValidateSwapOrderPriceWithTriggerAboveThresholdFunction
                validateSwapOrderPriceWithTriggerAboveThresholdFunction, BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<ValidateSwapOrderPriceWithTriggerAboveThresholdFunction, bool>(
                validateSwapOrderPriceWithTriggerAboveThresholdFunction, blockParameter);
        }


        public Task<bool> ValidateSwapOrderPriceWithTriggerAboveThresholdQueryAsync(List<string> path,
            BigInteger triggerRatio, BlockParameter blockParameter = null)
        {
            var validateSwapOrderPriceWithTriggerAboveThresholdFunction =
                new ValidateSwapOrderPriceWithTriggerAboveThresholdFunction();
            validateSwapOrderPriceWithTriggerAboveThresholdFunction.Path = path;
            validateSwapOrderPriceWithTriggerAboveThresholdFunction.TriggerRatio = triggerRatio;

            return ContractHandler.QueryAsync<ValidateSwapOrderPriceWithTriggerAboveThresholdFunction, bool>(
                validateSwapOrderPriceWithTriggerAboveThresholdFunction, blockParameter);
        }

        public Task<string> VaultQueryAsync(VaultFunction vaultFunction, BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<VaultFunction, string>(vaultFunction, blockParameter);
        }


        public Task<string> VaultQueryAsync(BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<VaultFunction, string>(null, blockParameter);
        }

        public Task<string> WethQueryAsync(WethFunction wethFunction, BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<WethFunction, string>(wethFunction, blockParameter);
        }


        public Task<string> WethQueryAsync(BlockParameter blockParameter = null)
        {
            return ContractHandler.QueryAsync<WethFunction, string>(null, blockParameter);
        }
    }
}