github.com/s7techlab/cckit@v0.10.5/examples/token/service/config/config_state.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"google.golang.org/protobuf/types/known/emptypb"
     7  
     8  	"github.com/s7techlab/cckit/router"
     9  )
    10  
    11  type StateService struct {
    12  }
    13  
    14  func NewStateService() *StateService {
    15  	return &StateService{}
    16  }
    17  
    18  func (s *StateService) SetConfig(ctx router.Context, config *Config) (*Config, error) {
    19  	err := State(ctx).Put(config)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	return config, nil
    24  }
    25  
    26  func (s *StateService) GetConfig(ctx router.Context, _ *emptypb.Empty) (*Config, error) {
    27  	config, err := State(ctx).Get(&Config{}, &Config{})
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	return config.(*Config), nil
    32  }
    33  
    34  // GetToken naming for token is[{TokenType}, {GroupIdPart1}, {GroupIdPart2}]
    35  func (s *StateService) GetToken(ctx router.Context, id *TokenId) (*Token, error) {
    36  	var (
    37  		tokenTypeName  string
    38  		tokenGroupName []string
    39  		tokenType      *TokenType
    40  		tokenGroup     *TokenGroup
    41  		err            error
    42  	)
    43  	if len(id.Token) > 0 {
    44  		tokenTypeName = id.Token[0]
    45  	}
    46  
    47  	if len(id.Token) > 1 {
    48  		tokenGroupName = id.Token[1:]
    49  	}
    50  
    51  	tokenType, err = s.GetTokenType(ctx, &TokenTypeId{Name: tokenTypeName})
    52  	if err != nil {
    53  		return nil, fmt.Errorf(`token type: %w`, err)
    54  	}
    55  
    56  	if len(tokenGroupName) > 0 {
    57  		tokenGroup, err = s.GetTokenGroup(ctx, &TokenGroupId{Name: tokenGroupName})
    58  		if err != nil {
    59  			return nil, fmt.Errorf(`token type: %w`, err)
    60  		}
    61  	}
    62  
    63  	return &Token{
    64  		Token: id.Token,
    65  		Type:  tokenType,
    66  		Group: tokenGroup,
    67  	}, nil
    68  }
    69  
    70  func (s *StateService) GetDefaultToken(ctx router.Context, _ *emptypb.Empty) (*Token, error) {
    71  	config, err := s.GetConfig(ctx, nil)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	if len(config.DefaultToken) > 0 {
    77  		return s.GetToken(ctx, &TokenId{Token: config.DefaultToken})
    78  	}
    79  
    80  	return nil, nil
    81  }
    82  
    83  func (s *StateService) CreateTokenType(ctx router.Context, req *CreateTokenTypeRequest) (*TokenType, error) {
    84  	if err := router.ValidateRequest(req); err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	tokenType := &TokenType{
    89  		Name:        req.Name,
    90  		Symbol:      req.Symbol,
    91  		Decimals:    req.Decimals,
    92  		TotalSupply: req.TotalSupply,
    93  		GroupType:   req.GroupType,
    94  	}
    95  
    96  	for _, m := range req.Meta {
    97  		tokenType.Meta = append(tokenType.Meta, &TokenMeta{
    98  			Key:   m.Key,
    99  			Value: m.Value,
   100  		})
   101  	}
   102  	if err := State(ctx).Insert(tokenType); err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	if err := Event(ctx).Set(&TokenTypeCreated{
   107  		Name:   req.Name,
   108  		Symbol: req.Symbol,
   109  	}); err != nil {
   110  		return nil, err
   111  	}
   112  	return tokenType, nil
   113  }
   114  
   115  func (s *StateService) GetTokenType(ctx router.Context, id *TokenTypeId) (*TokenType, error) {
   116  	tokenType, err := State(ctx).Get(id, &TokenType{})
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	return tokenType.(*TokenType), nil
   121  }
   122  
   123  func (s *StateService) ListTokenTypes(ctx router.Context, _ *emptypb.Empty) (*TokenTypes, error) {
   124  	tokenTypes, err := State(ctx).List(&TokenType{})
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  	return tokenTypes.(*TokenTypes), nil
   129  }
   130  
   131  func (s *StateService) UpdateTokenType(ctx router.Context, request *UpdateTokenTypeRequest) (*TokenType, error) {
   132  	//TODO implement me
   133  	panic("implement me")
   134  }
   135  
   136  func (s *StateService) DeleteTokenType(ctx router.Context, id *TokenTypeId) (*TokenType, error) {
   137  	//TODO implement me
   138  	panic("implement me")
   139  }
   140  
   141  func (s *StateService) GetTokenGroups(ctx router.Context, id *TokenTypeId) (*TokenGroups, error) {
   142  	tokenGroups, err := State(ctx).ListWith(&TokenGroup{}, []string{id.Name})
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	return tokenGroups.(*TokenGroups), nil
   147  }
   148  
   149  func (s *StateService) CreateTokenGroup(ctx router.Context, req *CreateTokenGroupRequest) (*TokenGroup, error) {
   150  	if err := router.ValidateRequest(req); err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	_, err := s.GetTokenType(ctx, &TokenTypeId{Name: req.TokenType})
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  
   159  	tokenGroup := &TokenGroup{
   160  		Name:        req.Name,
   161  		TokenType:   req.TokenType,
   162  		TotalSupply: 0,
   163  	}
   164  
   165  	for _, m := range req.Meta {
   166  		tokenGroup.Meta = append(tokenGroup.Meta, &TokenMeta{
   167  			Key:   m.Key,
   168  			Value: m.Value,
   169  		})
   170  	}
   171  	if err := State(ctx).Insert(tokenGroup); err != nil {
   172  		return nil, err
   173  	}
   174  
   175  	if err := Event(ctx).Set(&TokenGroupCreated{
   176  		Name:      req.Name,
   177  		TokenType: req.TokenType,
   178  	}); err != nil {
   179  		return nil, err
   180  	}
   181  	return tokenGroup, nil
   182  }
   183  
   184  func (s *StateService) GetTokenGroup(ctx router.Context, id *TokenGroupId) (*TokenGroup, error) {
   185  	tokenGroup, err := State(ctx).Get(id, &TokenGroup{})
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  	return tokenGroup.(*TokenGroup), nil
   190  }
   191  
   192  func (s *StateService) DeleteTokenGroup(ctx router.Context, id *TokenGroupId) (*Token, error) {
   193  	//TODO implement me
   194  	panic("implement me")
   195  }