forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency.go
46 lines (41 loc) · 1.01 KB
/
currency.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package factory
import (
"fmt"
"math/big"
"time"
"github.com/OpenBazaar/openbazaar-go/repo"
)
func NewCurrencyDefinition(code string) repo.CurrencyDefinition {
if code == "" {
code = "BTC"
}
bt := repo.DefaultBlockTime
if code == "LTC" || code == "TLTC" {
bt = 150 * time.Second
}
return repo.CurrencyDefinition{
Name: fmt.Sprintf("%scoin", code),
Code: repo.CurrencyCode(code),
Divisibility: 8,
CurrencyType: repo.Crypto,
BlockTime: bt,
}
}
func MustNewCurrencyValue(amount, code string) *repo.CurrencyValue {
amt, ok := new(big.Int).SetString(amount, 10)
if !ok {
panic(fmt.Sprintf("invalid CurrencyValue amount: %s", amount))
}
return &repo.CurrencyValue{
Amount: amt,
Currency: NewCurrencyDefinition(code),
}
}
func MustNewCurrencyValueUsingDiv(amount, code string, customDiv uint) *repo.CurrencyValue {
if customDiv == 0 {
panic("custom divisibility must be greater than 0")
}
v := MustNewCurrencyValue(amount, code)
v.Currency.Divisibility = customDiv
return v
}