Skip to content

Commit

Permalink
Update tests for round down drops
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Dusek committed Jun 13, 2024
1 parent 341f4c9 commit 1129ac8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 57 deletions.
10 changes: 5 additions & 5 deletions x/market/keeper/msg_server_cancel_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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, "182", memberBid.Balance.String())
require.Equal(t, "181", memberBid.Balance.String())
require.Equal(t, memberBid.Stop, uint64(0))

// Create Order
Expand All @@ -136,7 +136,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, "182", memberBid.Balance.String())
require.Equal(t, "181", memberBid.Balance.String())
require.Equal(t, memberBid.Stop, uint64(0))

memberBid.Stop = orders.Uid
Expand All @@ -155,7 +155,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, "182", memberBid.Balance.String())
require.Equal(t, "181", memberBid.Balance.String())
require.Equal(t, memberBid.Stop, uint64(orders.Uid))

//Validate Order
Expand Down Expand Up @@ -236,7 +236,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, "182", memberBid.Balance.String())
require.Equal(t, "181", memberBid.Balance.String())
require.Equal(t, memberBid.Stop, beforecount)

// Validate Order
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestCancelOrder_case1_market_filled(t *testing.T) {
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, "181", memberBid.Balance.String())
require.Equal(t, memberBid.Stop, uint64(0))

//Create Order
Expand Down
47 changes: 18 additions & 29 deletions x/market/keeper/msg_server_create_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package keeper

import (
"context"
"fmt"
"math/big"
"sort"
"strings"

Expand Down Expand Up @@ -59,44 +57,35 @@ func (k msgServer) CreateDrop(goCtx context.Context, msg *types.MsgCreateDrop) (
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "drops not >0")
}

dropAmtMember1, dropAmtMember2, error := dropAmounts(drops, pool, member1, member2)
dropAmtMember1, dropAmtMember2, error := dropAmounts(drops.Add(sdk.NewIntFromUint64(1)), pool, member1, member2)
if error != nil {
return nil, error
}

dropProduct := dropAmtMember1.Mul(dropAmtMember2)

tmp := big.NewInt(0)
tmp.Sqrt(dropProduct.BigInt())
sqrtDropProduct := sdk.NewIntFromBigInt(tmp)

// Drops basis is sqrt(XY)
// Change in XY should always be positive
// Pool creator should get more drops per sqrt(XY)
// Pool creator drops = sqrt(XY).
// Therefore sqrt(Product) must be equal to or greater than drops
if sqrtDropProduct.LT(drops) {
fmt.Printf("Sqrt ProductBeg: %s", sqrtDropProduct)
fmt.Printf("Drops: %s", drops)
return nil, sdkerrors.Wrapf(types.ErrSqrtProductLessThanDrops, "Sqrt of Product not greater than or equal to drops.")
if ((dropAmtMember1.Mul(pool.Drops)).Quo(member1.Balance)).LTE((dropAmtMember2.Mul(pool.Drops)).Quo(member2.Balance)) {
drops = (dropAmtMember1.Mul(pool.Drops)).Quo(member1.Balance)
} else {
drops = (dropAmtMember2.Mul(pool.Drops)).Quo(member2.Balance)
}

prevDrop, found := k.GetDrop(ctx, pool.LastDrop)
if !found {
return nil, sdkerrors.Wrap(types.ErrDropNotFound, "pool previous drop not found")
if !drops.GT(sdk.ZeroInt()) {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "drops not >0")
}

tmp = big.NewInt(0)
tmp.Sqrt(prevDrop.ProductBeg.BigInt())
sqrtPrevDropProduct := sdk.NewIntFromBigInt(tmp)
sqrtDropProduct := sqrtInt(dropAmtMember1.Mul(dropAmtMember2))

sqrtMembers := sqrtInt(member1.Balance.Mul(member2.Balance))

// Change in XY should always be positive
// Drop creator should never pay less sqrt(Product)
// per drop than the previous drop creator.
if (sqrtDropProduct.Quo(drops)).LT(sqrtPrevDropProduct.Quo(prevDrop.Drops)) {
return nil, sdkerrors.Wrapf(types.ErrSqrtProductOverDrops, "Sqrt of Product divided by drops less than previous.")
for (sqrtDropProduct.Quo(drops)).LT(sqrtMembers.Quo(pool.Drops)) {
dropAmtMember1, dropAmtMember2, error = dropAmounts(drops.Add(sdk.NewIntFromUint64(1)), pool, member1, member2)
if error != nil {
return nil, error
}
sqrtDropProduct = sqrtInt(dropAmtMember1.Mul(dropAmtMember2))
}

dropProduct := dropAmtMember1.Mul(dropAmtMember2)

coin1 := sdk.NewCoin(denom1, dropAmtMember1)
coin2 := sdk.NewCoin(denom2, dropAmtMember2)

Expand Down
18 changes: 8 additions & 10 deletions x/market/keeper/msg_server_create_drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestCreateDrop(t *testing.T) {
require.True(t, memberfound1)
require.Equal(t, memberB.DenomA, denomA)
require.Equal(t, memberB.DenomB, denomB)
require.Equal(t, "55", memberB.Balance.String())
require.Equal(t, "54", memberB.Balance.String())

owner, ok := testInput.MarketKeeper.GetDropsOwnerPair(testInput.Context, addr, pair)
require.True(t, ok)
Expand All @@ -108,7 +108,7 @@ func TestCreateDrop(t *testing.T) {
drop, dropFound := testInput.MarketKeeper.GetDrop(testInput.Context, createDropResponse.Uid)
require.True(t, dropFound)
require.Equal(t, drop.Pair, pair)
require.Equal(t, drop.Drops.String(), dropMsg.Drops)
require.Equal(t, createDropResponse.Drops, drop.Drops.String())
require.Equal(t, createDropResponse.Creator, dropMsg.GetCreator())

beforecount = aftercount
Expand All @@ -130,7 +130,7 @@ func TestCreateDrop(t *testing.T) {
rst2, found := testInput.MarketKeeper.GetPool(testInput.Context, pair)
require.True(t, found)
require.Equal(t, rst2.Pair, pair)
require.Equal(t, "166", rst2.Drops.String())
require.Equal(t, "165", rst2.Drops.String())
require.Equal(t, "154", rst2.Leaders[0].Drops.String())
require.Equal(t, rst2.Leaders[0].Address, addr)
require.Equal(t, rst2.Leaders[1].Address, addr2)
Expand All @@ -156,7 +156,7 @@ func TestCreateDrop(t *testing.T) {
pool, found = testInput.MarketKeeper.GetPool(testInput.Context, pair)
require.True(t, found)
require.Equal(t, pool.Pair, pair)
require.Equal(t, "1166", pool.Drops.String())
require.Equal(t, "1165", pool.Drops.String())
require.Equal(t, 3, len(pool.Leaders))
require.Equal(t, "1000", pool.Leaders[0].Drops.String())

Expand All @@ -176,11 +176,11 @@ func TestCreateDrop(t *testing.T) {
pool, found = testInput.MarketKeeper.GetPool(testInput.Context, pair)
require.True(t, found)
require.Equal(t, pool.Pair, pair)
require.Equal(t, "1566", pool.Drops.String())
require.Equal(t, "1565", pool.Drops.String())
require.Equal(t, 3, len(pool.Leaders))
require.Equal(t, "1400", pool.Leaders[0].Drops.String())
require.Equal(t, "154", pool.Leaders[1].Drops.String())
require.Equal(t, "12", pool.Leaders[2].Drops.String())
require.Equal(t, "11", pool.Leaders[2].Drops.String())
require.Equalf(t, addr3, pool.Leaders[0].Address, pool.Leaders[0].Address)
require.Equalf(t, addr, pool.Leaders[1].Address, addr3)
require.Equalf(t, addr2, pool.Leaders[2].Address, pool.Leaders[2].Address)
Expand Down Expand Up @@ -423,10 +423,8 @@ func TestZeroAmtPaid(t *testing.T) {
require.Equal(t, 1, len(pool.Leaders))
require.Equal(t, "63", pool.Leaders[0].Drops.String())

// Validate CreateDrop
// There should not be a situation where zero amt is paid for either member
// There is a round up (+1) to all dropAmounts
// Validate ZeroAmt
var d = types.MsgCreateDrop{Creator: addr, Pair: pair, Drops: "1"}
_, err = keeper.NewMsgServerImpl(*testInput.MarketKeeper).CreateDrop(sdk.WrapSDKContext(testInput.Context), &d)
require.NoError(t, err)
require.Error(t, err)
}
13 changes: 0 additions & 13 deletions x/market/keeper/msg_server_redeem_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,6 @@ func (k msgServer) RedeemDrop(goCtx context.Context, msg *types.MsgRedeemDrop) (
drop.TimeEnd = ctx.BlockHeader().Time.Unix()
drop.Coin1End = coinOwner1
drop.Coin2End = coinOwner2

// Product End Adjusted accounts for the round up division when calculating amount of Coins
// to contribute to pool given number of drops. Division rounds down, so +1 is added to number
// of coins. This rounding may result in the user receiving 1 less coin when redeeming if there has
// been no profit in the pool. We add this +1 coin back in for the productEndAdjusted to account
// for this potential error.
productEndAdjusted := (drop.Coin1End.Amount.Add(sdk.NewIntFromUint64(1))).Mul(drop.Coin2End.Amount.Add(sdk.NewIntFromUint64(1)))

// productEndAdjusted should never be less than Product Beg
if productEndAdjusted.LT(drop.ProductBeg) {
return nil, sdkerrors.Wrap(types.ErrProductLessThanExpected, "Product End Adjusted lower than Product Beg")
}

drop.ProductEnd = total1.Mul(total2)

// Set Pool Member and Drop
Expand Down

0 comments on commit 1129ac8

Please sign in to comment.