-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Neutron proposals fetching
- Loading branch information
1 parent
e6f583a
commit f82991c
Showing
9 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package neutron | ||
|
||
import ( | ||
"github.com/rs/zerolog" | ||
"main/pkg/http" | ||
"main/pkg/types" | ||
) | ||
|
||
type Fetcher struct { | ||
ChainConfig *types.Chain | ||
Logger zerolog.Logger | ||
Client *http.Client | ||
} | ||
|
||
func NewFetcher(chainConfig *types.Chain, logger zerolog.Logger) *Fetcher { | ||
return &Fetcher{ | ||
ChainConfig: chainConfig, | ||
Logger: logger.With().Str("component", "neutron_fetcher").Logger(), | ||
Client: http.NewClient(chainConfig.Name, chainConfig.LCDEndpoints, logger), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package neutron | ||
|
||
import "main/pkg/types" | ||
|
||
func (fetcher *Fetcher) GetChainParams() (*types.ChainWithVotingParams, []error) { | ||
// TODO: fix | ||
return &types.ChainWithVotingParams{ | ||
Chain: fetcher.ChainConfig, | ||
Params: []types.ChainParam{}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package neutron | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"main/pkg/fetchers/neutron/responses" | ||
"main/pkg/types" | ||
) | ||
|
||
func (fetcher *Fetcher) GetAllProposals() ([]types.Proposal, *types.QueryError) { | ||
query := base64.StdEncoding.EncodeToString([]byte("{\"list_proposals\": {}}")) | ||
|
||
url := fmt.Sprintf( | ||
"/cosmwasm/wasm/v1/contract/%s/smart/%s", | ||
fetcher.ChainConfig.NeutronSmartContract, | ||
query, | ||
) | ||
|
||
var proposals responses.ProposalsResponse | ||
if errs := fetcher.Client.Get(url, &proposals); len(errs) > 0 { | ||
return nil, &types.QueryError{ | ||
QueryError: nil, | ||
NodeErrors: errs, | ||
} | ||
} | ||
|
||
proposalsParsed, err := proposals.ToProposals() | ||
if err != nil { | ||
return nil, &types.QueryError{ | ||
QueryError: err, | ||
NodeErrors: nil, | ||
} | ||
} | ||
|
||
return proposalsParsed, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package responses | ||
|
||
import ( | ||
"main/pkg/types" | ||
"main/pkg/utils" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type ProposalWithID struct { | ||
ID int `json:"id"` | ||
Proposal Proposal `json:"proposal"` | ||
} | ||
type Proposal struct { | ||
Title string `json:"title"` | ||
Description string `json:"description"` | ||
Expiration struct { | ||
AtTime string `json:"at_time"` | ||
} `json:"expiration"` | ||
Status string `json:"status"` | ||
TotalPower string `json:"total_power"` | ||
} | ||
|
||
type ProposalsResponse struct { | ||
Data struct { | ||
Proposals []ProposalWithID `json:"proposals"` | ||
} `json:"data"` | ||
} | ||
|
||
func (p ProposalsResponse) ToProposals() ([]types.Proposal, error) { | ||
allProposals := utils.Filter(p.Data.Proposals, func(p ProposalWithID) bool { | ||
return p.Proposal.Status == "open" | ||
}) | ||
|
||
proposals := make([]types.Proposal, len(allProposals)) | ||
|
||
for index, proposal := range allProposals { | ||
expiresAt, err := strconv.ParseInt(proposal.Proposal.Expiration.AtTime, 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
proposals[index] = types.Proposal{ | ||
ID: strconv.Itoa(proposal.ID), | ||
Title: proposal.Proposal.Title, | ||
Description: proposal.Proposal.Description, | ||
EndTime: time.Unix(0, expiresAt), | ||
} | ||
} | ||
|
||
return proposals, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package neutron | ||
|
||
import "main/pkg/types" | ||
|
||
func (fetcher *Fetcher) GetTallies() (types.ChainTallyInfos, error) { | ||
// TODO: fix | ||
return types.ChainTallyInfos{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package neutron | ||
|
||
import "main/pkg/types" | ||
|
||
func (fetcher *Fetcher) GetVote(proposal, voter string) (*types.Vote, *types.QueryError) { | ||
// TODO: fix | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters