Skip to content

Commit

Permalink
feat: add Neutron params fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Dec 23, 2023
1 parent 770e864 commit 0d3653b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
23 changes: 17 additions & 6 deletions pkg/fetchers/neutron/params.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package neutron

import "main/pkg/types"
import (
"main/pkg/fetchers/neutron/responses"
"main/pkg/types"
)

func (fetcher *Fetcher) GetChainParams() (*types.ChainWithVotingParams, []error) {
// TODO: fix
return &types.ChainWithVotingParams{
Chain: fetcher.ChainConfig,
Params: []types.ChainParam{},
}, nil
query := "{\"config\":{}}"

var params responses.ParamsResponse
if err := fetcher.GetSmartContractState(query, &params); err != nil {
return nil, []error{err}
}

paramsParsed, errs := params.ToParams(fetcher.ChainConfig)
if len(errs) > 0 {
return nil, errs
}

return paramsParsed, nil
}
51 changes: 51 additions & 0 deletions pkg/fetchers/neutron/responses/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package responses

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

type ParamsResponse struct {
Data struct {
Threshold struct {
ThresholdQuorum struct {
Threshold struct {
Percent string `json:"percent"`
} `json:"threshold"`
Quorum struct {
Percent string `json:"percent"`
} `json:"quorum"`
} `json:"threshold_quorum"`
} `json:"threshold"`
MaxVotingPeriod struct {
Time int `json:"time"`
} `json:"max_voting_period"`
AllowRevoting bool `json:"allow_revoting"`
} `json:"data"`
}

func (params ParamsResponse) ToParams(chain *types.Chain) (*types.ChainWithVotingParams, []error) {
thresholdPercent, err := strconv.ParseFloat(params.Data.Threshold.ThresholdQuorum.Threshold.Percent, 64)
if err != nil {
return nil, []error{err}
}

quorumPercent, err := strconv.ParseFloat(params.Data.Threshold.ThresholdQuorum.Quorum.Percent, 64)
if err != nil {
return nil, []error{err}
}

return &types.ChainWithVotingParams{
Chain: chain,
Params: []types.ChainParam{
types.PercentParam{Description: "Threshold percent", Value: thresholdPercent},
types.PercentParam{Description: "Quorum percent", Value: quorumPercent},
types.DurationParam{
Description: "Max voting period",
Value: time.Duration(params.Data.MaxVotingPeriod.Time * 1e9),
},
types.BoolParam{Description: "Allow revoting", Value: params.Data.AllowRevoting},
},
}, []error{}
}
19 changes: 19 additions & 0 deletions pkg/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ func (p DurationParam) Serialize() string {

/* ------------------------------------------------ */

type BoolParam struct {
Description string
Value bool
}

func (p BoolParam) GetDescription() string {
return p.Description
}

func (p BoolParam) Serialize() string {
if p.Value {
return "Yes"
}

return "No"
}

/* ------------------------------------------------ */

type Amount struct {
Denom string
Amount string
Expand Down
14 changes: 14 additions & 0 deletions pkg/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func TestParamsDescription(t *testing.T) {
DurationParam{Description: "Description"}.GetDescription(),
"Wrong value!",
)

assert.Equal(
t,
"Description",
BoolParam{Description: "Description"}.GetDescription(),
"Wrong value!",
)
}

func TestPercentParamSerialize(t *testing.T) {
Expand Down Expand Up @@ -64,3 +71,10 @@ func TestDurationParamSerialize(t *testing.T) {

assert.Equal(t, "1 hour 1 minute", params.Serialize(), "Wrong value!")
}

func TestBoolParamSerialize(t *testing.T) {
t.Parallel()

assert.Equal(t, "Yes", BoolParam{Value: true}.Serialize(), "Wrong value!")
assert.Equal(t, "No", BoolParam{Value: false}.Serialize(), "Wrong value!")
}

0 comments on commit 0d3653b

Please sign in to comment.