refactor: split computeByRate to newRat

This commit is contained in:
Derrick Hammer 2024-03-14 07:17:58 -04:00
parent cc63ff2c6e
commit 19afa09c4d
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 12 additions and 3 deletions

View File

@ -299,15 +299,24 @@ func siacoinsFromRat(r *big.Rat) (types.Currency, error) {
} }
func computeByRate(num string, rate decimal.Decimal, name string) (*big.Rat, error) { func computeByRate(num string, rate decimal.Decimal, name string) (*big.Rat, error) {
parsedNum, err := newRat(num, name)
if err != nil {
return nil, err
}
parsedRate := new(big.Rat).Quo(parsedNum, rate.Rat())
return parsedRate, nil
}
func newRat(num string, name string) (*big.Rat, error) {
parsedNum, ok := new(big.Rat).SetString(num) parsedNum, ok := new(big.Rat).SetString(num)
if !ok { if !ok {
return nil, errors.New("failed to parse " + name) return nil, errors.New("failed to parse " + name)
} }
parsedRate := new(big.Rat).Quo(parsedNum, rate.Rat()) return parsedNum, nil
return parsedRate, nil
} }
func ratDivide(a *big.Rat, b uint64) *big.Rat { func ratDivide(a *big.Rat, b uint64) *big.Rat {