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

Audit/subaccount #256

Merged
merged 15 commits into from
Sep 21, 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
8 changes: 8 additions & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,14 @@ func NewAppKeeper(
appKeepers.HouseKeeper,
)

// ** Hooks ** \\

appKeepers.OrderbookKeeper.SetHooks(
orderbookmoduletypes.NewMultiOrderBookHooks(
appKeepers.SubaccountKeeper.Hooks(),
),
)

// // SGE modules \\\\

appKeepers.BetModule = betmodule.NewAppModule(
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module github.com/sge-network/sge
go 1.19

require (
cosmossdk.io/math v1.0.0-rc.0
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.1.2
github.com/cosmos/cosmos-sdk v0.46.14
github.com/cosmos/gogoproto v1.4.11
github.com/cosmos/ibc-go/v5 v5.2.1
github.com/cosmos/ibc-go/v5 v5.3.1
github.com/gogo/protobuf v1.3.3
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang/protobuf v1.5.3
github.com/golangci/golangci-lint v1.50.1
github.com/google/uuid v1.3.0
github.com/google/uuid v1.3.1
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/mrz1836/go-sanitize v1.3.1
Expand Down Expand Up @@ -61,7 +62,6 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
cloud.google.com/go/storage v1.30.1 // indirect
cosmossdk.io/errors v1.0.0-beta.7 // indirect
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoIS
collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE=
cosmossdk.io/errors v1.0.0-beta.7 h1:gypHW76pTQGVnHKo6QBkb4yFOJjC+sUGRc5Al3Odj1w=
cosmossdk.io/errors v1.0.0-beta.7/go.mod h1:mz6FQMJRku4bY7aqS/Gwfcmr/ue91roMEKAmDUDpBfE=
cosmossdk.io/math v1.0.0-rc.0 h1:ml46ukocrAAoBpYKMidF0R2tQJ1Uxfns0yH8wqgMAFc=
cosmossdk.io/math v1.0.0-rc.0/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k=
cosmossdk.io/math v1.1.2 h1:ORZetZCTyWkI5GlZ6CZS28fMHi83ZYf+A2vVnHNzZBM=
cosmossdk.io/math v1.1.2/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down Expand Up @@ -423,8 +423,8 @@ github.com/cosmos/gorocksdb v1.2.0 h1:d0l3jJG8M4hBouIZq0mDUHZ+zjOx044J3nGRskwTb4
github.com/cosmos/gorocksdb v1.2.0/go.mod h1:aaKvKItm514hKfNJpUJXnnOWeBnk2GL4+Qw9NHizILw=
github.com/cosmos/iavl v0.19.6 h1:XY78yEeNPrEYyNCKlqr9chrwoeSDJ0bV2VjocTk//OU=
github.com/cosmos/iavl v0.19.6/go.mod h1:X9PKD3J0iFxdmgNLa7b2LYWdsGd90ToV5cAONApkEPw=
github.com/cosmos/ibc-go/v5 v5.2.1 h1:i8Kl9KZfPJVYuihjbGPnjTQWecXQfRLz+QrjJGhzY9o=
github.com/cosmos/ibc-go/v5 v5.2.1/go.mod h1:eH18ghfqGGT9gDCTMDLWg1zPLXplSnPxXRNWPx98xhU=
github.com/cosmos/ibc-go/v5 v5.3.1 h1:zuaIWMEoXM9egWmmAqzBPYLvEjYgB6xy6wZoc8xsZ5U=
github.com/cosmos/ibc-go/v5 v5.3.1/go.mod h1:Q2W1rjtwR738fqFQhE66dZ0frsLlr+Wiezlwei0TMGo=
github.com/cosmos/keyring v1.2.0 h1:8C1lBP9xhImmIabyXW4c3vFjjLiBdGCmfLUfeZlV1Yo=
github.com/cosmos/keyring v1.2.0/go.mod h1:fc+wB5KTk9wQ9sDx0kFXB3A0MaeGHM9AwRStKOQ5vOA=
github.com/cosmos/ledger-cosmos-go v0.12.2 h1:/XYaBlE2BJxtvpkHiBm97gFGSGmYGKunKyF3nNqAXZA=
Expand Down Expand Up @@ -742,8 +742,9 @@ github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/enterprise-certificate-proxy v0.1.0/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/enterprise-certificate-proxy v0.2.0/go.mod h1:8C0jb7/mgJe/9KK8Lm7X9ctZC2t60YyIpYEI16jx0Qg=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,49 @@ syntax = "proto3";
package sgenetwork.sge.subaccount;

import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";

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

// Balance defines the balance of a subaccount.
message Balance {
// deposited_amount keeps track of how much was deposited so far in the subaccount.
// deposited_amount keeps track of how much was deposited so far in the
// subaccount.
string deposited_amount = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];

// spent_amount keeps track of how much was spent in the account in betting, house, staking, etc.
// spent_amount keeps track of how much was spent in the account in betting,
// house, staking, etc.
string spent_amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];

// withdrawn_amount keeps track of how much was withdrawn in the account after locked coins become free.
// withdrawn_amount keeps track of how much was withdrawn in the account after
// locked coins become free.
string withdrawm_amount = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];

// lost_amount keeps track of the amounts that were lost due to betting losses, slashing etc.
// lost_amount keeps track of the amounts that were lost due to betting
// losses, slashing etc.
string lost_amount = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
}

// LockedBalance defines a balance which is locked.
message LockedBalance {
google.protobuf.Timestamp unlock_time = 1 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
uint64 unlock_ts = 1 [
(gogoproto.customname) = "UnlockTS",
(gogoproto.jsontag) = "unlock_ts",
json_name = "unlock_ts"
];
string amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
}
5 changes: 3 additions & 2 deletions proto/sge/subaccount/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sgenetwork.sge.subaccount;

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

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

Expand All @@ -18,7 +18,8 @@ message GenesisState {
repeated GenesisSubaccount subaccounts = 3 [ (gogoproto.nullable) = false ];
}

// GenesisSubaccount defines the genesis subaccount containing owner, address and balance information.
// GenesisSubaccount defines the genesis subaccount containing owner, address
// and balance information.
message GenesisSubaccount {
// address is the address of the subaccount.
string address = 1;
Expand Down
3 changes: 0 additions & 3 deletions proto/sge/subaccount/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ option go_package = "github.com/sge-network/sge/x/subaccount/types";
// Params defines the parameters for the module.
message Params {
option (gogoproto.goproto_stringer) = false;

// balance_denom defines the denom of the locked balances.
string locked_balance_denom = 1;
}
17 changes: 6 additions & 11 deletions proto/sge/subaccount/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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/balance.proto";
import "sge/subaccount/params.proto";

// Query defines the gRPC querier service.
Expand All @@ -23,20 +23,15 @@ service Query {
// 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;
}
message QueryParamsResponse { sge.subaccount.Params params = 1; }

// QuerySubaccountRequest is the request type for the Query/Subaccount RPC
message QuerySubaccountRequest {
string subaccount_owner = 1;
}
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 ];
sge.subaccount.Balance balance = 2 [ (gogoproto.nullable) = false ];
repeated sge.subaccount.LockedBalance locked_balance = 3
[ (gogoproto.nullable) = false ];
}


85 changes: 42 additions & 43 deletions proto/sge/subaccount/tx.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
syntax = "proto3";
package sgenetwork.sge.subaccount;

import "sge/subaccount/subaccount.proto";
import "sge/subaccount/balance.proto";
import "sge/bet/tx.proto";
import "sge/house/tx.proto";
import "gogoproto/gogo.proto";
Expand All @@ -10,90 +10,89 @@ option go_package = "github.com/sge-network/sge/x/subaccount/types";

// Msg defines the Msg service.
service Msg {
// CreateSubAccount defines a method for creating a subaccount.
rpc CreateSubAccount(MsgCreateSubAccount) returns (MsgCreateSubAccountResponse);
// Create defines a method for creating a subaccount.
rpc Create(MsgCreate) returns (MsgCreateResponse);

// TopUp defines a method for topping up a subaccount.
rpc TopUp(MsgTopUp) returns (MsgTopUpResponse);

// WithdrawUnlockedBalances defines a method for withdrawing unlocked balances.
rpc WithdrawUnlockedBalances(MsgWithdrawUnlockedBalances) returns (MsgWithdrawUnlockedBalancesResponse);
// WithdrawUnlockedBalances defines a method for withdrawing unlocked
// balances.
rpc WithdrawUnlockedBalances(MsgWithdrawUnlockedBalances)
returns (MsgWithdrawUnlockedBalancesResponse);

// PlaceBet defines a method for placing a bet using a subaccount.
rpc Wager(MsgWager) returns (MsgWagerResponse);

// HouseDeposit defines a method for depositing funds to provide liquidity to a market.
// HouseDeposit defines a method for depositing funds to provide liquidity to
// a market.
rpc HouseDeposit(MsgHouseDeposit) returns (MsgHouseDepositResponse);

// HouseWithdraw defines a method for withdrawing funds from a market.
rpc HouseWithdraw(MsgHouseWithdraw) returns (MsgHouseWithdrawResponse);
}

// MsgCreateSubAccount defines the Msg/CreateSubAccount request type.
message MsgCreateSubAccount {
// sender is the msg signer.
string sender = 1;
// MsgCreate defines the Msg/Create request type.
message MsgCreate {
// creator is the msg signer.
string creator = 1;

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

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

// MsgCreateAccountResponse defines the Msg/CreateAccount response type.
message MsgCreateSubAccountResponse {}
message MsgCreateResponse {}

// MsgTopUp defines the Msg/TopUp request type.
message MsgTopUp {
// sender is the msg signer.
string sender = 1;
// creator is the msg signer.
string creator = 1;

// sub_account is the subaccount address.
string sub_account = 2;

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

// MsgTopUpResponse defines the Msg/TopUp response type.
message MsgTopUpResponse {}

// MsgWithdrawUnlockedBalances defines the Msg/WithdrawUnlockedBalances request type.
// MsgWithdrawUnlockedBalances defines the Msg/WithdrawUnlockedBalances request
// type.
message MsgWithdrawUnlockedBalances {
// sender is the subaccount owner.
string sender = 1;
// creator is the subaccount owner.
string creator = 1;
}

// MsgWithdrawUnlockedBalancesResponse defines the Msg/WithdrawUnlockedBalances response type.
// MsgWithdrawUnlockedBalancesResponse defines the Msg/WithdrawUnlockedBalances
// response type.
message MsgWithdrawUnlockedBalancesResponse {}

// MsgPlaceBet wraps the MsgPlaceBet message. We need it in order not to have double interface registration conflicts.
message MsgWager {
sgenetwork.sge.bet.MsgWager msg = 1;
}
// MsgPlaceBet wraps the MsgPlaceBet message. We need it in order not to have
// double interface registration conflicts.
message MsgWager { sgenetwork.sge.bet.MsgWager msg = 1; }

// MsgBetResponse wraps the MsgPlaceBetResponse message. We need it in order not to have double interface registration conflicts.
message MsgWagerResponse {
sgenetwork.sge.bet.MsgWagerResponse response = 1;
}
// MsgBetResponse wraps the MsgPlaceBetResponse message. We need it in order not
// to have double interface registration conflicts.
message MsgWagerResponse { sgenetwork.sge.bet.MsgWagerResponse response = 1; }

// MsgHouseDeposit wraps the MsgHouseDeposit message. We need it in order not to have double interface registration conflicts.
message MsgHouseDeposit {
sge.house.MsgDeposit msg = 1;
}
// MsgHouseDeposit wraps the MsgHouseDeposit message. We need it in order not to
// have double interface registration conflicts.
message MsgHouseDeposit { sge.house.MsgDeposit msg = 1; }

// MsgHouseDepositResponse wraps the MsgHouseDepositResponse message. We need it in order not to have double interface registration conflicts.
message MsgHouseDepositResponse {
sge.house.MsgDepositResponse response = 1;
}
// MsgHouseDepositResponse wraps the MsgHouseDepositResponse message. We need it
// in order not to have double interface registration conflicts.
message MsgHouseDepositResponse { sge.house.MsgDepositResponse response = 1; }

// MsgHouseWithdraw wraps the MsgHouseWithdraw message. We need it in order not to have double interface registration conflicts.
message MsgHouseWithdraw {
sge.house.MsgWithdraw msg = 1;
}
// MsgHouseWithdraw wraps the MsgHouseWithdraw message. We need it in order not
// to have double interface registration conflicts.
message MsgHouseWithdraw { sge.house.MsgWithdraw msg = 1; }

// MsgHouseWithdrawResponse wraps the MsgHouseWithdrawResponse message. We need it in order not to have double interface registration conflicts.
message MsgHouseWithdrawResponse {
sge.house.MsgWithdrawResponse response = 1;
}
// MsgHouseWithdrawResponse wraps the MsgHouseWithdrawResponse message. We need
// it in order not to have double interface registration conflicts.
message MsgHouseWithdrawResponse { sge.house.MsgWithdrawResponse response = 1; }
2 changes: 1 addition & 1 deletion scripts/protocgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -eo pipefail

# get protoc executions
go get github.com/cosmos/gogoproto@v1.4.10 2>/dev/null
go get github.com/cosmos/gogoproto@v1.4.11 2>/dev/null

# get cosmos sdk from github
go get github.com/cosmos/cosmos-sdk@v0.46.14 2>/dev/null
Expand Down
22 changes: 22 additions & 0 deletions x/bet/keeper/bet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/sge-network/sge/utils"
"github.com/sge-network/sge/x/bet/types"
)
Expand Down Expand Up @@ -168,3 +169,24 @@ func (k Keeper) GetSettledBets(ctx sdk.Context) (list []types.SettledBet, err er

return
}

func (k Keeper) PrepareBetObject(ctx sdk.Context, creator string, props *types.WagerProps) (*types.Bet, error) {
// Check if the value already exists
_, isFound := k.GetBetID(ctx, props.UID)
if isFound {
return nil, sdkerrors.Wrapf(types.ErrDuplicateUID, "%s", props.UID)
}

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

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

bet := types.NewBet(creator, props, payload.OddsType, payload.SelectedOdds)
return bet, nil
}
Loading