def get_utxo(cls, address: str, amount: float): api_key = os.environ.get('CRYPTOID_API_KEY') if not api_key: raise ValueError('API key for cryptoid is required to get UTXOs.') data = clove_req_json(f'{cls.cryptoid_url()}/api.dws?q=unspent&key={api_key}&active={address}') unspent = data.get('unspent_outputs', []) for output in unspent: output['value'] = int(output['value']) unspent = sorted(unspent, key=lambda k: k['value'], reverse=True) utxo = [] total = 0 for output in unspent: value = from_base_units(output['value']) utxo.append( Utxo( tx_id=output['tx_hash'], vout=output['tx_ouput_n'], value=value, tx_script=output['script'], ) ) total += value if total > amount: return utxo logger.debug(f'Cannot find enough UTXO\'s. Found %.8f from %.8f.', total, amount)
def get_contract_utxo(self, wallet=None, secret: str = None, refund: bool = False, contract: str = None) -> Utxo: ''' Creating UTXO object from contract. Args: wallet (obj): wallet object secret (str): tranaction secret (used to redeem contract) refund (bool): flag used for refund transactions contract (str): hex string with contract definition Returns: Utxo: Unspent transaction output object ''' return Utxo( tx_id=self.transaction_address, vout=0, value=self.value, tx_script=self.vout.scriptPubKey.hex(), wallet=wallet, secret=secret, refund=refund, contract=contract, )
def get_utxo(cls, address: str, amount: float): data = clove_req_json( f'{cls.blockcypher_url()}/addrs/{address}' '?limit=2000&unspentOnly=true&includeScript=true&confirmations=6') unspent = data.get('txrefs', []) for output in unspent: output['value'] = int(output['value']) unspent = sorted(unspent, key=lambda k: k['value'], reverse=True) utxo = [] total = 0 for output in unspent: value = from_base_units(output['value']) utxo.append( Utxo( tx_id=output['tx_hash'], vout=output['tx_output_n'], value=value, tx_script=output['script'], )) total += value if total > amount: return utxo logger.debug(f'Cannot find enough UTXO\'s. Found %.8f from %.8f.', total, amount)
def bob_utxo(bob_wallet): return [ Utxo( tx_id='56384654b9e21242588c8fa5f905808a96039a8e1257312f35e0b06c55fa19fb', vout=1, value=0.87961162, tx_script='76a9143f8870a5633e4fdac612fba47525fef082bbe96188ac', wallet=bob_wallet ), ]
def alice_utxo(alice_wallet): return [ Utxo( tx_id='6ecd66d88b1a976cde70ebbef1909edec5db80cff9b8b97024ea3805dbe28ab8', vout=1, value=0.78956946, tx_script='76a914812ff3e5afea281eb3dd7fce9b077e4ec6fba08b88ac', wallet=alice_wallet ), ]
def get_contract_utxo(self, wallet=None, secret=None, refund=False, contract=None): return Utxo( tx_id=self.transaction_address, vout=0, value=self.value, tx_script=self.vout.scriptPubKey.hex(), wallet=wallet, secret=secret, refund=refund, contract=contract, )
def get_utxo(cls, address: str, amount: float) -> Optional[list]: ''' Getting list of UTXO objects. Args: address (str): wallet address to look for UTXO amount (float): minimum value that should be satisfied in UTXO objects Returns: list, None: list of UTXO objects or None it there was not enough UTXO Example: >>> from clove.network import Litecoin >>> network = Litecoin() >>> network.get_utxo(address='LUAn5PWmsPavgz32mGkqsUuAKncftS37Jq', amount=0.01) [ Utxo(tx_id='0cd90567497823097d03464b4b2d08dd659f1c5621dd55e9540bc9bcd3e191ec', vout='0', value='0.00976168', tx_script='76a91485c0522f6e23beb11cc3d066cd20ed732648a4e688ac', wallet=None, secret=None, refund=False), # noqa: E501 Utxo(tx_id='a5c027027c695f403fe570850e35ffd44bb28479ecaaee039372015fe0aae7b2', vout='0', value='0.00097114', tx_script='76a91485c0522f6e23beb11cc3d066cd20ed732648a4e688ac', wallet=None, secret=None, refund=False) # noqa: E501 ] ''' data = clove_req_json( f'{cls.blockcypher_url()}/addrs/{address}' '?limit=2000&unspentOnly=true&includeScript=true&confirmations=6') if not data: logger.debug( f'Cannot find UTXO for address {address} ({cls.symbols[0]})') return unspent = data.get('txrefs', []) for output in unspent: output['value'] = int(output['value']) unspent = sorted(unspent, key=lambda k: k['value'], reverse=True) utxo = [] total = 0 for output in unspent: value = from_base_units(output['value']) utxo.append( Utxo( tx_id=output['tx_hash'], vout=output['tx_output_n'], value=value, tx_script=output['script'], )) total += value if total > amount: return utxo logger.debug(f'Cannot find enough UTXO\'s. Found %.8f from %.8f.', total, amount)
def get_utxo_from_api(network: str, address: str, amount: float, use_blockcypher: bool = False, testnet: bool = False, cryptoid_api_key: str = None) -> Optional[list]: from clove.network.bitcoin.utxo import Utxo if use_blockcypher: subnet = 'test3' if testnet else 'main' api_url = f'https://api.blockcypher.com/v1/{network}/{subnet}/addrs/{address}' \ f'?limit=2000&unspentOnly=true&includeScript=true&confirmations=6' unspent_key = 'txrefs' vout_key = 'tx_output_n' elif cryptoid_api_key is None: raise ValueError('API key for cryptoid is required to get UTXOs.') else: api_url = f'https://chainz.cryptoid.info/{network}/api.dws?q=unspent&key={cryptoid_api_key}&active={address}' unspent_key = 'unspent_outputs' vout_key = 'tx_ouput_n' data = clove_req_json(api_url) if data is None: logger.debug('Could not get UTXOs for address %s in %s network', address, network) return unspent = data.get(unspent_key, []) for output in unspent: output['value'] = int(output['value']) unspent = sorted(unspent, key=lambda k: k['value'], reverse=True) utxo = [] total = 0 for output in unspent: value = from_base_units(output['value']) utxo.append( Utxo( tx_id=output['tx_hash'], vout=output[vout_key], value=value, tx_script=output['script'], )) total += value if total > amount: return utxo logger.debug(f'Cannot find enough UTXO\'s. Found %.8f from %.8f.', total, amount)
def get_utxo(cls, address, amount): query = """ { getAddressTxs(_address: "%s") { nodes { voutsByTxId(condition: { spendingN: null }) { nodes { txId n value scriptPubKey } } } } } """ % (address) data = clove_req_json(f'{cls.api_url}/graphql', post_data={'query': query}) vouts = [] for node in data['data']['getAddressTxs']['nodes']: for vout in node['voutsByTxId']['nodes']: vouts.append(vout) unspent = sorted(vouts, key=lambda k: k['value'], reverse=True) utxo = [] total = 0 for output in unspent: value = float(output['value']) utxo.append( Utxo( tx_id=output['txId'], vout=output['n'], value=value, tx_script=output['scriptPubKey'], )) total += value if total > amount: return utxo logger.debug(f'Cannot find enough UTXO\'s. Found %.8f from %.8f.', total, amount)
def get_utxo(cls, address, amount): data = clove_req_json(f'{cls.api_url}/addrs/{address}/utxo') unspent = sorted(data, key=lambda k: k['satoshis'], reverse=True) utxo = [] total = 0 for output in unspent: value = from_base_units(output['satoshis']) utxo.append( Utxo( tx_id=output['txid'], vout=output['vout'], value=value, tx_script=output['scriptPubKey'], ) ) total += value if total > amount: return utxo logger.debug(f'Cannot find enough UTXO\'s. Found %.8f from %.8f.', total, amount)
def get_utxo(cls, address, amount): data = clove_req_json( f'https://mona.chainseeker.info/api/v1/utxos/{address}') unspent = sorted(data, key=lambda k: k['value'], reverse=True) utxo = [] total = 0 for output in unspent: value = from_base_units(output['value']) utxo.append( Utxo( tx_id=output['txid'], vout=output['vout'], value=value, tx_script=output['scriptPubKey']['hex'], )) total += value if total > amount: return utxo logger.debug(f'Cannot find enough UTXO\'s. Found %.8f from %.8f.', total, amount)
"vout": 1, "scriptPubKey": { "asm": "OP_DUP OP_HASH160 098671104a3dd5b8eb1559929221d946073a34ba OP_EQUALVERIFY OP_CHECKSIG", "hex": "76a9143804c5840717fb1c5c8ac0bd2726556a51e91fcd99ac", "type": "pubkeyhash", "address": "M8mXNKtwFoW765V8VEbhZ8TNCqywFr25in" }, "value": 15500105 }, ] expected_utxo = [ Utxo(tx_id= 'e0832ca854e4577cab20413013d6251c4a426022112d9ff222067bb5d8b6b723', vout=0, value=0.9000007, tx_script='76a9143804c5840717fb1c5c8ac0bd2726556a51e91fcd99ac'), Utxo(tx_id= '308b997d8583aa48a7b265246eb76e5d030495468bbb87989606aea769b03600', vout=1, value=0.15500105, tx_script='76a9143804c5840717fb1c5c8ac0bd2726556a51e91fcd99ac') ] expected_utxo_dicts = [utxo.__dict__ for utxo in expected_utxo] @mark.parametrize('network', networks) @patch('clove.utils.external_source.clove_req_json') @patch.dict('os.environ', {'CRYPTOID_API_KEY': 'test_api_key'}) def test_getting_utxo(json_response, network):