github.com/Finschia/finschia-sdk@v0.49.1/x/auth/vesting/types/period.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	yaml "gopkg.in/yaml.v2"
     9  
    10  	sdk "github.com/Finschia/finschia-sdk/types"
    11  )
    12  
    13  // Periods stores all vesting periods passed as part of a PeriodicVestingAccount
    14  type Periods []Period
    15  
    16  // Duration is converts the period Length from seconds to a time.Duration
    17  func (p Period) Duration() time.Duration {
    18  	return time.Duration(p.Length) * time.Second
    19  }
    20  
    21  // String implements the fmt.Stringer interface
    22  func (p Period) String() string {
    23  	out, _ := yaml.Marshal(p)
    24  	return string(out)
    25  }
    26  
    27  // TotalLength return the total length in seconds for a period
    28  func (p Periods) TotalLength() int64 {
    29  	var total int64
    30  	for _, period := range p {
    31  		total += period.Length
    32  	}
    33  	return total
    34  }
    35  
    36  // TotalDuration returns the total duration of the period
    37  func (p Periods) TotalDuration() time.Duration {
    38  	len := p.TotalLength()
    39  	return time.Duration(len) * time.Second
    40  }
    41  
    42  // TotalDuration returns the sum of coins for the period
    43  func (p Periods) TotalAmount() sdk.Coins {
    44  	total := sdk.Coins{}
    45  	for _, period := range p {
    46  		total = total.Add(period.Amount...)
    47  	}
    48  	return total
    49  }
    50  
    51  // String implements the fmt.Stringer interface
    52  func (p Periods) String() string {
    53  	periodsListString := make([]string, len(p))
    54  	for _, period := range p {
    55  		periodsListString = append(periodsListString, period.String())
    56  	}
    57  
    58  	return strings.TrimSpace(fmt.Sprintf(`Vesting Periods:
    59  		%s`, strings.Join(periodsListString, ", ")))
    60  }