sorare/tokens/monetary_amount.go

44 lines
1.2 KiB
Go

package tokens
import (
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"git.lehouerou.net/laurent/sorare/types"
)
type MonetaryAmount struct {
ReferenceCurrency types.SupportedCurrency `graphql:"referenceCurrency"`
Eur *decimal.Decimal `graphql:"eur"`
Usd *decimal.Decimal `graphql:"usd"`
Gbp *decimal.Decimal `graphql:"gbp"`
Wei *decimal.Decimal `graphql:"wei"`
}
func (m MonetaryAmount) AmountInReferenceCurrency() (decimal.Decimal, error) {
switch m.ReferenceCurrency {
case types.SupportedCurrencyEUR:
if m.Eur == nil {
return decimal.Zero, errors.Errorf("missing EUR amount")
}
return *m.Eur, nil
case types.SupportedCurrencyUSD:
if m.Usd == nil {
return decimal.Zero, errors.Errorf("missing USD amount")
}
return *m.Usd, nil
case types.SupportedCurrencyGBP:
if m.Gbp == nil {
return decimal.Zero, errors.Errorf("missing GBP amount")
}
return *m.Gbp, nil
case types.SupportedCurrencyWEI:
if m.Wei == nil {
return decimal.Zero, errors.Errorf("missing WEI amount")
}
return *m.Wei, nil
default:
return decimal.Zero, errors.Errorf("unsupported currency %s", m.ReferenceCurrency)
}
}