Skip to content

Commit

Permalink
chore: refactor params types (#60)
Browse files Browse the repository at this point in the history
* chore: refactor params types

* chore: add tests

* chore: refactor votes for cosmos

* chore: add tests for votes

* chore: removed unused code

* chore: add tests for proposal

* chore: add tests for link

* chore: added chain types
  • Loading branch information
freak12techno committed Dec 21, 2023
1 parent 1eb2212 commit 9b06d50
Show file tree
Hide file tree
Showing 18 changed files with 584 additions and 303 deletions.
104 changes: 3 additions & 101 deletions pkg/data/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"fmt"
"main/pkg/fetchers/cosmos"
"main/pkg/types"
"strconv"
"sync"
"time"

"github.com/rs/zerolog"
)
Expand Down Expand Up @@ -61,104 +59,6 @@ func (m *Manager) GetTallies() (map[string]types.ChainTallyInfos, error) {
return tallies, nil
}

func (m *Manager) GetChainParams(chain *types.Chain) (*types.ChainWithVotingParams, []error) {
var wg sync.WaitGroup
var mutex sync.Mutex

errors := make([]error, 0)
params := &types.ParamsResponse{}

rpc := cosmos.NewRPC(chain, m.Logger)

wg.Add(3)

go func() {
defer wg.Done()

votingParams, err := rpc.GetGovParams("voting")
mutex.Lock()
defer mutex.Unlock()

if err != nil {
errors = append(errors, err)
return
}

params.VotingParams = votingParams.VotingParams
}()

go func() {
defer wg.Done()

depositParams, err := rpc.GetGovParams("deposit")
mutex.Lock()
defer mutex.Unlock()

if err != nil {
errors = append(errors, err)
return
}

params.DepositParams = depositParams.DepositParams
}()

go func() {
defer wg.Done()

tallyingParams, err := rpc.GetGovParams("tallying")
mutex.Lock()
defer mutex.Unlock()

if err != nil {
errors = append(errors, err)
return
}

params.TallyParams = tallyingParams.TallyParams
}()

wg.Wait()

if len(errors) > 0 {
return nil, errors
}

quorum, err := strconv.ParseFloat(params.TallyParams.Quorum, 64)
if err != nil {
return nil, []error{err}
}

threshold, err := strconv.ParseFloat(params.TallyParams.Threshold, 64)
if err != nil {
return nil, []error{err}
}

vetoThreshold, err := strconv.ParseFloat(params.TallyParams.VetoThreshold, 64)
if err != nil {
return nil, []error{err}
}

votingPeriod, err := time.ParseDuration(params.VotingParams.VotingPeriod)
if err != nil {
return nil, []error{err}
}

maxDepositPeriod, err := time.ParseDuration(params.DepositParams.MaxDepositPeriod)
if err != nil {
return nil, []error{err}
}

return &types.ChainWithVotingParams{
Chain: chain,
VotingPeriod: votingPeriod,
MaxDepositPeriod: maxDepositPeriod,
MinDepositAmount: params.DepositParams.MinDepositAmount,
Quorum: quorum,
Threshold: threshold,
VetoThreshold: vetoThreshold,
}, nil
}

func (m *Manager) GetParams() (map[string]types.ChainWithVotingParams, error) {
var wg sync.WaitGroup
var mutex sync.Mutex
Expand All @@ -172,7 +72,9 @@ func (m *Manager) GetParams() (map[string]types.ChainWithVotingParams, error) {
go func(chain *types.Chain) {
defer wg.Done()

chainParams, errs := m.GetChainParams(chain)
rpc := cosmos.NewRPC(chain, m.Logger)

chainParams, errs := rpc.GetChainParams()
mutex.Lock()
defer mutex.Unlock()

Expand Down
32 changes: 0 additions & 32 deletions pkg/fetchers/cosmos/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cosmos

import (
"encoding/json"
"fmt"
"main/pkg/fetchers/cosmos/responses"
"net/http"
"time"
Expand Down Expand Up @@ -38,23 +37,6 @@ func (rpc *RPC) GetAllProposals() ([]types.Proposal, *types.QueryError) {
return rpc.GetAllV1beta1Proposals()
}

func (rpc *RPC) GetTally(proposal string) (*types.Tally, *types.QueryError) {
url := fmt.Sprintf(
"/cosmos/gov/v1beta1/proposals/%s/tally",
proposal,
)

var tally responses.TallyRPCResponse
if errs := rpc.Get(url, &tally); len(errs) > 0 {
return nil, &types.QueryError{
QueryError: nil,
NodeErrors: errs,
}
}

return tally.Tally.ToTally(), nil
}

func (rpc *RPC) GetStakingPool() (*responses.PoolRPCResponse, *types.QueryError) {
url := "/cosmos/staking/v1beta1/pool"

Expand All @@ -69,20 +51,6 @@ func (rpc *RPC) GetStakingPool() (*responses.PoolRPCResponse, *types.QueryError)
return &pool, nil
}

func (rpc *RPC) GetGovParams(paramsType string) (*types.ParamsResponse, *types.QueryError) {
url := fmt.Sprintf("/cosmos/gov/v1beta1/params/%s", paramsType)

var pool types.ParamsResponse
if errs := rpc.Get(url, &pool); len(errs) > 0 {
return nil, &types.QueryError{
QueryError: nil,
NodeErrors: errs,
}
}

return &pool, nil
}

func (rpc *RPC) Get(url string, target interface{}) []types.NodeError {
nodeErrors := make([]types.NodeError, len(rpc.URLs))

Expand Down
85 changes: 85 additions & 0 deletions pkg/fetchers/cosmos/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cosmos

import (
"fmt"
"main/pkg/fetchers/cosmos/responses"
"main/pkg/types"
"sync"
)

func (rpc *RPC) GetGovParams(paramsType string) (*responses.ParamsResponse, *types.QueryError) {
url := fmt.Sprintf("/cosmos/gov/v1beta1/params/%s", paramsType)

var params responses.ParamsResponse
if errs := rpc.Get(url, &params); len(errs) > 0 {
return nil, &types.QueryError{
QueryError: nil,
NodeErrors: errs,
}
}

return &params, nil
}

func (rpc *RPC) GetChainParams() (*types.ChainWithVotingParams, []error) {
var wg sync.WaitGroup
var mutex sync.Mutex

errors := make([]error, 0)
params := &responses.ParamsResponse{}

wg.Add(3)

go func() {
defer wg.Done()

votingParams, err := rpc.GetGovParams("voting")
mutex.Lock()
defer mutex.Unlock()

if err != nil {
errors = append(errors, err)
return
}

params.VotingParams = votingParams.VotingParams
}()

go func() {
defer wg.Done()

depositParams, err := rpc.GetGovParams("deposit")
mutex.Lock()
defer mutex.Unlock()

if err != nil {
errors = append(errors, err)
return
}

params.DepositParams = depositParams.DepositParams
}()

go func() {
defer wg.Done()

tallyingParams, err := rpc.GetGovParams("tallying")
mutex.Lock()
defer mutex.Unlock()

if err != nil {
errors = append(errors, err)
return
}

params.TallyParams = tallyingParams.TallyParams
}()

wg.Wait()

if len(errors) > 0 {
return nil, errors
}

return params.ToParams(rpc.ChainConfig)
}
76 changes: 76 additions & 0 deletions pkg/fetchers/cosmos/responses/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package responses

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

type ParamsResponse struct {
VotingParams VotingParams `json:"voting_params"`
DepositParams DepositParams `json:"deposit_params"`
TallyParams TallyParams `json:"tally_params"`
}

type VotingParams struct {
VotingPeriod string `json:"voting_period"`
}

type DepositParams struct {
MinDepositAmount []Amount `json:"min_deposit"`
MaxDepositPeriod string `json:"max_deposit_period"`
}

type Amount struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
}

type TallyParams struct {
Quorum string `json:"quorum"`
Threshold string `json:"threshold"`
VetoThreshold string `json:"veto_threshold"`
}

func (params ParamsResponse) ToParams(chain *types.Chain) (*types.ChainWithVotingParams, []error) {
quorum, err := strconv.ParseFloat(params.TallyParams.Quorum, 64)
if err != nil {
return nil, []error{err}
}

threshold, err := strconv.ParseFloat(params.TallyParams.Threshold, 64)
if err != nil {
return nil, []error{err}
}

vetoThreshold, err := strconv.ParseFloat(params.TallyParams.VetoThreshold, 64)
if err != nil {
return nil, []error{err}
}

votingPeriod, err := time.ParseDuration(params.VotingParams.VotingPeriod)
if err != nil {
return nil, []error{err}
}

maxDepositPeriod, err := time.ParseDuration(params.DepositParams.MaxDepositPeriod)
if err != nil {
return nil, []error{err}
}

return &types.ChainWithVotingParams{
Chain: chain,
VotingPeriod: votingPeriod,
MaxDepositPeriod: maxDepositPeriod,
MinDepositAmount: utils.Map(params.DepositParams.MinDepositAmount, func(amount Amount) types.Amount {
return types.Amount{
Denom: amount.Denom,
Amount: amount.Amount,
}
}),
Quorum: quorum,
Threshold: threshold,
VetoThreshold: vetoThreshold,
}, nil
}
22 changes: 20 additions & 2 deletions pkg/fetchers/cosmos/responses/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func (v VoteRPCResponse) IsError() bool {
}

func (v VoteRPCResponse) ToVote() (*types.Vote, error) {
votesMap := map[string]string{
"VOTE_OPTION_YES": "Yes",
"VOTE_OPTION_ABSTAIN": "Abstain",
"VOTE_OPTION_NO": "No",
"VOTE_OPTION_NO_WITH_VETO": "No with veto",
}

var options []types.VoteOption

if len(v.Vote.Options) > 0 {
Expand All @@ -41,15 +48,26 @@ func (v VoteRPCResponse) ToVote() (*types.Vote, error) {
return nil, err
}

voteOption, found := votesMap[option.Option]
if !found {
voteOption = option.Option
}

options[index] = types.VoteOption{
Option: types.VoteType(option.Option),
Option: voteOption,
Weight: weight,
}
}
} else {
options = make([]types.VoteOption, 1)

voteOption, found := votesMap[v.Vote.Option]
if !found {
voteOption = v.Vote.Option
}

options[0] = types.VoteOption{
Option: types.VoteType(v.Vote.Option),
Option: voteOption,
Weight: 1,
}
}
Expand Down
Loading

0 comments on commit 9b06d50

Please sign in to comment.