status-go/protocol/communities/community_token_permission.go

79 lines
1.8 KiB
Go

package communities
import (
"reflect"
"slices"
"github.com/status-im/status-go/protocol/protobuf"
)
type TokenPermissionState uint8
const (
TokenPermissionApproved TokenPermissionState = iota
TokenPermissionAdditionPending
TokenPermissionUpdatePending
TokenPermissionRemovalPending
)
type CommunityTokenPermission struct {
*protobuf.CommunityTokenPermission
State TokenPermissionState `json:"state,omitempty"`
}
func NewCommunityTokenPermission(base *protobuf.CommunityTokenPermission) *CommunityTokenPermission {
return &CommunityTokenPermission{
CommunityTokenPermission: base,
State: TokenPermissionApproved,
}
}
func (p *CommunityTokenPermission) Equals(other *CommunityTokenPermission) bool {
if p.Id != other.Id ||
p.Type != other.Type ||
len(p.TokenCriteria) != len(other.TokenCriteria) ||
len(p.ChatIds) != len(other.ChatIds) ||
p.IsPrivate != other.IsPrivate ||
p.State != other.State {
return false
}
for i := range p.TokenCriteria {
if !compareTokenCriteria(p.TokenCriteria[i], other.TokenCriteria[i]) {
return false
}
}
return reflect.DeepEqual(p.ChatIds, other.ChatIds)
}
func (p *CommunityTokenPermission) HasChat(chatId string) bool {
return slices.Contains(p.ChatIds, chatId)
}
func (p *CommunityTokenPermission) ChatIdsAsMap() map[string]struct{} {
chats := map[string]struct{}{}
for _, id := range p.GetChatIds() {
chats[id] = struct{}{}
}
return chats
}
func compareTokenCriteria(a, b *protobuf.TokenCriteria) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return a.Type == b.Type &&
a.Symbol == b.Symbol &&
a.Name == b.Name &&
a.Amount == b.Amount &&
a.EnsPattern == b.EnsPattern &&
a.Decimals == b.Decimals &&
reflect.DeepEqual(a.ContractAddresses, b.ContractAddresses) &&
reflect.DeepEqual(a.TokenIds, b.TokenIds)
}