-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpending-tx.ls
82 lines (82 loc) · 2.87 KB
/
pending-tx.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
80
81
82
require! {
\localStorage
\./json-parse.ls
\./api.ls
\moment
\./math.ls : { plus }
\prelude-ls : { foldl, map }
}
set = (config, arr, cb)->
name = get-name config
value = JSON.stringify arr
local-storage.set-item name, value
cb null
#saved = (name)->
# key = get-key name
# (local-storage.get-item(key) ? "[]") isnt "[]"
get-all = (config, cb)->
name = get-name config
data = local-storage.get-item(name) ? ""
return cb null, [] if data is ""
err, arr <- json-parse data
return cb err if err?
cb null, arr
get-one = (config, cb)->
return cb "tx is required to get one" if not config.tx?
err, arr <- get-all config
return cb err if err?
index = arr.map(-> it.0).index-of(config.tx)
return cb "not found" if index is -1
cb null, arr[index]
export remove-tx = (config, cb)->
return cb "network is required to remove tx" if not config.network?
return cb "store is required to remove tx" if not config.store?
return cb "token is required to remove tx" if not config.token?
return cb "tx is required to remove tx" if not config.tx?
err, arr <- get-all config
return cb err if err?
return cb "empty array" if arr.length is 0
index = arr.map(-> it.0).index-of(config.tx)
return cb "not found" if index is -1
arr.splice index, 1
err <- set config, arr
return cb err if err?
cb null, \done
export get-pending-amount = (config, cb)->
return cb "network is required" if not config.network?
return cb "store is required" if not config.store?
return cb "token is required" if not config.token?
err, arr <- get-all config
return cb err if err?
total =
arr
|> map (.1)
|> foldl plus, 0
cb null, total
#export check-tx = ({ store, network, tx }, cb)->
# err, item <- get-one { network, store, tx }
# return cb err if err?
# cb null
export get-pending-txs = (config, cb)->
err, arr <- get-all config
cb null, arr
get-name = ({ network, store, token })->
mode = store.current.network #testnet or mainnet
index = store.current.account-index
"ptx-#{mode}-#{token}-#{index}"
export create-pending-tx = (config, cb)->
{ store, network, token, tx, amount-send, amount-send-fee, from, to, recipient } = config
return cb "token is required" if typeof! token isnt \String
return cb "store is required" if typeof! store isnt \Object
return cb "network is required" if typeof! network isnt \Object
return cb "tx is required" if typeof! tx isnt \String
return cb "callback is required" if typeof! cb isnt \Function
err, arr <- get-all config
return cb err if err?
now = moment!.unix!
to2 = recipient ? to
pendingTxInfo = [tx, amount-send, amount-send-fee, now, from, to2, network.token]
arr.push pendingTxInfo
err <- set config, arr
return cb err if err?
cb null, pendingTxInfo