@ -2,6 +2,9 @@ import settings
import requests
from requests . auth import HTTPDigestAuth
from funding . orm . orm import User
class Daemon :
def __init__ ( self ) :
self . url = settings . RPC_LOCATION
@ -9,67 +12,141 @@ class Daemon:
self . password = settings . RPC_PASSWORD
self . headers = { " User-Agent " : " Mozilla " }
def create_address ( self , label_name) :
def create_address ( self , account_index, label_name) :
data = {
' method ' : ' create_address ' ,
' params ' : { ' account_index ' : 0 , ' label ' : label_name } ,
' params ' : { ' account_index ' : account_index , ' label ' : ' p_ %s ' % label_name } ,
' jsonrpc ' : ' 2.0 ' ,
' id ' : ' 0 '
}
return self . _make_request ( data )
try :
result = self . _make_request ( data )
return result [ ' result ' ]
except :
return
def create_account ( self , pid ) :
data = {
' method ' : ' create_account ' ,
' params ' : { ' label ' : ' %s ' % pid } ,
' params ' : { ' label ' : ' p_ %s ' % pid } ,
' jsonrpc ' : ' 2.0 ' ,
' id ' : ' 0 '
}
return self . _make_request ( data )
try :
result = self . _make_request ( data )
return result [ ' result ' ]
except :
return
def get_address ( self , index , proposal_id ) :
def get_accounts ( self , proposal_id : int = None ) :
data = {
' method ' : ' get_accounts ' ,
' jsonrpc ' : ' 2.0 ' ,
' id ' : ' 0 '
}
try :
result = self . _make_request ( data )
result = result [ ' result ' ]
if isinstance ( proposal_id , int ) :
account_user = [ acc for acc in result . get ( ' subaddress_accounts ' , [ ] ) if acc [ ' label ' ] == ' p_ %d ' % proposal_id ]
if account_user :
return account_user [ 0 ]
else :
return
return result
except Exception as ex :
return
def get_address ( self , account_index : int , proposal_id : int = None ) :
data = {
' method ' : ' getaddress ' ,
' params ' : { ' account_index ' : proposal_id , ' address_index ' : ' [0] ' } ,
' params ' : { ' account_index ' : account_index } ,
' jsonrpc ' : ' 2.0 ' ,
' id ' : ' 0 '
}
try :
result = self . _make_request ( data )
return result [ ' result ' ]
addresses = result [ ' result ' ] [ ' addresses ' ]
if isinstance ( proposal_id , int ) :
address = [ addy for addy in addresses if addy [ ' label ' ] == ' p_ %d ' % proposal_id ]
if address :
return address [ 0 ]
else :
return
return addresses
except :
return
def get_transfers_in ( self , index , proposal_id ) :
def get_transfers_in ( self , proposal ) :
daemon = Daemon ( )
account = daemon . get_accounts ( proposal . id )
if not account :
raise Exception ( ' wallet error; pid not found found ' )
index = account [ ' account_index ' ]
address = daemon . get_address ( index , proposal_id = proposal . id )
if not address :
print ( ' Could not fetch transfers_in for proposal id %d ' % proposal . id )
return { ' sum ' : [ ] , ' txs ' : [ ] }
data = {
" method " : " get_transfers " ,
" params " : { " pool " : True , " in " : True , " account_index " : proposal_id } ,
" params " : { " pool " : True , " in " : True , " account_index " : in dex } ,
" jsonrpc " : " 2.0 " ,
" id " : " 0 " ,
}
data = self . _make_request ( data )
data = data [ ' result ' ] . get ( ' in ' , [ ] )
for d in data :
d [ ' amount_human ' ] = float ( d [ ' amount ' ] ) / 1e12
# filter by current proposal
txs = [ tx for tx in data if tx . get ( ' address ' ) == address [ ' address ' ] ]
for d in txs :
d [ ' amount_human ' ] = float ( d [ ' amount ' ] ) / 1e11
return {
' sum ' : sum ( [ float ( z [ ' amount ' ] ) / 1e12 for z in data ] ) ,
' txs ' : data
' sum ' : sum ( [ float ( z [ ' amount ' ] ) / 1e1 1 for z in txs ] ) ,
' txs ' : txs
}
def get_transfers_out ( self , index , proposal_id ) :
def get_transfers_out ( self , proposal ) :
daemon = Daemon ( )
account = daemon . get_accounts ( proposal . id )
if not account :
raise Exception ( ' wallet error; pid not found found ' )
index = account [ ' account_index ' ]
address = daemon . get_address ( index , proposal_id = proposal . id )
if not address :
print ( ' Could not fetch transfers_in for proposal id %d ' % proposal . id )
return { ' sum ' : [ ] , ' txs ' : [ ] }
data = {
" method " : " get_transfers " ,
" params " : { " pool " : True , " out " : True , " account_index " : proposal_id } ,
" params " : { " pool " : True , " out " : True , " account_index " : in dex } ,
" jsonrpc " : " 2.0 " ,
" id " : " 0 " ,
}
data = self . _make_request ( data )
data = data [ ' result ' ] . get ( ' out ' , [ ] )
for d in data :
d [ ' amount_human ' ] = float ( d [ ' amount ' ] ) / 1e12
# filter by current proposal
txs = [ tx for tx in data if tx . get ( ' address ' ) == address [ ' address ' ] ]
for d in txs :
d [ ' amount_human ' ] = float ( d [ ' amount ' ] ) / 1e11
return {
' sum ' : sum ( [ float ( z [ ' amount ' ] ) / 1e12 for z in data ] ) ,
' txs ' : data
' sum ' : sum ( [ float ( z [ ' amount ' ] ) / 1e1 1 for z in txs ] ) ,
' txs ' : txs
}
def _make_request ( self , data ) :