Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEX-994 dex-common #678

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
  •  
  •  
  •  
10 changes: 9 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ lazy val commonOwaspSettings = Seq(
dependencyCheckAssemblyAnalyzerEnabled := Some(false)
)

lazy val `dex-common` = project
.settings(commonOwaspSettings)
.dependsOn(`waves-grpc`)

// Used in unit and integration tests
lazy val `dex-test-common` = project.settings(commonOwaspSettings).dependsOn(`waves-integration`)

lazy val dex = project
.settings(commonOwaspSettings)
.dependsOn(
`waves-integration`,
`dex-common` % "compile->compile;test->test",
`dex-test-common` % "test->compile"
)

Expand Down Expand Up @@ -53,14 +58,16 @@ lazy val `waves-ext` = project
.settings(commonOwaspSettings)
.dependsOn(
`waves-grpc`,
`dex-common` % "test->test",
`dex-test-common` % "test->compile"
)

lazy val `waves-integration` = project.settings(commonOwaspSettings).dependsOn(`waves-grpc`)
lazy val `waves-integration` = project.settings(commonOwaspSettings).dependsOn(`waves-grpc`, `dex-common` % "compile->compile;test->test")

lazy val `waves-integration-it` = project
.settings(commonOwaspSettings)
.dependsOn(
`dex-common` % "compile->compile;test->test",
`waves-integration`,
`dex-it-common`
)
Expand All @@ -84,6 +91,7 @@ lazy val root = (project in file("."))
.settings(name := "dex-root")
.settings(commonOwaspSettings)
.aggregate(
`dex-common`,
dex,
`dex-it`,
`dex-load`,
Expand Down
3 changes: 3 additions & 0 deletions dex-common/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description := "Shared classes for matcher, waves-integration and liquidity pools project"

libraryDependencies ++= Dependencies.Module.dexCommon
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.actors.address.AddressActor.Reply.GetState
import com.wavesplatform.dex.domain.asset.Asset
import com.wavesplatform.dex.domain.order.Order
import io.swagger.annotations.ApiModelProperty
Expand All @@ -21,15 +20,4 @@ object HttpAddressState {

implicit val HttpAddressStateFormat: Format[HttpAddressState] = Json.format

def apply(s: GetState): HttpAddressState = new HttpAddressState(
s.balances.regular.xs,
s.balances.reserved.xs,
s.balances.allTradableBalance.xs,
s.balances.unconfirmed.xs,
s.balances.outgoingLeasing.getOrElse(0L),
s.balances.notCreatedTxs map { case (k, v) => (k.toString, v.pessimisticChanges.xs) },
s.balances.notObservedTxs map { case (k, v) => (k.toString, v.executionTotalVolumeDiff.xs) },
s.placementQueue.toList
)

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.actors.OrderBookDirectoryActor.AssetInfo
import io.swagger.annotations.ApiModelProperty
import play.api.libs.json.{Format, Json}

case class HttpAssetInfo(@ApiModelProperty(example = "8") decimals: Int)

object HttpAssetInfo {
implicit val httpAssetInfoFormat: Format[HttpAssetInfo] = Json.format[HttpAssetInfo]
def fromAssetInfo(ai: AssetInfo): HttpAssetInfo = HttpAssetInfo(ai.decimals)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.wavesplatform.dex.api.http.entities

import enumeratum.{Enum, EnumEntry, PlayLowercaseJsonEnum}

sealed trait HttpAssetType extends EnumEntry

object HttpAssetType extends Enum[HttpAssetType] with PlayLowercaseJsonEnum[HttpAssetType] {
override val values = findValues

case object Amount extends HttpAssetType

case object Price extends HttpAssetType

case object Spending extends HttpAssetType

case object Receiving extends HttpAssetType

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.domain.order.OrderType

final case class HttpLastTrade(price: Long, amount: Long, side: OrderType) {
override def toString: String = s"LastTrade(p=$price, a=$amount, $side)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.wavesplatform.dex.api.http.entities

final case class HttpLevelAgg(amount: Long, price: Long)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wavesplatform.dex.api.http.entities

case class HttpMarketStatus(
lastTrade: Option[HttpLastTrade],
bestBid: Option[HttpLevelAgg],
bestAsk: Option[HttpLevelAgg]
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.json
import com.wavesplatform.dex.utils.JsonImplicits
import io.swagger.annotations.ApiModelProperty
import play.api.libs.json.{Format, Json, OFormat}

case class HttpMatchingRules(@ApiModelProperty(dataType = "string", example = "0.0001") tickSize: Double)

object HttpMatchingRules {
implicit val doubleFormat: Format[Double] = json.stringAsDoubleFormat
implicit val doubleFormat: Format[Double] = JsonImplicits.stringAsDoubleFormat
implicit val httpMatchingRulesFormat: OFormat[HttpMatchingRules] = Json.format[HttpMatchingRules]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ import com.wavesplatform.dex.domain.asset.Asset.{IssuedAsset, Waves}
import com.wavesplatform.dex.domain.asset.{Asset, AssetPair}
import com.wavesplatform.dex.domain.bytes.ByteStr
import com.wavesplatform.dex.domain.model.Denormalization
import com.wavesplatform.dex.model.LevelAgg
import com.wavesplatform.dex.tool.LocaleUtils

import java.text.DecimalFormat

@JsonSerialize(using = classOf[HttpOrderBook.Serializer])
case class HttpOrderBook(timestamp: Long, pair: AssetPair, bids: Seq[LevelAgg], asks: Seq[LevelAgg], assetPairDecimals: Option[(Int, Int)] = None)
case class HttpOrderBook(
timestamp: Long,
pair: AssetPair,
bids: Seq[HttpLevelAgg],
asks: Seq[HttpLevelAgg],
assetPairDecimals: Option[(Int, Int)] = None
)

object HttpOrderBook {

Expand Down Expand Up @@ -49,7 +54,7 @@ object HttpOrderBook {
private def formatValue(value: BigDecimal, decimals: Int): String =
new DecimalFormat(s"0.${"0" * decimals}", LocaleUtils.symbols).format(value)

private def denormalizeAndSerializeSide(side: Seq[LevelAgg], amountAssetDecimals: Int, priceAssetDecimals: Int, jg: JsonGenerator): Unit =
private def denormalizeAndSerializeSide(side: Seq[HttpLevelAgg], amountAssetDecimals: Int, priceAssetDecimals: Int, jg: JsonGenerator): Unit =
side.foreach { levelAgg =>
val denormalizedPrice = Denormalization.denormalizePrice(levelAgg.price, amountAssetDecimals, priceAssetDecimals)
val denormalizedAmount = Denormalization.denormalizeAmountAndFee(levelAgg.amount, amountAssetDecimals)
Expand Down
24 changes: 2 additions & 22 deletions ...p/entities/HttpOrderBookHistoryItem.scala → ...p/entities/HttpOrderBookHistoryItem.scala
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.db.OrderDb.orderIdOrdering
import com.wavesplatform.dex.domain.asset.{Asset, AssetPair}
import com.wavesplatform.dex.domain.order.{Order, OrderType}
import com.wavesplatform.dex.model.{AcceptedOrderType, OrderInfo, OrderStatus}
import com.wavesplatform.dex.domain.order.{AcceptedOrderType, Order, OrderType}
import io.swagger.annotations.ApiModelProperty
import play.api.libs.json.{Json, OFormat}

Expand Down Expand Up @@ -52,24 +50,6 @@ object HttpOrderBookHistoryItem {

implicit val httpOrderBookHistoryItemFormat: OFormat[HttpOrderBookHistoryItem] = Json.format

val httpOrderBookHistoryItemOrdering: Ordering[HttpOrderBookHistoryItem] = orderIdOrdering.on(item => (item.id, item.timestamp))

def fromOrderInfo(id: Order.Id, info: OrderInfo[OrderStatus]): HttpOrderBookHistoryItem = HttpOrderBookHistoryItem(
id = id,
`type` = info.side,
orderType = info.orderType,
amount = info.amount,
filled = info.status.filledAmount,
price = info.price,
fee = info.matcherFee,
filledFee = info.status.filledFee,
feeAsset = info.feeAsset,
timestamp = info.timestamp,
status = info.status.name,
assetPair = info.assetPair,
avgWeighedPrice = info.avgWeighedPrice,
version = info.orderVersion,
totalExecutedPriceAssets = info.totalExecutedPriceAssets
)
val httpOrderBookHistoryItemOrdering: Ordering[HttpOrderBookHistoryItem] = Order.orderIdOrdering.on(item => (item.id, item.timestamp))

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.actors.orderbook.AggregatedOrderBookActor.MarketStatus
import com.wavesplatform.dex.domain.order.OrderType
import com.wavesplatform.dex.model.{LastTrade, LevelAgg}
import io.swagger.annotations.ApiModelProperty
import play.api.libs.json.{Json, OFormat}

Expand All @@ -22,32 +20,32 @@ case class HttpOrderBookStatus(
) {

@ApiModelProperty(hidden = true)
val lastTrade: Option[LastTrade] =
val lastTrade: Option[HttpLastTrade] =
for {
lp <- lastPrice
la <- lastAmount
ls <- lastSide
} yield LastTrade(lp, la, ls)
} yield HttpLastTrade(lp, la, ls)

@ApiModelProperty(hidden = true)
val bestBid: Option[LevelAgg] =
val bestBid: Option[HttpLevelAgg] =
for {
bba <- bidAmount
bbp <- bid
} yield LevelAgg(bba, bbp)
} yield HttpLevelAgg(bba, bbp)

@ApiModelProperty(hidden = true)
val bestAsk: Option[LevelAgg] =
val bestAsk: Option[HttpLevelAgg] =
for {
baa <- askAmount
bap <- ask
} yield LevelAgg(baa, bap)
} yield HttpLevelAgg(baa, bap)

}

object HttpOrderBookStatus {

def fromMarketStatus(ms: MarketStatus): HttpOrderBookStatus =
def fromMarketStatus(ms: HttpMarketStatus): HttpOrderBookStatus =
HttpOrderBookStatus(
lastPrice = ms.lastTrade.map(_.price),
lastAmount = ms.lastTrade.map(_.amount),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.api.http.entities.HttpOrderFeeMode.{FeeModeDynamic, FeeModeFixed, FeeModePercent}
import com.wavesplatform.dex.domain.asset.Asset
import com.wavesplatform.dex.settings.{AssetType, OrderFeeSettings}
import io.swagger.annotations.{ApiModel, ApiModelProperty}
import play.api.libs.json._

Expand Down Expand Up @@ -59,7 +58,7 @@ object HttpOrderFeeMode {
dataType = "string",
allowableValues = "amount, price, spending, receiving",
example = "price"
) `type`: AssetType,
) `type`: HttpAssetType,
@ApiModelProperty(
value = "Min fee in percents",
dataType = "number",
Expand All @@ -86,10 +85,4 @@ object HttpOrderFeeMode {

private def toJson[T](key: String, x: T)(implicit w: Writes[T]): JsObject = Json.obj(key -> w.writes(x))

def fromSettings(settings: OrderFeeSettings, matcherAccountFee: Long, allRates: Map[Asset, Double]): HttpOrderFeeMode = settings match {
case x: OrderFeeSettings.DynamicSettings => FeeModeDynamic(x.maxBaseFee + matcherAccountFee, allRates)
case OrderFeeSettings.FixedSettings(assetId, minFee) => FeeModeFixed(assetId, minFee)
case OrderFeeSettings.PercentSettings(assetType, minFee) => FeeModePercent(assetType, minFee)
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.json
import com.wavesplatform.dex.settings.OrderRestrictionsSettings
import com.wavesplatform.dex.utils.JsonImplicits
import io.swagger.annotations.ApiModelProperty
import play.api.libs.json.{Format, Json, OFormat}

Expand All @@ -16,17 +15,7 @@ case class HttpOrderRestrictions(

object HttpOrderRestrictions {

implicit val doubleFormat: Format[Double] = json.stringAsDoubleFormat
implicit val doubleFormat: Format[Double] = JsonImplicits.stringAsDoubleFormat
implicit val httpOrderRestrictionsFormat: OFormat[HttpOrderRestrictions] = Json.format[HttpOrderRestrictions]

def fromSettings(settings: OrderRestrictionsSettings): HttpOrderRestrictions =
HttpOrderRestrictions(
stepAmount = settings.stepAmount,
minAmount = settings.minAmount,
maxAmount = settings.maxAmount,
stepPrice = settings.stepPrice,
minPrice = settings.minPrice,
maxPrice = settings.maxPrice
)

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.wavesplatform.dex.api.http.entities

import cats.syntax.option._
import com.wavesplatform.dex.api.http.entities.HttpOrderStatus.Status
import com.wavesplatform.dex.meta.getSimpleName
import com.wavesplatform.dex.model.OrderStatus
import io.swagger.annotations.ApiModelProperty
import play.api.libs.json.{Format, Json, Reads, Writes}

Expand All @@ -30,14 +28,6 @@ object HttpOrderStatus {

implicit val httpOrderStatusFormat: Format[HttpOrderStatus] = Json.format

def from(x: OrderStatus): HttpOrderStatus = x match {
case OrderStatus.Accepted => HttpOrderStatus(Status.Accepted)
case OrderStatus.NotFound => HttpOrderStatus(Status.NotFound, message = Some("The limit order is not found"))
case OrderStatus.PartiallyFilled(filledAmount, filledFee) => HttpOrderStatus(Status.PartiallyFilled, filledAmount.some, filledFee.some)
case OrderStatus.Filled(filledAmount, filledFee) => HttpOrderStatus(Status.Filled, filledAmount.some, filledFee.some)
case OrderStatus.Cancelled(filledAmount, filledFee) => HttpOrderStatus(Status.Cancelled, filledAmount.some, filledFee.some)
}

sealed abstract class Status extends Product with Serializable {
val name: String = getSimpleName(this)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.utils.JsonImplicits
import io.swagger.annotations.{ApiModel, ApiModelProperty}
import play.api.libs.json.{Format, Json}

Expand All @@ -24,7 +25,7 @@ object HttpSuccessfulBatchCancel {
implicit val httpSuccessfulBatchCancelFormat: Format[HttpSuccessfulBatchCancel] = {

implicit val ef: Format[Either[HttpError, HttpSuccessfulSingleCancel]] =
com.wavesplatform.dex.json.eitherFormat[HttpError, HttpSuccessfulSingleCancel]
JsonImplicits.eitherFormat[HttpError, HttpSuccessfulSingleCancel]

Json.format
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.app.MatcherStatus
import com.wavesplatform.dex.grpc.integration.clients.combined.CombinedStream
import com.wavesplatform.dex.statuses.{CombinedStreamStatus, MatcherStatus}
import io.swagger.annotations.ApiModelProperty
import play.api.libs.json.{Format, Json}

case class HttpSystemStatus(
@ApiModelProperty() service: MatcherStatus,
@ApiModelProperty() blockchain: CombinedStream.Status
@ApiModelProperty() blockchain: CombinedStreamStatus
)

object HttpSystemStatus {

implicit val HttpSystemStatusFormat: Format[HttpSystemStatus] = Json.format

def apply(service: MatcherStatus, blockchain: CombinedStream.Status) = new HttpSystemStatus(service, blockchain)
def apply(service: MatcherStatus, blockchain: CombinedStreamStatus) = new HttpSystemStatus(service, blockchain)

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.wavesplatform.dex.api.http.entities

import com.wavesplatform.dex.model.LevelAgg
import io.swagger.annotations.ApiModelProperty
import play.api.libs.json.{Json, Reads}

case class HttpV0LevelAgg(@ApiModelProperty(example = "83187648950") amount: Long, @ApiModelProperty(example = "12079") price: Long)

object HttpV0LevelAgg {
implicit val httpV0LevelAggReads: Reads[HttpV0LevelAgg] = Json.reads
def fromLevelAgg(la: LevelAgg): HttpV0LevelAgg = HttpV0LevelAgg(la.amount, la.price)

}
Loading