Guest User

Untitled

a guest
Feb 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #you can chagne the method spendables_for_address in pycoin.services.blockcypher with this
  2. def spendables_for_address(self, address,amount = None ):
  3. """
  4. Return a list of Spendable objects for the
  5. given bitcoin address.
  6. """
  7. """simple output like pycoin without amount"""
  8. if amount is None :
  9. spendables = []
  10. url_append = "?unspentOnly=true&token=%s&includeScript=true" % self.api_key
  11. url = self.base_url("addrs/%s%s" % (address, url_append))
  12. result = json.loads(urlopen(url).read().decode("utf8"))
  13. for txn in result.get("txrefs", []):
  14. coin_value = txn.get("value")
  15. script = h2b(txn.get("script"))
  16. previous_hash = h2b_rev(txn.get("tx_hash"))
  17. previous_index = txn.get("tx_output_n")
  18. spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
  19. return spendables
  20. """"for the specified amount it will return an list with
  21. total spendable and total amount"""
  22. """
  23. example out= spendables_for_address('address',11)
  24. spendable = out[0]
  25. total = out[1]
  26. change = total-fee-amount
  27. """
  28.  
  29. else :
  30. spendables = []
  31. url_append = "?unspentOnly=true&token=%s&includeScript=true" % self.api_key
  32. url = self.base_url("addrs/%s%s" % (address, url_append))
  33. result = json.loads(urlopen(url).read().decode("utf8"))
  34. total_amount = 0
  35. list_spend = result.get("txrefs", [])
  36. if len(list_spend) == 0:
  37. raise Exception("No spendable outputs found")
  38. unspents = sorted(list_spend, key=lambda d: d['value'], reverse = True)
  39. for txn in unspents:
  40. coin_value = txn.get("value")
  41. total_amount = total_amount + coin_value
  42. script = h2b(txn.get("script"))
  43. previous_hash = h2b_rev(txn.get("tx_hash"))
  44. previous_index = txn.get("tx_output_n")
  45. spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
  46. if total_amount > amount :
  47. break
  48. return [spendables, total_amount]
Add Comment
Please, Sign In to add comment