Skip to content

Commit

Permalink
Update tests to Sqrt(XY) pool basis
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Dusek committed Jun 8, 2024
1 parent 9a23f7a commit 2b70a42
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions x/market/keeper/msg_server_cancel_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@ package keeper_test

import (
"strconv"
"strings"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "github.com/pendulum-labs/market/testutil/keeper"
"github.com/pendulum-labs/market/testutil/sample"
"github.com/pendulum-labs/market/x/market/keeper"
"github.com/pendulum-labs/market/x/market/types"
"github.com/stretchr/testify/require"
)

func TestCancelOrder_case1_stop(t *testing.T) {
testInput := keepertest.CreateTestEnvironment(t)
// TestData
testdata := testData{coinAStr: "30CoinA", coinBStr: "40CoinB", RateAstrArray: []string{"60", "70"}, RateBstrArray: []string{"80", "90"}}
coinPair, _ := sample.SampleCoins("70CoinA", "70CoinB")
denomA, denomB := sample.SampleDenoms(coinPair)
pair := strings.Join([]string{denomA, denomB}, ",")

// MintCoins
require.NoError(t, testInput.BankKeeper.MintCoins(testInput.Context, types.ModuleName, coinPair))
Expand Down Expand Up @@ -61,7 +54,7 @@ func TestCancelOrder_case1_stop(t *testing.T) {
require.True(t, memberfound)
require.Equal(t, memberA.DenomA, denomB)
require.Equal(t, memberA.DenomB, denomA)
require.Equal(t, "33", memberA.Balance.String())
require.Equal(t, "136", memberA.Balance.String())
require.Equal(t, memberA.Stop, uint64(0))

// Cancel Order
Expand All @@ -77,7 +70,7 @@ func TestCancelOrder_case1_stop(t *testing.T) {
require.True(t, memberfound)
require.Equal(t, memberA.DenomA, denomB)
require.Equal(t, memberA.DenomB, denomA)
require.Equal(t, "33", memberA.Balance.String())
require.Equal(t, "136", memberA.Balance.String())
require.Equal(t, memberA.Stop, uint64(0))

//Validate Order
Expand All @@ -94,11 +87,6 @@ func TestCancelOrder_case1_stop(t *testing.T) {
func TestCancelOrder_case1_limit(t *testing.T) {

testInput := keepertest.CreateTestEnvironment(t)
// TestData
testdata := testData{coinAStr: "30CoinA", coinBStr: "40CoinB", RateAstrArray: []string{"60", "70"}, RateBstrArray: []string{"80", "90"}}
coinPair, _ := sample.SampleCoins("70CoinA", "70CoinB")
denomA, denomB := sample.SampleDenoms(coinPair)
pair := strings.Join([]string{denomA, denomB}, ",")

// MintCoins
require.NoError(t, testInput.BankKeeper.MintCoins(testInput.Context, types.ModuleName, coinPair))
Expand All @@ -122,6 +110,14 @@ func TestCancelOrder_case1_limit(t *testing.T) {

balanceBefore := testInput.BankKeeper.GetBalance(testInput.Context, sdk.AccAddress(addr), denomB)

//Validate GetMember
memberBid, memberfoundBid := testInput.MarketKeeper.GetMember(testInput.Context, denomA, denomB)
require.True(t, memberfoundBid)
require.Equal(t, memberBid.DenomA, denomA)
require.Equal(t, memberBid.DenomB, denomB)
require.Equal(t, "182", memberBid.Balance.String())
require.Equal(t, memberBid.Stop, uint64(0))

// Create Order
var o = types.MsgCreateOrder{Creator: addr, DenomAsk: denomA, DenomBid: denomB, Rate: testdata.RateAstrArray, OrderType: "limit", Amount: "0", Prev: "0", Next: "0"}
_, err = keeper.NewMsgServerImpl(*testInput.MarketKeeper).CreateOrder(sdk.WrapSDKContext(testInput.Context), &o)
Expand All @@ -138,11 +134,11 @@ func TestCancelOrder_case1_limit(t *testing.T) {
require.Equal(t, orders.Amount.String(), o.Amount)

//Validate GetMember
memberBid, memberfoundBid := testInput.MarketKeeper.GetMember(testInput.Context, orders.DenomAsk, orders.DenomBid)
memberBid, memberfoundBid = testInput.MarketKeeper.GetMember(testInput.Context, orders.DenomAsk, orders.DenomBid)
require.True(t, memberfoundBid)
require.Equal(t, memberBid.DenomA, denomA)
require.Equal(t, memberBid.DenomB, denomB)
require.Equal(t, "44", memberBid.Balance.String())
require.Equal(t, "182", memberBid.Balance.String())
require.Equal(t, memberBid.Stop, uint64(0))

memberBid.Stop = orders.Uid
Expand All @@ -161,7 +157,7 @@ func TestCancelOrder_case1_limit(t *testing.T) {
require.True(t, memberfoundBid)
require.Equal(t, memberBid.DenomA, denomA)
require.Equal(t, memberBid.DenomB, denomB)
require.Equal(t, "44", memberBid.Balance.String())
require.Equal(t, "182", memberBid.Balance.String())
require.Equal(t, memberBid.Stop, uint64(orders.Uid))

//Validate Order
Expand All @@ -173,16 +169,23 @@ func TestCancelOrder_case1_limit(t *testing.T) {
require.Equal(t, orders.Amount.String(), o.Amount)
require.Equal(t, orders.OrderType, "limit")

// Validate GetPool
rst, found := testInput.MarketKeeper.GetPool(testInput.Context, pair)
require.True(t, found)

// Calculate Product After
memberAsk, memberAskFound := testInput.MarketKeeper.GetMember(testInput.Context, denomB, denomA)
require.True(t, memberAskFound)
memberBid, memberBidFound := testInput.MarketKeeper.GetMember(testInput.Context, denomA, denomB)
require.True(t, memberBidFound)

productAfter := memberAsk.Balance.Mul(memberBid.Balance)
require.True(t, productAfter.GTE(rst.Drops))
}

func TestCancelOrder_case2_stop(t *testing.T) {

testInput := keepertest.CreateTestEnvironment(t)
// TestData
testdata := testData{coinAStr: "30CoinA", coinBStr: "40CoinB", RateAstrArray: []string{"60", "70"}, RateBstrArray: []string{"80", "90"}}
coinPair, _ := sample.SampleCoins("70CoinA", "70CoinB")
denomA, denomB := sample.SampleDenoms(coinPair)
pair := strings.Join([]string{denomA, denomB}, ",")

// MintCoins
require.NoError(t, testInput.BankKeeper.MintCoins(testInput.Context, types.ModuleName, coinPair))
Expand Down Expand Up @@ -226,7 +229,7 @@ func TestCancelOrder_case2_stop(t *testing.T) {
require.True(t, memberAskfound)
require.Equal(t, memberAsk.DenomA, denomB)
require.Equal(t, memberAsk.DenomB, denomA)
require.Equal(t, "33", memberAsk.Balance.String())
require.Equal(t, "136", memberAsk.Balance.String())
require.Equal(t, memberAsk.Stop, uint64(0))

o.Next = strconv.FormatUint(beforecount, 10)
Expand All @@ -251,7 +254,7 @@ func TestCancelOrder_case2_stop(t *testing.T) {
require.True(t, memberBidfound)
require.Equal(t, memberBid.DenomA, denomA)
require.Equal(t, memberBid.DenomB, denomB)
require.Equal(t, "44", memberBid.Balance.String())
require.Equal(t, "182", memberBid.Balance.String())
require.Equal(t, memberBid.Stop, beforecount)

// Validate Order
Expand Down Expand Up @@ -299,7 +302,7 @@ func TestCancelOrderEmptyPool(t *testing.T) {
require.True(t, memberAskfound)
require.Equal(t, memberAsk.DenomA, denomB)
require.Equal(t, memberAsk.DenomB, denomA)
require.Equal(t, "33", memberAsk.Balance.String())
require.Equal(t, "136", memberAsk.Balance.String())
require.Equal(t, memberAsk.Stop, uint64(0))

// Validate RedeemDrop
Expand Down

0 comments on commit 2b70a42

Please sign in to comment.