-
Notifications
You must be signed in to change notification settings - Fork 24
/
coin.go
117 lines (101 loc) · 2.3 KB
/
coin.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package coin
// Copyright (c) 2015-2019 Bitontop Technologies Inc.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
import (
"errors"
"fmt"
"sort"
"strconv"
"strings"
cmap "github.com/orcaman/concurrent-map"
)
// modeling structure and functions,
// don't modify unless bugs or new features
type Coin struct { // might also understand as public chain or token
ID int
Code string `json:"code"`
Name string `json:"name"`
Website string `json:"website"`
Explorer string `json:"explorer"`
Health string // the health of the chain
Blockheigh int
Blocktime int // in seconds
// Chain ChainType
}
var LastID int
var coinMap cmap.ConcurrentMap
func Init() {
if coinMap == nil {
coinMap = cmap.New()
}
}
func GenerateCoinID() int {
return LastID + 1
}
func GetCoinByID(id int) *Coin {
if tmp, ok := coinMap.Get(fmt.Sprintf("%d", id)); ok {
return tmp.(*Coin)
}
return nil
}
func GetCoinID(code string) int {
coin := GetCoin(code)
if coin == nil {
return -1 // return -1 if coin not found
}
return coin.ID
}
func GetCoin(code string) *Coin {
code = strings.TrimSpace(strings.ToUpper(code)) //trim for psql space
for _, id := range coinMap.Keys() {
if tmp, ok := coinMap.Get(id); ok {
c := tmp.(*Coin)
if c.Code == code {
return c
}
}
}
return nil
}
func ExistID(id int) bool {
if tmp, ok := coinMap.Get(fmt.Sprintf("%d", id)); ok {
c := tmp.(*Coin)
if c.Code != "" {
// log.Printf("Exist id, coin: %+v", c)
return true
}
}
return false
}
func GetCoins() []*Coin {
coins := []*Coin{}
keySort := []int{}
for _, key := range coinMap.Keys() {
id, _ := strconv.Atoi(key)
keySort = append(keySort, id)
}
sort.Ints(keySort)
for _, key := range keySort {
coins = append(coins, GetCoinByID(key))
}
return coins
}
func AddCoin(coin *Coin) error {
if coin != nil && coin.Code != "" {
if coin.ID == 0 {
coin.ID = GenerateCoinID()
} else if ExistID(coin.ID) {
coin.ID = GenerateCoinID()
}
coin.Code = strings.ToUpper(coin.Code)
coinMap.Set(fmt.Sprintf("%d", coin.ID), coin)
LastID = coin.ID
} else {
return errors.New("code is not assign yet")
}
return nil
}
func DeleteCoin(coin *Coin) {
coinMap.Remove(fmt.Sprintf("%d", coin.ID))
}