-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.go
51 lines (41 loc) · 1017 Bytes
/
asset.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
package passet
import (
"context"
"sync"
"github.com/KarpelesLab/rest"
"github.com/KarpelesLab/xuid"
)
type Asset struct {
Id xuid.XUID `json:"Unit__"`
Key string // USD, etc
Name string
Symbol *string // can be null if no symbol available
SymbolPosition string `json:"Symbol_Position"` // before | after
Decimals int `json:",string"` // force decimals to this value
DisplayDecimals int `json:"Display_Decimals,string"`
Type string // currency | crypto_token
}
var (
assetMap = make(map[string]*Asset)
assetMapLk sync.RWMutex
)
// GetAsset returns the informations for a given asset
func GetAsset(k string) *Asset {
assetMapLk.RLock()
v, ok := assetMap[k]
assetMapLk.RUnlock()
if ok {
return v
}
err := rest.Apply(context.Background(), "Unit/"+k, "GET", nil, &v)
if err != nil {
return nil
}
if v.Key != k {
return nil
}
assetMapLk.Lock()
assetMap[k] = v
assetMapLk.Unlock()
return v
}