Skip to content

Commit

Permalink
chore: refactor proposal type (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Dec 20, 2023
1 parent 4c3b95d commit ab5a1e6
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 99 deletions.
8 changes: 4 additions & 4 deletions pkg/fetchers/cosmos/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (rpc *RPC) GetAllV1beta1Proposals() ([]types.Proposal, *types.QueryError) {
offset,
)

var batchProposals types.V1Beta1ProposalsRPCResponse
var batchProposals responses.V1Beta1ProposalsRPCResponse
if errs := rpc.Get(url, &batchProposals); len(errs) > 0 {
return nil, &types.QueryError{
QueryError: nil,
Expand All @@ -65,7 +65,7 @@ func (rpc *RPC) GetAllV1beta1Proposals() ([]types.Proposal, *types.QueryError) {
}
}

parsedProposals := utils.Map(batchProposals.Proposals, func(p types.V1beta1Proposal) types.Proposal {
parsedProposals := utils.Map(batchProposals.Proposals, func(p responses.V1beta1Proposal) types.Proposal {
return p.ToProposal()
})
proposals = append(proposals, parsedProposals...)
Expand All @@ -91,7 +91,7 @@ func (rpc *RPC) GetAllV1Proposals() ([]types.Proposal, *types.QueryError) {
offset,
)

var batchProposals types.V1ProposalsRPCResponse
var batchProposals responses.V1ProposalsRPCResponse
if errs := rpc.Get(url, &batchProposals); len(errs) > 0 {
return nil, &types.QueryError{
QueryError: nil,
Expand All @@ -105,7 +105,7 @@ func (rpc *RPC) GetAllV1Proposals() ([]types.Proposal, *types.QueryError) {
}
}

parsedProposals := utils.Map(batchProposals.Proposals, func(p types.V1Proposal) types.Proposal {
parsedProposals := utils.Map(batchProposals.Proposals, func(p responses.V1Proposal) types.Proposal {
return p.ToProposal()
})
proposals = append(proposals, parsedProposals...)
Expand Down
61 changes: 61 additions & 0 deletions pkg/fetchers/cosmos/responses/proposal_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package responses

import (
"main/pkg/types"
"main/pkg/utils"
"strings"
"time"
)

// cosmos/gov/v1beta1/proposals?pagination.limit=1000&pagination.offset=0

type V1ProposalMessage struct {
Content ProposalContent `json:"content"`
}

type V1Proposal struct {
ProposalID string `json:"id"`
Status string `json:"status"`
VotingEndTime time.Time `json:"voting_end_time"`
Messages []V1ProposalMessage `json:"messages"`

Title string `json:"title"`
Summary string `json:"summary"`
}

func (p V1Proposal) ToProposal() types.Proposal {
// Some chains (namely, Quicksilver) do not have title and description fields,
// instead they have content.title and content.description per each message.
// Others (namely, Kujira) have title and summary text.
// This should work for all of them.
title := p.Title
if title == "" {
titles := utils.Map(p.Messages, func(m V1ProposalMessage) string {
return m.Content.Title
})

title = strings.Join(titles, ", ")
}

description := p.Summary
if description == "" {
descriptions := utils.Map(p.Messages, func(m V1ProposalMessage) string {
return m.Content.Description
})

description = strings.Join(descriptions, ", ")
}

return types.Proposal{
ID: p.ProposalID,
Title: title,
Description: description,
EndTime: p.VotingEndTime,
}
}

type V1ProposalsRPCResponse struct {
Code int64 `json:"code"`
Message string `json:"message"`
Proposals []V1Proposal `json:"proposals"`
}
35 changes: 35 additions & 0 deletions pkg/fetchers/cosmos/responses/proposal_v1beta1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package responses

import (
"main/pkg/types"
"time"
)

// cosmos/gov/v1beta1/proposals?pagination.limit=1000&pagination.offset=0

type V1beta1Proposal struct {
ProposalID string `json:"proposal_id"`
Status string `json:"status"`
Content *ProposalContent `json:"content"`
VotingEndTime time.Time `json:"voting_end_time"`
}

type ProposalContent struct {
Title string `json:"title"`
Description string `json:"description"`
}

type V1Beta1ProposalsRPCResponse struct {
Code int64 `json:"code"`
Message string `json:"message"`
Proposals []V1beta1Proposal `json:"proposals"`
}

func (p V1beta1Proposal) ToProposal() types.Proposal {
return types.Proposal{
ID: p.ProposalID,
Title: p.Content.Title,
Description: p.Content.Description,
EndTime: p.VotingEndTime,
}
}
17 changes: 17 additions & 0 deletions pkg/types/proposal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package types

import (
"main/pkg/utils"
"time"
)

type Proposal struct {
ID string
Title string
Description string
EndTime time.Time
}

func (p Proposal) GetTimeLeft() string {
return utils.FormatDuration(time.Until(p.EndTime).Round(time.Second))
}
82 changes: 0 additions & 82 deletions pkg/types/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,88 +10,6 @@ import (
"cosmossdk.io/math"
)

// cosmos/gov/v1beta1/proposals?pagination.limit=1000&pagination.offset=0

type V1beta1Proposal struct {
ProposalID string `json:"proposal_id"`
Status string `json:"status"`
Content *ProposalContent `json:"content"`
VotingEndTime time.Time `json:"voting_end_time"`
}

func (p V1beta1Proposal) ToProposal() Proposal {
return Proposal{
ID: p.ProposalID,
Title: p.Content.Title,
Description: p.Content.Description,
EndTime: p.VotingEndTime,
}
}

type ProposalContent struct {
Title string `json:"title"`
Description string `json:"description"`
}

type V1Beta1ProposalsRPCResponse struct {
Code int64 `json:"code"`
Message string `json:"message"`
Proposals []V1beta1Proposal `json:"proposals"`
}

// cosmos/gov/v1beta1/proposals?pagination.limit=1000&pagination.offset=0

type V1ProposalMessage struct {
Content ProposalContent `json:"content"`
}

type V1Proposal struct {
ProposalID string `json:"id"`
Status string `json:"status"`
VotingEndTime time.Time `json:"voting_end_time"`
Messages []V1ProposalMessage `json:"messages"`

Title string `json:"title"`
Summary string `json:"summary"`
}

func (p V1Proposal) ToProposal() Proposal {
// Some chains (namely, Quicksilver) do not have title and description fields,
// instead they have content.title and content.description per each message.
// Others (namely, Kujira) have title and summary text.
// This should work for all of them.
title := p.Title
if title == "" {
titles := utils.Map(p.Messages, func(m V1ProposalMessage) string {
return m.Content.Title
})

title = strings.Join(titles, ", ")
}

description := p.Summary
if description == "" {
descriptions := utils.Map(p.Messages, func(m V1ProposalMessage) string {
return m.Content.Description
})

description = strings.Join(descriptions, ", ")
}

return Proposal{
ID: p.ProposalID,
Title: title,
Description: description,
EndTime: p.VotingEndTime,
}
}

type V1ProposalsRPCResponse struct {
Code int64 `json:"code"`
Message string `json:"message"`
Proposals []V1Proposal `json:"proposals"`
}

type TallyRPCResponse struct {
Code int64 `json:"code"`
Message string `json:"message"`
Expand Down
13 changes: 0 additions & 13 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package types

import (
"fmt"
"main/pkg/utils"
"time"

"cosmossdk.io/math"
)
Expand All @@ -21,17 +19,6 @@ func (l Link) Serialize() string {
return fmt.Sprintf("<a href='%s'>%s</a>", l.Href, l.Name)
}

type Proposal struct {
ID string
Title string
Description string
EndTime time.Time
}

func (p Proposal) GetTimeLeft() string {
return utils.FormatDuration(time.Until(p.EndTime).Round(time.Second))
}

type TallyInfo struct {
Proposal Proposal
Tally Tally
Expand Down

0 comments on commit ab5a1e6

Please sign in to comment.