Skip to content

Commit

Permalink
Merge pull request #192 from scorpioborn/refactor/bet-place-payload
Browse files Browse the repository at this point in the history
Refactor/ App Keepers pointers and Bet
  • Loading branch information
3eyedraga authored Jun 28, 2023
2 parents 06d158e + fbf93a3 commit 6b44895
Show file tree
Hide file tree
Showing 19 changed files with 222 additions and 196 deletions.
92 changes: 51 additions & 41 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ type AppKeepers struct {
tkeys map[string]*sdk.TransientStoreKey
memKeys map[string]*sdk.MemoryStoreKey

// keepers
// SDK keepers
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
MintKeeper mintkeeper.Keeper
DistrKeeper distrkeeper.Keeper
GovKeeper govkeeper.Keeper
CrisisKeeper crisiskeeper.Keeper
Expand All @@ -101,26 +100,30 @@ type AppKeepers struct {
FeeGrantKeeper feegrantkeeper.Keeper
AuthzKeeper authzkeeper.Keeper

MarketKeeper marketmodulekeeper.Keeper
BetKeeper betmodulekeeper.Keeper
OVMKeeper ovmmodulekeeper.Keeper
OrderbookKeeper orderbookmodulekeeper.Keeper
HouseKeeper housemodulekeeper.Keeper
MarketModule marketmodule.AppModule
//// SGE keepers \\\\
BetKeeper *betmodulekeeper.Keeper
MarketKeeper *marketmodulekeeper.Keeper
MintKeeper mintkeeper.Keeper
HouseKeeper *housemodulekeeper.Keeper
OrderbookKeeper *orderbookmodulekeeper.Keeper
OVMKeeper *ovmmodulekeeper.Keeper

//// SGE modules \\\\
BetModule betmodule.AppModule
OrderbookModule orderbookmodule.AppModule
MarketModule marketmodule.AppModule
HouseModule housemodule.AppModule

// modules
ICAModule ica.AppModule
TransferModule transfer.AppModule
OVMModule ovmmodule.AppModule
OrderbookModule orderbookmodule.AppModule
OVMModule ovmmodule.AppModule

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedICAControllerKeeper capabilitykeeper.ScopedKeeper
ScopedICAHostKeeper capabilitykeeper.ScopedKeeper

// IBC Modules
ICAModule ica.AppModule
TransferModule transfer.AppModule
}

func NewAppKeeper(
Expand Down Expand Up @@ -214,17 +217,6 @@ func NewAppKeeper(
appKeepers.GetSubspace(stakingtypes.ModuleName),
)

appKeepers.OrderbookKeeper = *orderbookmodulekeeper.NewKeeper(
appCodec,
appKeepers.keys[orderbookmoduletypes.StoreKey],
appKeepers.GetSubspace(orderbookmoduletypes.ModuleName),
orderbookmodulekeeper.SdkExpectedKeepers{
BankKeeper: appKeepers.BankKeeper,
AccountKeeper: appKeepers.AccountKeeper,
FeeGrantKeeper: appKeepers.FeeGrantKeeper,
},
)

appKeepers.MintKeeper = *mintkeeper.NewKeeper(
appCodec,
appKeepers.keys[minttypes.StoreKey],
Expand Down Expand Up @@ -347,14 +339,27 @@ func NewAppKeeper(
appKeepers.SlashingKeeper,
)

appKeepers.OVMKeeper = *ovmmodulekeeper.NewKeeper(
//// SGE keepers \\\\

appKeepers.OrderbookKeeper = orderbookmodulekeeper.NewKeeper(
appCodec,
appKeepers.keys[orderbookmoduletypes.StoreKey],
appKeepers.GetSubspace(orderbookmoduletypes.ModuleName),
orderbookmodulekeeper.SdkExpectedKeepers{
BankKeeper: appKeepers.BankKeeper,
AccountKeeper: appKeepers.AccountKeeper,
FeeGrantKeeper: appKeepers.FeeGrantKeeper,
},
)

appKeepers.OVMKeeper = ovmmodulekeeper.NewKeeper(
appCodec,
appKeepers.keys[ovmmoduletypes.StoreKey],
appKeepers.keys[ovmmoduletypes.MemStoreKey],
appKeepers.GetSubspace(ovmmoduletypes.ModuleName),
)

appKeepers.MarketKeeper = *marketmodulekeeper.NewKeeper(
appKeepers.MarketKeeper = marketmodulekeeper.NewKeeper(
appCodec,
appKeepers.keys[marketmoduletypes.StoreKey],
appKeepers.keys[marketmoduletypes.MemStoreKey],
Expand All @@ -363,7 +368,7 @@ func NewAppKeeper(
appKeepers.MarketKeeper.SetOVMKeeper(appKeepers.OVMKeeper)
appKeepers.MarketKeeper.SetOrderbookKeeper(appKeepers.OrderbookKeeper)

appKeepers.BetKeeper = *betmodulekeeper.NewKeeper(
appKeepers.BetKeeper = betmodulekeeper.NewKeeper(
appCodec,
appKeepers.keys[betmoduletypes.StoreKey],
appKeepers.keys[betmoduletypes.MemStoreKey],
Expand All @@ -377,7 +382,7 @@ func NewAppKeeper(
appKeepers.OrderbookKeeper.SetMarketKeeper(appKeepers.MarketKeeper)
appKeepers.OrderbookKeeper.SetOVMKeeper(appKeepers.OVMKeeper)

appKeepers.HouseKeeper = *housemodulekeeper.NewKeeper(
appKeepers.HouseKeeper = housemodulekeeper.NewKeeper(
appCodec,
appKeepers.keys[housemoduletypes.StoreKey],
appKeepers.OrderbookKeeper,
Expand All @@ -389,32 +394,37 @@ func NewAppKeeper(
)
appKeepers.OrderbookKeeper.SetHouseKeeper(appKeepers.HouseKeeper)

appKeepers.OVMModule = ovmmodule.NewAppModule(
//// SGE modules \\\\

appKeepers.BetModule = betmodule.NewAppModule(
appCodec,
appKeepers.OVMKeeper,
*appKeepers.BetKeeper,
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
appKeepers.MarketKeeper,
appKeepers.OrderbookKeeper,
appKeepers.OVMKeeper,
)
appKeepers.MarketModule = marketmodule.NewAppModule(
appCodec,
appKeepers.MarketKeeper,
*appKeepers.MarketKeeper,
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
appKeepers.OVMKeeper,
)
appKeepers.BetModule = betmodule.NewAppModule(
appKeepers.HouseModule = housemodule.NewAppModule(
appCodec,
appKeepers.BetKeeper,
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
appKeepers.MarketKeeper,
appKeepers.OrderbookKeeper,
appKeepers.OVMKeeper,
*appKeepers.HouseKeeper,
)
appKeepers.HouseModule = housemodule.NewAppModule(appCodec, appKeepers.HouseKeeper)
appKeepers.OrderbookModule = orderbookmodule.NewAppModule(
appCodec,
appKeepers.OrderbookKeeper,
*appKeepers.OrderbookKeeper,
)
appKeepers.OVMModule = ovmmodule.NewAppModule(
appCodec,
*appKeepers.OVMKeeper,
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
)

// Create static IBC router, add transfer route, then set and seal it
Expand Down
4 changes: 2 additions & 2 deletions x/bet/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func TestGenesis(t *testing.T) {
},
}

bet.InitGenesis(ctx, tApp.BetKeeper, genesisState)
got := bet.ExportGenesis(ctx, tApp.BetKeeper)
bet.InitGenesis(ctx, *tApp.BetKeeper, genesisState)
got := bet.ExportGenesis(ctx, *tApp.BetKeeper)
require.NotNil(t, got)

nullify.Fill(&genesisState)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions x/bet/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func setupKeeperAndApp(t testing.TB) (*simappUtil.TestApp, *keeper.KeeperTest, s
tApp, ctx, err := simappUtil.GetTestObjects()
require.NoError(t, err)

return tApp, &tApp.BetKeeper, ctx
return tApp, tApp.BetKeeper, ctx
}

func setupKeeper(t testing.TB) (*keeper.KeeperTest, sdk.Context) {
Expand Down Expand Up @@ -81,7 +81,7 @@ func addTestMarket(t testing.TB, tApp *simappUtil.TestApp, ctx sdk.Context) {
Ticket: testAddMarketTicket,
}
wctx := sdk.WrapSDKContext(ctx)
marketSrv := marketkeeper.NewMsgServerImpl(tApp.MarketKeeper)
marketSrv := marketkeeper.NewMsgServerImpl(*tApp.MarketKeeper)
resAddMarket, err := marketSrv.AddMarket(wctx, testAddMarket)
require.Nil(t, err)
require.NotNil(t, resAddMarket)
Expand Down Expand Up @@ -115,7 +115,7 @@ func addTestMarketBatch(
Ticket: testAddMarketTicket,
}
wctx := sdk.WrapSDKContext(ctx)
marketSrv := marketkeeper.NewMsgServerImpl(tApp.MarketKeeper)
marketSrv := marketkeeper.NewMsgServerImpl(*tApp.MarketKeeper)
resAddMarket, err := marketSrv.AddMarket(wctx, testAddMarket)
require.Nil(t, err)
require.NotNil(t, resAddMarket)
Expand All @@ -133,7 +133,7 @@ func placeTestBet(
) {
testCreator = simappUtil.TestParamUsers["user1"].Address.String()
wctx := sdk.WrapSDKContext(ctx)
betSrv := keeper.NewMsgServerImpl(tApp.BetKeeper)
betSrv := keeper.NewMsgServerImpl(*tApp.BetKeeper)
testKyc := &sgetypes.KycDataPayload{
Approved: true,
ID: testCreator,
Expand Down
8 changes: 4 additions & 4 deletions x/bet/keeper/msg_server_bet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ func (k msgServer) PlaceBet(
return nil, sdkerrors.Wrapf(types.ErrDuplicateUID, "%s", msg.Bet.UID)
}

ticketData := &types.BetPlacementTicketPayload{}
err := k.ovmKeeper.VerifyTicketUnmarshal(sdk.WrapSDKContext(ctx), msg.Bet.Ticket, &ticketData)
payload := &types.BetPlacementTicketPayload{}
err := k.ovmKeeper.VerifyTicketUnmarshal(sdk.WrapSDKContext(ctx), msg.Bet.Ticket, &payload)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrInTicketVerification, "%s", err)
}

if err = ticketData.Validate(msg.Creator); err != nil {
if err = payload.Validate(msg.Creator); err != nil {
return nil, sdkerrors.Wrapf(types.ErrInTicketValidation, "%s", err)
}

bet := types.NewBet(msg.Creator, msg.Bet, ticketData.OddsType, ticketData.SelectedOdds)
bet := types.NewBet(msg.Creator, msg.Bet, payload.OddsType, payload.SelectedOdds)

if err := k.Keeper.PlaceBet(ctx, bet); err != nil {
return nil, sdkerrors.Wrapf(types.ErrInBetPlacement, "%s", err)
Expand Down
24 changes: 24 additions & 0 deletions x/bet/types/place_bet_fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package types

import (
"strings"

"github.com/sge-network/sge/utils"
)

// BetFieldsValidation validates fields of the given bet
func BetFieldsValidation(bet *PlaceBetFields) error {
if !utils.IsValidUID(bet.UID) {
return ErrInvalidBetUID
}

if bet.Amount.IsNil() || !bet.Amount.IsPositive() {
return ErrInvalidAmount
}

if bet.Ticket == "" || strings.Contains(bet.Ticket, " ") {
return ErrInvalidTicket
}

return nil
}
80 changes: 80 additions & 0 deletions x/bet/types/place_bet_fields_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package types_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sge-network/sge/x/bet/types"
"github.com/stretchr/testify/require"
)

func TestBetFieldsValidation(t *testing.T) {
tcs := []struct {
desc string
bet *types.PlaceBetFields
err error
}{
{
desc: "space in UID",
bet: &types.PlaceBetFields{
UID: " ",
Amount: sdk.NewInt(int64(10)),
Ticket: "Ticket",
},
err: types.ErrInvalidBetUID,
},
{
desc: "invalid UID",
bet: &types.PlaceBetFields{
UID: "invalidUID",
Amount: sdk.NewInt(int64(10)),
Ticket: "Ticket",
},
err: types.ErrInvalidBetUID,
},
{
desc: "invalid amount",
bet: &types.PlaceBetFields{
UID: "6e31c60f-2025-48ce-ae79-1dc110f16355",
Amount: sdk.NewInt(int64(-1)),
Ticket: "Ticket",
},
err: types.ErrInvalidAmount,
},
{
desc: "empty amount",
bet: &types.PlaceBetFields{
UID: "6e31c60f-2025-48ce-ae79-1dc110f16355",
Ticket: "Ticket",
},
err: types.ErrInvalidAmount,
},
{
desc: "space in ticket",
bet: &types.PlaceBetFields{
UID: "6e31c60f-2025-48ce-ae79-1dc110f16355",
Amount: sdk.NewInt(int64(10)),
Ticket: " ",
},
err: types.ErrInvalidTicket,
},
{
desc: "valid message",
bet: &types.PlaceBetFields{
UID: "6e31c60f-2025-48ce-ae79-1dc110f16355",
Amount: sdk.NewInt(int64(10)),
Ticket: "Ticket",
},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
err := types.BetFieldsValidation(tc.bet)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
return
}
require.NoError(t, err)
})
}
}
48 changes: 48 additions & 0 deletions x/bet/types/ticket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package types

import (
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/sge-network/sge/utils"
)

// Validate validates fields of the given ticketData
func (payload *BetPlacementTicketPayload) Validate(creator string) error {
if payload.SelectedOdds == nil {
return ErrOddsDataNotFound
}

if !payload.KycData.Validate(creator) {
return sdkerrors.Wrapf(ErrUserKycFailed, "%s", creator)
}

if !utils.IsValidUID(payload.SelectedOdds.MarketUID) {
return ErrInvalidMarketUID
}

if !utils.IsValidUID(payload.SelectedOdds.UID) {
return ErrInvalidOddsUID
}

if len(strings.TrimSpace(payload.SelectedOdds.Value)) == 0 {
return ErrEmptyOddsValue
}

if payload.SelectedOdds.MaxLossMultiplier.IsNil() ||
payload.SelectedOdds.MaxLossMultiplier.LTE(sdk.ZeroDec()) {
return ErrMaxLossMultiplierCanNotBeZero
}

if payload.SelectedOdds.MaxLossMultiplier.GT(sdk.OneDec()) {
return ErrMaxLossMultiplierCanNotBeMoreThanOne
}

if payload.OddsType < OddsType_ODDS_TYPE_DECIMAL ||
payload.OddsType > OddsType_ODDS_TYPE_MONEYLINE {
return ErrInvalidOddsType
}

return nil
}
Loading

0 comments on commit 6b44895

Please sign in to comment.