Skip to content

Commit

Permalink
fix: fix lint issues and tests (#44)
Browse files Browse the repository at this point in the history
* refactor: split OrderBook into two OrderBookTicks

OrderBookTicks implements OrderSource

* feat: match only when matchable

* refactor: split functions properly

* chore: add comments for TickToIndex and TickFromIndex

* fix: fix MatchOrders (WIP)

* fix: add stop condition for price discovery

* feat: complete matching logic

* chore: delete outdated tests

* refactor: export some fields and rename variables

* style: rearrange code blocks

* feat: Match returns swap price and matched flag

* fix: simplify FindPrice and add AddOrders to OrderBookTicks

* test: add tests

* fix: Match returns OrderBook

* feat: implement PoolOrderSource

* fix: add matching iteration's stop condition

* fix: fix iteration stop condition

* fix: fix iteration stop condition (again)

* fix: update swap price discovery logic

also remove prec parameter from OrderSource methods

* fix: matched is true only when matching has happened

* chore: fix lint issues and tests
  • Loading branch information
hallazzang authored Jan 12, 2022
1 parent aa896b7 commit 16290ec
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
6 changes: 2 additions & 4 deletions cmd/crescentd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -40,8 +39,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
depCdc := clientCtx.Codec
cdc := depCdc.(codec.Codec)
cdc := clientCtx.Codec

serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
Expand Down Expand Up @@ -160,7 +158,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa

appState[authtypes.ModuleName] = authGenStateBz

bankGenState := banktypes.GetGenesisStateFromAppState(depCdc, appState)
bankGenState := banktypes.GetGenesisStateFromAppState(cdc, appState)
bankGenState.Balances = append(bankGenState.Balances, balances)
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)
Expand Down
1 change: 1 addition & 0 deletions x/liquidity/types/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func newBuyOrder(price string, amount int64) *types.Order {
return types.NewOrder(sdk.AccAddress{}, types.SwapDirectionBuy, newDec(price), sdk.NewInt(amount))
}

//nolint
func newSellOrder(price string, amount int64) *types.Order {
return types.NewOrder(sdk.AccAddress{}, types.SwapDirectionSell, newDec(price), sdk.NewInt(amount))
}
Expand Down
14 changes: 7 additions & 7 deletions x/liquidity/types/orderbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,39 @@ func TestOrderBookTicks_FindPrice(t *testing.T) {

func TestOrderBookTicks_AddOrder(t *testing.T) {
checkSorted := func(ticks *types.OrderBookTicks) {
require.True(t, sort.SliceIsSorted(ticks, func(i, j int) bool {
require.True(t, sort.SliceIsSorted(ticks.Ticks, func(i, j int) bool {
return ticks.Ticks[i].Price.GTE(ticks.Ticks[j].Price)
}), "ticks must be sorted")
}

ticks := testOrderBookTicks()
checkSorted(ticks)
require.Len(t, ticks, 11)
require.Len(t, ticks.Ticks, 11)

// Same price already exists
ticks.AddOrder(newBuyOrder("18.0", 1000))
checkSorted(ticks)
require.Len(t, ticks, 11)
require.Len(t, ticks.Ticks, 11)

// New price. We don't care about the tick precision here
ticks.AddOrder(newBuyOrder("18.000000000000000001", 1000))
checkSorted(ticks)
require.Len(t, ticks, 12)
require.Len(t, ticks.Ticks, 12)

// Add an order with same price as above again
ticks.AddOrder(newBuyOrder("18.000000000000000001", 1000))
checkSorted(ticks)
require.Len(t, ticks, 12)
require.Len(t, ticks.Ticks, 12)

// Add an order with higher price than the highest price in ticks.
ticks.AddOrder(newBuyOrder("21.0", 1000))
checkSorted(ticks)
require.Len(t, ticks, 13)
require.Len(t, ticks.Ticks, 13)

// Add an order with lower price than the lowest price in ticks.
ticks.AddOrder(newBuyOrder("9.0", 1000))
checkSorted(ticks)
require.Len(t, ticks, 14)
require.Len(t, ticks.Ticks, 14)
}

func TestOrderBookTicks_AmountGTE(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion x/liquidity/types/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ func (os PoolOrderSource) AmountLTE(price sdk.Dec) sdk.Int {

func (os PoolOrderSource) Orders(price sdk.Dec) Orders {
panic("not implemented")
return nil
}

func (os PoolOrderSource) UpTick(price sdk.Dec) (tick sdk.Dec, found bool) {
Expand Down

0 comments on commit 16290ec

Please sign in to comment.