Skip to content

Instantly share code, notes, and snippets.

@detroitpro
Created January 5, 2018 19:55
Show Gist options
  • Save detroitpro/03734a1a7b58d2e401e3e7421acc9641 to your computer and use it in GitHub Desktop.
Save detroitpro/03734a1a7b58d2e401e3e7421acc9641 to your computer and use it in GitHub Desktop.
Find Candidate UTXO
import random
import decimal
cost = 0.10
sending_amount = 0.71
class UTXO(object):
id = 0
amount = 0
# The class "constructor" - It's actually an initializer
def __init__(self, id, amount):
self.id = id
self.amount = amount
def __str__(self):
return "UTXO: id %s amount %s" % (self.id, self.amount)
def random_amount():
a = float(random.randrange(0, 100))/100
return a
utxo_list = []
for x in range(0, 10):
utxo_list.append(UTXO(x, random_amount()))
#add items for testing
utxo_list.append(UTXO(10, sending_amount))
utxo_list.append(UTXO(12, cost + sending_amount + random_amount()))
utxo_list.append(UTXO(11, cost + sending_amount))
utxo_sorted = sorted(utxo_list, key=lambda utxo: utxo.amount)
def find_candidate(unspent, fee, amount):
total = fee + amount
for utxo in unspent:
print(utxo)
if utxo.amount >= total:
return utxo
candidate = find_candidate(utxo_sorted, cost, sending_amount)
if(candidate != None):
print("Candidate:", candidate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment