Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #you can chagne the method spendables_for_address in pycoin.services.blockcypher with this
- def spendables_for_address(self, address,amount = None ):
- """
- Return a list of Spendable objects for the
- given bitcoin address.
- """
- """simple output like pycoin without amount"""
- if amount is None :
- spendables = []
- url_append = "?unspentOnly=true&token=%s&includeScript=true" % self.api_key
- url = self.base_url("addrs/%s%s" % (address, url_append))
- result = json.loads(urlopen(url).read().decode("utf8"))
- for txn in result.get("txrefs", []):
- coin_value = txn.get("value")
- script = h2b(txn.get("script"))
- previous_hash = h2b_rev(txn.get("tx_hash"))
- previous_index = txn.get("tx_output_n")
- spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
- return spendables
- """"for the specified amount it will return an list with
- total spendable and total amount"""
- """
- example out= spendables_for_address('address',11)
- spendable = out[0]
- total = out[1]
- change = total-fee-amount
- """
- else :
- spendables = []
- url_append = "?unspentOnly=true&token=%s&includeScript=true" % self.api_key
- url = self.base_url("addrs/%s%s" % (address, url_append))
- result = json.loads(urlopen(url).read().decode("utf8"))
- total_amount = 0
- list_spend = result.get("txrefs", [])
- if len(list_spend) == 0:
- raise Exception("No spendable outputs found")
- unspents = sorted(list_spend, key=lambda d: d['value'], reverse = True)
- for txn in unspents:
- coin_value = txn.get("value")
- total_amount = total_amount + coin_value
- script = h2b(txn.get("script"))
- previous_hash = h2b_rev(txn.get("tx_hash"))
- previous_index = txn.get("tx_output_n")
- spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
- if total_amount > amount :
- break
- return [spendables, total_amount]
Add Comment
Please, Sign In to add comment