Author

Topic: Blockchain.info's send many payments API problem - Invalid JSON recipients (Read 480 times)

newbie
Activity: 26
Merit: 0
It seems blockchain.info is having problems: https://bitcointalksearch.org/topic/blockchain-annoying-issue-1204476

I will update the status in this thread when I get news. But I don't know if it's related to your issue.



Im not sure if there is an issue with their API. I can send a single payment through their API. It is something in my code I would imagine but I have no idea....
legendary
Activity: 3906
Merit: 6249
Decentralization Maximalist
It seems blockchain.info is having problems: https://bitcointalksearch.org/topic/blockchain-annoying-issue-1204476

I will update the status in this thread when I get news. But I don't know if it's related to your issue.

newbie
Activity: 26
Merit: 0
Hi guys. I am using Blockchain.info's API to send multiple payments. I believe I have everything how it should be however when I run the code I get the following Error: RuntimeError: ERROR: Invalid Recipients JSON. Please make sure it is url encoded and consult the docs. The docs can be found here: https://blockchain.info/api/blockchain_wallet_api

The Python library I am using can be found here: https://github.com/p4u/blockchain.py/blob/master/blockchain.py

The only other post on this issue is posted by the original creator of the library, he said the problem was that the amounts cannot be a decimal, mine are not however.Post can be found here: https://bitcointalksearch.org/topic/blockchaininfo-api-problem-in-sendmany-600870

Code:
from __future__ import print_function
from itertools import islice, imap
import csv, requests, json, math
from collections import defaultdict
import requests
import urllib
import json
from os.path import expanduser
import configparser


class Wallet:
    guid        = 'g'
    isAccount   = 0
    isKey       = 0
    password1   = 'x'
    password2   = 'y'
    url         = ''

def __init__(self, guid = 'g', password1 = 'x', password2 = 'y'):

    if guid.count('-') > 0:
        self.isAccount = 1
        if password1 == '': # wallet guid's contain -
            raise ValueError('No password with guid.')
    else:
        self.isKey = 1

    self.guid = guid
    self.url = 'https://blockchain.info/merchant/' + guid + '/'

    self.password1 = password1
    self.password2 = password2

    r = requests.get('http://api.blockcypher.com/v1/btc/main/addrs/A/balance')
    balance = r.json()['balance']

    with open("Entries#x1.csv") as f,open("winningnumbers.csv") as nums:
        nums = set(imap(str.rstrip, nums))
        r = csv.reader(f)
        results = defaultdict(list)
        for row in r:
            results[sum(n in nums for n in islice(row, 1, None))].append(row[0])

    self.number_matched_0 = results[0]
    self.number_matched_1 = results[1]
    self.number_matched_2 = results[2]
    self.number_matched_3 = results[3]
    self.number_matched_4 = results[4]
    self.number_matched_5 = results[5]

    self.number_matched_5_json = json.dumps(self.number_matched_5, sort_keys = True, indent = 4)

    print(self.number_matched_5_json)

    if len(self.number_matched_3) == 0:
        print('Nobody matched 3 numbers')
    else:
        self.tx_amount_3 = int((balance*0.001)/ len(self.number_matched_3))

    if len(self.number_matched_4) == 0:
        print('Nobody matched 4 numbers')
    else:
        self.tx_amount_4 = int((balance*0.1)/ len(self.number_matched_4))

    if len(self.number_matched_5) == 0:
        print('Nobody matched 3 numbers')
    else:
        self.tx_amount_5 = int((balance*0.4)/ len(self.number_matched_5))

    self.d = {el: self.tx_amount_5 for el in json.loads(self.number_matched_5_json)}
    print(self.d)

    self.d_url_enc = urllib.urlencode(self.d)

def Call(self, method, data = {}):
    if self.password1 != '':
        data['password'] = self.password1
    if self.password2 != '':
        data['second_password'] = self.password2

    response = requests.post(self.url + method,params=data)

    json = response.json()
    if 'error' in json:
        raise RuntimeError('ERROR: ' + json['error'])

    return json

def SendPayment(self, toaddr, amount, fromaddr = 'A', shared = 0, fee = 0.0001, note = True):
    data = {}
    data['to'] = toaddr
    data['amount'] = self.tx_amount_5
    data['fee'] = fee
    data['recipients'] = self.d_url_enc

    if fromaddr:
        data['from'] = fromaddr

    if shared:
        data['shared'] = 'true'

    if note:
        data['note'] = 'n'

    response = self.Call('payment',data)

def SendManyPayment(self, fromaddr = True, shared = False, fee = 0.0001, note = True):
    data = {}
    recipients = self.d_url_enc
    data['recipients'] = recipients.__str__().replace("'",'"')
    data['fee'] = str(fee)
    if fromaddr:
        data['from'] = 'A'
    if shared:
        data['shared'] = 'true'
    else:
        data['shared'] = 'false'
    if note:
        data['note'] = 'n'
    response = self.Call('sendmany',data)

    return response

print(Wallet().SendManyPayment())
Jump to: