-
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.
- Loading branch information
1 parent
1eb2212
commit 8607d9d
Showing
7 changed files
with
225 additions
and
197 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
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,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, ¶ms); len(errs) > 0 { | ||
return nil, &types.QueryError{ | ||
QueryError: nil, | ||
NodeErrors: errs, | ||
} | ||
} | ||
|
||
return ¶ms, 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) | ||
} |
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,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 | ||
} |
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
Oops, something went wrong.