From ab5a1e6d4ef5f58b2975acea29a9bbae30f0ac23 Mon Sep 17 00:00:00 2001 From: Sergey <83376337+freak12techno@users.noreply.github.com> Date: Wed, 20 Dec 2023 18:43:03 +0300 Subject: [PATCH] chore: refactor proposal type (#58) --- pkg/fetchers/cosmos/fetcher.go | 8 +- pkg/fetchers/cosmos/responses/proposal_v1.go | 61 ++++++++++++++ .../cosmos/responses/proposal_v1beta1.go | 35 ++++++++ pkg/types/proposal.go | 17 ++++ pkg/types/responses.go | 82 ------------------- pkg/types/types.go | 13 --- 6 files changed, 117 insertions(+), 99 deletions(-) create mode 100644 pkg/fetchers/cosmos/responses/proposal_v1.go create mode 100644 pkg/fetchers/cosmos/responses/proposal_v1beta1.go create mode 100644 pkg/types/proposal.go diff --git a/pkg/fetchers/cosmos/fetcher.go b/pkg/fetchers/cosmos/fetcher.go index a7d91a5..2366135 100644 --- a/pkg/fetchers/cosmos/fetcher.go +++ b/pkg/fetchers/cosmos/fetcher.go @@ -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, @@ -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...) @@ -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, @@ -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...) diff --git a/pkg/fetchers/cosmos/responses/proposal_v1.go b/pkg/fetchers/cosmos/responses/proposal_v1.go new file mode 100644 index 0000000..cdd486d --- /dev/null +++ b/pkg/fetchers/cosmos/responses/proposal_v1.go @@ -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"` +} diff --git a/pkg/fetchers/cosmos/responses/proposal_v1beta1.go b/pkg/fetchers/cosmos/responses/proposal_v1beta1.go new file mode 100644 index 0000000..d7b4241 --- /dev/null +++ b/pkg/fetchers/cosmos/responses/proposal_v1beta1.go @@ -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, + } +} diff --git a/pkg/types/proposal.go b/pkg/types/proposal.go new file mode 100644 index 0000000..2ad8eec --- /dev/null +++ b/pkg/types/proposal.go @@ -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)) +} diff --git a/pkg/types/responses.go b/pkg/types/responses.go index 071d02d..73ffadb 100644 --- a/pkg/types/responses.go +++ b/pkg/types/responses.go @@ -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"` diff --git a/pkg/types/types.go b/pkg/types/types.go index 791e3cf..c086419 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -2,8 +2,6 @@ package types import ( "fmt" - "main/pkg/utils" - "time" "cosmossdk.io/math" ) @@ -21,17 +19,6 @@ func (l Link) Serialize() string { return fmt.Sprintf("%s", 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