github.com/status-im/status-go@v1.1.0/protocol/communities/community_token_permission.go (about)

     1  package communities
     2  
     3  import (
     4  	"reflect"
     5  	"slices"
     6  
     7  	"github.com/status-im/status-go/protocol/protobuf"
     8  )
     9  
    10  type TokenPermissionState uint8
    11  
    12  const (
    13  	TokenPermissionApproved TokenPermissionState = iota
    14  	TokenPermissionAdditionPending
    15  	TokenPermissionUpdatePending
    16  	TokenPermissionRemovalPending
    17  )
    18  
    19  type CommunityTokenPermission struct {
    20  	*protobuf.CommunityTokenPermission
    21  	State TokenPermissionState `json:"state,omitempty"`
    22  }
    23  
    24  func NewCommunityTokenPermission(base *protobuf.CommunityTokenPermission) *CommunityTokenPermission {
    25  	return &CommunityTokenPermission{
    26  		CommunityTokenPermission: base,
    27  		State:                    TokenPermissionApproved,
    28  	}
    29  }
    30  
    31  func (p *CommunityTokenPermission) Equals(other *CommunityTokenPermission) bool {
    32  	if p.Id != other.Id ||
    33  		p.Type != other.Type ||
    34  		len(p.TokenCriteria) != len(other.TokenCriteria) ||
    35  		len(p.ChatIds) != len(other.ChatIds) ||
    36  		p.IsPrivate != other.IsPrivate ||
    37  		p.State != other.State {
    38  		return false
    39  	}
    40  
    41  	for i := range p.TokenCriteria {
    42  		if !compareTokenCriteria(p.TokenCriteria[i], other.TokenCriteria[i]) {
    43  			return false
    44  		}
    45  	}
    46  
    47  	return reflect.DeepEqual(p.ChatIds, other.ChatIds)
    48  }
    49  
    50  func (p *CommunityTokenPermission) HasChat(chatId string) bool {
    51  	return slices.Contains(p.ChatIds, chatId)
    52  }
    53  
    54  func (p *CommunityTokenPermission) ChatIdsAsMap() map[string]struct{} {
    55  	chats := map[string]struct{}{}
    56  	for _, id := range p.GetChatIds() {
    57  		chats[id] = struct{}{}
    58  	}
    59  	return chats
    60  }
    61  
    62  func compareTokenCriteria(a, b *protobuf.TokenCriteria) bool {
    63  	if a == nil && b == nil {
    64  		return true
    65  	}
    66  	if a == nil || b == nil {
    67  		return false
    68  	}
    69  
    70  	return a.Type == b.Type &&
    71  		a.Symbol == b.Symbol &&
    72  		a.Name == b.Name &&
    73  		a.Amount == b.Amount &&
    74  		a.EnsPattern == b.EnsPattern &&
    75  		a.Decimals == b.Decimals &&
    76  		reflect.DeepEqual(a.ContractAddresses, b.ContractAddresses) &&
    77  		reflect.DeepEqual(a.TokenIds, b.TokenIds)
    78  }