-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc-certain-wallet.ls
79 lines (77 loc) · 2.69 KB
/
calc-certain-wallet.ls
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
require! {
\./api.ls : { get-balance }
\./math.ls : { times, plus }
\prelude-ls : { find, map, each, pairs-to-obj, foldl, filter }
\./workflow.ls : { run, task }
\./round5.ls
\./round-human.ls
#\./pending-tx.ls : { get-pending-amount }
}
calc-wallet = (store, token, cb)->
return cb "Store is required" if not store?
{ wallets } = store.current.account
{ rates } = store
current = {token-index:0}
index = 0
wallets |> each (it)->
if it.coin.token is token then
current.token-index = index
index++
wallet = wallets |> find (-> it.coin.token is token)
return cb null if not wallet?
state =
balance-usd: 0
build-loader = (wallet)-> task (cb)->
{ token } = wallet.coin
token = wallet.coin.token.to-lower-case!
usd-rate = rates[token] ? \..
# convert usd-rate to string because bigint does not like number type and can throw exception
usd-rate = usd-rate + ''
wallet.usd-rate =
| usd-rate is \.. => 0
| _ => round5 usd-rate
eur-rate = \0.893191
btc-rate = \0
wallet.eur-rate =
| usd-rate is \.. => \..
| _ => round5 (usd-rate `times` eur-rate)
wallet.btc-rate =
| usd-rate is \.. => \..
| _ => round5 (usd-rate `times` btc-rate)
wallet.status = \loading
try
err, balance <- get-balance { wallet.address, wallet.network, token, account: { wallet.address, wallet.private-key } }
console.error "#{token} get-balance error:" err if err?
pending-sent = 0
wallet.pending-sent = pending-sent
wallet.balance =
| isNaN(balance) => ".."
| _ => balance
wallet.balance-usd =
| isNaN(usd-rate) or isNaN(balance) => ".."
| _ => balance `times` usd-rate
balance-usd-current =
| isNaN(wallet.balance-usd) => 0
| _ => wallet.balance-usd
wallet.status =
| isNaN(balance) || err? => 'error'
| _ => 'loaded'
wallet.state =
| err? => 'error'
| _ => 'success'
cb!
catch err
wallet.status = "error"
wallet.state = "error"
cb!
loaders =
[wallet] |> map build-loader
tasks =
loaders
|> map -> [loaders.index-of(it).to-string!, it]
|> pairs-to-obj
<- run [tasks] .then
if store.current.account.wallets[current.token-index]?
store.current.account.wallets[current.token-index] = wallet
cb null
module.exports = calc-wallet