|
| 1 | +import json, re |
| 2 | +from sqltools import * |
| 3 | +from blockchain_utils import * |
| 4 | + |
| 5 | +def get_balancedata(address): |
| 6 | + addr = re.sub(r'\W+', '', address) #check alphanumeric |
| 7 | + ROWS=dbSelect("""select |
| 8 | + f1.propertyid, sp.propertytype, f1.balanceavailable, f1.pendingpos, f1.pendingneg |
| 9 | + from |
| 10 | + (select |
| 11 | + COALESCE(s1.propertyid,s2.propertyid) as propertyid, COALESCE(s1.balanceavailable,0) as balanceavailable, |
| 12 | + COALESCE(s2.pendingpos,0) as pendingpos,COALESCE(s2.pendingneg,0) as pendingneg |
| 13 | + from |
| 14 | + (select propertyid,balanceavailable |
| 15 | + from addressbalances |
| 16 | + where address=%s) s1 |
| 17 | + full join |
| 18 | + (SELECT atx.propertyid, |
| 19 | + sum(CASE WHEN atx.balanceavailablecreditdebit > 0 THEN atx.balanceavailablecreditdebit ELSE 0 END) AS pendingpos, |
| 20 | + sum(CASE WHEN atx.balanceavailablecreditdebit < 0 THEN atx.balanceavailablecreditdebit ELSE 0 END) AS pendingneg |
| 21 | + from |
| 22 | + addressesintxs atx, transactions tx |
| 23 | + where |
| 24 | + atx.txdbserialnum=tx.txdbserialnum |
| 25 | + and tx.txstate='pending' |
| 26 | + and tx.txdbserialnum<-1 |
| 27 | + and atx.address=%s |
| 28 | + group by |
| 29 | + atx.propertyid) s2 |
| 30 | + on s1.propertyid=s2.propertyid) f1 |
| 31 | + inner join smartproperties sp |
| 32 | + on f1.propertyid=sp.propertyid and (sp.protocol='Omni' or sp.protocol='Mastercoin' or sp.protocol='Bitcoin') |
| 33 | + order by f1.propertyid""",(addr,addr)) |
| 34 | + |
| 35 | + balance_data = { 'balance': [] } |
| 36 | + ret = bc_getbalance(addr) |
| 37 | + out = ret['bal'] |
| 38 | + err = ret['error'] |
| 39 | + for balrow in ROWS: |
| 40 | + cID = str(int(balrow[0])) #currency id |
| 41 | + sym_t = ('BTC' if cID == '0' else ('OMNI' if cID == '1' else ('T-OMNI' if cID == '2' else 'SP' + cID) ) ) #symbol template |
| 42 | + #1 = new indivisible property, 2=new divisible property (per spec) |
| 43 | + divi = True if int(balrow[1]) == 2 else False |
| 44 | + res = { 'symbol' : sym_t, 'divisible' : divi, 'id' : cID } |
| 45 | + res['pendingpos'] = str(long(balrow[3])) |
| 46 | + res['pendingneg'] = str(long(balrow[4])) |
| 47 | + if cID == '0': |
| 48 | + #get btc balance from bc api's |
| 49 | + if err != None or out == '': |
| 50 | + #btc_balance[ 'value' ] = str(long(-555)) |
| 51 | + btc_balance[ 'value' ] = str(long(0)) |
| 52 | + else: |
| 53 | + try: |
| 54 | + if balrow[4] < 0: |
| 55 | + #res['value'] = str(long( json.loads( out )[0][ 'paid' ]) + str(long(balrow[4])) |
| 56 | + #res['value'] = str(long( json.loads( out )['data']['balance']*1e8) + str(long(balrow[4])) |
| 57 | + res['value'] = str(long( out ) + long(balrow[4])) |
| 58 | + else: |
| 59 | + #res['value'] = str(long( json.loads( out )[0][ 'paid' ])) |
| 60 | + #res['value'] = str(long( json.loads( out )['data']['balance']*1e8)) |
| 61 | + res['value'] = str(long( out )) |
| 62 | + except ValueError: |
| 63 | + #btc_balance[ 'value' ] = str(long(-555)) |
| 64 | + btc_balance[ 'value' ] = str(long(0)) |
| 65 | + else: |
| 66 | + #get regular balance from db |
| 67 | + if balrow[4] < 0: |
| 68 | + #update the 'available' balance immediately when the sender sent something. prevent double spend |
| 69 | + res['value'] = str(long(balrow[2]+balrow[4])) |
| 70 | + else: |
| 71 | + res['value'] = str(long(balrow[2])) |
| 72 | + |
| 73 | + #res['reserved_balance'] = ('%.8f' % float(balrow[5])).rstrip('0').rstrip('.') |
| 74 | + balance_data['balance'].append(res) |
| 75 | + |
| 76 | + #check if we got BTC data from DB, if not trigger manually add |
| 77 | + addbtc=True |
| 78 | + for x in balance_data['balance']: |
| 79 | + if "BTC" in x['symbol']: |
| 80 | + addbtc=False |
| 81 | + |
| 82 | + if addbtc: |
| 83 | + btc_balance = { 'symbol': 'BTC', 'divisible': True, 'id' : 0 } |
| 84 | + if err != None or out == '': |
| 85 | + #btc_balance[ 'value' ] = str(long(-555)) |
| 86 | + btc_balance[ 'value' ] = str(long(0)) |
| 87 | + else: |
| 88 | + try: |
| 89 | + #btc_balance[ 'value' ] = str(long( json.loads( out )[0][ 'paid' ])) |
| 90 | + #btc_balance[ 'value' ] = str(long( json.loads( out )['data']['balance']*1e8 )) |
| 91 | + btc_balance[ 'value' ] = str(long( out )) |
| 92 | + except ValueError: |
| 93 | + #btc_balance[ 'value' ] = str(long(-555)) |
| 94 | + btc_balance[ 'value' ] = str(long(0)) |
| 95 | + btc_balance['pendingpos'] = str(long(0)) |
| 96 | + btc_balance['pendingneg'] = str(long(0)) |
| 97 | + balance_data['balance'].append(btc_balance) |
| 98 | + |
| 99 | + return balance_data |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +def get_bulkbalancedata(addresses): |
| 104 | + btclist=bc_getbulkbalance(addresses) |
| 105 | + |
| 106 | + retval = {} |
| 107 | + |
| 108 | + for address in addresses: |
| 109 | + addr = re.sub(r'\W+', '', address) #check alphanumeric |
| 110 | + ROWS=dbSelect("""select |
| 111 | + f1.propertyid, sp.propertytype, f1.balanceavailable, f1.pendingpos, f1.pendingneg |
| 112 | + from |
| 113 | + (select |
| 114 | + COALESCE(s1.propertyid,s2.propertyid) as propertyid, COALESCE(s1.balanceavailable,0) as balanceavailable, |
| 115 | + COALESCE(s2.pendingpos,0) as pendingpos,COALESCE(s2.pendingneg,0) as pendingneg |
| 116 | + from |
| 117 | + (select propertyid,balanceavailable |
| 118 | + from addressbalances |
| 119 | + where address=%s) s1 |
| 120 | + full join |
| 121 | + (SELECT atx.propertyid, |
| 122 | + sum(CASE WHEN atx.balanceavailablecreditdebit > 0 THEN atx.balanceavailablecreditdebit ELSE 0 END) AS pendingpos, |
| 123 | + sum(CASE WHEN atx.balanceavailablecreditdebit < 0 THEN atx.balanceavailablecreditdebit ELSE 0 END) AS pendingneg |
| 124 | + from |
| 125 | + addressesintxs atx, transactions tx |
| 126 | + where |
| 127 | + atx.txdbserialnum=tx.txdbserialnum |
| 128 | + and tx.txstate='pending' |
| 129 | + and tx.txdbserialnum<-1 |
| 130 | + and atx.address=%s |
| 131 | + group by |
| 132 | + atx.propertyid) s2 |
| 133 | + on s1.propertyid=s2.propertyid) f1 |
| 134 | + inner join smartproperties sp |
| 135 | + on f1.propertyid=sp.propertyid and (sp.protocol='Omni' or sp.protocol='Mastercoin' or sp.protocol='Bitcoin') |
| 136 | + order by f1.propertyid""",(addr,addr)) |
| 137 | + |
| 138 | + balance_data = { 'balance': [] } |
| 139 | + try: |
| 140 | + if address in btclist: |
| 141 | + out = btclist[address] |
| 142 | + err = None |
| 143 | + else: |
| 144 | + out = '' |
| 145 | + err = "Missing" |
| 146 | + except TypeError: |
| 147 | + out = '' |
| 148 | + err = "Missing" |
| 149 | + |
| 150 | + for balrow in ROWS: |
| 151 | + cID = str(int(balrow[0])) #currency id |
| 152 | + sym_t = ('BTC' if cID == '0' else ('OMNI' if cID == '1' else ('T-OMNI' if cID == '2' else 'SP' + cID) ) ) #symbol template |
| 153 | + #1 = new indivisible property, 2=new divisible property (per spec) |
| 154 | + divi = True if int(balrow[1]) == 2 else False |
| 155 | + res = { 'symbol' : sym_t, 'divisible' : divi, 'id' : cID } |
| 156 | + res['pendingpos'] = str(long(balrow[3])) |
| 157 | + res['pendingneg'] = str(long(balrow[4])) |
| 158 | + if cID == '0': |
| 159 | + #get btc balance from bc api's |
| 160 | + if err != None or out == '': |
| 161 | + #btc_balance[ 'value' ] = str(long(-555)) |
| 162 | + btc_balance[ 'value' ] = str(long(0)) |
| 163 | + else: |
| 164 | + try: |
| 165 | + if balrow[4] < 0: |
| 166 | + #res['value'] = str(long( json.loads( out )[0][ 'paid' ]) + str(long(balrow[4])) |
| 167 | + res['value'] = str(long( out ) + long(balrow[4])) |
| 168 | + else: |
| 169 | + #res['value'] = str(long( json.loads( out )[0][ 'paid' ])) |
| 170 | + res['value'] = str(long( out )) |
| 171 | + except ValueError: |
| 172 | + #btc_balance[ 'value' ] = str(long(-555)) |
| 173 | + btc_balance[ 'value' ] = str(long(0)) |
| 174 | + else: |
| 175 | + #get regular balance from db |
| 176 | + if balrow[4] < 0: |
| 177 | + #update the 'available' balance immediately when the sender sent something. prevent double spend |
| 178 | + res['value'] = str(long(balrow[2]+balrow[4])) |
| 179 | + else: |
| 180 | + res['value'] = str(long(balrow[2])) |
| 181 | + |
| 182 | + #res['reserved_balance'] = ('%.8f' % float(balrow[5])).rstrip('0').rstrip('.') |
| 183 | + balance_data['balance'].append(res) |
| 184 | + |
| 185 | + #check if we got BTC data from DB, if not trigger manually add |
| 186 | + addbtc=True |
| 187 | + for x in balance_data['balance']: |
| 188 | + if "BTC" in x['symbol']: |
| 189 | + addbtc=False |
| 190 | + |
| 191 | + if addbtc: |
| 192 | + btc_balance = { 'symbol': 'BTC', 'divisible': True, 'id' : 0 } |
| 193 | + if err != None or out == '': |
| 194 | + #btc_balance[ 'value' ] = str(long(-555)) |
| 195 | + btc_balance[ 'value' ] = str(long(0)) |
| 196 | + else: |
| 197 | + try: |
| 198 | + #btc_balance[ 'value' ] = str(long( json.loads( out )[0][ 'paid' ])) |
| 199 | + btc_balance[ 'value' ] = str(long( out )) |
| 200 | + except ValueError: |
| 201 | + #btc_balance[ 'value' ] = str(long(-555)) |
| 202 | + btc_balance[ 'value' ] = str(long(0)) |
| 203 | + btc_balance['pendingpos'] = str(long(0)) |
| 204 | + btc_balance['pendingneg'] = str(long(0)) |
| 205 | + balance_data['balance'].append(btc_balance) |
| 206 | + |
| 207 | + retval[address]=balance_data |
| 208 | + return retval |
0 commit comments