forked from ellipti-labs/terra-oracle
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pair.go
70 lines (57 loc) · 1.38 KB
/
pair.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Pair indicates how many {Quote} currency are needed to purchase one {Base} currency.
// And, pair should be immutable. So, if you want to set value in pair, you should return new structure.
type Pair interface {
String() string
Base() string
Quote() string
Price() (sdk.Dec, error)
SetPrice(sdk.Dec) Pair
// Maybe these can be used in future?
// func (func Pair) Volume() (sdk.Dec, error)
// func (func Pair) Bid() (sdk.Dec, error)
// func (func Pair) Ask() (sdk.Dec, error)
// func (func Pair) Spread() (sdk.Dec, error)
}
type BasePair struct {
base string
quote string
price sdk.Dec
}
var _ Pair = BasePair{}
func NewPair(base string, quote string) BasePair {
return BasePair{
base: base,
quote: quote,
price: sdk.ZeroDec(),
}
}
func PairStr(base string, quote string) string {
return base + "/" + quote
}
func (pair BasePair) String() string {
return PairStr(pair.base, pair.quote)
}
func (pair BasePair) Base() string {
return pair.base
}
func (pair BasePair) Quote() string {
return pair.quote
}
func (pair BasePair) Price() (sdk.Dec, error) {
if pair.price.LTE(sdk.ZeroDec()) {
return sdk.ZeroDec(), fmt.Errorf("invalid price")
}
return pair.price, nil
}
func (pair BasePair) SetPrice(price sdk.Dec) Pair {
return BasePair{
base: pair.base,
quote: pair.quote,
price: price,
}
}