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

Integrate order sequence into new DEX events and order queries. #1027

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/coreum/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func Lint(ctx context.Context, deps types.DepsFunc) error {
CompileAllSmartContracts,
formatProto,
lintProto,
// breakingProto, // TODO: uncomment in next PR
// breakingProto, // FIXME: uncomment in next PR
)
return golang.Lint(ctx, deps)
}
Expand Down
2 changes: 1 addition & 1 deletion build/coreum/generate-proto-breaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

//go:embed proto-breaking.tmpl.json
//nolint:unused // TODO: uncomment in next PR
//nolint:unused // FIXME: uncomment in next PR
var configBreakingTmpl string

//nolint:deadcode
Expand Down
66 changes: 39 additions & 27 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@
- [coreum/dex/v1/event.proto](#coreum/dex/v1/event.proto)
- [EventOrderClosed](#coreum.dex.v1.EventOrderClosed)
- [EventOrderCreated](#coreum.dex.v1.EventOrderCreated)
- [EventOrderPlaced](#coreum.dex.v1.EventOrderPlaced)
- [EventOrderReduced](#coreum.dex.v1.EventOrderReduced)

- [coreum/dex/v1/genesis.proto](#coreum/dex/v1/genesis.proto)
- [AccountDenomOrdersCount](#coreum.dex.v1.AccountDenomOrdersCount)
- [GenesisState](#coreum.dex.v1.GenesisState)
- [OrderBookDataWithID](#coreum.dex.v1.OrderBookDataWithID)
- [OrderWithSequence](#coreum.dex.v1.OrderWithSequence)

- [coreum/dex/v1/order.proto](#coreum/dex/v1/order.proto)
- [CancelGoodTil](#coreum.dex.v1.CancelGoodTil)
Expand Down Expand Up @@ -4488,14 +4488,18 @@ EventGas is emitted by deterministic gas module to report gas information.
### EventOrderClosed

```
EventOrderClosed is emitted when the order is closed during matching or manually.
EventOrderClosed is emitted when the order is closed during matching or manually, and removed from the order book.
```



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `order` | [Order](#coreum.dex.v1.Order) | | |
| `creator` | [string](#string) | | `creator is order creator address.` |
| `id` | [string](#string) | | `id is unique order ID.` |
| `sequence` | [uint64](#uint64) | | `sequence is unique order sequence.` |
| `remaining_quantity` | [string](#string) | | `remaining_quantity is remaining filling quantity sell/buy.` |
| `remaining_balance` | [string](#string) | | `remaining_balance is remaining order balance.` |



Expand All @@ -4514,7 +4518,32 @@ EventOrderCreated is emitted when the limit order is saved to the order book.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `order` | [Order](#coreum.dex.v1.Order) | | |
| `creator` | [string](#string) | | `creator is order creator address.` |
| `id` | [string](#string) | | `id is unique order ID.` |
| `sequence` | [uint64](#uint64) | | `sequence is unique order sequence.` |
| `remaining_quantity` | [string](#string) | | `remaining_quantity is remaining filling quantity sell/buy.` |
| `remaining_balance` | [string](#string) | | `remaining_balance is remaining order balance.` |






<a name="coreum.dex.v1.EventOrderPlaced"></a>

### EventOrderPlaced

```
EventOrderPlaced is emitted when a new order is placed and new sequence is generated for it.
```



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `creator` | [string](#string) | | `creator is order creator address.` |
| `id` | [string](#string) | | `id is unique order ID.` |
| `sequence` | [uint64](#uint64) | | `sequence is unique order sequence.` |



Expand All @@ -4535,6 +4564,7 @@ EventOrderReduced is emitted when the order is reduced during the matching.
| ----- | ---- | ----- | ----------- |
| `creator` | [string](#string) | | `creator is order creator address.` |
| `id` | [string](#string) | | `id is unique order ID.` |
| `sequence` | [uint64](#uint64) | | `sequence is unique order sequence.` |
| `sent_coin` | [string](#string) | | `sent_coin is coin sent during matching.` |
| `received_coin` | [string](#string) | | `received_coin is coin received during matching.` |

Expand Down Expand Up @@ -4594,7 +4624,8 @@ GenesisState defines the module genesis state.
| ----- | ---- | ----- | ----------- |
| `params` | [Params](#coreum.dex.v1.Params) | | `params defines all the parameters of the module.` |
| `order_books` | [OrderBookDataWithID](#coreum.dex.v1.OrderBookDataWithID) | repeated | |
| `orders` | [OrderWithSequence](#coreum.dex.v1.OrderWithSequence) | repeated | |
| `orders` | [Order](#coreum.dex.v1.Order) | repeated | |
| `order_sequence` | [uint64](#uint64) | | `order_sequence is current order sequence;` |
| `accounts_denoms_orders_counts` | [AccountDenomOrdersCount](#coreum.dex.v1.AccountDenomOrdersCount) | repeated | |


Expand All @@ -4621,26 +4652,6 @@ OrderBookDataWithID is a order book data with it's corresponding ID.




<a name="coreum.dex.v1.OrderWithSequence"></a>

### OrderWithSequence

```
OrderWithSequence is a order with it's corresponding sequence.
```



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `sequence` | [uint64](#uint64) | | `sequence is order sequence.` |
| `order` | [Order](#coreum.dex.v1.Order) | | `data is order book data.` |





<!-- end messages -->

<!-- end enums -->
Expand Down Expand Up @@ -4671,7 +4682,7 @@ CancelGoodTil is a cancel good til message for the delay router.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `creator` | [string](#string) | | `creator is order creator address.` |
| `order_seq` | [uint64](#uint64) | | `order_seq is order sequence.` |
| `order_sequence` | [uint64](#uint64) | | `order_sequence is order sequence.` |



Expand Down Expand Up @@ -4713,6 +4724,7 @@ Order is a DEX order.
| `creator` | [string](#string) | | `creator is order creator address.` |
| `type` | [OrderType](#coreum.dex.v1.OrderType) | | `type is order type.` |
| `id` | [string](#string) | | `id is unique order ID.` |
| `sequence` | [uint64](#uint64) | | `sequence is unique order sequence generated at the time of the order placement.` |
| `base_denom` | [string](#string) | | `base_denom is base order denom.` |
| `quote_denom` | [string](#string) | | `quote_denom is quote order denom` |
| `price` | [string](#string) | | `price is value of one unit of the base_denom expressed in terms of the quote_denom.` |
Expand Down Expand Up @@ -4764,7 +4776,7 @@ OrderBookRecord is a single order book record.
| `order_book_id` | [uint32](#uint32) | | `order_book_id is order book ID.` |
| `side` | [Side](#coreum.dex.v1.Side) | | `side is order side.` |
| `price` | [string](#string) | | `price is order book record price.` |
| `order_seq` | [uint64](#uint64) | | `order_seq is order sequence.` |
| `order_sequence` | [uint64](#uint64) | | `order_sequence is order sequence.` |
| `order_id` | [string](#string) | | `order ID provided by the creator.` |
| `account_number` | [uint64](#uint64) | | `account_number is account number which corresponds the order creator.` |
| `remaining_quantity` | [string](#string) | | `remaining_quantity is remaining filling quantity sell/buy.` |
Expand Down
5 changes: 5 additions & 0 deletions docs/static/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -10101,6 +10101,11 @@
"type": "string",
"description": "id is unique order ID."
},
"sequence": {
"type": "string",
"format": "uint64",
"description": "sequence is unique order sequence generated at the time of the order placement."
},
"base_denom": {
"type": "string",
"description": "base_denom is base order denom."
Expand Down
89 changes: 57 additions & 32 deletions integration-tests/modules/dex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestLimitOrdersMatching(t *testing.T) {
Creator: acc1.String(),
Type: dextypes.ORDER_TYPE_LIMIT,
ID: "id1",
Sequence: sellOrderRes.Order.Sequence,
BaseDenom: denom1,
QuoteDenom: denom2,
Price: lo.ToPtr(dextypes.MustNewPriceFromString("1e-1")),
Expand Down Expand Up @@ -141,6 +142,7 @@ func TestLimitOrdersMatching(t *testing.T) {
Creator: acc2.String(),
Type: dextypes.ORDER_TYPE_LIMIT,
ID: "id1", // same ID allowed for different users
Sequence: buyOrderRes.Order.Sequence,
BaseDenom: denom1,
QuoteDenom: denom2,
Price: lo.ToPtr(dextypes.MustNewPriceFromString("11e-2")),
Expand Down Expand Up @@ -615,6 +617,7 @@ func TestOrderBooksAndOrdersQueries(t *testing.T) {
acc1OrderPlaceMsgs...,
)
requireT.NoError(err)
acc1Orders = fillOrderSequences(ctx, t, chain.ClientContext, acc1Orders)

// create acc2 orders
acc2Orders := []dextypes.Order{
Expand Down Expand Up @@ -661,6 +664,7 @@ func TestOrderBooksAndOrdersQueries(t *testing.T) {
acc2OrderPlaceMsgs...,
)
requireT.NoError(err)
acc2Orders = fillOrderSequences(ctx, t, chain.ClientContext, acc2Orders)

// check order books query
orderBooksRes, err := dexClient.OrderBooks(ctx, &dextypes.QueryOrderBooksRequest{})
Expand Down Expand Up @@ -1608,6 +1612,7 @@ func TestLimitOrdersMatchingWithBurnRate(t *testing.T) {
Creator: acc1.String(),
Type: dextypes.ORDER_TYPE_LIMIT,
ID: "id1",
Sequence: sellOrderRes.Order.Sequence,
BaseDenom: denom1,
QuoteDenom: denom2,
Price: lo.ToPtr(dextypes.MustNewPriceFromString("1e-1")),
Expand Down Expand Up @@ -1757,6 +1762,7 @@ func TestLimitOrdersMatchingWithCommissionRate(t *testing.T) {
Creator: acc1.String(),
Type: dextypes.ORDER_TYPE_LIMIT,
ID: "id1",
Sequence: sellOrderRes.Order.Sequence,
BaseDenom: denom1,
QuoteDenom: denom2,
Price: lo.ToPtr(dextypes.MustNewPriceFromString("1e-1")),
Expand Down Expand Up @@ -2155,38 +2161,6 @@ func TestCancelOrdersByDenom(t *testing.T) {
requireT.Empty(orderRes.Orders)
}

func issueFT(
ctx context.Context,
t *testing.T,
chain integration.CoreumChain,
issuer sdk.AccAddress,
initialAmount sdkmath.Int,
features ...assetfttypes.Feature,
) string {
chain.FundAccountWithOptions(ctx, t, issuer, integration.BalancesOptions{
Messages: []sdk.Msg{
&assetfttypes.MsgIssue{},
},
Amount: chain.QueryAssetFTParams(ctx, t).IssueFee.Amount,
})
issueMsg := &assetfttypes.MsgIssue{
Issuer: issuer.String(),
Symbol: "TKN" + uuid.NewString()[:4],
Subunit: "tkn" + uuid.NewString()[:4],
Precision: 5,
InitialAmount: initialAmount,
Features: features,
}
_, err := client.BroadcastTx(
ctx,
chain.ClientContext.WithFromAddress(issuer),
chain.TxFactory().WithGas(chain.GasLimitByMsgs(issueMsg)),
issueMsg,
)
require.NoError(t, err)
return assetfttypes.BuildDenom(issueMsg.Subunit, issuer)
}

// TestAssetFTBlockSmartContractsFeatureWithDEX tests the dex module integration with the asset ft
// block_smart_contracts features.
func TestAssetFTBlockSmartContractsFeatureWithDEX(t *testing.T) {
Expand Down Expand Up @@ -2522,6 +2496,57 @@ func TestLimitOrdersMatchingWithAssetBurning(t *testing.T) {
requireT.Equal(sdkmath.NewInt(0).String(), balanceRes.LockedInDEX.String())
}

func issueFT(
ctx context.Context,
t *testing.T,
chain integration.CoreumChain,
issuer sdk.AccAddress,
initialAmount sdkmath.Int,
features ...assetfttypes.Feature,
) string {
chain.FundAccountWithOptions(ctx, t, issuer, integration.BalancesOptions{
Messages: []sdk.Msg{
&assetfttypes.MsgIssue{},
},
Amount: chain.QueryAssetFTParams(ctx, t).IssueFee.Amount,
})
issueMsg := &assetfttypes.MsgIssue{
Issuer: issuer.String(),
Symbol: "TKN" + uuid.NewString()[:4],
Subunit: "tkn" + uuid.NewString()[:4],
Precision: 5,
InitialAmount: initialAmount,
Features: features,
}
_, err := client.BroadcastTx(
ctx,
chain.ClientContext.WithFromAddress(issuer),
chain.TxFactory().WithGas(chain.GasLimitByMsgs(issueMsg)),
issueMsg,
)
require.NoError(t, err)
return assetfttypes.BuildDenom(issueMsg.Subunit, issuer)
}

func fillOrderSequences(
ctx context.Context,
t *testing.T,
clientCtx client.Context,
orders []dextypes.Order,
) []dextypes.Order {
dexClient := dextypes.NewQueryClient(clientCtx)
for i, order := range orders {
res, err := dexClient.Order(ctx, &dextypes.QueryOrderRequest{
Creator: order.Creator,
Id: order.ID,
})
require.NoError(t, err)
orders[i].Sequence = res.Order.Sequence
}

return orders
}

func ordersToPlaceMsgs(orders []dextypes.Order) []sdk.Msg {
return lo.Map(orders, func(order dextypes.Order, _ int) sdk.Msg {
return &dextypes.MsgPlaceOrder{
Expand Down
2 changes: 2 additions & 0 deletions integration-tests/modules/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3614,6 +3614,8 @@ func TestVestingToWASMContract(t *testing.T) {

// TestWASMDEXInContract verifies that smart contract is able to execute all Coreum DEX messages and queries.
func TestWASMDEXInContract(t *testing.T) {
t.Skip() // FIXME(dzmitryhil) restore once we merge and regenerate protos and contract

t.Parallel()

ctx, chain := integrationtests.NewCoreumTestingContext(t)
Expand Down
Loading