-1

I am working on a python django project where users send bitcoins to external addresses. this is how the payment module looks like. I have tried many suggestions posted here with little help.

Below is my code. I would appreciate any third eye who could help me troubleshot this error.

import decimal as Decimal
import bitcoinrpc

class Withdrawal(models.Model):
    amount = models.OneToOneField("Amount", null=True)
    payment = models.OneToOneField(Payment, null=True, blank=True)
    to = models.CharField(max_length=35, null=True, blank=True)
    account = models.ForeignKey(Account)
    complete = models.BooleanField(default=False)
    txid = models.CharField(max_length=64, blank=True, null=True)
def __str__(self):
    return u"{} from {} to {}".format(self.amount, self.account, self.to)

def alert(self):
    """Sends email to all staff users about failed withdrawal"""
    for u in User.objects.filter(is_superuser=True):
        if u.email != u'':
            message = "Withdrawal {}, by {}, for {} " \
                      "has failed due to an insufficient balance."
            message = message.format(self.pk,
                                     self.account.user.email,
                                     self.amount)
            send_mail('FAILED WITHDRAWAL',
                      message,
                      settings.DEFAULT_FROM_EMAIL,
                      [u.email],
                      fail_silently=False)
    raise Exception("Insufficient funds")  

def send(self):
    """Sends payment on the blockchain, or if unable calls self.alert"""
    if self.complete:
        raise Exception("Sorry, Can't send a completed payment")
    s = get_server()

    fee = Decimal(str(PAYMENTS_FEE))
    final_amount = self.amount.base_amt - fee
    inputs = s.proxy.listunspent(0,
                                 9999999)

    total = sum([t['amount'] for t in inputs])
    if total  0:
        outputs[get_change()] = Decimal(total) -final_amount - fee
    raw = s.createrawtransaction(inputs, outputs)

    s.walletpassphrase(PAYMENTS_PASSPHRASE, 10)
    signed = s.signrawtransaction(raw)
    sent = s.proxy.sendrawtransaction(signed['hex'])
    s.walletlock()

    self.complete = True
    self.txid = sent
    self.save()
    if self.payment:
        self.payment.txid = sent
        self.confirmed = True
        self.payment.save()

    print sent


@staticmethod
def process_payments():
    """Try to send all present payments"""
    for w in Withdrawal.objects.filter(complete=False):
        w.send()

When this code is executed,an error occurs. This is the traceback

Traceback (most recent call last):
  File "/home/mart/forexapp/manage.py", line 10, in 
    execute_from_command_line(sys.argv)
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/mart/forexapp/payments/management/commands/process_payments.py", line 6, in handle
    Withdrawal.process_payments()
  File "/home/mart/forexapp/payments/models.py", line 35, in process_payments
    w.send()
  File "/home/mart/forexapp/payments/models.py", line 23, in send
    raw = s.createrawtransaction(inputs, outputs)
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/bitcoinrpc/connection.py", line 357, in createrawtransaction
    return self.proxy.createrawtransaction(inputs, outputs)
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/bitcoinrpc/proxy.py", line 91, in __call__
    'id': self.__idcnt})
  File "/usr/lib/python2.7/json/__init__.py", line 244, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Decimal('0.0016512') is not JSON serializable

Anything wrong i'm doing here?

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Mart
  • 1

1 Answers1

-1

Decimal is a object and the JSON is looking for a number. Need to convert the decimal to a float.

Oriphiel
  • 59
  • 1
  • 7