Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gitchand opened this issue Dec 27, 2019 · 5 comments
Open

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

gitchand opened this issue Dec 27, 2019 · 5 comments

Comments

@gitchand
Copy link

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' ".

@richardkiss
Copy link
Owner

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

@gitchand
Copy link
Author

gitchand commented Jan 6, 2020

`
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

`

@richardkiss
Copy link
Owner

richardkiss commented Jan 6, 2020

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.

@gitchand
Copy link
Author

gitchand commented Jan 6, 2020

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

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

@richardkiss
Copy link
Owner

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 f611038 which will now raise the correct exception.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants