-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtokenchain.go
268 lines (250 loc) · 5.94 KB
/
tokenchain.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"bytes"
"database/sql"
"encoding/hex"
"errors"
"math/big"
"path/filepath"
"sort"
"sync"
"github.com/hectorchu/gonano/rpc"
"github.com/hectorchu/gonano/wallet"
"github.com/hectorchu/nano-token-protocol/tokenchain"
_ "github.com/mattn/go-sqlite3"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
type tokenChainManager struct {
m, mDB sync.Mutex
chains map[string]*tokenchain.Chain
tokens map[string]*tokenchain.Token
}
func newTokenChainManager() *tokenChainManager {
return &tokenChainManager{
chains: make(map[string]*tokenchain.Chain),
tokens: make(map[string]*tokenchain.Token),
}
}
func (tcm *tokenChainManager) getTokens() (tokens []*tokenchain.Token) {
tokens = make([]*tokenchain.Token, 0, len(tcm.tokens))
for _, token := range tcm.tokens {
tokens = append(tokens, token)
}
sort.Slice(tokens, func(i, j int) bool {
return bytes.Compare(tokens[i].Hash(), tokens[j].Hash()) < 0
})
return
}
func (tcm *tokenChainManager) getBalance(token *tokenchain.Token, account string) (balance *big.Int) {
tcm.m.Lock()
balance = token.Balance(account)
tcm.m.Unlock()
return
}
func (tcm *tokenChainManager) createToken(
chain *tokenchain.Chain, a *wallet.Account, name string, supply *big.Int, decimals byte,
) (token *tokenchain.Token, err error) {
if chain == nil {
if chain, err = tokenchain.NewChain(rpcURL); err != nil {
return
}
if _, err = a.Send(chain.Address(), big.NewInt(1)); err != nil {
return
}
if err = chain.WaitForOpen(); err != nil {
return
}
tcm.m.Lock()
tcm.chains[chain.Address()] = chain
tcm.m.Unlock()
}
client := rpc.Client{URL: rpcURL}
rep, err := client.AccountRepresentative(a.Address())
if err != nil {
return
}
tcm.m.Lock()
token, err = tokenchain.TokenGenesis(chain, a, name, supply, decimals)
tcm.m.Unlock()
if err != nil {
return
}
if _, err = a.ChangeRep(rep); err != nil {
return
}
tcm.tokens[string(token.Hash())] = token
return
}
func (tcm *tokenChainManager) transferToken(
token *tokenchain.Token, a *wallet.Account, account string, amount *big.Int,
) (hash rpc.BlockHash, err error) {
client := rpc.Client{URL: rpcURL}
rep, err := client.AccountRepresentative(a.Address())
if err != nil {
return
}
tcm.m.Lock()
hash, err = token.Transfer(a, account, amount)
tcm.m.Unlock()
if err != nil {
return
}
_, err = a.ChangeRep(rep)
return
}
func (tcm *tokenChainManager) fetchChain(address string) (chain *tokenchain.Chain, err error) {
chain, ok := tcm.chains[address]
if ok {
return
}
if chain, err = tokenchain.LoadChain(address, rpcURL); err != nil {
return
}
tcm.m.Lock()
tcm.chains[address] = chain
if err = chain.Parse(); err == nil {
err = tcm.withDB(chain.SaveState)
}
tcm.m.Unlock()
return
}
func (tcm *tokenChainManager) fetchToken(hash rpc.BlockHash) (token *tokenchain.Token, err error) {
token, ok := tcm.tokens[string(hash)]
if ok {
return
}
client := rpc.Client{URL: rpcURL}
block, err := client.BlockInfo(hash)
if err != nil {
return
}
chain, err := tcm.fetchChain(block.BlockAccount)
if err != nil {
return
}
tcm.m.Lock()
token, err = chain.Token(hash)
tcm.m.Unlock()
if err != nil {
return
}
tcm.tokens[string(hash)] = token
return
}
func (tcm *tokenChainManager) isChainAddress(address string) (ok bool) {
tcm.m.Lock()
_, ok = tcm.chains[address]
tcm.m.Unlock()
return
}
func (tcm *tokenChainManager) withDB(cb func(*sql.DB) error) (err error) {
home, err := homedir.Dir()
if err != nil {
return
}
tcm.mDB.Lock()
defer tcm.mDB.Unlock()
db, err := sql.Open("sqlite3", filepath.Join(home, "tokenchains.db"))
if err != nil {
return
}
defer db.Close()
return cb(db)
}
func (tcm *tokenChainManager) load() (err error) {
tcm.withDB(tcm.loadChains)
if err = tcm.loadTokens(); err != nil {
return
}
wsClient.subscribe(func(block *rpc.Block) {
tcm.m.Lock()
if chain, ok := tcm.chains[block.Account]; ok {
if chain.Parse() == nil {
tcm.withDB(chain.SaveState)
}
}
tcm.m.Unlock()
})
go func() {
tcm.m.Lock()
for _, chain := range tcm.chains {
if chain.Parse() == nil {
tcm.withDB(chain.SaveState)
}
}
tcm.m.Unlock()
}()
return
}
func (tcm *tokenChainManager) loadChains(db *sql.DB) (err error) {
rows, err := db.Query("SELECT seed FROM chains")
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var seedStr string
if err := rows.Scan(&seedStr); err != nil {
return err
}
seed, err := hex.DecodeString(seedStr)
if err != nil {
return err
}
chain, err := tokenchain.NewChainFromSeed(seed, rpcURL)
if err != nil {
return err
}
if err = chain.LoadState(db); err != nil {
return err
}
tcm.chains[chain.Address()] = chain
}
return rows.Err()
}
func (tcm *tokenChainManager) loadTokens() (err error) {
for _, h := range viper.GetStringSlice("tokens") {
hash, err := hex.DecodeString(h)
if err != nil {
return err
}
for _, chain := range tcm.chains {
if token, err := chain.Token(hash); err == nil {
tcm.tokens[string(hash)] = token
break
}
}
if _, err = tcm.fetchToken(hash); err != nil {
return err
}
}
return
}
func (tcm *tokenChainManager) save() (err error) {
tokens := make([]string, 0, len(tcm.tokens))
for hash := range tcm.tokens {
tokens = append(tokens, rpc.BlockHash(hash).String())
}
viper.Set("tokens", tokens)
return viper.WriteConfig()
}
func (tcm *tokenChainManager) amountToString(amount *big.Int, decimals byte) string {
x := big.NewInt(10)
exp := x.Exp(x, big.NewInt(int64(decimals)), nil)
r := new(big.Rat).SetFrac(amount, exp)
return r.FloatString(int(decimals))
}
func (tcm *tokenChainManager) amountFromString(s string, decimals byte) (amount *big.Int, err error) {
x := big.NewInt(10)
exp := x.Exp(x, big.NewInt(int64(decimals)), nil)
r, ok := new(big.Rat).SetString(s)
if !ok {
return nil, errors.New("Unable to parse amount")
}
r = r.Mul(r, new(big.Rat).SetInt(exp))
if !r.IsInt() {
return nil, errors.New("Unable to parse amount")
}
return r.Num(), nil
}