Skip to content

Commit

Permalink
feat: add governance type (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankLi123 authored Sep 3, 2024
1 parent 86a87f6 commit 49989a1
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 20 deletions.
49 changes: 49 additions & 0 deletions schema/metadata/governance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package metadata

import (
"github.com/rss3-network/protocol-go/schema"
"github.com/rss3-network/protocol-go/schema/typex"
)

var _ Metadata = (*GovernanceProposal)(nil)

func (t GovernanceProposal) Type() schema.Type {
return typex.GovernanceProposal
}

type GovernanceProposal struct {
ID string `json:"id"`
Body string `json:"body"`
StartBlock string `json:"start_block"`
EndBlock string `json:"end_block"`
Options []string `json:"options"`
Link string `json:"link"`
}

var _ Metadata = (*GovernanceVote)(nil)

func (t GovernanceVote) Type() schema.Type {
return typex.GovernanceVote
}

//go:generate go run --mod=mod github.com/dmarkham/enumer --values --type=GovernanceVoteAction --transform=snake --trimprefix=ActionGovernanceVote --output governance_vote.go --json --sql
type GovernanceVoteAction uint64

var _ Metadata = (*GovernanceVoteAction)(nil)

type GovernanceVote struct {
Action GovernanceVoteAction `json:"action"`
Count uint64 `json:"count"`
Reason string `json:"reason"`
Proposal GovernanceProposal `json:"token"`
}

func (r GovernanceVoteAction) Type() schema.Type {
return typex.GovernanceVote
}

const (
ActionGovernanceFor GovernanceVoteAction = iota + 1
ActionGovernanceAgainst
ActionGovernanceAbstain
)
21 changes: 21 additions & 0 deletions schema/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func Unmarshal(metadataType schema.Type, data json.RawMessage) (Metadata, error)
return unmarshalCollectibleMetadata(metadataType, data)
case tag.Exchange:
return unmarshalExchangeMetadata(metadataType, data)
case tag.Governance:
return unmarshalGovernanceMetadata(metadataType, data)
case tag.Metaverse:
return unmarshalMetaverseMetadata(metadataType, data)
case tag.RSS:
Expand Down Expand Up @@ -53,6 +55,25 @@ func unmarshalCollectibleMetadata(metadataType schema.Type, data json.RawMessage
return result, nil
}

func unmarshalGovernanceMetadata(metadataType schema.Type, data json.RawMessage) (Metadata, error) {
var result Metadata

switch metadataType {
case typex.GovernanceProposal:
result = new(GovernanceProposal)
case typex.GovernanceVote:
result = new(GovernanceVote)
default:
return nil, fmt.Errorf("invalid metadata type: %s.%s", metadataType.Tag(), metadataType.Name())
}

if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("invalid metadata: %w", err)
}

return result, nil
}

func unmarshalTransactionMetadata(metadataType schema.Type, data json.RawMessage) (Metadata, error) {
var result Metadata

Expand Down
1 change: 1 addition & 0 deletions schema/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
Unknown Tag = iota
Collectible
Exchange
Governance
Metaverse
RSS
Social
Expand Down
44 changes: 24 additions & 20 deletions schema/tag/tag_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions schema/tag_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func ParseTypeFromString(parentTag tag.Tag, typeValue string) (Type, error) {
return typex.CollectibleTypeString(typeValue)
case tag.Exchange:
return typex.ExchangeTypeString(typeValue)
case tag.Governance:
return typex.GovernanceTypeString(typeValue)
case tag.Metaverse:
return typex.MetaverseTypeString(typeValue)
case tag.Social:
Expand Down Expand Up @@ -58,6 +60,8 @@ func GetTypesByTag(tagValue tag.Tag) []Type {
return convertToTypeSlice(typex.CollectibleTypeValues())
case tag.Exchange:
return convertToTypeSlice(typex.ExchangeTypeValues())
case tag.Governance:
return convertToTypeSlice(typex.GovernanceTypeValues())
case tag.Metaverse:
return convertToTypeSlice(typex.MetaverseTypeValues())
case tag.Social:
Expand Down
21 changes: 21 additions & 0 deletions schema/typex/governance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package typex

import "github.com/rss3-network/protocol-go/schema/tag"

//go:generate go run --mod=mod github.com/dmarkham/enumer --values --type=GovernanceType --transform=snake --trimprefix=Governance --output governance_string.go --json --sql
type GovernanceType uint64

//goland:noinspection GoMixedReceiverTypes
func (i GovernanceType) Name() string {
return i.String()
}

//goland:noinspection GoMixedReceiverTypes
func (i GovernanceType) Tag() tag.Tag {
return tag.Governance
}

const (
GovernanceProposal GovernanceType = iota + 1
GovernanceVote
)
132 changes: 132 additions & 0 deletions schema/typex/governance_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 49989a1

Please sign in to comment.