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

refactor: use sdk.Dec for quantities #168

Merged
merged 32 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6871442
refactor: use sdk.Dec in protobuf
kingcre Jul 18, 2023
01456f1
refactor: use sdk.Dec for matching (WIP)
kingcre Jul 18, 2023
3b4776f
fix: reject non integer quantities
kingcre Jul 19, 2023
3f650bd
fix: finally uncomment sanity check
kingcre Jul 19, 2023
7c4b51d
test: fix test
kingcre Jul 19, 2023
8c218d7
chore: small code optimization
kingcre Jul 19, 2023
b133382
fix: fix bug in ConstructTempOrderBookSide
kingcre Jul 19, 2023
5948fa3
refactor: remove transient balance and QueueSendCoins
kingcre Jul 20, 2023
59b0ffb
chore: do not sort orders
kingcre Jul 20, 2023
d17639f
chore: remove dead code
kingcre Jul 20, 2023
472c98b
chore: remove prints
kingcre Jul 20, 2023
61994c8
fix: refine matching logic
kingcre Jul 24, 2023
f0fd3b9
refactor: rename TempOrder to MemOrder
kingcre Jul 24, 2023
33a563d
refactor: refactoring matching logic
kingcre Jul 24, 2023
bc47f1e
refactor: move matching logic into types
kingcre Jul 26, 2023
f94b071
feat: add RewardsGrowthGlobalInvariant
kingcre Jul 27, 2023
08fc9b8
chore: return insufficient fee error nicely
kingcre Jul 27, 2023
c69d78c
test: add missing simulation operations
kingcre Jul 27, 2023
e9cb9ad
test: add missing simulation operation
kingcre Jul 28, 2023
941d9f0
test: simulate MsgCollect
kingcre Jul 28, 2023
30030eb
refactor: naturally sort order book orders by using negative order id
kingcre Jul 31, 2023
ca3ab02
fix: temporarily disable tick spacing 1 in sim test
kingcre Jul 31, 2023
af476f9
test: fix test
kingcre Jul 31, 2023
de3d3ca
feat: add helper function
kingcre Jul 31, 2023
5fc54d4
Merge branch 'amm-module' into dec-matching-2
kingcre Aug 1, 2023
c55175a
test: fix test
kingcre Aug 1, 2023
e5a01f9
test: fix test
kingcre Aug 1, 2023
475e840
fix: use cache context in CanCollectInvariant
kingcre Aug 3, 2023
cc6f875
test: fix invariant test
kingcre Aug 3, 2023
47425ab
chore: move fee related function to fee.go
kingcre Aug 3, 2023
667523b
fix: use escrow correctly
kingcre Aug 2, 2023
1ed6b2e
fix: fix bug in batch matching
kingcre Aug 3, 2023
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
3 changes: 2 additions & 1 deletion app/ante/msg_ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/authz"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

utils "github.com/crescent-network/crescent/v5/types"
claimtypes "github.com/crescent-network/crescent/v5/x/claim/types"
exchangetypes "github.com/crescent-network/crescent/v5/x/exchange/types"
farmingtypes "github.com/crescent-network/crescent/v5/x/farming/types"
Expand Down Expand Up @@ -471,7 +472,7 @@ func (suite *AnteTestSuite) TestMixedBatchMsg() {
)

msg := testdata.NewTestMsg(acc)
batchMsg := exchangetypes.NewMsgPlaceBatchLimitOrder(acc, 1, true, sdk.ZeroDec(), sdk.ZeroInt(), 0)
batchMsg := exchangetypes.NewMsgPlaceBatchLimitOrder(acc, 1, true, utils.ParseDec("1.2"), utils.ParseDec("1000000"), 0)
authzMsg := authz.NewMsgExec(acc, []sdk.Msg{batchMsg, msg})
authzMsg2 := authz.NewMsgExec(acc, []sdk.Msg{batchMsg})
authzMsg3 := authz.NewMsgExec(acc, []sdk.Msg{batchMsg, batchMsg})
Expand Down
2 changes: 0 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ func NewApp(
)
tkeys := sdk.NewTransientStoreKeys(
paramstypes.TStoreKey,
exchangetypes.TStoreKey,
)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)

Expand Down Expand Up @@ -541,7 +540,6 @@ func NewApp(
app.ExchangeKeeper = exchangekeeper.NewKeeper(
appCodec,
keys[exchangetypes.StoreKey],
tkeys[exchangetypes.TStoreKey],
app.GetSubspace(exchangetypes.ModuleName),
app.BankKeeper,
)
Expand Down
12 changes: 6 additions & 6 deletions app/testutil/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *TestSuite) CreateMarket(baseDenom, quoteDenom string) exchangetypes.Mar
}

func (s *TestSuite) PlaceLimitOrder(
marketId uint64, ordererAddr sdk.AccAddress, isBuy bool, price sdk.Dec, qty sdk.Int, lifespan time.Duration) (orderId uint64, order exchangetypes.Order, res exchangetypes.ExecuteOrderResult) {
marketId uint64, ordererAddr sdk.AccAddress, isBuy bool, price, qty sdk.Dec, lifespan time.Duration) (orderId uint64, order exchangetypes.Order, res exchangetypes.ExecuteOrderResult) {
s.T().Helper()
var err error
orderId, order, res, err = s.App.ExchangeKeeper.PlaceLimitOrder(s.Ctx, marketId, ordererAddr, isBuy, price, qty, lifespan)
Expand All @@ -31,7 +31,7 @@ func (s *TestSuite) PlaceLimitOrder(
}

func (s *TestSuite) PlaceBatchLimitOrder(
marketId uint64, ordererAddr sdk.AccAddress, isBuy bool, price sdk.Dec, qty sdk.Int, lifespan time.Duration) (order exchangetypes.Order) {
marketId uint64, ordererAddr sdk.AccAddress, isBuy bool, price, qty sdk.Dec, lifespan time.Duration) (order exchangetypes.Order) {
s.T().Helper()
var err error
order, err = s.App.ExchangeKeeper.PlaceBatchLimitOrder(s.Ctx, marketId, ordererAddr, isBuy, price, qty, lifespan)
Expand All @@ -40,7 +40,7 @@ func (s *TestSuite) PlaceBatchLimitOrder(
}

func (s *TestSuite) PlaceMMLimitOrder(
marketId uint64, ordererAddr sdk.AccAddress, isBuy bool, price sdk.Dec, qty sdk.Int, lifespan time.Duration) (orderId uint64, order exchangetypes.Order, res exchangetypes.ExecuteOrderResult) {
marketId uint64, ordererAddr sdk.AccAddress, isBuy bool, price, qty sdk.Dec, lifespan time.Duration) (orderId uint64, order exchangetypes.Order, res exchangetypes.ExecuteOrderResult) {
s.T().Helper()
var err error
orderId, order, res, err = s.App.ExchangeKeeper.PlaceMMLimitOrder(s.Ctx, marketId, ordererAddr, isBuy, price, qty, lifespan)
Expand All @@ -49,7 +49,7 @@ func (s *TestSuite) PlaceMMLimitOrder(
}

func (s *TestSuite) PlaceMMBatchLimitOrder(
marketId uint64, ordererAddr sdk.AccAddress, isBuy bool, price sdk.Dec, qty sdk.Int, lifespan time.Duration) (order exchangetypes.Order) {
marketId uint64, ordererAddr sdk.AccAddress, isBuy bool, price, qty sdk.Dec, lifespan time.Duration) (order exchangetypes.Order) {
s.T().Helper()
var err error
order, err = s.App.ExchangeKeeper.PlaceMMBatchLimitOrder(s.Ctx, marketId, ordererAddr, isBuy, price, qty, lifespan)
Expand All @@ -58,7 +58,7 @@ func (s *TestSuite) PlaceMMBatchLimitOrder(
}

func (s *TestSuite) PlaceMarketOrder(
marketId uint64, ordererAddr sdk.AccAddress, isBuy bool, qty sdk.Int) (orderId uint64, res exchangetypes.ExecuteOrderResult) {
marketId uint64, ordererAddr sdk.AccAddress, isBuy bool, qty sdk.Dec) (orderId uint64, res exchangetypes.ExecuteOrderResult) {
s.T().Helper()
var err error
orderId, res, err = s.App.ExchangeKeeper.PlaceMarketOrder(s.Ctx, marketId, ordererAddr, isBuy, qty)
Expand All @@ -83,7 +83,7 @@ func (s *TestSuite) CancelAllOrders(ordererAddr sdk.AccAddress, marketId uint64)
}

func (s *TestSuite) SwapExactAmountIn(
ordererAddr sdk.AccAddress, routes []uint64, input, minOutput sdk.Coin, simulate bool) (output sdk.Coin, results []exchangetypes.SwapRouteResult) {
ordererAddr sdk.AccAddress, routes []uint64, input, minOutput sdk.DecCoin, simulate bool) (output sdk.DecCoin, results []exchangetypes.SwapRouteResult) {
s.T().Helper()
var err error
output, results, err = s.App.ExchangeKeeper.SwapExactAmountIn(s.Ctx, ordererAddr, routes, input, minOutput, simulate)
Expand Down
48 changes: 48 additions & 0 deletions app/testutil/test_suite.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package testutil

import (
"fmt"
"testing"
"time"

"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -37,6 +40,7 @@ func (s *TestSuite) BeginBlock(blockTimeDelta time.Duration) {
WithBlockTime(s.Ctx.BlockTime().Add(blockTimeDelta)).
BlockHeader()
s.App.BeginBlock(abci.RequestBeginBlock{Header: newHeader})
s.App.MidBlock(abci.RequestMidBlock{Txs: nil})
s.Ctx = s.App.BaseApp.NewContext(false, newHeader)
}

Expand Down Expand Up @@ -97,3 +101,47 @@ func (s *TestSuite) CheckEvent(evtType proto.Message, attrs map[string][]byte) {
}
s.FailNowf("CheckEvent failed", "event with type %s not found", evtTypeName)
}

func (s *TestSuite) AssertEqual(exp, got any) {
s.T().Helper()
var equal bool
switch exp := exp.(type) {
case sdk.Int:
equal = exp.Equal(got.(sdk.Int))
case sdk.Dec:
equal = exp.Equal(got.(sdk.Dec))
case sdk.Coin:
equal = exp.IsEqual(got.(sdk.Coin))
case sdk.Coins:
equal = exp.IsEqual(got.(sdk.Coins))
case sdk.DecCoin:
equal = exp.IsEqual(got.(sdk.DecCoin))
case sdk.DecCoins:
equal = exp.IsEqual(got.(sdk.DecCoins))
default:
panic(fmt.Sprintf("unsupported type: %T", exp))
}
s.Assert().True(equal, "expected:\t%v\ngot:\t\t%v", exp, got)
}

func AssertEqual(t *testing.T, exp, got any) {
t.Helper()
var equal bool
switch exp := exp.(type) {
case sdk.Int:
equal = exp.Equal(got.(sdk.Int))
case sdk.Dec:
equal = exp.Equal(got.(sdk.Dec))
case sdk.Coin:
equal = exp.IsEqual(got.(sdk.Coin))
case sdk.Coins:
equal = exp.IsEqual(got.(sdk.Coins))
case sdk.DecCoin:
equal = exp.IsEqual(got.(sdk.DecCoin))
case sdk.DecCoins:
equal = exp.IsEqual(got.(sdk.DecCoins))
default:
panic(fmt.Sprintf("unsupported type: %T", exp))
}
assert.True(t, equal, "expected:\t%v\ngot:\t\t%v", exp, got)
}
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

51 changes: 29 additions & 22 deletions client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10539,10 +10539,11 @@ paths:
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a decimal
amount.


NOTE: The amount field is an Int which implements the custom
NOTE: The amount field is an Dec which implements the custom
method

signatures required by gogoproto.
Expand All @@ -10562,10 +10563,11 @@ paths:
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a
decimal amount.


NOTE: The amount field is an Int which implements the
NOTE: The amount field is an Dec which implements the
custom method

signatures required by gogoproto.
Expand All @@ -10577,10 +10579,11 @@ paths:
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a
decimal amount.


NOTE: The amount field is an Int which implements the
NOTE: The amount field is an Dec which implements the
custom method

signatures required by gogoproto.
Expand All @@ -10592,10 +10595,11 @@ paths:
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a
decimal amount.


NOTE: The amount field is an Int which implements the
NOTE: The amount field is an Dec which implements the
custom method

signatures required by gogoproto.
Expand Down Expand Up @@ -18714,9 +18718,9 @@ definitions:
amount:
type: string
description: |-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a decimal amount.

NOTE: The amount field is an Int which implements the custom method
NOTE: The amount field is an Dec which implements the custom method
signatures required by gogoproto.
results:
type: array
Expand All @@ -18734,10 +18738,11 @@ definitions:
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a decimal
amount.


NOTE: The amount field is an Int which implements the custom
NOTE: The amount field is an Dec which implements the custom
method

signatures required by gogoproto.
Expand All @@ -18749,10 +18754,11 @@ definitions:
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a decimal
amount.


NOTE: The amount field is an Int which implements the custom
NOTE: The amount field is an Dec which implements the custom
method

signatures required by gogoproto.
Expand All @@ -18764,10 +18770,11 @@ definitions:
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a decimal
amount.


NOTE: The amount field is an Int which implements the custom
NOTE: The amount field is an Dec which implements the custom
method

signatures required by gogoproto.
Expand Down Expand Up @@ -18918,9 +18925,9 @@ definitions:
amount:
type: string
description: |-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a decimal amount.

NOTE: The amount field is an Int which implements the custom method
NOTE: The amount field is an Dec which implements the custom method
signatures required by gogoproto.
output:
type: object
Expand All @@ -18930,9 +18937,9 @@ definitions:
amount:
type: string
description: |-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a decimal amount.

NOTE: The amount field is an Int which implements the custom method
NOTE: The amount field is an Dec which implements the custom method
signatures required by gogoproto.
fee:
type: object
Expand All @@ -18942,9 +18949,9 @@ definitions:
amount:
type: string
description: |-
Coin defines a token with a denomination and an amount.
DecCoin defines a token with a denomination and a decimal amount.

NOTE: The amount field is an Int which implements the custom method
NOTE: The amount field is an Dec which implements the custom method
signatures required by gogoproto.
crescent.amm.v1beta1.FarmingPlan:
type: object
Expand Down
Loading
Loading