/
utils.go
78 lines (66 loc) · 2.61 KB
/
utils.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
71
72
73
74
75
76
77
78
package types
import (
"fmt"
"sort"
"time"
"github.com/osmosis-labs/osmosis/osmomath"
)
var (
MaxSpotPrice = osmomath.NewDec(2).Power(128).Sub(osmomath.OneDec())
MaxSpotPriceBigDec = osmomath.BigDecFromDec(MaxSpotPrice)
)
// GetAllUniqueDenomPairs returns all unique pairs of denoms, where for every pair
// (X, Y), X < Y.
// The pair (X,Y) should only appear once in the list. Denoms are lexicographically sorted.
// Panics if finds duplicate pairs.
//
// NOTE: Sorts the input denoms slice.
func GetAllUniqueDenomPairs(denoms []string) []DenomPair {
// get denoms in ascending order
sort.Strings(denoms)
denomPairs := []DenomPair{}
for i := 0; i < len(denoms); i++ {
for j := i + 1; j < len(denoms); j++ {
if denoms[i] == denoms[j] {
panic("input had duplicated denom")
}
denomPairs = append(denomPairs, DenomPair{Denom0: denoms[i], Denom1: denoms[j]})
}
}
return denomPairs
}
// SpotPriceMulDuration returns the spot price multiplied by the time delta,
// that is the spot price between the current and last TWAP record.
// A single second accounts for 1_000_000_000 when converted to int64.
func SpotPriceMulDuration(sp osmomath.Dec, timeDeltaMs int64) osmomath.Dec {
return sp.MulInt64(timeDeltaMs)
}
// AccumDiffDivDuration returns the osmomath.Decated difference dividosmomath.Deche the
// time delta, that is the spot price between the current and last TWAP record.
func AccumDiffDivDuration(accumDiff osmomath.Dec, timeDeltaMs int64) osmomath.Dec {
return accumDiff.QuoInt64(timeDeltaMs)
}
// LexicographicalOrderDenoms takes two denoms and returns them to be in lexicographically ascending order.
// In other words, the first returned denom string will be the lexicographically smaller of the two denoms.
// If the denoms are equal, an error will be returned.
func LexicographicalOrderDenoms(denom0, denom1 string) (string, string, error) {
if denom0 == denom1 {
return "", "", fmt.Errorf("both assets cannot be of the same denom: assetA: %s, assetB: %s", denom0, denom1)
}
if denom0 > denom1 {
denom0, denom1 = denom1, denom0
}
return denom0, denom1, nil
}
// CanonicalTimeMs returns the canonical time in milliseconds used for twap
// math computations in UTC. Removes any monotonic clock reading prior to conversion to ms.
// In twap, we assume all calculations are done in milliseconds. Therefore, this conversion
// is necessary to make sure that there are no rounding errors.
func CanonicalTimeMs(twapTime time.Time) int64 {
return twapTime.Round(0).UnixMilli()
}
// DenomPair contains pair of assetA and assetB denoms which belong to a pool.
type DenomPair struct {
Denom0 string
Denom1 string
}