Skip to content

Commit

Permalink
Add unassign msg handler
Browse files Browse the repository at this point in the history
  • Loading branch information
trinitys7 committed Jan 16, 2025
1 parent fc22983 commit 230b6fd
Show file tree
Hide file tree
Showing 6 changed files with 658 additions and 67 deletions.
13 changes: 13 additions & 0 deletions proto/realionetwork/asset/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ service Msg {
// this line is used by starport scaffolding # proto/tx/rpc
rpc CreateToken(MsgCreateToken) returns (MsgCreateTokenResponse);
rpc AssignRoles(MsgAssignRoles) returns (MsgAssignRolesResponse);
rpc UnassignRoles(MsgUnassignRoles) returns (MsgUnassignRolesResponse);
}

message MsgCreateToken {
Expand Down Expand Up @@ -53,6 +54,18 @@ message MsgAssignRoles {

message MsgAssignRolesResponse {}

message MsgUnassignRoles {
option (cosmos.msg.v1.signer) = "issuer";
// issuer is the address that defines the token
string issuer = 1;

string token_id = 2;
repeated string managers = 3;
repeated string distributors = 4;
}

message MsgUnassignRolesResponse {}

message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";
// authority is the address that controls the module (defaults to x/gov unless
Expand Down
25 changes: 13 additions & 12 deletions x/asset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Keeper struct {
Token collections.Map[string, types.Token]
TokenManagement collections.Map[string, types.TokenManagement]
TokenDistribution collections.Map[string, types.TokenDistribution]
WhitelistAddresses collections.Map[sdk.AccAddress, bool]
WhitelistAddresses collections.Map[string, bool]
}

// NewKeeper returns a new Keeper object with a given codec, dedicated
Expand All @@ -44,15 +44,16 @@ func NewKeeper(
) *Keeper {
sb := collections.NewSchemaBuilder(storeService)
k := Keeper{
cdc: cdc,
storeService: storeService,
authority: authority,
bk: bk,
ak: ak,
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
Token: collections.NewMap(sb, types.TokenKeyPrefix, "token", collections.StringKey, codec.CollValue[types.Token](cdc)),
TokenManagement: collections.NewMap(sb, types.TokenKeyPrefix, "token_management", collections.StringKey, codec.CollValue[types.TokenManagement](cdc)),
TokenDistribution: collections.NewMap(sb, types.TokenKeyPrefix, "token_distribution", collections.StringKey, codec.CollValue[types.TokenDistribution](cdc)),
cdc: cdc,
storeService: storeService,
authority: authority,
bk: bk,
ak: ak,
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
Token: collections.NewMap(sb, types.TokenKey, "token", collections.StringKey, codec.CollValue[types.Token](cdc)),
TokenManagement: collections.NewMap(sb, types.TokenManagementKey, "token_management", collections.StringKey, codec.CollValue[types.TokenManagement](cdc)),
TokenDistribution: collections.NewMap(sb, types.TokenDistributionKey, "token_distribution", collections.StringKey, codec.CollValue[types.TokenDistribution](cdc)),
WhitelistAddresses: collections.NewMap(sb, types.WhitelistAddressesKey, "whitelist_addresses", collections.StringKey, collections.BoolValue),
}

schema, err := sb.Build()
Expand All @@ -68,8 +69,8 @@ func (k Keeper) Logger(ctx context.Context) log.Logger {
return sdkCtx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

func (k Keeper) GetWhitelistAddress(ctx context.Context, accAddr sdk.AccAddress) bool {
found, err := k.WhitelistAddresses.Get(ctx, accAddr)
func (k Keeper) GetWhitelistAddress(ctx context.Context, address string) bool {
found, err := k.WhitelistAddresses.Get(ctx, address)
if err != nil {
return false
}
Expand Down
72 changes: 63 additions & 9 deletions x/asset/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"fmt"
"slices"
"strings"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -34,12 +35,7 @@ func (ms msgServer) CreateToken(ctx context.Context, msg *types.MsgCreateToken)
return nil, err
}

issuerAddr, err := ms.ak.AddressCodec().StringToBytes(msg.Issuer)
if err != nil {
return nil, errorsmod.Wrapf(types.ErrAccAddress, err.Error())
}

if !ms.GetWhitelistAddress(ctx, issuerAddr) {
if !ms.GetWhitelistAddress(ctx, msg.Issuer) {
return nil, errorsmod.Wrapf(types.ErrUnauthorize, "issuer not in whitelisted addresses")
}

Expand All @@ -55,7 +51,7 @@ func (ms msgServer) CreateToken(ctx context.Context, msg *types.MsgCreateToken)
// TODO: create evm precompile here

token := types.NewToken(tokenId, msg.Name, msg.Decimal, msg.Description, msg.Symbol, msg.Issuer)
err = ms.Token.Set(ctx, tokenId, token)
err := ms.Token.Set(ctx, tokenId, token)
if err != nil {
return nil, errorsmod.Wrap(types.ErrTokenSet, err.Error())
}
Expand Down Expand Up @@ -110,7 +106,10 @@ func (ms msgServer) AssignRoles(ctx context.Context, msg *types.MsgAssignRoles)
if err != nil {
return nil, errorsmod.Wrapf(types.ErrTokenManagementGet, err.Error())
}
tokenManagement.Managers = msg.Managers
newManagers := append(tokenManagement.Managers, msg.Managers...)
slices.Sort(newManagers)
tokenManagement.Managers = slices.Compact(newManagers)

err = ms.TokenManagement.Set(ctx, msg.TokenId, tokenManagement)
if err != nil {
return nil, errorsmod.Wrap(types.ErrTokenManagementSet, err.Error())
Expand All @@ -120,7 +119,10 @@ func (ms msgServer) AssignRoles(ctx context.Context, msg *types.MsgAssignRoles)
if err != nil {
return nil, errorsmod.Wrapf(types.ErrTokenDistributionGet, err.Error())
}
tokenDistribution.Distributors = msg.Distributors
newDistributors := append(tokenDistribution.Distributors, msg.Distributors...)
slices.Sort(newDistributors)
tokenDistribution.Distributors = slices.Compact(newDistributors)

err = ms.TokenDistribution.Set(ctx, msg.TokenId, tokenDistribution)
if err != nil {
return nil, errorsmod.Wrap(types.ErrTokenDistributionSet, err.Error())
Expand All @@ -136,6 +138,58 @@ func (ms msgServer) AssignRoles(ctx context.Context, msg *types.MsgAssignRoles)
return &types.MsgAssignRolesResponse{}, nil
}

func (ms msgServer) UnassignRoles(ctx context.Context, msg *types.MsgUnassignRoles) (*types.MsgUnassignRolesResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

if err := msg.ValidateBasic(); err != nil {
return nil, err
}

token, err := ms.Token.Get(ctx, msg.TokenId)
if err != nil {
return nil, errorsmod.Wrapf(types.ErrTokenGet, err.Error())
}

if msg.Issuer != token.Issuer {
return nil, errorsmod.Wrapf(types.ErrUnauthorize, "issuer not the creator of the token")
}

tokenManagement, err := ms.TokenManagement.Get(ctx, msg.TokenId)
if err != nil {
return nil, errorsmod.Wrapf(types.ErrTokenManagementGet, err.Error())
}
tokenManagement.Managers = slices.DeleteFunc(tokenManagement.Managers, func(manager string) bool {
return slices.Contains(msg.Managers, manager)
})

err = ms.TokenManagement.Set(ctx, msg.TokenId, tokenManagement)
if err != nil {
return nil, errorsmod.Wrap(types.ErrTokenManagementSet, err.Error())
}

tokenDistribution, err := ms.TokenDistribution.Get(ctx, msg.TokenId)
if err != nil {
return nil, errorsmod.Wrapf(types.ErrTokenDistributionGet, err.Error())
}

tokenDistribution.Distributors = slices.DeleteFunc(tokenDistribution.Distributors, func(distributor string) bool {
return slices.Contains(msg.Distributors, distributor)
})
err = ms.TokenDistribution.Set(ctx, msg.TokenId, tokenDistribution)
if err != nil {
return nil, errorsmod.Wrap(types.ErrTokenDistributionSet, err.Error())
}

sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeTokenAuthorizeUpdated,
sdk.NewAttribute(types.AttributeKeyTokenId, msg.TokenId),
),
)

return &types.MsgUnassignRolesResponse{}, nil
}

// UpdateParams updates the params.
func (ms msgServer) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if ms.authority != msg.Authority {
Expand Down
8 changes: 5 additions & 3 deletions x/asset/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
)

var (
ParamsKey = collections.NewPrefix(0)
TokenKeyPrefix = collections.NewPrefix(1)
IssuerPrefixKey = "issuer"
ParamsKey = collections.NewPrefix(0)
TokenKey = collections.NewPrefix(1)
TokenManagementKey = collections.NewPrefix(2)
TokenDistributionKey = collections.NewPrefix(3)
WhitelistAddressesKey = collections.NewPrefix(4)
)

const (
Expand Down
32 changes: 32 additions & 0 deletions x/asset/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,35 @@ func (msg *MsgAssignRoles) ValidateBasic() error {

return ValidateTokenId(msg.TokenId)
}

func NewMsgUnassignRoles(issuer string, tokenId string, managers []string, distributors []string) *MsgUnassignRoles {
return &MsgUnassignRoles{
Issuer: issuer,
TokenId: tokenId,
Managers: managers,
Distributors: distributors,
}
}

func (msg *MsgUnassignRoles) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Issuer)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid issuer address (%s)", err)
}

for _, manager := range msg.Managers {
_, err := sdk.AccAddressFromBech32(manager)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid manager address (%s): %s", manager, err)
}
}

for _, distributor := range msg.Distributors {
_, err := sdk.AccAddressFromBech32(distributor)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid distributor address (%s): %s", distributor, err)
}
}

return ValidateTokenId(msg.TokenId)
}
Loading

0 comments on commit 230b6fd

Please sign in to comment.