Add transfer handling
This commit is contained in:
parent
176ab69638
commit
40fd464a12
|
@ -1,5 +1,5 @@
|
||||||
from .address import Address
|
from .address import Address
|
||||||
from .account import Account
|
from .account import Account
|
||||||
from .wallet import Wallet
|
from .wallet import Wallet
|
||||||
from .numbers import from_atomic, to_atomic
|
from .numbers import from_atomic, to_atomic, as_monero
|
||||||
from . import prio
|
from . import prio
|
||||||
|
|
|
@ -27,11 +27,21 @@ class Account(object):
|
||||||
def get_payments_out(self):
|
def get_payments_out(self):
|
||||||
return self._backend.get_payments_out(account=self.index)
|
return self._backend.get_payments_out(account=self.index)
|
||||||
|
|
||||||
def transfer(self, address, amount, priority=prio.NORMAL, mixin=5):
|
def transfer(self, address, amount, priority=prio.NORMAL, mixin=5, unlock_time=0):
|
||||||
pass
|
return self._backend.transfer(
|
||||||
|
[(address, amount)],
|
||||||
|
priority,
|
||||||
|
mixin,
|
||||||
|
unlock_time,
|
||||||
|
account=self.index)
|
||||||
|
|
||||||
def transfer_multi(self, destinations, priority=prio.NORMAL, mixin=5):
|
def transfer_multiple(self, destinations, priority=prio.NORMAL, mixin=5, unlock_time=0):
|
||||||
"""
|
"""
|
||||||
destinations = [(address, amount), ...]
|
destinations = [(address, amount), ...]
|
||||||
"""
|
"""
|
||||||
pass
|
return self._backend.transfer(
|
||||||
|
destinations,
|
||||||
|
priority,
|
||||||
|
mixin,
|
||||||
|
unlock_time,
|
||||||
|
account=self.index)
|
||||||
|
|
|
@ -56,22 +56,58 @@ class JSONRPC(object):
|
||||||
def get_payments_in(self, account=0):
|
def get_payments_in(self, account=0):
|
||||||
_payments = self.raw_request('get_transfers',
|
_payments = self.raw_request('get_transfers',
|
||||||
{'account_index': account, 'in': True, 'out': False, 'pool': False})
|
{'account_index': account, 'in': True, 'out': False, 'pool': False})
|
||||||
return map(self._pythonify_tx, _payments.get('in', []))
|
return map(self._pythonify_payment, _payments.get('in', []))
|
||||||
|
|
||||||
def get_payments_out(self, account=0):
|
def get_payments_out(self, account=0):
|
||||||
_payments = self.raw_request('get_transfers',
|
_payments = self.raw_request('get_transfers',
|
||||||
{'account_index': account, 'in': False, 'out': True, 'pool': False})
|
{'account_index': account, 'in': False, 'out': True, 'pool': False})
|
||||||
return map(self._pythonify_tx, _payments.get('out', ''))
|
return map(self._pythonify_payment, _payments.get('out', ''))
|
||||||
|
|
||||||
|
def _pythonify_payment(self, pm):
|
||||||
|
return {
|
||||||
|
'id': pm['txid'],
|
||||||
|
'when': datetime.fromtimestamp(pm['timestamp']),
|
||||||
|
'amount': from_atomic(pm['amount']),
|
||||||
|
'fee': from_atomic(pm['fee']),
|
||||||
|
'height': pm['height'],
|
||||||
|
'payment_id': pm['payment_id'],
|
||||||
|
'note': pm['note']
|
||||||
|
}
|
||||||
|
|
||||||
|
def transfer(self, destinations, priority, mixin, unlock_time, account=0):
|
||||||
|
print(destinations)
|
||||||
|
data = {
|
||||||
|
'account_index': account,
|
||||||
|
'destinations': list(map(
|
||||||
|
lambda dst: {'address': str(Address(dst[0])), 'amount': to_atomic(dst[1])},
|
||||||
|
destinations)),
|
||||||
|
'mixin': mixin,
|
||||||
|
'priority': priority,
|
||||||
|
'unlock_time': 0,
|
||||||
|
'get_tx_keys': True,
|
||||||
|
'get_tx_hex': True,
|
||||||
|
'new_algorithm': True,
|
||||||
|
}
|
||||||
|
_transfers = self.raw_request('transfer_split', data)
|
||||||
|
keys = ('hash', 'amount', 'fee', 'key', 'blob')
|
||||||
|
return list(map(
|
||||||
|
self._pythonify_tx,
|
||||||
|
[ dict(_tx) for _tx in map(
|
||||||
|
lambda vs: zip(keys,vs),
|
||||||
|
zip(
|
||||||
|
*[_transfers[k] for k in (
|
||||||
|
'tx_hash_list', 'amount_list', 'fee_list', 'tx_key_list', 'tx_blob_list')
|
||||||
|
]
|
||||||
|
))
|
||||||
|
]))
|
||||||
|
|
||||||
def _pythonify_tx(self, tx):
|
def _pythonify_tx(self, tx):
|
||||||
return {
|
return {
|
||||||
'id': tx['txid'],
|
'id': tx['hash'],
|
||||||
'when': datetime.fromtimestamp(tx['timestamp']),
|
|
||||||
'amount': from_atomic(tx['amount']),
|
'amount': from_atomic(tx['amount']),
|
||||||
'fee': from_atomic(tx['fee']),
|
'fee': from_atomic(tx['fee']),
|
||||||
'height': tx['height'],
|
'key': tx['key'],
|
||||||
'payment_id': tx['payment_id'],
|
'blob': tx.get('blob', None),
|
||||||
'note': tx['note']
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def raw_request(self, method, params=None):
|
def raw_request(self, method, params=None):
|
||||||
|
@ -97,7 +133,7 @@ class JSONRPC(object):
|
||||||
# TODO: resolve code, raise exception
|
# TODO: resolve code, raise exception
|
||||||
_log.error(u"JSON RPC error:\n{result}".format(result=_ppresult))
|
_log.error(u"JSON RPC error:\n{result}".format(result=_ppresult))
|
||||||
if err['code'] in _err2exc:
|
if err['code'] in _err2exc:
|
||||||
raise _err2exc[err['code']](err['message'], method=method, data=data, result=result)
|
raise _err2exc[err['code']](err['message'])
|
||||||
else:
|
else:
|
||||||
raise RPCError(
|
raise RPCError(
|
||||||
"Method '{method}' failed with RPC Error of unknown code {code}, "
|
"Method '{method}' failed with RPC Error of unknown code {code}, "
|
||||||
|
@ -105,17 +141,8 @@ class JSONRPC(object):
|
||||||
return result['result']
|
return result['result']
|
||||||
|
|
||||||
|
|
||||||
class RPCError(exceptions.MoneroException):
|
class RPCError(exceptions.BackendException):
|
||||||
def __init__(self, message, method=None, data=None, result=None):
|
pass
|
||||||
self.method = method
|
|
||||||
self.data = data
|
|
||||||
self.result = result
|
|
||||||
super().__init__(message)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return "'{method}': {error}".format(
|
|
||||||
method=self.method,
|
|
||||||
error=super().__str__())
|
|
||||||
|
|
||||||
|
|
||||||
class Unauthorized(RPCError):
|
class Unauthorized(RPCError):
|
||||||
|
@ -127,5 +154,8 @@ class MethodNotFound(RPCError):
|
||||||
|
|
||||||
|
|
||||||
_err2exc = {
|
_err2exc = {
|
||||||
|
-2: exceptions.WrongAddress,
|
||||||
|
-4: exceptions.NotEnoughUnlockedMoney,
|
||||||
|
-20: exceptions.AmountIsZero,
|
||||||
-32601: MethodNotFound,
|
-32601: MethodNotFound,
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,5 +7,14 @@ class BackendException(MoneroException):
|
||||||
class AccountException(MoneroException):
|
class AccountException(MoneroException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class WrongAddress(AccountException):
|
||||||
|
pass
|
||||||
|
|
||||||
class NotEnoughMoney(AccountException):
|
class NotEnoughMoney(AccountException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class NotEnoughUnlockedMoney(NotEnoughMoney):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class AmountIsZero(AccountException):
|
||||||
|
pass
|
||||||
|
|
|
@ -3,7 +3,13 @@ from decimal import Decimal
|
||||||
PICONERO = Decimal('0.000000000001')
|
PICONERO = Decimal('0.000000000001')
|
||||||
|
|
||||||
def to_atomic(amount):
|
def to_atomic(amount):
|
||||||
|
"""Convert Monero decimal to atomic integer of piconero."""
|
||||||
return int(amount * 10**12)
|
return int(amount * 10**12)
|
||||||
|
|
||||||
def from_atomic(amount):
|
def from_atomic(amount):
|
||||||
|
"""Convert atomic integer of piconero to Monero decimal."""
|
||||||
return (Decimal(amount) * PICONERO).quantize(PICONERO)
|
return (Decimal(amount) * PICONERO).quantize(PICONERO)
|
||||||
|
|
||||||
|
def as_monero(amount):
|
||||||
|
"""Return the amount rounded to maximal Monero precision."""
|
||||||
|
return Decimal(amount).quantize(PICONERO)
|
||||||
|
|
|
@ -34,11 +34,20 @@ class Wallet(object):
|
||||||
def get_payments_out(self):
|
def get_payments_out(self):
|
||||||
return self.accounts[0].get_payments_out()
|
return self.accounts[0].get_payments_out()
|
||||||
|
|
||||||
def transfer(self, address, amount, priority=prio.NORMAL, mixin=5):
|
def transfer(self, address, amount, priority=prio.NORMAL, mixin=5, unlock_time=0):
|
||||||
self.accounts[0].transfer(address, amount, priority=priority, mixin=mixin)
|
return self.accounts[0].transfer(
|
||||||
|
address,
|
||||||
|
amount,
|
||||||
|
priority=priority,
|
||||||
|
mixin=mixin,
|
||||||
|
unlock_time=unlock_time)
|
||||||
|
|
||||||
def transfer_multi(self, destinations, priority=prio.NORMAL, mixin=5):
|
def transfer_multiple(self, destinations, priority=prio.NORMAL, mixin=5, unlock_time=0):
|
||||||
"""
|
"""
|
||||||
destinations = [(address, amount), ...]
|
destinations = [(address, amount), ...]
|
||||||
"""
|
"""
|
||||||
return self.accounts[0].transfer_multi(destinations, priority=priority, mixin=mixin)
|
return self.accounts[0].transfer_multiple(
|
||||||
|
destinations,
|
||||||
|
priority=priority,
|
||||||
|
mixin=mixin,
|
||||||
|
unlock_time=unlock_time)
|
||||||
|
|
Loading…
Reference in New Issue