Skip to content

Commit

Permalink
impl some new msg
Browse files Browse the repository at this point in the history
  • Loading branch information
neitdung committed Jul 18, 2024
1 parent 383bd82 commit c92f8a5
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 170 deletions.
11 changes: 8 additions & 3 deletions proto/realionetwork/asset/v1/token.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ option go_package = "github.com/realiotech/realio-network/x/asset/types";

// Token represents an asset in the module
message Token {
option (gogoproto.equal) = false;
option (gogoproto.equal) = false;

string token_id = 1;
string name = 2;
Expand All @@ -18,7 +18,12 @@ message Token {
}

message TokenManagement {
string manager = 1[(cosmos_proto.scalar) = "cosmos.AddressString"];
string manager = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
bool add_new_privilege = 2;
repeated string excluded_privileges = 3;
}
}

message Balance {
string address = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
uint64 amount = 2;
}
5 changes: 2 additions & 3 deletions proto/realionetwork/asset/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ option go_package = "github.com/realiotech/realio-network/x/asset/types";

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/bank/v1beta1/genesis.proto";
import "google/protobuf/any.proto";
import "realionetwork/asset/v1/token.proto";

// Msg defines the Msg service.
service Msg {
Expand Down Expand Up @@ -49,9 +49,8 @@ message MsgUpdateTokenResponse {}
message MsgAllocateToken {
string manager = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string token_id = 2;
repeated cosmos.bank.v1beta1.Balance balances = 3
repeated Balance balances = 3
[ (gogoproto.nullable) = false ];
repeated google.protobuf.Any vesting_balance = 4;
}

message MsgAllocateTokenResponse {}
Expand Down
116 changes: 112 additions & 4 deletions x/asset/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"slices"

Check failure on line 7 in x/asset/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/slices)

Check failure on line 7 in x/asset/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / Analyze

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.2/x64/src/slices)

Check failure on line 7 in x/asset/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/slices)

errorsmod "cosmossdk.io/errors"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -87,21 +88,128 @@ func (k msgServer) UpdateToken(goCtx context.Context, msg *types.MsgUpdateToken)
}

func (k msgServer) AllocateToken(goCtx context.Context, msg *types.MsgAllocateToken) (*types.MsgAllocateTokenResponse, error) {
return nil, nil
ctx := sdk.UnwrapSDKContext(goCtx)
isFound := k.bankKeeper.HasSupply(ctx, msg.TokenId)
if isFound {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "token with denom %s already allocate", msg.TokenId)
}

tm, found := k.GetTokenManagement(ctx, msg.TokenId)
if !found {
return nil, errorsmod.Wrapf(sdkerrors.ErrNotFound, "token with denom %s is not exists", msg.TokenId)
}
if tm.Manager != msg.Manager {
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "sender is not token manager")
}

for _, balance := range msg.Balances {
userAcc, err := sdk.AccAddressFromBech32(balance.Address)
if err != nil {
return nil, err
}

mintCoins := sdk.NewCoins(sdk.NewCoin(msg.TokenId, sdk.NewIntFromUint64(balance.Amount)))
err = k.bankKeeper.MintCoins(ctx, types.ModuleName, mintCoins)
if err != nil {
return nil, err
}

err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, userAcc, mintCoins)
if err != nil {
return nil, err
}
}
return &types.MsgAllocateTokenResponse{}, nil
}

func (k msgServer) AssignPrivilege(goCtx context.Context, msg *types.MsgAssignPrivilege) (*types.MsgAssignPrivilegeResponse, error) {
return nil, nil
ctx := sdk.UnwrapSDKContext(goCtx)
if err := msg.ValidateBasic(); err != nil {
return nil, err
}

tm, found := k.GetTokenManagement(ctx, msg.TokenId)
if !found {
return nil, errorsmod.Wrapf(sdkerrors.ErrNotFound, "token with denom %s is not exists", msg.TokenId)
}
if tm.Manager != msg.Manager {
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "sender is not token manager")
}
if slices.Contains(tm.ExcludedPrivileges, msg.Privilege) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "privilege %s is excluded", msg.Privilege)
}

for _, user := range msg.AssignedTo {
userAcc, err := sdk.AccAddressFromBech32(user)
if err != nil {
return nil, err
}
k.SetTokenPrivilegedAccount(ctx, msg.TokenId, msg.Privilege, userAcc)
}

return &types.MsgAssignPrivilegeResponse{}, nil
}

func (k msgServer) UnassignPrivilege(goCtx context.Context, msg *types.MsgUnassignPrivilege) (*types.MsgUnassignPrivilegeResponse, error) {
return nil, nil
ctx := sdk.UnwrapSDKContext(goCtx)
if err := msg.ValidateBasic(); err != nil {
return nil, err
}

tm, found := k.GetTokenManagement(ctx, msg.TokenId)
if !found {
return nil, errorsmod.Wrapf(sdkerrors.ErrNotFound, "token with denom %s is not exists", msg.TokenId)
}
if tm.Manager != msg.Manager {
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "sender is not token manager")
}

for _, user := range msg.UnassignedFrom {
userAcc, err := sdk.AccAddressFromBech32(user)
if err != nil {
return nil, err
}
k.DeleteTokenPrivilegedAccount(ctx, msg.TokenId, msg.Privilege, userAcc)
}

return &types.MsgUnassignPrivilegeResponse{}, nil
}

func (k msgServer) DisablePrivilege(goCtx context.Context, msg *types.MsgDisablePrivilege) (*types.MsgDisablePrivilegeResponse, error) {
return nil, nil
ctx := sdk.UnwrapSDKContext(goCtx)
if err := msg.ValidateBasic(); err != nil {
return nil, err
}

tm, found := k.GetTokenManagement(ctx, msg.TokenId)
if !found {
return nil, errorsmod.Wrapf(sdkerrors.ErrNotFound, "token with denom %s is not exists", msg.TokenId)
}
if tm.Manager != msg.Manager {
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "sender is not token manager")
}
if slices.Contains(tm.ExcludedPrivileges, msg.DisabledPrivilege) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "privilege %s is already excluded", msg.DisabledPrivilege)
}

tm.ExcludedPrivileges = append(tm.ExcludedPrivileges, msg.DisabledPrivilege)
k.SetTokenManagement(ctx, msg.TokenId, tm)
return &types.MsgDisablePrivilegeResponse{}, nil
}

func (k msgServer) ExecutePrivilege(goCtx context.Context, msg *types.MsgExecutePrivilege) (*types.MsgExecutePrivilegeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := msg.ValidateBasic(); err != nil {
return nil, err
}

tm, found := k.GetTokenManagement(ctx, msg.TokenId)

Check failure on line 206 in x/asset/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / lint

tm declared and not used (typecheck)

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This definition of tm is never used.
if !found {
return nil, errorsmod.Wrapf(sdkerrors.ErrNotFound, "token with denom %s is not exists", msg.TokenId)
}
// if slices.Contains tm.ExcludedPrivileges != msg.Manager {
// return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "sender is not token manager")
// }

return nil, nil
}
18 changes: 17 additions & 1 deletion x/asset/keeper/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,24 @@ func (k Keeper) SetTokenPrivilegedAccount(
address sdk.AccAddress,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrivilegedAccountsKey)
key := append([]byte(tokenId), []byte(privilege)...)
key := append([]byte(tokenId), address.Bytes()...)

var values []string
bz := store.Get(key)
k.cdc.MustUnmarshal(bz, values)

Check failure on line 98 in x/asset/keeper/token.go

View workflow job for this annotation

GitHub Actions / lint

cannot use values (variable of type []string) as "github.com/cosmos/cosmos-sdk/codec".ProtoMarshaler value in argument to k.cdc.MustUnmarshal: []string does not implement "github.com/cosmos/cosmos-sdk/codec".ProtoMarshaler (missing method Marshal) (typecheck)

store.Set(key, address)
}

func (k Keeper) DeleteTokenPrivilegedAccount(
ctx sdk.Context,
tokenId string,
privilege string,
address sdk.AccAddress,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrivilegedAccountsKey)
key := append([]byte(tokenId), []byte(privilege)...)
store.Delete(key)
}

func (k Keeper) GetTokenPrivilegedAccount(
Expand Down
11 changes: 4 additions & 7 deletions x/asset/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

// asset message types
Expand Down Expand Up @@ -123,14 +122,12 @@ func (msg MsgUpdateToken) ValidateBasic() error {
func NewMsgAllocateToken(
manager string,
tokenId string,

Check warning on line 124 in x/asset/types/msg.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: func parameter tokenId should be tokenID (revive)
balances []banktypes.Balance,
vestingBalance []*codectypes.Any,
balances []Balance,
) *MsgAllocateToken {
return &MsgAllocateToken{
Manager: manager,
TokenId: tokenId,
Balances: balances,
VestingBalance: vestingBalance,
Manager: manager,
TokenId: tokenId,
Balances: balances,
}
}

Expand Down
5 changes: 5 additions & 0 deletions x/asset/types/privilege.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

type PrivilegeImplMsg interface {
NeedPrivilege() string
}
Loading

0 comments on commit c92f8a5

Please sign in to comment.