-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathproxycoind.rb
More file actions
executable file
·185 lines (175 loc) · 4.9 KB
/
proxycoind.rb
File metadata and controls
executable file
·185 lines (175 loc) · 4.9 KB
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
#!/usr/bin/ruby
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'rubygems'
require 'redis'
require 'bitcoin_rpc'
file = File.new("proxycoind.log", 'a+')
STDOUT.reopen(file)
file.sync = true
@@config = YAML.load_file('config.yml')
class Redis
def setm(k, o)
set(k, Marshal.dump(o))
end
def getm(k)
m = get(k)
m ? Marshal.load(m) : nil
end
end
@@redis = Redis.new
def getrpc(coinname)
d = @@config['coins'][coinname]
uri = "http://#{d['user']}:#{d['password']}@#{d['host']}:#{d['port']}"
BitcoinRPC.new(uri)
end
def newaccount(accounts, accountid)
accounts[accountid] ||= {
'balance' => 0.0,
}
end
def checkmove(rpc, accounts, balancebasename)
txs = rpc.listtransactions('*', 10000) # TODO
p txs.size
txs.each do |tx|
confirmations = tx['confirmations']
txid = tx['txid']
amount = tx['amount']
cat = tx['category']
case cat
when 'move'
accountid = tx['account']
amount = tx['amount']
account = newaccount(accounts, accountid)
account['balance'] += amount
balancename = "#{balancebasename}#{accountid}"
@@redis.setnx(balancename, 0.0)
@@redis.incrbyfloat(balancename, amount)
p [:move, accountid, amount]
else
p cat # TODO
end
end
end
def poll(rpc, blockhash, accounts, pendingtxs, pendingflag, balancebasename)
receiveonly = blockhash != ''
confirmedheight = 6 # TODO
params = [blockhash]
params << 400 if pendingflag # TODO
result = rpc.listsinceblock(*params)
lastblock = result['lastblock']
txs = result['transactions']
p txs.size
completedtxs = []
txs.each do |tx|
confirmations = tx['confirmations']
txid = tx['txid']
confirmed = confirmations >= confirmedheight
if pendingflag
next if !pendingtxs.include?(txid)
p [:pending2, txid[0,6], confirmations]
next if !confirmed
completedtxs << txid
elsif !confirmed
p [:pending1, txid[0,6], confirmations]
pendingtxs << txid unless pendingtxs.include?(txid)
next
end
amount = tx['amount']
cat = tx['category']
case cat
when 'receive'
accountaddr = tx['address']
accountid = rpc.getaccount(accountaddr)
account = newaccount(accounts, accountid)
account['balance'] += amount
balancename = "#{balancebasename}#{accountid}"
@@redis.setnx(balancename, 0.0)
@@redis.incrbyfloat(balancename, amount)
p [:receive, amount, accountid, confirmations]
when 'send'
accountid = tx['account']
fee = tx['fee']
amount += fee
unless receiveonly
account = newaccount(accounts, accountid)
account['balance'] += amount
balancename = "#{balancebasename}#{accountid}"
@@redis.setnx(balancename, 0.0)
@@redis.incrbyfloat(balancename, amount)
end
p [:send, amount, accountid]
when 'generate'
accountid = tx['account']
unless receiveonly
account = newaccount(accounts, accountid)
account['balance'] += amount
balancename = "#{balancebasename}#{accountid}"
@@redis.setnx(balancename, 0.0)
@@redis.incrbyfloat(balancename, amount)
end
p [:generate, amount, accountid, confirmations]
when 'immature'
accountid = tx['account']
unless receiveonly
account = newaccount(accounts, accountid)
account['balance'] += amount
balancename = "#{balancebasename}#{accountid}"
@@redis.setnx(balancename, 0.0)
@@redis.incrbyfloat(balancename, amount)
end
p [:immature, amount, accountid, confirmations]
when 'orphan'
p :orphan # TODO
else
p cat # TODO
end
end
completedtxs.uniq.each do |txid|
pendingtxs.delete(txid)
end
lastblock
end
def main
interval = 60
loop do
begin
coins = @@config['coins']
coins.each do |coinid,v|
next unless v['proxycoind']
rpc = getrpc(coinid)
coindbname = "proxycoind:#{coinid}"
balancebasename = "proxycoind:balance:#{coinid}:"
coininfo = @@redis.getm(coindbname) || {
'blockhash' => '',
'accounts' => {},
'pendingtxs' => [],
'pendingblockhash' => '',
}
blockhash = coininfo['blockhash']
accounts = coininfo['accounts']
pendingtxs = coininfo['pendingtxs']
if blockhash == ''
checkmove(rpc, accounts, balancebasename)
end
blockhash = poll(rpc, blockhash, accounts, pendingtxs, false,
balancebasename)
coininfo['blockhash'] = blockhash
pendingblockhash = coininfo['pendingblockhash']
pendingblockhash = poll(rpc, pendingblockhash, accounts, pendingtxs,
true, balancebasename)
coininfo['pendingtxs'] = pendingtxs
coininfo['pendingblockhash'] = pendingblockhash
@@redis.setm(coindbname, coininfo)
accounts.each do |account|
p account
end
p blockhash
end
rescue => x
p x
raise x # TODO
end
sleep interval
end
end
main