Skip to content

Commit

Permalink
Add events and tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Dusek committed Jun 10, 2024
1 parent 25c21aa commit 94f7ce3
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 163 deletions.
10 changes: 5 additions & 5 deletions x/market/client/cli/query_drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func networkWithDropObjects(t *testing.T, n int) (*network.Network, []types.Drop

for i := 0; i < n; i++ {
drop := types.Drop{
Uid: uint64(i),
Owner: strconv.Itoa(i),
Pair: strconv.Itoa(i),
Drops: sdk.NewIntFromUint64(uint64(i)),
Product: sdk.NewIntFromUint64(uint64(i)),
Uid: uint64(i),
Owner: strconv.Itoa(i),
Pair: strconv.Itoa(i),
Drops: sdk.NewIntFromUint64(uint64(i)),
ProductBeg: sdk.NewIntFromUint64(uint64(i)),
}
nullify.Fill(&drop)
state.DropList = append(state.DropList, drop)
Expand Down
24 changes: 12 additions & 12 deletions x/market/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ func TestGenesis(t *testing.T) {
},
DropList: []types.Drop{
{
Uid: 0,
Owner: "0",
Pair: "0",
Drops: sdk.NewIntFromUint64(uint64(0)),
Product: sdk.NewIntFromUint64(uint64(0)),
Active: true,
Uid: 0,
Owner: "0",
Pair: "0",
Drops: sdk.NewIntFromUint64(uint64(0)),
ProductBeg: sdk.NewIntFromUint64(uint64(0)),
Active: true,
},
{
Uid: 1,
Owner: "1",
Pair: "1",
Drops: sdk.NewIntFromUint64(uint64(1)),
Product: sdk.NewIntFromUint64(uint64(0)),
Active: true,
Uid: 1,
Owner: "1",
Pair: "1",
Drops: sdk.NewIntFromUint64(uint64(1)),
ProductBeg: sdk.NewIntFromUint64(uint64(0)),
Active: true,
},
},
MemberList: []types.Member{
Expand Down
40 changes: 40 additions & 0 deletions x/market/keeper/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"math/big"
"sort"
"strconv"
"strings"

"github.com/cosmos/cosmos-sdk/store/prefix"
Expand All @@ -21,6 +22,45 @@ func (k Keeper) SetDrop(ctx sdk.Context, drop types.Drop) {
store.Set(types.DropKey(
drop.Uid,
), a)

if drop.Active {
// drop creation event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeDrop,
sdk.NewAttribute(types.AttributeKeyUid, strconv.FormatUint(drop.Uid, 10)),
sdk.NewAttribute(types.AttributeKeyOwner, drop.Owner),
sdk.NewAttribute(types.AttributeKeyActive, strconv.FormatBool(drop.Active)),
sdk.NewAttribute(types.AttributeKeyPair, drop.Pair),
sdk.NewAttribute(types.AttributeKeyDrops, drop.Drops.String()),
sdk.NewAttribute(types.AttributeKeyBeginTime, strconv.FormatInt(drop.TimeBeg, 10)),
sdk.NewAttribute(types.AttributeKeyCoin1Beg, drop.Coin1Beg.String()),
sdk.NewAttribute(types.AttributeKeyCoin2Beg, drop.Coin2Beg.String()),
sdk.NewAttribute(types.AttributeKeyProductBeg, drop.ProductBeg.String()),
),
)
} else {
// drop end event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeDrop,
sdk.NewAttribute(types.AttributeKeyUid, strconv.FormatUint(drop.Uid, 10)),
sdk.NewAttribute(types.AttributeKeyOwner, drop.Owner),
sdk.NewAttribute(types.AttributeKeyActive, strconv.FormatBool(drop.Active)),
sdk.NewAttribute(types.AttributeKeyPair, drop.Pair),
sdk.NewAttribute(types.AttributeKeyDrops, drop.Drops.String()),
sdk.NewAttribute(types.AttributeKeyBeginTime, strconv.FormatInt(drop.TimeBeg, 10)),
sdk.NewAttribute(types.AttributeKeyCoin1Beg, drop.Coin1Beg.String()),
sdk.NewAttribute(types.AttributeKeyCoin2Beg, drop.Coin2Beg.String()),
sdk.NewAttribute(types.AttributeKeyProductBeg, drop.ProductBeg.String()),
sdk.NewAttribute(types.AttributeKeyEndTime, strconv.FormatInt(drop.TimeBeg, 10)),
sdk.NewAttribute(types.AttributeKeyCoin1End, drop.Coin1End.String()),
sdk.NewAttribute(types.AttributeKeyCoin2End, drop.Coin2End.String()),
sdk.NewAttribute(types.AttributeKeyProductEnd, drop.ProductEnd.String()),
),
)
}

}

// GetDrop returns a drop from its index
Expand Down
2 changes: 1 addition & 1 deletion x/market/keeper/drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func createNDrop(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Drop {
items[i].Owner = strconv.Itoa(i)
items[i].Pair = strconv.Itoa(i)
items[i].Drops = sdk.NewIntFromUint64(uint64(i))
items[i].Product = sdk.NewIntFromUint64(uint64(i))
items[i].ProductBeg = sdk.NewIntFromUint64(uint64(i))

keeper.SetDrop(ctx, items[i])
}
Expand Down
26 changes: 17 additions & 9 deletions x/market/keeper/msg_server_cancel_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ 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 orderCommon(t *testing.T, testInput keepertest.TestInput) {
func orderCommon(t *testing.T, testInput keepertest.TestInput) (pair string, denomA string, denomB string, testdata testData) {
coinPair, _ := sample.SampleCoins("10000CoinA", "10000CoinB")
testdata = testData{coinAStr: "30CoinA", coinBStr: "40CoinB", RateAstrArray: []string{"60", "70"}, RateBstrArray: []string{"80", "90"}}
denomA = "CoinA"
denomB = "CoinB"
pair = strings.Join([]string{denomA, denomB}, ",")

// MintCoins
require.NoError(t, testInput.BankKeeper.MintCoins(testInput.Context, types.ModuleName, coinPair))

Expand All @@ -29,12 +37,14 @@ func orderCommon(t *testing.T, testInput keepertest.TestInput) {
var d = types.MsgCreateDrop{Creator: addr, Pair: pair, Drops: "120"}
_, err = keeper.NewMsgServerImpl(*testInput.MarketKeeper).CreateDrop(sdk.WrapSDKContext(testInput.Context), &d)
require.NoError(t, err)

return
}

func TestCancelOrder_case1_stop(t *testing.T) {
testInput := keepertest.CreateTestEnvironment(t)

orderCommon(t, testInput)
_, denomA, denomB, testdata := orderCommon(t, testInput)

beforecount := testInput.MarketKeeper.GetUidCount(testInput.Context)

Expand Down Expand Up @@ -92,7 +102,7 @@ func TestCancelOrder_case1_limit(t *testing.T) {

testInput := keepertest.CreateTestEnvironment(t)

orderCommon(t, testInput)
pair, denomA, denomB, testdata := orderCommon(t, testInput)

beforecount := testInput.MarketKeeper.GetUidCount(testInput.Context)

Expand Down Expand Up @@ -175,7 +185,7 @@ func TestCancelOrder_case2_stop(t *testing.T) {

testInput := keepertest.CreateTestEnvironment(t)

orderCommon(t, testInput)
_, denomA, denomB, testdata := orderCommon(t, testInput)

beforecount := testInput.MarketKeeper.GetUidCount(testInput.Context)

Expand Down Expand Up @@ -280,16 +290,14 @@ func TestCancelOrderEmptyPool(t *testing.T) {
// Validate RedeemDrop
Uid := strconv.FormatUint(1, 10)
var rd = types.MsgRedeemDrop{Creator: addr, Uid: Uid}
createRedeemDropResponse, redeemdropErr := keeper.NewMsgServerImpl(*testInput.MarketKeeper).RedeemDrop(sdk.WrapSDKContext(testInput.Context), &rd)
_, redeemdropErr := keeper.NewMsgServerImpl(*testInput.MarketKeeper).RedeemDrop(sdk.WrapSDKContext(testInput.Context), &rd)
require.NoError(t, redeemdropErr)
require.Contains(t, rd.GetCreator(), createRedeemDropResponse.String())

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

// Create Pool
var p = types.MsgCreatePool{CoinA: testdata.coinAStr, CoinB: testdata.coinBStr, Creator: addr}
Expand All @@ -311,7 +319,7 @@ func TestCancelOrder_case1_market_filled(t *testing.T) {

testInput := keepertest.CreateTestEnvironment(t)

orderCommon(t, testInput)
_, denomA, denomB, _ := orderCommon(t, testInput)

//Validate GetMember
memberBid, memberfoundBid := testInput.MarketKeeper.GetMember(testInput.Context, denomA, denomB)
Expand Down
2 changes: 1 addition & 1 deletion x/market/keeper/msg_server_create_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (k msgServer) CreateDrop(goCtx context.Context, msg *types.MsgCreateDrop) (
// Pool creator drops = sqrt(XY).
// Therefore sqrt(Product) must be equal to or greater than drops
if sqrtDropProduct.LT(drops) {
fmt.Printf("Sqrt Product: %s", sqrtDropProduct)
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.")
}
Expand Down
Loading

0 comments on commit 94f7ce3

Please sign in to comment.