2024-02-22 07:09:31 +00:00
|
|
|
package config
|
|
|
|
|
2024-03-11 10:42:51 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
)
|
2024-02-28 14:04:47 +00:00
|
|
|
|
|
|
|
var _ Validator = (*SiaConfig)(nil)
|
2024-03-10 16:41:42 +00:00
|
|
|
var _ Defaults = (*SiaConfig)(nil)
|
2024-02-28 14:04:47 +00:00
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
type SiaConfig struct {
|
2024-03-11 11:50:07 +00:00
|
|
|
Key string `mapstructure:"key"`
|
|
|
|
URL string `mapstructure:"url"`
|
|
|
|
PriceHistoryDays uint64 `mapstructure:"price_history_days"`
|
|
|
|
MaxUploadPrice string `mapstructure:"max_upload_price"`
|
|
|
|
MaxDownloadPrice string `mapstructure:"max_download_price"`
|
|
|
|
MaxStoragePrice string `mapstructure:"max_storage_price"`
|
|
|
|
MaxContractSCPrice string `mapstructure:"max_contract_sc_price"`
|
|
|
|
MaxRPCSCPrice string `mapstructure:"max_rpc_sc_price"`
|
2024-03-10 16:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s SiaConfig) Defaults() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2024-03-10 18:36:45 +00:00
|
|
|
"max_rpc_sc_price": 0.1,
|
2024-03-10 18:35:37 +00:00
|
|
|
"max_contract_sc_price": 1,
|
|
|
|
"price_history_days": 90,
|
2024-03-10 16:41:42 +00:00
|
|
|
}
|
2024-02-22 07:09:31 +00:00
|
|
|
}
|
2024-02-28 14:04:47 +00:00
|
|
|
|
|
|
|
func (s SiaConfig) Validate() error {
|
|
|
|
if s.Key == "" {
|
|
|
|
return errors.New("core.storage.sia.key is required")
|
|
|
|
}
|
|
|
|
if s.URL == "" {
|
|
|
|
return errors.New("core.storage.sia.url is required")
|
|
|
|
}
|
2024-03-10 16:41:42 +00:00
|
|
|
|
2024-03-11 11:50:07 +00:00
|
|
|
if err := validateStringNumber(s.MaxUploadPrice, "core.storage.sia.max_upload_price"); err != nil {
|
|
|
|
return err
|
2024-03-10 16:41:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 11:50:07 +00:00
|
|
|
if err := validateStringNumber(s.MaxDownloadPrice, "core.storage.sia.max_download_price"); err != nil {
|
|
|
|
return err
|
2024-03-10 16:41:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 11:50:07 +00:00
|
|
|
if err := validateStringNumber(s.MaxStoragePrice, "core.storage.sia.max_storage_price"); err != nil {
|
|
|
|
return err
|
2024-03-10 16:41:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 11:50:07 +00:00
|
|
|
if err := validateStringNumber(s.MaxContractSCPrice, "core.storage.sia.max_contract_sc_price"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-11 10:42:51 +00:00
|
|
|
|
2024-03-11 11:50:07 +00:00
|
|
|
if err := validateStringNumber(s.MaxRPCSCPrice, "core.storage.sia.max_rpc_sc_price"); err != nil {
|
2024-03-11 10:42:51 +00:00
|
|
|
return err
|
2024-03-10 16:41:42 +00:00
|
|
|
}
|
2024-03-11 11:50:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateStringNumber(s string, name string) error {
|
|
|
|
if s == "" {
|
|
|
|
return errors.New(name + " is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
rat, ok := new(big.Rat).SetString(s)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("failed to parse " + name)
|
|
|
|
}
|
2024-03-10 16:41:42 +00:00
|
|
|
|
2024-03-11 10:42:51 +00:00
|
|
|
if rat.Cmp(new(big.Rat).SetUint64(0)) <= 0 {
|
2024-03-11 11:50:07 +00:00
|
|
|
return errors.New(name + " must be greater than 0")
|
2024-03-10 16:41:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-28 14:04:47 +00:00
|
|
|
return nil
|
|
|
|
}
|