Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subaccount module part 6 #233

Merged
merged 33 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ type AppKeepers struct {
TransferKeeper ibctransferkeeper.Keeper
FeeGrantKeeper feegrantkeeper.Keeper
AuthzKeeper authzkeeper.Keeper
SubaccountKeeper subaccountkeeper.Keeper

// // SGE keepers \\\\
BetKeeper *betmodulekeeper.Keeper
Expand All @@ -111,14 +110,15 @@ type AppKeepers struct {
HouseKeeper *housemodulekeeper.Keeper
OrderbookKeeper *orderbookmodulekeeper.Keeper
OVMKeeper *ovmmodulekeeper.Keeper
SubaccountModule subaccount.AppModule
SubaccountKeeper *subaccountkeeper.Keeper

// // SGE modules \\\\
BetModule betmodule.AppModule
MarketModule marketmodule.AppModule
HouseModule housemodule.AppModule
OrderbookModule orderbookmodule.AppModule
OVMModule ovmmodule.AppModule
BetModule betmodule.AppModule
MarketModule marketmodule.AppModule
HouseModule housemodule.AppModule
OrderbookModule orderbookmodule.AppModule
OVMModule ovmmodule.AppModule
SubaccountModule subaccount.AppModule

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
Expand Down Expand Up @@ -399,6 +399,18 @@ func NewAppKeeper(
)
appKeepers.OrderbookKeeper.SetHouseKeeper(appKeepers.HouseKeeper)

appKeepers.SubaccountKeeper = subaccountkeeper.NewKeeper(
appCodec,
appKeepers.keys[subaccounttypes.StoreKey],
appKeepers.GetSubspace(subaccounttypes.ModuleName),
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
appKeepers.OVMKeeper,
appKeepers.BetKeeper,
appKeepers.OrderbookKeeper,
appKeepers.HouseKeeper,
)

// // SGE modules \\\\

appKeepers.BetModule = betmodule.NewAppModule(
Expand Down Expand Up @@ -431,17 +443,8 @@ func NewAppKeeper(
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
)
appKeepers.SubaccountKeeper = subaccountkeeper.NewKeeper(
appCodec,
appKeepers.keys[subaccounttypes.StoreKey],
appKeepers.GetSubspace(subaccounttypes.ModuleName),
appKeepers.BankKeeper,
appKeepers.OVMKeeper,
appKeepers.BetKeeper,
appKeepers.OrderbookKeeper,
appKeepers.HouseKeeper,
)
appKeepers.SubaccountModule = subaccount.NewAppModule(appKeepers.SubaccountKeeper)

appKeepers.SubaccountModule = subaccount.NewAppModule(*appKeepers.SubaccountKeeper)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := ibcporttypes.NewRouter()
Expand Down
21 changes: 21 additions & 0 deletions proto/sge/subaccount/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@ package sgenetwork.sge.subaccount;

import "gogoproto/gogo.proto";
import "sge/subaccount/params.proto";
import "sge/subaccount/subaccount.proto";

option go_package = "github.com/sge-network/sge/x/subaccount/types";

// GenesisState defines the subaccount module's genesis state.
message GenesisState {
// params contains the subaccount parameters.
Params params = 1 [ (gogoproto.nullable) = false ];

uint64 subaccount_id = 2;

// subaccounts contains all the subaccounts.
repeated GenesisSubaccount subaccounts = 3 [ (gogoproto.nullable) = false ];
}

// GenesisSubaccount defines the genesis subaccount containing owner, address and balance information.
message GenesisSubaccount {
// address is the address of the subaccount.
string address = 1;

// owner is the owner of the subaccount.
string owner = 2;

// balance defines the balance status of a subaccount
Balance balance = 3 [ (gogoproto.nullable) = false ];

// locked_balances defines the lockup of balances history of a subaccount
repeated LockedBalance locked_balances = 4 [ (gogoproto.nullable) = false ];
}
37 changes: 36 additions & 1 deletion proto/sge/subaccount/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,40 @@ package sgenetwork.sge.subaccount;

option go_package = "github.com/sge-network/sge/x/subaccount/types";

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "sge/subaccount/subaccount.proto";
import "sge/subaccount/params.proto";

// Query defines the gRPC querier service.
service Query {}
service Query {
// Subaccount fetches a subaccount given the owner.
rpc Subaccount(QuerySubaccountRequest) returns (QuerySubaccountResponse) {
option (google.api.http).get = "/sge/subaccount/subaccount";
};
// Params returns the subaccount module parameters.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/sge/subaccount/params";
};
}

// QueryParamsRequest is the request type for the Query/Params RPC method
message QueryParamsRequest {}
// QueryParamsResponse is the response type for the Query/Params RPC method
message QueryParamsResponse {
sge.subaccount.Params params = 1;
}

// QuerySubaccountRequest is the request type for the Query/Subaccount RPC
message QuerySubaccountRequest {
string subaccount_owner = 1;
}

// QuerySubaccountResponse is the response type for the Query/Subaccount RPC
message QuerySubaccountResponse {
string subaccount_address = 1;
sge.subaccount.Balance balance = 2 [(gogoproto.nullable) = false ];
repeated sge.subaccount.LockedBalance locked_balance = 3 [(gogoproto.nullable) = false ];
}


5 changes: 3 additions & 2 deletions proto/sge/subaccount/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sgenetwork.sge.subaccount;
import "sge/subaccount/subaccount.proto";
import "sge/bet/tx.proto";
import "sge/house/tx.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/sge-network/sge/x/subaccount/types";

Expand Down Expand Up @@ -37,7 +38,7 @@ message MsgCreateSubAccount {
string sub_account_owner = 2;

// locked_balances is the list of balance locks.
repeated LockedBalance locked_balances = 3;
repeated LockedBalance locked_balances = 3 [(gogoproto.nullable) = false];
}

// MsgCreateAccountResponse defines the Msg/CreateAccount response type.
Expand All @@ -52,7 +53,7 @@ message MsgTopUp {
string sub_account = 2;

// locked_balances is the list of balance locks.
repeated LockedBalance locked_balances = 3;
repeated LockedBalance locked_balances = 3 [(gogoproto.nullable) = false];
}

// MsgTopUpResponse defines the Msg/TopUp response type.
Expand Down
8 changes: 5 additions & 3 deletions x/orderbook/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ type Hook interface {
AfterBettorWin(ctx sdk.Context, bettor sdk.AccAddress, originalAmount, profit sdk.Int)
AfterBettorLoss(ctx sdk.Context, bettor sdk.AccAddress, originalAmount sdk.Int)
AfterBettorRefund(ctx sdk.Context, bettor sdk.AccAddress, originalAmount, fee sdk.Int)
AfterHouseWin(ctx sdk.Context, house sdk.AccAddress, originalAmount, profit sdk.Int, fee *sdk.Int)
AfterHouseLoss(ctx sdk.Context, house sdk.AccAddress, originalAmount sdk.Int, lostAmt sdk.Int, fee *sdk.Int)
AfterHouseRefund(ctx sdk.Context, house sdk.AccAddress, originalAmount, fee sdk.Int)

AfterHouseWin(ctx sdk.Context, house sdk.AccAddress, originalAmount, profit sdk.Int)
AfterHouseLoss(ctx sdk.Context, house sdk.AccAddress, originalAmount sdk.Int, lostAmt sdk.Int)
AfterHouseRefund(ctx sdk.Context, house sdk.AccAddress, originalAmount sdk.Int)
AfterHouseFeeRefund(ctx sdk.Context, house sdk.AccAddress, fee sdk.Int)
}

func (k *Keeper) RegisterHook(hook Hook) {
Expand Down
52 changes: 23 additions & 29 deletions x/orderbook/keeper/orderbook_settle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
"log"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -108,26 +109,30 @@ func (k Keeper) settleParticipation(

refundHouseDepositFeeToDepositor := false

var (
profit sdk.Int
originalAmount sdk.Int
feeRefund *sdk.Int
cancelled bool
)

switch market.Status {
case markettypes.MarketStatus_MARKET_STATUS_RESULT_DECLARED:
depositPlusProfit := bp.Liquidity.Add(bp.ActualProfit)
log.Printf("orderbook_settle.goL115: market status declared")
// refund participant's account from orderbook liquidity pool.
if err := k.refund(types.OrderBookLiquidityFunder{}, ctx, depositorAddress, depositPlusProfit); err != nil {
return err
}
if bp.NotParticipatedInBetFulfillment() {
refundHouseDepositFeeToDepositor = true
}
// prepare hook variables.
profit = bp.ActualProfit
originalAmount = bp.Liquidity
if bp.ActualProfit.IsNegative() {
log.Printf("orderbook_settle.goL123: market declared loss")
for _, h := range k.hooks {
log.Printf("orderbook_settle.goL125: market declared loss hook call")
h.AfterHouseLoss(ctx, depositorAddress, bp.Liquidity, bp.ActualProfit.Abs())
}
} else {
log.Printf("orderbook_settle.goL129: market declared win")
for _, h := range k.hooks {
log.Printf("orderbook_settle.goL130: market declared win hook call")
h.AfterHouseWin(ctx, depositorAddress, bp.Liquidity, bp.ActualProfit)
}
}

case markettypes.MarketStatus_MARKET_STATUS_CANCELED,
markettypes.MarketStatus_MARKET_STATUS_ABORTED:
Expand All @@ -136,9 +141,11 @@ func (k Keeper) settleParticipation(
return err
}
refundHouseDepositFeeToDepositor = true
profit = sdk.ZeroInt()
originalAmount = bp.Liquidity
cancelled = true
log.Printf("orderbook_settle.goL139: market cancelled, hooks to execute: %d", len(k.hooks))
for _, h := range k.hooks {
log.Printf("orderbook_settle.goL141: market cancelled hook call")
h.AfterHouseRefund(ctx, depositorAddress, bp.Liquidity)
}
default:
return sdkerrors.Wrapf(
types.ErrUnknownMarketStatus,
Expand All @@ -153,7 +160,9 @@ func (k Keeper) settleParticipation(
if err := k.refund(housetypes.HouseFeeCollectorFunder{}, ctx, depositorAddress, bp.Fee); err != nil {
return err
}
feeRefund = &bp.Fee
for _, h := range k.hooks {
h.AfterHouseFeeRefund(ctx, depositorAddress, bp.Fee)
}
} else {
// refund participant's account from house fee collector.
if err := k.refund(housetypes.HouseFeeCollectorFunder{}, ctx, sdk.MustAccAddressFromBech32(market.Creator), bp.Fee); err != nil {
Expand All @@ -164,20 +173,5 @@ func (k Keeper) settleParticipation(
bp.IsSettled = true
k.SetOrderBookParticipation(ctx, bp)

// call hooks
switch {
case cancelled:
for _, h := range k.hooks {
h.AfterHouseRefund(ctx, depositorAddress, originalAmount, *feeRefund)
}
case profit.IsNegative():
for _, h := range k.hooks {
h.AfterHouseLoss(ctx, depositorAddress, originalAmount, profit.Abs(), feeRefund)
}
case profit.IsPositive(), profit.IsZero():
for _, h := range k.hooks {
h.AfterHouseWin(ctx, depositorAddress, originalAmount, profit, feeRefund)
}
}
return nil
}
36 changes: 34 additions & 2 deletions x/subaccount/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package cli

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"

"github.com/sge-network/sge/x/bet/types"
"github.com/sge-network/sge/x/subaccount/types"
)

// GetQueryCmd returns the cli query commands for this module
Expand All @@ -24,7 +27,36 @@ func GetQueryCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand()
cmd.AddCommand(QuerySubaccount())

return cmd
}

func QuerySubaccount() *cobra.Command {
cmd := &cobra.Command{
Use: "subaccount [owner-address]",
Short: "queries the subaccount of an owner",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

_, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

res, err := queryClient.Subaccount(context.Background(), &types.QuerySubaccountRequest{SubaccountOwner: args[0]})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
Loading