1// Copyright (C) 2020 Storj Labs, Inc.
2// See LICENSE for copying information.
3
4package compensation
5
6import (
7	"github.com/shopspring/decimal"
8	"github.com/spf13/pflag"
9)
10
11// Rates configures the payment rates for network operations.
12type Rates struct {
13	AtRestGBHours Rate // For data at rest in dollars per gigabyte-hour.
14	GetTB         Rate // For data the node has sent for reads in dollars per terabyte.
15	PutTB         Rate // For data the node has received for writes in dollars per terabyte.
16	GetRepairTB   Rate // For data the node has sent for repairs in dollars per terabyte.
17	PutRepairTB   Rate // For data the node has received for repairs in dollars per terabyte.
18	GetAuditTB    Rate // For data the node has sent for audits in dollars per terabyte.
19}
20
21// Rate is a wrapper type around a decimal.Decimal.
22type Rate decimal.Decimal
23
24var _ pflag.Value = (*Rate)(nil)
25
26// RateFromString parses the string form of the rate into a Rate.
27func RateFromString(value string) (Rate, error) {
28	r, err := decimal.NewFromString(value)
29	if err != nil {
30		return Rate{}, err
31	}
32	return Rate(r), nil
33}
34
35// String returns the string form of the Rate.
36func (rate Rate) String() string {
37	return decimal.Decimal(rate).String()
38}
39
40// Set updates the Rate to be equal to the parsed string.
41func (rate *Rate) Set(s string) error {
42	r, err := decimal.NewFromString(s)
43	if err != nil {
44		return err
45	}
46	*rate = Rate(r)
47	return nil
48}
49
50// Type returns a unique string representing the type of the Rate.
51func (rate Rate) Type() string {
52	return "rate"
53}
54
55// RequireRateFromString parses the Rate from the string or panics.
56func RequireRateFromString(s string) Rate {
57	return Rate(decimal.RequireFromString(s))
58}
59