Skip to content

Commit

Permalink
Merge branch 'master' into milad/update-readme-initial-setup
Browse files Browse the repository at this point in the history
  • Loading branch information
miladz68 authored Dec 19, 2024
2 parents f1f3c4b + 764cbf9 commit 57ad88d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 58 deletions.
49 changes: 22 additions & 27 deletions x/dex/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ const (
GoodTilBlockHeightFlag = "good-til-block-height"
// GoodTilBlockTimeFlag is good til block time flag.
GoodTilBlockTimeFlag = "good-til-block-time"
// OrderTypeLimit is limit order type.
OrderTypeLimit = "limit"
// OrderTypeMarket is limit order market.
OrderTypeMarket = "market"
// TimeInForce is time-in-force flag.
TimeInForce = "time-in-force"
)
Expand Down Expand Up @@ -60,16 +56,19 @@ func GetTxCmd() *cobra.Command {
func CmdPlaceOrder() *cobra.Command {
availableTimeInForces := lo.Values(types.TimeInForce_name)
sort.Strings(availableTimeInForces)
availableOrderTypes := lo.Values(types.OrderType_name)
sort.Strings(availableTimeInForces)
availableSides := lo.Values(types.Side_name)
sort.Strings(availableTimeInForces)
cmd := &cobra.Command{
Use: "place-order [type (limit,market)] [id] [base_denom] [quote_denom] [quantity] [side] --price 123e-2 --time-in-force=" + strings.Join(availableTimeInForces, ",") + " --good-til-block-height=123 --good-til-block-time=1727124446 --from [sender]", //nolint:lll // string example
Use: "place-order [type (" + strings.Join(availableOrderTypes, ",") + ")] [id] [base_denom] [quote_denom] [quantity] [side (" + strings.Join(availableSides, ",") + ")] --price 123e-2 --time-in-force=" + strings.Join(availableTimeInForces, ",") + " --good-til-block-height=123 --good-til-block-time=1727124446 --from [sender]", //nolint:lll // string example
Args: cobra.ExactArgs(6),
Short: "Place new order",
Long: strings.TrimSpace(
fmt.Sprintf(`Place new order.
Example:
$ %s tx %s place-order id1 denom1 denom2 123e-2 10000 buy --from [sender]
`,
$ %s tx %s place-order ORDER_TYPE_LIMIT "my-order-id1" denom1 denom2 1000 SIDE_SELL --price 12e-1 --time-in-force=TIME_IN_FORCE_GTC --from [sender]`, //nolint:lll // string example
version.AppName, types.ModuleName,
),
),
Expand All @@ -81,25 +80,8 @@ $ %s tx %s place-order id1 denom1 denom2 123e-2 10000 buy --from [sender]

sender := clientCtx.GetFromAddress()

var orderType types.OrderType
timeInForceString, err := cmd.Flags().GetString(TimeInForce)
timeInForceInt, ok := types.TimeInForce_value[timeInForceString]
orderType, ok := types.OrderType_value[args[0]]
if !ok {
return errors.Errorf(
"unknown TimeInForce '%s',available TimeInForces: %s",
timeInForceString, strings.Join(availableTimeInForces, ","),
)
}
if err != nil {
return errors.WithStack(err)
}
timeInForce := types.TimeInForce(timeInForceInt)
switch args[0] {
case OrderTypeLimit:
orderType = types.ORDER_TYPE_LIMIT
case OrderTypeMarket:
orderType = types.ORDER_TYPE_MARKET
default:
return errors.Errorf("unknown type '%s'", args[0])
}

Expand All @@ -109,7 +91,7 @@ $ %s tx %s place-order id1 denom1 denom2 123e-2 10000 buy --from [sender]

quantity, ok := sdkmath.NewIntFromString(args[4])
if !ok {
return sdkerrors.Wrap(err, "invalid quantity")
return errors.New("invalid quantity")
}

side, ok := types.Side_value[args[5]]
Expand Down Expand Up @@ -144,9 +126,22 @@ $ %s tx %s place-order id1 denom1 denom2 123e-2 10000 buy --from [sender]
goodTilBlockTime = lo.ToPtr(time.Unix(goodTilBlockTimeNum, 0))
}

timeInForceString, err := cmd.Flags().GetString(TimeInForce)
timeInForceInt, ok := types.TimeInForce_value[timeInForceString]
if !ok {
return errors.Errorf(
"unknown TimeInForce '%s',available TimeInForces: %s",
timeInForceString, strings.Join(availableTimeInForces, ","),
)
}
if err != nil {
return errors.WithStack(err)
}
timeInForce := types.TimeInForce(timeInForceInt)

msg := &types.MsgPlaceOrder{
Sender: sender.String(),
Type: orderType,
Type: types.OrderType(orderType),
ID: id,
BaseDenom: baseDenom,
QuoteDenom: quoteDenom,
Expand Down
12 changes: 1 addition & 11 deletions x/dex/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,8 @@ func placeOrder(
testNetwork *network.Network,
order types.Order,
) {
var orderType string
switch order.Type {
case types.ORDER_TYPE_LIMIT:
orderType = cli.OrderTypeLimit
case types.ORDER_TYPE_MARKET:
orderType = cli.OrderTypeMarket
default:
requireT.Fail(fmt.Sprintf("unknown type '%s'", order.Type))
}

args := []string{
orderType,
order.Type.String(),
order.ID,
order.BaseDenom,
order.QuoteDenom,
Expand Down
4 changes: 1 addition & 3 deletions x/dex/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,6 @@ func TestKeeper_ComputePriceTick(t *testing.T) {
base: 3.0,
quote: 27.123,
},

{
name: "10000.0/10000.0",
base: 10000.0,
Expand Down Expand Up @@ -820,7 +819,6 @@ func TestKeeper_ComputePriceTick(t *testing.T) {
base: 3.0,
quote: 1.0,
},

{
name: "3100000.0/8.0",
base: 3100000.0,
Expand Down Expand Up @@ -1247,7 +1245,7 @@ func getAccountDenomsOrdersCount(
}

func assertTickCalculations(t *testing.T, base, quote float64) {
tickExponent := -5
tickExponent := -8

finalTickExp := math.Floor(math.Log10(quote/base)) + float64(tickExponent)
finalTick := math.Pow(10, finalTickExp)
Expand Down
32 changes: 15 additions & 17 deletions x/dex/spec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Users can place orders with the following attributes:
* `quote_denom` - when you buy, you are selling the `quote_denom`, when you sell, you are buying the `quote_denom`.
* `price` - value of one unit of the `base_denom` expressed in terms of the `quote_denom`. It indicates how much of the
`quote_denom` is needed to buy one unit of the `base_denom`.
* `quantity` - is amount of the base `base_denom` being traded.
* `quantity` - is amount of the `base_denom` being traded.
* `side`
* `sell` - means that the order is to sell `base_denom` `quantity` with the `price`.
* `buy` - means that the order is to buy `base_denom` `quantity` with the `price`.
Expand Down Expand Up @@ -140,7 +140,7 @@ whether within the same trading pair or its inverse, optimizing the trading expe
To provide a better trading experience we define the [price_tick](https://www.investopedia.com/terms/t/tick.asp) for
each order book. The `price_tick` mostly depends on the price of the assets traded, that's why we can define the
variable for a token used to define the order book `price_tick`. This variable is named `unified_ref_amount`.
`unified_ref_amount`for token represents the amount of the token subunit you need to pay to buy 1 USD dollar. If the
`unified_ref_amount` for token represents the amount of the token subunit you need to pay to buy 1 USD dollar. If the
token is issued on the Coreum chain, that variable can be set/updated by the token admin. If the token is IBC token,
or the token doesn't have and admin this variable can be set/updated by the chain governance. If the
`unified_ref_amount` is not set for a token, the `unified_ref_amount` is equal to 10^6.
Expand All @@ -158,11 +158,11 @@ Tick size example:

| unified_ref_amount(AAA) | unified_ref_amount(BBB) | price_tick(AAA/BBB) | price_tick(BBB/AAA) |
|-------------------------|-------------------------|---------------------|---------------------|
| 10000.0 | 10000.0 | 10^-5 | 10^-5 |
| 3000.0 | 20.0 | 10^-8 | 10^-3 |
| 3100000.0 | 8.0 | 10^-11 | 1 |
| 0.00017 | 100.0 | 1 | 10^-11 |
| 0.000001 | 10000000 | 10^8 | 10^-18 |
| 10000.0 | 10000.0 | 10^-8 | 10^-8 |
| 3000.0 | 20.0 | 10^-11 | 10^-6 |
| 3100000.0 | 8.0 | 10^-14 | 10^-3 |
| 0.00017 | 100.0 | 10^-3 | 10^-14 |
| 0.000001 | 10000000 | 10^5 | 10^-21 |

The update of the `unified_ref_amount` doesn't affect the created orders.

Expand Down Expand Up @@ -192,12 +192,12 @@ Examples:

### Balance locking/freezing/whitelisting/clawback.

When a user places an order we lock the coins in the assetft (similar to freezing), both assetft and native coins. Also,
we reserve the expected receiving amount if whitelisting for the token the user expects to receive is enabled.
At the time of the placement we enforce all assetft rules. If, at the time of matching, the assetft rules for the
maker orders are changed, the orders will be still executed with the amounts in the order book. That's why to avoid
unexpected behavior with the `freezing/whitelisting/clawback` the token admin should [cancel](#Order-cancellation) users
orders before update the rules.
When a user places an order we lock the coins in the assetft (similar to freezing). Also, we reserve the expected
receiving amount if whitelisting for the token the user expects to receive is enabled. At the time of the placement we
enforce all assetft rules. If, at the time of matching, the assetft rules for the maker orders are changed, the orders
will be still executed with the amounts in the order book. That's why to avoid unexpected behavior with the
`freezing/whitelisting/clawback` the token admin should [cancel](#Order-cancellation) user's orders before update the
rules.

### Time in force

Expand Down Expand Up @@ -228,12 +228,12 @@ The `good_til` setting specifies how long an order remains active based on certa
This feature introduces an order reserve requirement for each order placed on the chain. The reserve acts as a security
deposit that ensures users have a vested interest in their orders, helping to prevent spam and malicious activities.
Once the order is executed that reserve is released.
The default reserve amount is `10 CORE` and can be updated by the governance.
The default reserve amount is `10 CORE` and can be updated by the governance.

### Max orders limit

The number of active orders a user can have for each denom is limited by a value called `max_orders_per_denom`,
which is determined by DEX governance.
which is determined by DEX governance. The default value is 100.

### Events

Expand Down Expand Up @@ -261,8 +261,6 @@ It grants specific administrative or governance roles the power to manage and ov
safeguard against potential issues such as erroneous trades, malicious activity, or market manipulation. For the token
admin to cancel the user's orders, the `dex_order_cancellation` feature must be enabled.

### Restricted trade

### Block DEX

The asset FT token admin at time of issues can set the `block_dex` feature, which will block the DEX completely for the
Expand Down

0 comments on commit 57ad88d

Please sign in to comment.