Skip to content

Commit

Permalink
chore: refactor params type (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Dec 22, 2023
1 parent 92229b2 commit 7fd94ab
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 57 deletions.
29 changes: 17 additions & 12 deletions pkg/fetchers/cosmos/responses/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,22 @@ func (params ParamsResponse) ToParams(chain *types.Chain) (*types.ChainWithVotin
}

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,
Chain: chain,
Params: []types.ChainParam{
types.DurationParam{Description: "Voting period", Value: votingPeriod},
types.DurationParam{Description: "Max deposit period", Value: maxDepositPeriod},
types.AmountsParam{
Description: "Min deposit amount",
Value: utils.Map(params.DepositParams.MinDepositAmount, func(amount Amount) types.Amount {
return types.Amount{
Denom: amount.Denom,
Amount: amount.Amount,
}
}),
},
types.PercentParam{Description: "Quorum", Value: quorum},
types.PercentParam{Description: "Threshold", Value: threshold},
types.PercentParam{Description: "Veto threshold", Value: vetoThreshold},
},
}, nil
}
67 changes: 48 additions & 19 deletions pkg/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,64 @@ import (
"main/pkg/utils"
)

type Amount struct {
Denom string
Amount string
type ChainWithVotingParams struct {
Chain *Chain
Params []ChainParam
}

type ChainWithVotingParams struct {
Chain *Chain
VotingPeriod time.Duration
MinDepositAmount []Amount
MaxDepositPeriod time.Duration
Quorum float64
Threshold float64
VetoThreshold float64
type ChainParam interface {
GetDescription() string
Serialize() string
}

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

type PercentParam struct {
Description string
Value float64
}

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

func (p PercentParam) Serialize() string {
return fmt.Sprintf("%.2f%%", p.Value*100)
}

func (c ChainWithVotingParams) FormatQuorum() string {
return fmt.Sprintf("%.2f%%", c.Quorum*100)
/* ------------------------------------------------ */

type DurationParam struct {
Description string
Value time.Duration
}

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

func (p DurationParam) Serialize() string {
return utils.FormatDuration(p.Value)
}

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

type Amount struct {
Denom string
Amount string
}

func (c ChainWithVotingParams) FormatThreshold() string {
return fmt.Sprintf("%.2f%%", c.Threshold*100)
type AmountsParam struct {
Description string
Value []Amount
}

func (c ChainWithVotingParams) FormatVetoThreshold() string {
return fmt.Sprintf("%.2f%%", c.VetoThreshold*100)
func (p AmountsParam) GetDescription() string {
return p.Description
}

func (c ChainWithVotingParams) FormatMinDepositAmount() string {
amountsAsStrings := utils.Map(c.MinDepositAmount, func(a Amount) string {
func (p AmountsParam) Serialize() string {
amountsAsStrings := utils.Map(p.Value, func(a Amount) string {
return a.Amount + " " + a.Denom
})
return strings.Join(amountsAsStrings, ",")
Expand Down
56 changes: 36 additions & 20 deletions pkg/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,65 @@ package types

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestParamsFormatQuorum(t *testing.T) {
func TestParamsDescription(t *testing.T) {
t.Parallel()

params := ChainWithVotingParams{
Quorum: 0.4,
}
assert.Equal(
t,
"Description",
PercentParam{Description: "Description"}.GetDescription(),
"Wrong value!",
)

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

assert.Equal(t, "40.00%", params.FormatQuorum(), "Wrong value!")
assert.Equal(
t,
"Description",
DurationParam{Description: "Description"}.GetDescription(),
"Wrong value!",
)
}

func TestParamsFormatThreshold(t *testing.T) {
func TestPercentParamSerialize(t *testing.T) {
t.Parallel()

params := ChainWithVotingParams{
Threshold: 0.4,
params := PercentParam{
Value: 0.4,
}

assert.Equal(t, "40.00%", params.FormatThreshold(), "Wrong value!")
assert.Equal(t, "40.00%", params.Serialize(), "Wrong value!")
}

func TestParamsFormatVetoThreshold(t *testing.T) {
func TestAmountParamSerialize(t *testing.T) {
t.Parallel()

params := ChainWithVotingParams{
VetoThreshold: 0.4,
params := AmountsParam{
Value: []Amount{
{Denom: "stake", Amount: "100"},
{Denom: "test", Amount: "100"},
},
}

assert.Equal(t, "40.00%", params.FormatVetoThreshold(), "Wrong value!")
assert.Equal(t, "100 stake,100 test", params.Serialize(), "Wrong value!")
}

func TestParamsFormatFormatMinDepositAmount(t *testing.T) {
func TestDurationParamSerialize(t *testing.T) {
t.Parallel()

params := ChainWithVotingParams{
MinDepositAmount: []Amount{
{Denom: "stake", Amount: "100"},
{Denom: "test", Amount: "100"},
},
params := DurationParam{
Value: time.Hour + time.Minute,
}

assert.Equal(t, "100 stake,100 test", params.FormatMinDepositAmount(), "Wrong value!")
assert.Equal(t, "1 hour 1 minute", params.Serialize(), "Wrong value!")
}
10 changes: 4 additions & 6 deletions templates/telegram/params.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{{- range $chainName, $params := . }}
<strong>Params of chain {{ $params.Chain.GetName }}:</strong>
Voting period: {{ FormatDuration $params.VotingPeriod }}
Max deposit period: {{ FormatDuration $params.MaxDepositPeriod }}
Min deposit amount: {{ $params.FormatMinDepositAmount }}
Quorum: {{ $params.FormatQuorum }}
Threshold: {{ $params.FormatThreshold }}
Veto threshold: {{ $params.FormatVetoThreshold }}
{{- range $param := .Params }}
{{ $param.GetDescription }}: {{ $param.Serialize }}
{{- end }}

{{ end }}

0 comments on commit 7fd94ab

Please sign in to comment.