Bitcoin Forum
May 11, 2024, 10:50:01 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Python wrapper for bitcoind  (Read 10685 times)
db (OP)
Sr. Member
****
Offline Offline

Activity: 279
Merit: 261



View Profile
July 08, 2010, 11:50:43 PM
 #1

While creating the Bitcoin automation for mullvad.net I made a python wrapper around bitcoind. Should anyone else find such a thing useful it is available here:

https://www.mullvad.net/download/bitcoin.py

Usage example:

>>> import bitcoin
>>> bitcoin.getaddressesbylabel("foo")
['16au2i9ny5pnwCPnoekov2hHMhtS7qcFjR', '19BkB63mo3NQRPNeHkc3bWnKGBU7oL8Dp7']
>>> bitcoin.getreceivedbylabel("Your Address")
6.0
>>> bitcoin.listreceivedbyaddress()
{'16au2i9ny5pnwCPnoekov2hHMhtS7qcFjR': {'amount': 2000.0, 'label': 'foo', 'confirmations': 226, 'address': '16au2i9ny5pnwCPnoekov2hHMhtS7qcFjR'}, '1FLD8wCNNBh2wg2mnGjD9ogoLaRxpSz7TW': {'amount': 6.0, 'label': 'Your Address', 'confirmations': 343, 'address': '1FLD8wCNNBh2wg2mnGjD9ogoLaRxpSz7TW'}}
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1234.5)
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bitcoin.py", line 73, in sendtoaddress
    _cmd("sendtoaddress %s %d %s %s" % (bitcoinaddress, amount, comment, comment_to))
  File "bitcoin.py", line 86, in _cmd
    return _parse(_cmd_raw(args))[0]
  File "bitcoin.py", line 92, in _cmd_raw
    raise BitcoinError(errors)
bitcoin.BitcoinError: error: Insufficient funds

>>>

1715467801
Hero Member
*
Offline Offline

Posts: 1715467801

View Profile Personal Message (Offline)

Ignore
1715467801
Reply with quote  #2

1715467801
Report to moderator
1715467801
Hero Member
*
Offline Offline

Posts: 1715467801

View Profile Personal Message (Offline)

Ignore
1715467801
Reply with quote  #2

1715467801
Report to moderator
1715467801
Hero Member
*
Offline Offline

Posts: 1715467801

View Profile Personal Message (Offline)

Ignore
1715467801
Reply with quote  #2

1715467801
Report to moderator
You can see the statistics of your reports to moderators on the "Report to moderator" pages.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
NewLibertyStandard
Sr. Member
****
Offline Offline

Activity: 252
Merit: 268



View Profile WWW
July 09, 2010, 03:37:21 AM
 #2

That's great! Thanks db!

Treazant: A Fullever Rewarding Bitcoin - Backup Your Wallet TODAY to Double Your Money! - Dual Currency Donation Address: 1Dnvwj3hAGSwFPMnkJZvi3KnaqksRPa74p
phelix
Legendary
*
Offline Offline

Activity: 1708
Merit: 1020



View Profile
August 14, 2011, 09:45:28 AM
 #3

That's great! Thanks db!
+1
etotheipi
Legendary
*
expert
Offline Offline

Activity: 1428
Merit: 1093


Core Armory Developer


View Profile WWW
August 14, 2011, 09:07:35 PM
 #4

Looks great, db.

Just a minor recommendation: given the ease with which you can catch errors in python, you might consider an except(BitcoinError) clause to gracefully fail when you have insufficient funds, or other error.   Your original post showed this:

Code:
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bitcoin.py", line 73, in sendtoaddress
    _cmd("sendtoaddress %s %d %s %s" % (bitcoinaddress, amount, comment, comment_to))
  File "bitcoin.py", line 86, in _cmd
    return _parse(_cmd_raw(args))[0]
  File "bitcoin.py", line 92, in _cmd_raw
    raise BitcoinError(errors)
bitcoin.BitcoinError: error: Insufficient funds

It should be pretty easy to produce something like this, instead:

Code:
>>> bitcoin.sendtoaddress("1Nsq3itZULUZjtZGcjNrtZtwT8aMsHu1R1", 1000000)
***ERROR:  Insufficient funds***
   Attempted Transaction:  1000000
   Available Balance:       922100

Or whatever info/format-string you want, but those Traceback messages tend to scare users.

Founder and CEO of Armory Technologies, Inc.
Armory Bitcoin Wallet: Bringing cold storage to the average user!
Only use Armory software signed by the Armory Offline Signing Key (0x98832223)

Please donate to the Armory project by clicking here!    (or donate directly via 1QBDLYTDFHHZAABYSKGKPWKLSXZWCCJQBX -- yes, it's a real address!)
phelix
Legendary
*
Offline Offline

Activity: 1708
Merit: 1020



View Profile
August 14, 2011, 11:16:55 PM
 #5

[...]
Or whatever info/format-string you want, but those Traceback messages tend to scare users.

lol, for me it's the other way round.

it may be true but for me tracebacks are much more useful. custom error messages make finding bugs much harder and it can be a pain to reinstall the tracebacks.

etotheipi
Legendary
*
expert
Offline Offline

Activity: 1428
Merit: 1093


Core Armory Developer


View Profile WWW
August 15, 2011, 12:13:46 AM
 #6

I'm differentiating users from developers, here.  I recognize the Tracebacks are useful for developers, but as a user, I don't plan to be debugging his code.  Plus, this isn't the result of a bug--it's a legitimate validation failure response.   Not a big deal, it was just a recommendation...


Founder and CEO of Armory Technologies, Inc.
Armory Bitcoin Wallet: Bringing cold storage to the average user!
Only use Armory software signed by the Armory Offline Signing Key (0x98832223)

Please donate to the Armory project by clicking here!    (or donate directly via 1QBDLYTDFHHZAABYSKGKPWKLSXZWCCJQBX -- yes, it's a real address!)
phelix
Legendary
*
Offline Offline

Activity: 1708
Merit: 1020



View Profile
April 08, 2012, 03:39:36 PM
 #7

fyi: namecoind wrapper forked: http://dot-bit.org/forum/viewtopic.php?f=2&t=482
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!