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 9, 2024
1 parent 89de17b commit f9851e6
Showing 1 changed file with 60 additions and 91 deletions.
151 changes: 60 additions & 91 deletions x/market/keeper/msg_server_redeem_drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestRedeemDrop(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}, ",")

func redeemCommon(t *testing.T, testInput keepertest.TestInput) {
// MintCoins
require.NoError(t, testInput.BankKeeper.MintCoins(testInput.Context, types.ModuleName, coinPair))

Expand All @@ -46,131 +38,108 @@ func TestRedeemDrop(t *testing.T) {
require.NoError(t, err)
require.NoError(t, testInput.BankKeeper.SendCoinsFromModuleToAccount(testInput.Context, types.ModuleName, requestAddress3, coinPair))

// GetUidCount before CreatePool
beforecount := testInput.MarketKeeper.GetUidCount(testInput.Context)

// Create Pool
var p = types.MsgCreatePool{CoinA: testdata.coinAStr, CoinB: testdata.coinBStr, Creator: addr}
response, err := keeper.NewMsgServerImpl(*testInput.MarketKeeper).CreatePool(sdk.WrapSDKContext(testInput.Context), &p)
_, _ = keeper.NewMsgServerImpl(*testInput.MarketKeeper).CreatePool(sdk.WrapSDKContext(testInput.Context), &p)

// Validate CreatePool
require.NoError(t, err)
require.Contains(t, p.GetCreator(), response.String())
require.Contains(t, p.GetCoinA(), response.String())
require.Contains(t, p.GetCoinB(), response.String())

// Validate SetUidCount function.
aftercount := testInput.MarketKeeper.GetUidCount(testInput.Context)
require.Equal(t, beforecount+1, aftercount)
}

// Validate GetDrop
drops, dropFound := testInput.MarketKeeper.GetDrop(testInput.Context, beforecount)
require.True(t, dropFound)
require.Equal(t, drops.Pair, pair)
func TestRedeemDrop(t *testing.T) {
testInput := keepertest.CreateTestEnvironment(t)

// Validate GetPool
rst1, found := testInput.MarketKeeper.GetPool(testInput.Context, pair)
require.True(t, found)
require.Equal(t, rst1.Pair, pair)
require.Equal(t, "1200", rst1.Drops.String())
require.Equal(t, 1, len(rst1.Leaders))
require.Equal(t, "1200", rst1.Leaders[0].Drops.String())
redeemCommon(t, testInput)

// Validate CreateDrop
var d = types.MsgCreateDrop{Creator: addr2, Pair: pair, Drops: "120"}
d := types.MsgCreateDrop{Creator: addr2, Pair: pair, Drops: "120"}
createDropResponse, err := keeper.NewMsgServerImpl(*testInput.MarketKeeper).CreateDrop(sdk.WrapSDKContext(testInput.Context), &d)
require.NoError(t, err)

// Validate GetMember
members, memberfound := testInput.MarketKeeper.GetMember(testInput.Context, denomB, denomA)
members1, memberfound1 := testInput.MarketKeeper.GetMember(testInput.Context, denomA, denomB)
require.True(t, memberfound)
require.Equal(t, members.DenomA, denomB)
require.Equal(t, members.DenomB, denomA)
require.Equal(t, "33", members.Balance.String())

require.True(t, memberfound1)
require.Equal(t, members1.DenomA, denomA)
require.Equal(t, members1.DenomB, denomB)
require.Equal(t, "44", members1.Balance.String())

// Validate GetPool
rst, found := testInput.MarketKeeper.GetPool(testInput.Context, pair)
require.True(t, found)
require.Equal(t, rst.Pair, pair)
require.Equal(t, "1320", rst.Drops.String())
require.Equal(t, 2, len(rst.Leaders))
require.Equal(t, addr2, rst.Leaders[1].Address)
require.Equal(t, "1200", rst.Leaders[0].Drops.String())

// Validate GetDrop
drops1, drop1Found := testInput.MarketKeeper.GetDrop(testInput.Context, aftercount)
drop1, drop1Found := testInput.MarketKeeper.GetDrop(testInput.Context, createDropResponse.Uid)
require.True(t, drop1Found)
require.Equal(t, drops1.Pair, pair)
require.Equal(t, drops1.Drops.String(), d.Drops)
require.Contains(t, d.GetCreator(), createDropResponse.String())
require.Equal(t, drop1.Pair, pair)
require.Equal(t, drop1.Drops.String(), d.Drops)
require.Contains(t, d.GetCreator(), createDropResponse.Creator)

// Validate RedeemDrop
Uid := strconv.FormatUint(drops1.Uid, 10)
Uid := strconv.FormatUint(drop1.Uid, 10)
var rd = types.MsgRedeemDrop{Creator: addr2, Uid: Uid}
createRedeemDropResponse, redeemdropErr := keeper.NewMsgServerImpl(*testInput.MarketKeeper).RedeemDrop(sdk.WrapSDKContext(testInput.Context), &rd)
require.NoError(t, redeemdropErr)
require.Contains(t, rd.GetCreator(), createRedeemDropResponse.String())
require.Contains(t, rd.GetCreator(), createRedeemDropResponse.Creator)

// Validate Drop After Redeem Drop
drops1, drop1Found = testInput.MarketKeeper.GetDrop(testInput.Context, aftercount)
drop1, drop1Found = testInput.MarketKeeper.GetDrop(testInput.Context, createDropResponse.Uid)
require.True(t, drop1Found)
require.Equal(t, drops1.Pair, pair)
require.Equal(t, drops1.Drops.String(), d.Drops)
require.Contains(t, d.GetCreator(), createDropResponse.String())
require.False(t, drops1.Active)
require.Equal(t, drop1.Pair, pair)
require.Equal(t, drop1.Drops.String(), d.Drops)
require.Contains(t, d.GetCreator(), createDropResponse.Creator)
require.False(t, drop1.Active)

// Validate GetPool After Redeem Drop
rst, found = testInput.MarketKeeper.GetPool(testInput.Context, pair)
pool, found := testInput.MarketKeeper.GetPool(testInput.Context, pair)
require.True(t, found)
require.Equal(t, rst.Pair, pair)
require.Equal(t, "1200", rst.Drops.String())
require.Equal(t, "1200", rst.Leaders[0].Drops.String())
require.Equal(t, 1, len(rst.Leaders))
require.Equal(t, addr, rst.Leaders[0].Address)
require.Equal(t, pool.Pair, pair)
require.Equal(t, "34", pool.Drops.String())
require.Equal(t, "34", pool.Leaders[0].Drops.String())
require.Equal(t, 1, len(pool.Leaders))
require.Equal(t, addr, pool.Leaders[0].Address)

owner, ok := testInput.MarketKeeper.GetDropsOwnerPair(testInput.Context, addr, pair)
require.True(t, ok)
require.Truef(t, owner.Sum.Equal(sdk.NewInt(1200)), owner.Sum.String())
require.Truef(t, owner.Sum.Equal(sdk.NewInt(34)), owner.Sum.String())

pairs, ok := testInput.MarketKeeper.GetPairs(testInput.Context, addr)
require.True(t, ok)
require.Truef(t, pairs.Pairs[0] == pair, pairs.String())

// Validate GetMember After Redeem Drop
members, memberfound = testInput.MarketKeeper.GetMember(testInput.Context, denomB, denomA)
members1, memberfound1 = testInput.MarketKeeper.GetMember(testInput.Context, denomA, denomB)
require.True(t, memberfound)
require.Equal(t, members.DenomA, denomB)
require.Equal(t, members.DenomB, denomA)
require.Equal(t, members.Balance.String(), "30")
require.True(t, memberfound1)
require.Equal(t, members1.DenomA, denomA)
require.Equal(t, members1.DenomB, denomB)
require.Equal(t, members1.Balance.String(), "40")
memberA, memberFoundA := testInput.MarketKeeper.GetMember(testInput.Context, denomB, denomA)
memberB, memberFoundB := testInput.MarketKeeper.GetMember(testInput.Context, denomA, denomB)
require.True(t, memberFoundA)
require.Equal(t, memberA.DenomA, denomB)
require.Equal(t, memberA.DenomB, denomA)
require.Equal(t, memberA.Balance.String(), "31")
require.True(t, memberFoundB)
require.Equal(t, memberB.DenomA, denomA)
require.Equal(t, memberB.DenomB, denomB)
require.Equal(t, memberB.Balance.String(), "41")

// 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(pool.Drops))

// Validate RedeemDrop
Uid2 := strconv.FormatUint(beforecount, 10)
Uid2 := strconv.FormatUint(createDropResponse.Uid-1, 10)
var rd2 = types.MsgRedeemDrop{Creator: addr, Uid: Uid2}
createRedeemDropResponse2, redeemdropErr2 := keeper.NewMsgServerImpl(*testInput.MarketKeeper).RedeemDrop(sdk.WrapSDKContext(testInput.Context), &rd2)
require.NoError(t, redeemdropErr2)
require.Contains(t, rd2.GetCreator(), createRedeemDropResponse2.String())
require.Contains(t, rd2.GetCreator(), createRedeemDropResponse2.Creator)

// Validate RedeemDrop Duplicate Error
_, redeemdropErr3 := keeper.NewMsgServerImpl(*testInput.MarketKeeper).RedeemDrop(sdk.WrapSDKContext(testInput.Context), &rd2)
require.Error(t, redeemdropErr3)

// Validate GetPool After Redeem Drop
rst, found = testInput.MarketKeeper.GetPool(testInput.Context, pair)
pool, found = testInput.MarketKeeper.GetPool(testInput.Context, pair)
require.True(t, found)
require.Equal(t, rst.Pair, pair)
require.Equal(t, rst.Drops.String(), "0")
require.Equal(t, 0, len(rst.Leaders))
require.Equal(t, pool.Pair, pair)
require.Equal(t, pool.Drops.String(), "0")
require.Equal(t, 0, len(pool.Leaders))

// 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(pool.Drops))

pairs, ok = testInput.MarketKeeper.GetPairs(testInput.Context, addr)
require.True(t, ok)
Expand Down Expand Up @@ -215,7 +184,7 @@ func TestRedeemDrop_WithBurnCoin(t *testing.T) {
rst1, found := testInput.MarketKeeper.GetPool(testInput.Context, pair)
require.True(t, found)
require.Equal(t, rst1.Pair, pair)
require.Equal(t, "70000", rst1.Drops.String())
require.Equal(t, "264", rst1.Drops.String())
}

func TestRedeemDrop_NumericalLimits(t *testing.T) {
Expand Down

0 comments on commit f9851e6

Please sign in to comment.