import { TradeLog, DerivativeTradeLog, SubaccountPosition, BinaryOptionsMarket, SpotLimitOrder, DerivativeLimitOrder, SpotMarket, DerivativeMarket, PerpetualMarketInfo, PerpetualMarketFunding, ExpiryFuturesMarketInfo, DepositUpdate, DerivativeMarketOrder, FeeDiscountSchedule, TradingRewardCampaignInfo, CampaignRewardPool, AccountRewards, DerivativeOrder } from "./exchange";
import { Coin } from "../../../cosmos/base/v1beta1/coin";
import * as _m0 from "protobufjs/minimal";
function createBaseEventBatchSpotExecution() {
  return {
    marketId: "",
    isBuy: false,
    executionType: 0,
    trades: []
  };
}
export const EventBatchSpotExecution = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    if (message.isBuy === true) {
      writer.uint32(16).bool(message.isBuy);
    }
    if (message.executionType !== 0) {
      writer.uint32(24).int32(message.executionType);
    }
    for (const v of message.trades) {
      TradeLog.encode(v, writer.uint32(34).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventBatchSpotExecution();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.isBuy = reader.bool();
          break;
        case 3:
          message.executionType = reader.int32();
          break;
        case 4:
          message.trades.push(TradeLog.decode(reader, reader.uint32()));
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId, _object$isBuy, _object$executionType, _object$trades;
    const message = createBaseEventBatchSpotExecution();
    message.marketId = (_object$marketId = object.marketId) !== null && _object$marketId !== void 0 ? _object$marketId : "";
    message.isBuy = (_object$isBuy = object.isBuy) !== null && _object$isBuy !== void 0 ? _object$isBuy : false;
    message.executionType = (_object$executionType = object.executionType) !== null && _object$executionType !== void 0 ? _object$executionType : 0;
    message.trades = ((_object$trades = object.trades) === null || _object$trades === void 0 ? void 0 : _object$trades.map(e => TradeLog.fromPartial(e))) || [];
    return message;
  }
};
function createBaseEventBatchDerivativeExecution() {
  return {
    marketId: "",
    isBuy: false,
    isLiquidation: false,
    cumulativeFunding: undefined,
    executionType: 0,
    trades: []
  };
}
export const EventBatchDerivativeExecution = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    if (message.isBuy === true) {
      writer.uint32(16).bool(message.isBuy);
    }
    if (message.isLiquidation === true) {
      writer.uint32(24).bool(message.isLiquidation);
    }
    if (message.cumulativeFunding !== undefined) {
      writer.uint32(34).string(message.cumulativeFunding);
    }
    if (message.executionType !== 0) {
      writer.uint32(40).int32(message.executionType);
    }
    for (const v of message.trades) {
      DerivativeTradeLog.encode(v, writer.uint32(50).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventBatchDerivativeExecution();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.isBuy = reader.bool();
          break;
        case 3:
          message.isLiquidation = reader.bool();
          break;
        case 4:
          message.cumulativeFunding = reader.string();
          break;
        case 5:
          message.executionType = reader.int32();
          break;
        case 6:
          message.trades.push(DerivativeTradeLog.decode(reader, reader.uint32()));
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId2, _object$isBuy2, _object$isLiquidation, _object$cumulativeFun, _object$executionType2, _object$trades2;
    const message = createBaseEventBatchDerivativeExecution();
    message.marketId = (_object$marketId2 = object.marketId) !== null && _object$marketId2 !== void 0 ? _object$marketId2 : "";
    message.isBuy = (_object$isBuy2 = object.isBuy) !== null && _object$isBuy2 !== void 0 ? _object$isBuy2 : false;
    message.isLiquidation = (_object$isLiquidation = object.isLiquidation) !== null && _object$isLiquidation !== void 0 ? _object$isLiquidation : false;
    message.cumulativeFunding = (_object$cumulativeFun = object.cumulativeFunding) !== null && _object$cumulativeFun !== void 0 ? _object$cumulativeFun : undefined;
    message.executionType = (_object$executionType2 = object.executionType) !== null && _object$executionType2 !== void 0 ? _object$executionType2 : 0;
    message.trades = ((_object$trades2 = object.trades) === null || _object$trades2 === void 0 ? void 0 : _object$trades2.map(e => DerivativeTradeLog.fromPartial(e))) || [];
    return message;
  }
};
function createBaseEventLostFundsFromLiquidation() {
  return {
    marketId: "",
    subaccountId: new Uint8Array(),
    lostFundsFromAvailableDuringPayout: "",
    lostFundsFromOrderCancels: ""
  };
}
export const EventLostFundsFromLiquidation = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    if (message.subaccountId.length !== 0) {
      writer.uint32(18).bytes(message.subaccountId);
    }
    if (message.lostFundsFromAvailableDuringPayout !== "") {
      writer.uint32(26).string(message.lostFundsFromAvailableDuringPayout);
    }
    if (message.lostFundsFromOrderCancels !== "") {
      writer.uint32(34).string(message.lostFundsFromOrderCancels);
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventLostFundsFromLiquidation();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.subaccountId = reader.bytes();
          break;
        case 3:
          message.lostFundsFromAvailableDuringPayout = reader.string();
          break;
        case 4:
          message.lostFundsFromOrderCancels = reader.string();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId3, _object$subaccountId, _object$lostFundsFrom, _object$lostFundsFrom2;
    const message = createBaseEventLostFundsFromLiquidation();
    message.marketId = (_object$marketId3 = object.marketId) !== null && _object$marketId3 !== void 0 ? _object$marketId3 : "";
    message.subaccountId = (_object$subaccountId = object.subaccountId) !== null && _object$subaccountId !== void 0 ? _object$subaccountId : new Uint8Array();
    message.lostFundsFromAvailableDuringPayout = (_object$lostFundsFrom = object.lostFundsFromAvailableDuringPayout) !== null && _object$lostFundsFrom !== void 0 ? _object$lostFundsFrom : "";
    message.lostFundsFromOrderCancels = (_object$lostFundsFrom2 = object.lostFundsFromOrderCancels) !== null && _object$lostFundsFrom2 !== void 0 ? _object$lostFundsFrom2 : "";
    return message;
  }
};
function createBaseEventBatchDerivativePosition() {
  return {
    marketId: "",
    positions: []
  };
}
export const EventBatchDerivativePosition = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    for (const v of message.positions) {
      SubaccountPosition.encode(v, writer.uint32(18).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventBatchDerivativePosition();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.positions.push(SubaccountPosition.decode(reader, reader.uint32()));
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId4, _object$positions;
    const message = createBaseEventBatchDerivativePosition();
    message.marketId = (_object$marketId4 = object.marketId) !== null && _object$marketId4 !== void 0 ? _object$marketId4 : "";
    message.positions = ((_object$positions = object.positions) === null || _object$positions === void 0 ? void 0 : _object$positions.map(e => SubaccountPosition.fromPartial(e))) || [];
    return message;
  }
};
function createBaseEventDerivativeMarketPaused() {
  return {
    marketId: "",
    settlePrice: "",
    totalMissingFunds: "",
    missingFundsRate: ""
  };
}
export const EventDerivativeMarketPaused = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    if (message.settlePrice !== "") {
      writer.uint32(18).string(message.settlePrice);
    }
    if (message.totalMissingFunds !== "") {
      writer.uint32(26).string(message.totalMissingFunds);
    }
    if (message.missingFundsRate !== "") {
      writer.uint32(34).string(message.missingFundsRate);
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventDerivativeMarketPaused();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.settlePrice = reader.string();
          break;
        case 3:
          message.totalMissingFunds = reader.string();
          break;
        case 4:
          message.missingFundsRate = reader.string();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId5, _object$settlePrice, _object$totalMissingF, _object$missingFundsR;
    const message = createBaseEventDerivativeMarketPaused();
    message.marketId = (_object$marketId5 = object.marketId) !== null && _object$marketId5 !== void 0 ? _object$marketId5 : "";
    message.settlePrice = (_object$settlePrice = object.settlePrice) !== null && _object$settlePrice !== void 0 ? _object$settlePrice : "";
    message.totalMissingFunds = (_object$totalMissingF = object.totalMissingFunds) !== null && _object$totalMissingF !== void 0 ? _object$totalMissingF : "";
    message.missingFundsRate = (_object$missingFundsR = object.missingFundsRate) !== null && _object$missingFundsR !== void 0 ? _object$missingFundsR : "";
    return message;
  }
};
function createBaseEventBinaryOptionsMarketUpdate() {
  return {
    market: undefined
  };
}
export const EventBinaryOptionsMarketUpdate = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.market !== undefined) {
      BinaryOptionsMarket.encode(message.market, writer.uint32(10).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventBinaryOptionsMarketUpdate();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.market = BinaryOptionsMarket.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    const message = createBaseEventBinaryOptionsMarketUpdate();
    message.market = object.market !== undefined && object.market !== null ? BinaryOptionsMarket.fromPartial(object.market) : undefined;
    return message;
  }
};
function createBaseEventNewSpotOrders() {
  return {
    marketId: "",
    buyOrders: [],
    sellOrders: []
  };
}
export const EventNewSpotOrders = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    for (const v of message.buyOrders) {
      SpotLimitOrder.encode(v, writer.uint32(18).fork()).ldelim();
    }
    for (const v of message.sellOrders) {
      SpotLimitOrder.encode(v, writer.uint32(26).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventNewSpotOrders();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.buyOrders.push(SpotLimitOrder.decode(reader, reader.uint32()));
          break;
        case 3:
          message.sellOrders.push(SpotLimitOrder.decode(reader, reader.uint32()));
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId6, _object$buyOrders, _object$sellOrders;
    const message = createBaseEventNewSpotOrders();
    message.marketId = (_object$marketId6 = object.marketId) !== null && _object$marketId6 !== void 0 ? _object$marketId6 : "";
    message.buyOrders = ((_object$buyOrders = object.buyOrders) === null || _object$buyOrders === void 0 ? void 0 : _object$buyOrders.map(e => SpotLimitOrder.fromPartial(e))) || [];
    message.sellOrders = ((_object$sellOrders = object.sellOrders) === null || _object$sellOrders === void 0 ? void 0 : _object$sellOrders.map(e => SpotLimitOrder.fromPartial(e))) || [];
    return message;
  }
};
function createBaseEventNewDerivativeOrders() {
  return {
    marketId: "",
    buyOrders: [],
    sellOrders: []
  };
}
export const EventNewDerivativeOrders = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    for (const v of message.buyOrders) {
      DerivativeLimitOrder.encode(v, writer.uint32(18).fork()).ldelim();
    }
    for (const v of message.sellOrders) {
      DerivativeLimitOrder.encode(v, writer.uint32(26).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventNewDerivativeOrders();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.buyOrders.push(DerivativeLimitOrder.decode(reader, reader.uint32()));
          break;
        case 3:
          message.sellOrders.push(DerivativeLimitOrder.decode(reader, reader.uint32()));
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId7, _object$buyOrders2, _object$sellOrders2;
    const message = createBaseEventNewDerivativeOrders();
    message.marketId = (_object$marketId7 = object.marketId) !== null && _object$marketId7 !== void 0 ? _object$marketId7 : "";
    message.buyOrders = ((_object$buyOrders2 = object.buyOrders) === null || _object$buyOrders2 === void 0 ? void 0 : _object$buyOrders2.map(e => DerivativeLimitOrder.fromPartial(e))) || [];
    message.sellOrders = ((_object$sellOrders2 = object.sellOrders) === null || _object$sellOrders2 === void 0 ? void 0 : _object$sellOrders2.map(e => DerivativeLimitOrder.fromPartial(e))) || [];
    return message;
  }
};
function createBaseEventCancelSpotOrder() {
  return {
    marketId: "",
    order: undefined
  };
}
export const EventCancelSpotOrder = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    if (message.order !== undefined) {
      SpotLimitOrder.encode(message.order, writer.uint32(18).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventCancelSpotOrder();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.order = SpotLimitOrder.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId8;
    const message = createBaseEventCancelSpotOrder();
    message.marketId = (_object$marketId8 = object.marketId) !== null && _object$marketId8 !== void 0 ? _object$marketId8 : "";
    message.order = object.order !== undefined && object.order !== null ? SpotLimitOrder.fromPartial(object.order) : undefined;
    return message;
  }
};
function createBaseEventSpotMarketUpdate() {
  return {
    market: undefined
  };
}
export const EventSpotMarketUpdate = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.market !== undefined) {
      SpotMarket.encode(message.market, writer.uint32(10).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventSpotMarketUpdate();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.market = SpotMarket.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    const message = createBaseEventSpotMarketUpdate();
    message.market = object.market !== undefined && object.market !== null ? SpotMarket.fromPartial(object.market) : undefined;
    return message;
  }
};
function createBaseEventPerpetualMarketUpdate() {
  return {
    market: undefined,
    perpetualMarketInfo: undefined,
    funding: undefined
  };
}
export const EventPerpetualMarketUpdate = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.market !== undefined) {
      DerivativeMarket.encode(message.market, writer.uint32(10).fork()).ldelim();
    }
    if (message.perpetualMarketInfo !== undefined) {
      PerpetualMarketInfo.encode(message.perpetualMarketInfo, writer.uint32(18).fork()).ldelim();
    }
    if (message.funding !== undefined) {
      PerpetualMarketFunding.encode(message.funding, writer.uint32(26).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventPerpetualMarketUpdate();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.market = DerivativeMarket.decode(reader, reader.uint32());
          break;
        case 2:
          message.perpetualMarketInfo = PerpetualMarketInfo.decode(reader, reader.uint32());
          break;
        case 3:
          message.funding = PerpetualMarketFunding.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    const message = createBaseEventPerpetualMarketUpdate();
    message.market = object.market !== undefined && object.market !== null ? DerivativeMarket.fromPartial(object.market) : undefined;
    message.perpetualMarketInfo = object.perpetualMarketInfo !== undefined && object.perpetualMarketInfo !== null ? PerpetualMarketInfo.fromPartial(object.perpetualMarketInfo) : undefined;
    message.funding = object.funding !== undefined && object.funding !== null ? PerpetualMarketFunding.fromPartial(object.funding) : undefined;
    return message;
  }
};
function createBaseEventExpiryFuturesMarketUpdate() {
  return {
    market: undefined,
    expiryFuturesMarketInfo: undefined
  };
}
export const EventExpiryFuturesMarketUpdate = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.market !== undefined) {
      DerivativeMarket.encode(message.market, writer.uint32(10).fork()).ldelim();
    }
    if (message.expiryFuturesMarketInfo !== undefined) {
      ExpiryFuturesMarketInfo.encode(message.expiryFuturesMarketInfo, writer.uint32(26).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventExpiryFuturesMarketUpdate();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.market = DerivativeMarket.decode(reader, reader.uint32());
          break;
        case 3:
          message.expiryFuturesMarketInfo = ExpiryFuturesMarketInfo.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    const message = createBaseEventExpiryFuturesMarketUpdate();
    message.market = object.market !== undefined && object.market !== null ? DerivativeMarket.fromPartial(object.market) : undefined;
    message.expiryFuturesMarketInfo = object.expiryFuturesMarketInfo !== undefined && object.expiryFuturesMarketInfo !== null ? ExpiryFuturesMarketInfo.fromPartial(object.expiryFuturesMarketInfo) : undefined;
    return message;
  }
};
function createBaseEventPerpetualMarketFundingUpdate() {
  return {
    marketId: "",
    funding: undefined,
    isHourlyFunding: false,
    fundingRate: undefined,
    markPrice: undefined
  };
}
export const EventPerpetualMarketFundingUpdate = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    if (message.funding !== undefined) {
      PerpetualMarketFunding.encode(message.funding, writer.uint32(18).fork()).ldelim();
    }
    if (message.isHourlyFunding === true) {
      writer.uint32(24).bool(message.isHourlyFunding);
    }
    if (message.fundingRate !== undefined) {
      writer.uint32(34).string(message.fundingRate);
    }
    if (message.markPrice !== undefined) {
      writer.uint32(42).string(message.markPrice);
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventPerpetualMarketFundingUpdate();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.funding = PerpetualMarketFunding.decode(reader, reader.uint32());
          break;
        case 3:
          message.isHourlyFunding = reader.bool();
          break;
        case 4:
          message.fundingRate = reader.string();
          break;
        case 5:
          message.markPrice = reader.string();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId9, _object$isHourlyFundi, _object$fundingRate, _object$markPrice;
    const message = createBaseEventPerpetualMarketFundingUpdate();
    message.marketId = (_object$marketId9 = object.marketId) !== null && _object$marketId9 !== void 0 ? _object$marketId9 : "";
    message.funding = object.funding !== undefined && object.funding !== null ? PerpetualMarketFunding.fromPartial(object.funding) : undefined;
    message.isHourlyFunding = (_object$isHourlyFundi = object.isHourlyFunding) !== null && _object$isHourlyFundi !== void 0 ? _object$isHourlyFundi : false;
    message.fundingRate = (_object$fundingRate = object.fundingRate) !== null && _object$fundingRate !== void 0 ? _object$fundingRate : undefined;
    message.markPrice = (_object$markPrice = object.markPrice) !== null && _object$markPrice !== void 0 ? _object$markPrice : undefined;
    return message;
  }
};
function createBaseEventSubaccountDeposit() {
  return {
    srcAddress: "",
    subaccountId: new Uint8Array(),
    amount: undefined
  };
}
export const EventSubaccountDeposit = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.srcAddress !== "") {
      writer.uint32(10).string(message.srcAddress);
    }
    if (message.subaccountId.length !== 0) {
      writer.uint32(18).bytes(message.subaccountId);
    }
    if (message.amount !== undefined) {
      Coin.encode(message.amount, writer.uint32(26).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventSubaccountDeposit();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.srcAddress = reader.string();
          break;
        case 2:
          message.subaccountId = reader.bytes();
          break;
        case 3:
          message.amount = Coin.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$srcAddress, _object$subaccountId2;
    const message = createBaseEventSubaccountDeposit();
    message.srcAddress = (_object$srcAddress = object.srcAddress) !== null && _object$srcAddress !== void 0 ? _object$srcAddress : "";
    message.subaccountId = (_object$subaccountId2 = object.subaccountId) !== null && _object$subaccountId2 !== void 0 ? _object$subaccountId2 : new Uint8Array();
    message.amount = object.amount !== undefined && object.amount !== null ? Coin.fromPartial(object.amount) : undefined;
    return message;
  }
};
function createBaseEventSubaccountWithdraw() {
  return {
    subaccountId: new Uint8Array(),
    dstAddress: "",
    amount: undefined
  };
}
export const EventSubaccountWithdraw = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.subaccountId.length !== 0) {
      writer.uint32(10).bytes(message.subaccountId);
    }
    if (message.dstAddress !== "") {
      writer.uint32(18).string(message.dstAddress);
    }
    if (message.amount !== undefined) {
      Coin.encode(message.amount, writer.uint32(26).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventSubaccountWithdraw();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.subaccountId = reader.bytes();
          break;
        case 2:
          message.dstAddress = reader.string();
          break;
        case 3:
          message.amount = Coin.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$subaccountId3, _object$dstAddress;
    const message = createBaseEventSubaccountWithdraw();
    message.subaccountId = (_object$subaccountId3 = object.subaccountId) !== null && _object$subaccountId3 !== void 0 ? _object$subaccountId3 : new Uint8Array();
    message.dstAddress = (_object$dstAddress = object.dstAddress) !== null && _object$dstAddress !== void 0 ? _object$dstAddress : "";
    message.amount = object.amount !== undefined && object.amount !== null ? Coin.fromPartial(object.amount) : undefined;
    return message;
  }
};
function createBaseEventSubaccountBalanceTransfer() {
  return {
    srcSubaccountId: "",
    dstSubaccountId: "",
    amount: undefined
  };
}
export const EventSubaccountBalanceTransfer = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.srcSubaccountId !== "") {
      writer.uint32(10).string(message.srcSubaccountId);
    }
    if (message.dstSubaccountId !== "") {
      writer.uint32(18).string(message.dstSubaccountId);
    }
    if (message.amount !== undefined) {
      Coin.encode(message.amount, writer.uint32(26).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventSubaccountBalanceTransfer();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.srcSubaccountId = reader.string();
          break;
        case 2:
          message.dstSubaccountId = reader.string();
          break;
        case 3:
          message.amount = Coin.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$srcSubaccount, _object$dstSubaccount;
    const message = createBaseEventSubaccountBalanceTransfer();
    message.srcSubaccountId = (_object$srcSubaccount = object.srcSubaccountId) !== null && _object$srcSubaccount !== void 0 ? _object$srcSubaccount : "";
    message.dstSubaccountId = (_object$dstSubaccount = object.dstSubaccountId) !== null && _object$dstSubaccount !== void 0 ? _object$dstSubaccount : "";
    message.amount = object.amount !== undefined && object.amount !== null ? Coin.fromPartial(object.amount) : undefined;
    return message;
  }
};
function createBaseEventBatchDepositUpdate() {
  return {
    depositUpdates: []
  };
}
export const EventBatchDepositUpdate = {
  encode(message, writer = _m0.Writer.create()) {
    for (const v of message.depositUpdates) {
      DepositUpdate.encode(v, writer.uint32(10).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventBatchDepositUpdate();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.depositUpdates.push(DepositUpdate.decode(reader, reader.uint32()));
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$depositUpdate;
    const message = createBaseEventBatchDepositUpdate();
    message.depositUpdates = ((_object$depositUpdate = object.depositUpdates) === null || _object$depositUpdate === void 0 ? void 0 : _object$depositUpdate.map(e => DepositUpdate.fromPartial(e))) || [];
    return message;
  }
};
function createBaseDerivativeMarketOrderCancel() {
  return {
    marketOrder: undefined,
    cancelQuantity: ""
  };
}
export const DerivativeMarketOrderCancel = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketOrder !== undefined) {
      DerivativeMarketOrder.encode(message.marketOrder, writer.uint32(10).fork()).ldelim();
    }
    if (message.cancelQuantity !== "") {
      writer.uint32(18).string(message.cancelQuantity);
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseDerivativeMarketOrderCancel();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketOrder = DerivativeMarketOrder.decode(reader, reader.uint32());
          break;
        case 2:
          message.cancelQuantity = reader.string();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$cancelQuantit;
    const message = createBaseDerivativeMarketOrderCancel();
    message.marketOrder = object.marketOrder !== undefined && object.marketOrder !== null ? DerivativeMarketOrder.fromPartial(object.marketOrder) : undefined;
    message.cancelQuantity = (_object$cancelQuantit = object.cancelQuantity) !== null && _object$cancelQuantit !== void 0 ? _object$cancelQuantit : "";
    return message;
  }
};
function createBaseEventCancelDerivativeOrder() {
  return {
    marketId: "",
    isLimitCancel: false,
    limitOrder: undefined,
    marketOrderCancel: undefined
  };
}
export const EventCancelDerivativeOrder = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    if (message.isLimitCancel === true) {
      writer.uint32(16).bool(message.isLimitCancel);
    }
    if (message.limitOrder !== undefined) {
      DerivativeLimitOrder.encode(message.limitOrder, writer.uint32(26).fork()).ldelim();
    }
    if (message.marketOrderCancel !== undefined) {
      DerivativeMarketOrderCancel.encode(message.marketOrderCancel, writer.uint32(34).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventCancelDerivativeOrder();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.isLimitCancel = reader.bool();
          break;
        case 3:
          message.limitOrder = DerivativeLimitOrder.decode(reader, reader.uint32());
          break;
        case 4:
          message.marketOrderCancel = DerivativeMarketOrderCancel.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId10, _object$isLimitCancel;
    const message = createBaseEventCancelDerivativeOrder();
    message.marketId = (_object$marketId10 = object.marketId) !== null && _object$marketId10 !== void 0 ? _object$marketId10 : "";
    message.isLimitCancel = (_object$isLimitCancel = object.isLimitCancel) !== null && _object$isLimitCancel !== void 0 ? _object$isLimitCancel : false;
    message.limitOrder = object.limitOrder !== undefined && object.limitOrder !== null ? DerivativeLimitOrder.fromPartial(object.limitOrder) : undefined;
    message.marketOrderCancel = object.marketOrderCancel !== undefined && object.marketOrderCancel !== null ? DerivativeMarketOrderCancel.fromPartial(object.marketOrderCancel) : undefined;
    return message;
  }
};
function createBaseEventFeeDiscountSchedule() {
  return {
    schedule: undefined
  };
}
export const EventFeeDiscountSchedule = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.schedule !== undefined) {
      FeeDiscountSchedule.encode(message.schedule, writer.uint32(10).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventFeeDiscountSchedule();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.schedule = FeeDiscountSchedule.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    const message = createBaseEventFeeDiscountSchedule();
    message.schedule = object.schedule !== undefined && object.schedule !== null ? FeeDiscountSchedule.fromPartial(object.schedule) : undefined;
    return message;
  }
};
function createBaseEventTradingRewardCampaignUpdate() {
  return {
    campaignInfo: undefined,
    campaignRewardPools: []
  };
}
export const EventTradingRewardCampaignUpdate = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.campaignInfo !== undefined) {
      TradingRewardCampaignInfo.encode(message.campaignInfo, writer.uint32(10).fork()).ldelim();
    }
    for (const v of message.campaignRewardPools) {
      CampaignRewardPool.encode(v, writer.uint32(18).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventTradingRewardCampaignUpdate();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.campaignInfo = TradingRewardCampaignInfo.decode(reader, reader.uint32());
          break;
        case 2:
          message.campaignRewardPools.push(CampaignRewardPool.decode(reader, reader.uint32()));
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$campaignRewar;
    const message = createBaseEventTradingRewardCampaignUpdate();
    message.campaignInfo = object.campaignInfo !== undefined && object.campaignInfo !== null ? TradingRewardCampaignInfo.fromPartial(object.campaignInfo) : undefined;
    message.campaignRewardPools = ((_object$campaignRewar = object.campaignRewardPools) === null || _object$campaignRewar === void 0 ? void 0 : _object$campaignRewar.map(e => CampaignRewardPool.fromPartial(e))) || [];
    return message;
  }
};
function createBaseEventTradingRewardDistribution() {
  return {
    accountRewards: []
  };
}
export const EventTradingRewardDistribution = {
  encode(message, writer = _m0.Writer.create()) {
    for (const v of message.accountRewards) {
      AccountRewards.encode(v, writer.uint32(10).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventTradingRewardDistribution();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.accountRewards.push(AccountRewards.decode(reader, reader.uint32()));
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$accountReward;
    const message = createBaseEventTradingRewardDistribution();
    message.accountRewards = ((_object$accountReward = object.accountRewards) === null || _object$accountReward === void 0 ? void 0 : _object$accountReward.map(e => AccountRewards.fromPartial(e))) || [];
    return message;
  }
};
function createBaseEventNewConditionalDerivativeOrder() {
  return {
    marketId: "",
    order: undefined,
    hash: new Uint8Array(),
    isMarket: false
  };
}
export const EventNewConditionalDerivativeOrder = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    if (message.order !== undefined) {
      DerivativeOrder.encode(message.order, writer.uint32(18).fork()).ldelim();
    }
    if (message.hash.length !== 0) {
      writer.uint32(26).bytes(message.hash);
    }
    if (message.isMarket === true) {
      writer.uint32(32).bool(message.isMarket);
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventNewConditionalDerivativeOrder();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.order = DerivativeOrder.decode(reader, reader.uint32());
          break;
        case 3:
          message.hash = reader.bytes();
          break;
        case 4:
          message.isMarket = reader.bool();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId11, _object$hash, _object$isMarket;
    const message = createBaseEventNewConditionalDerivativeOrder();
    message.marketId = (_object$marketId11 = object.marketId) !== null && _object$marketId11 !== void 0 ? _object$marketId11 : "";
    message.order = object.order !== undefined && object.order !== null ? DerivativeOrder.fromPartial(object.order) : undefined;
    message.hash = (_object$hash = object.hash) !== null && _object$hash !== void 0 ? _object$hash : new Uint8Array();
    message.isMarket = (_object$isMarket = object.isMarket) !== null && _object$isMarket !== void 0 ? _object$isMarket : false;
    return message;
  }
};
function createBaseEventCancelConditionalDerivativeOrder() {
  return {
    marketId: "",
    isLimitCancel: false,
    limitOrder: undefined,
    marketOrder: undefined
  };
}
export const EventCancelConditionalDerivativeOrder = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId !== "") {
      writer.uint32(10).string(message.marketId);
    }
    if (message.isLimitCancel === true) {
      writer.uint32(16).bool(message.isLimitCancel);
    }
    if (message.limitOrder !== undefined) {
      DerivativeLimitOrder.encode(message.limitOrder, writer.uint32(26).fork()).ldelim();
    }
    if (message.marketOrder !== undefined) {
      DerivativeMarketOrder.encode(message.marketOrder, writer.uint32(34).fork()).ldelim();
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventCancelConditionalDerivativeOrder();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.string();
          break;
        case 2:
          message.isLimitCancel = reader.bool();
          break;
        case 3:
          message.limitOrder = DerivativeLimitOrder.decode(reader, reader.uint32());
          break;
        case 4:
          message.marketOrder = DerivativeMarketOrder.decode(reader, reader.uint32());
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId12, _object$isLimitCancel2;
    const message = createBaseEventCancelConditionalDerivativeOrder();
    message.marketId = (_object$marketId12 = object.marketId) !== null && _object$marketId12 !== void 0 ? _object$marketId12 : "";
    message.isLimitCancel = (_object$isLimitCancel2 = object.isLimitCancel) !== null && _object$isLimitCancel2 !== void 0 ? _object$isLimitCancel2 : false;
    message.limitOrder = object.limitOrder !== undefined && object.limitOrder !== null ? DerivativeLimitOrder.fromPartial(object.limitOrder) : undefined;
    message.marketOrder = object.marketOrder !== undefined && object.marketOrder !== null ? DerivativeMarketOrder.fromPartial(object.marketOrder) : undefined;
    return message;
  }
};
function createBaseEventConditionalDerivativeOrderTrigger() {
  return {
    marketId: new Uint8Array(),
    isLimitTrigger: false,
    triggeredOrderHash: new Uint8Array(),
    placedOrderHash: new Uint8Array()
  };
}
export const EventConditionalDerivativeOrderTrigger = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.marketId.length !== 0) {
      writer.uint32(10).bytes(message.marketId);
    }
    if (message.isLimitTrigger === true) {
      writer.uint32(16).bool(message.isLimitTrigger);
    }
    if (message.triggeredOrderHash.length !== 0) {
      writer.uint32(26).bytes(message.triggeredOrderHash);
    }
    if (message.placedOrderHash.length !== 0) {
      writer.uint32(34).bytes(message.placedOrderHash);
    }
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventConditionalDerivativeOrderTrigger();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.marketId = reader.bytes();
          break;
        case 2:
          message.isLimitTrigger = reader.bool();
          break;
        case 3:
          message.triggeredOrderHash = reader.bytes();
          break;
        case 4:
          message.placedOrderHash = reader.bytes();
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$marketId13, _object$isLimitTrigge, _object$triggeredOrde, _object$placedOrderHa;
    const message = createBaseEventConditionalDerivativeOrderTrigger();
    message.marketId = (_object$marketId13 = object.marketId) !== null && _object$marketId13 !== void 0 ? _object$marketId13 : new Uint8Array();
    message.isLimitTrigger = (_object$isLimitTrigge = object.isLimitTrigger) !== null && _object$isLimitTrigge !== void 0 ? _object$isLimitTrigge : false;
    message.triggeredOrderHash = (_object$triggeredOrde = object.triggeredOrderHash) !== null && _object$triggeredOrde !== void 0 ? _object$triggeredOrde : new Uint8Array();
    message.placedOrderHash = (_object$placedOrderHa = object.placedOrderHash) !== null && _object$placedOrderHa !== void 0 ? _object$placedOrderHa : new Uint8Array();
    return message;
  }
};
function createBaseEventOrderFail() {
  return {
    account: new Uint8Array(),
    hashes: [],
    flags: []
  };
}
export const EventOrderFail = {
  encode(message, writer = _m0.Writer.create()) {
    if (message.account.length !== 0) {
      writer.uint32(10).bytes(message.account);
    }
    for (const v of message.hashes) {
      writer.uint32(18).bytes(v);
    }
    writer.uint32(26).fork();
    for (const v of message.flags) {
      writer.uint32(v);
    }
    writer.ldelim();
    return writer;
  },
  decode(input, length) {
    const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseEventOrderFail();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          message.account = reader.bytes();
          break;
        case 2:
          message.hashes.push(reader.bytes());
          break;
        case 3:
          if ((tag & 7) === 2) {
            const end2 = reader.uint32() + reader.pos;
            while (reader.pos < end2) {
              message.flags.push(reader.uint32());
            }
          } else {
            message.flags.push(reader.uint32());
          }
          break;
        default:
          reader.skipType(tag & 7);
          break;
      }
    }
    return message;
  },
  fromPartial(object) {
    var _object$account, _object$hashes, _object$flags;
    const message = createBaseEventOrderFail();
    message.account = (_object$account = object.account) !== null && _object$account !== void 0 ? _object$account : new Uint8Array();
    message.hashes = ((_object$hashes = object.hashes) === null || _object$hashes === void 0 ? void 0 : _object$hashes.map(e => e)) || [];
    message.flags = ((_object$flags = object.flags) === null || _object$flags === void 0 ? void 0 : _object$flags.map(e => e)) || [];
    return message;
  }
};