Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor 260 #261

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions x/collector/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package cli

import (
flag "github.com/spf13/pflag"
"strconv"
"strings"
)

const (
FlagAddLookupParamsTable = "collector-lookup-table-file"
FlagAuctionControlParams = "auction-control-params-file"
)

func ParseStringFromString(s string, separator string) ([]string, error) {
var parsedStrings []string
for _, s := range strings.Split(s, separator) {
Expand Down Expand Up @@ -42,3 +48,44 @@ func ParseUint64SliceFromString(s string, separator string) ([]uint64, error) {
}
return parsedInts, nil
}

func FlagSetCreateLookupTableParamsMapping() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)

fs.String(FlagAddLookupParamsTable, "", "create lookup table params json file path")
return fs
}

func FlagSetAuctionControlParamsMapping() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)

fs.String(FlagAuctionControlParams, "", "auction control params json file path")
return fs
}

type createLookupTableParamsInputs struct {
AppID string `json:"app_id"`
CollectorAssetID string `json:"collector_asset_id"`
SecondaryAssetID string `json:"secondary_asset_id"`
SurplusThreshold string `json:"surplus_threshold"`
DebtThreshold string `json:"debt_threshold"`
LockerSavingRate string `json:"locker_saving_rate"`
LotSize string `json:"lot_size"`
BidFactor string `json:"bid_factor"`
DebtLotSize string `json:"debt_lot_size"`
Title string
Description string
Deposit string
}

type auctionControlParamsInputs struct {
AppID string `json:"app_id"`
AssetID string `json:"asset_id"`
SurplusAuction string `json:"surplus_auction"`
DebtAuction string `json:"debt_auction"`
AssetOutOraclePrice string `json:"asset_out_oracle_price"`
AssetOutPrice string `json:"asset_out_price"`
Title string
Description string
Deposit string
}
94 changes: 94 additions & 0 deletions x/collector/client/cli/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cli

import (
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/pflag"
"io/ioutil"
)

type XCreateLookupTableParamsInputs createLookupTableParamsInputs
type XAuctionControlParamsInputs auctionControlParamsInputs

type XCreateLookupTableParamsInputsExceptions struct {
XCreateLookupTableParamsInputs
Other *string // Other won't raise an error
}

type XAuctionControlParamsInputsExceptions struct {
XAuctionControlParamsInputs
Other *string // Other won't raise an error
}

// UnmarshalJSON should error if there are fields unexpected.
func (release *createLookupTableParamsInputs) UnmarshalJSON(data []byte) error {
var createLookupTableParamsE XCreateLookupTableParamsInputsExceptions
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields() // Force

if err := dec.Decode(&createLookupTableParamsE); err != nil {
return err
}

*release = createLookupTableParamsInputs(createLookupTableParamsE.XCreateLookupTableParamsInputs)
return nil
}

// UnmarshalJSON should error if there are fields unexpected.
func (release *auctionControlParamsInputs) UnmarshalJSON(data []byte) error {
var createAuctionControlParamsE XAuctionControlParamsInputsExceptions
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields() // Force

if err := dec.Decode(&createAuctionControlParamsE); err != nil {
return err
}

*release = auctionControlParamsInputs(createAuctionControlParamsE.XAuctionControlParamsInputs)
return nil
}

func parseLookupTableParamsFlags(fs *pflag.FlagSet) (*createLookupTableParamsInputs, error) {
lookupTableParams := &createLookupTableParamsInputs{}
lookupTableParamsFile, _ := fs.GetString(FlagAddLookupParamsTable)

if lookupTableParamsFile == "" {
return nil, fmt.Errorf("must pass in a lookup table params json using the --%s flag", FlagAddLookupParamsTable)
}

contents, err := ioutil.ReadFile(lookupTableParamsFile)
if err != nil {
return nil, err
}

// make exception if unknown field exists
err = lookupTableParams.UnmarshalJSON(contents)
if err != nil {
return nil, err
}

return lookupTableParams, nil
}

func parseAuctionControlParamsFlags(fs *pflag.FlagSet) (*auctionControlParamsInputs, error) {
auctionControlParams := &auctionControlParamsInputs{}
auctionControlParamsFile, _ := fs.GetString(FlagAuctionControlParams)

if auctionControlParamsFile == "" {
return nil, fmt.Errorf("must pass in a auction control params json using the --%s flag", FlagAuctionControlParams)
}

contents, err := ioutil.ReadFile(auctionControlParamsFile)
if err != nil {
return nil, err
}

// make exception if unknown field exists
err = auctionControlParams.UnmarshalJSON(contents)
if err != nil {
return nil, err
}

return auctionControlParams, nil
}
Loading