pycoin icon indicating copy to clipboard operation
pycoin copied to clipboard

AttributeError: 'Spendable' object has no attribute 'address'

Open gitchand opened this issue 5 years ago • 5 comments

I used "create_signed_tx" from "network.tx_utils.create_signed_tx" to generate a groestlcoin transaction and gettting "AttributeError: 'Spendable' object has no attribute 'address' ".

gitchand avatar Dec 27 '19 12:12 gitchand

Sorry for delay, I was on vacation for a bit. Can you give me a short program that reproduces the problem?

richardkiss avatar Jan 06 '20 06:01 richardkiss

` from pycoin.networks.registry import network_for_netcode from pycoin.encoding.hexbytes import h2b, h2b_rev from pycoin.coins.bitcoin.Spendable import Spendable import requests

def create_raw_transaction(sender_address, receiver_address, amount, wif, fee=0, netcode='GRS', testnet=False): _netcodes = {'LTC': 'litecoin', 'DOGE': 'dogecoin', 'DASH': 'dash', 'GRS': 'groestlcoin', 'BTC': 'bitcoin'} url = "https://api.blockchair.com/%s/dashboards/address/%s" r = requests.get(url % (_netcodes[netcode], sender_address)).json()

scrpt = r['data'][sender_address]['address']['script_hex']
print("scrpt ", scrpt)
spendables = []
list_spend = r['data'][sender_address]['utxo']
total_amount = 0
if len(list_spend) == 0:
    raise Exception("No spendable outputs found")
spend_list = []
for u in r['data'][sender_address]['utxo']:
    coin_value = u['value']
    script = scrpt
    previous_hash = u['transaction_hash']
    previous_index = u['index']
    d = {
        'coin_value': coin_value,
        'script': script,
        'previous_hash': previous_hash,
        'previous_index': previous_index
    }
    spend_list.append(d)
unspents = sorted(spend_list, key=lambda d: d['coin_value'], reverse=True)
for u in unspents:
    coin_value = u['coin_value']
    script = h2b(u["script"])
    previous_hash = h2b_rev(u["previous_hash"])
    previous_index = u["previous_index"]
    spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
    total_amount = total_amount + coin_value
    if total_amount >= amount:
        break
res = [spendables, total_amount]
spend = res[0]
total_amount = res[1]
change = total_amount - amount - fee
pay = [(receiver_address, amount)]
if change > 0:
    pay.append((sender_address, change))

network = network_for_netcode(netcode)
# trx = network.tx_utils.create_tx(spend, pay, fee=fee)
# network.tx_utils.sign_tx(trx, [wif, ])
trx = network.tx_utils.create_signed_tx(spendables=spend, payables=pay, wifs=[wif], fee=fee)
print(trx, trx.as_hex())
return trx

`

gitchand avatar Jan 06 '20 06:01 gitchand

Try using network.tx.Spendable instead of pycoin.coins.bitcoin.Spendable. That may not completely fix it.

Note that when I run this code, I don't get the error, as create_raw_transaction is never called. Can you include the code that invokes it? Leaving out private keys, of course.

richardkiss avatar Jan 06 '20 07:01 richardkiss

tx_id = create_raw_transaction("1GtqAR9y5KuCEhB3uVZL8wosUHUQoYVhvh", "1GtqAR9y5KuCEhB3uVZL8wosUHUQoYVhvh", 110000, "*****", netcode="BTC")

Add the above line of code at the end and then run that code.

gitchand avatar Jan 06 '20 07:01 gitchand

It looks like the problem is that WIF that you posted (which I removed just in case real money ends up backing it) does not correspond to the addresses. There is also a bug fixed in f6110388c0c56db9edf31358e50546add35f98df which will now raise the correct exception.

Let me know if this is enough to resolve your issue or if you need more guidance.

richardkiss avatar Jan 06 '20 07:01 richardkiss