Source code for pyircbot.modules.DogeRPC

#!/usr/bin/env python
"""
.. module:: DogeRPC
    :synopsis: Provides a service for interacting with dogecoind.

.. moduleauthor:: Dave Pedu <dave@davepedu.com>

"""

from pyircbot.modulebase import ModuleBase
from bitcoinrpc.authproxy import AuthServiceProxy


[docs]class DogeRPC(ModuleBase): def __init__(self, bot, moduleName): ModuleBase.__init__(self, bot, moduleName) self.services = ["dogerpc"] self.rpc = DogeController(self)
[docs] def getBal(self, acct): "Get a balance of a local address or an account " return self.getAcctBal(acct)
[docs] def getAcctAddr(self, acct): "Returns the address for an account. creates if necessary " self.rpc.ping() addrs = self.rpc.con.getaddressesbyaccount(acct) if len(addrs) == 0: return self.rpc.con.getnewaddress(acct) return addrs[0]
[docs] def getAcctBal(self, acct): "Returns an account's balance" self.rpc.ping() return float(self.rpc.con.getbalance(acct))
[docs] def canMove(self, fromAcct, toAcct, amount): "True or false if fromAcct can afford to give toAcct an amount of coins " balfrom = self.getAcctBal(fromAcct) return balfrom >= amount
[docs] def move(self, fromAcct, toAcct, amount): "Move coins from one account to another " self.rpc.ping() if self.canMove(fromAcct, toAcct, amount): return self.rpc.con.move(fromAcct, toAcct, amount) return False
[docs] def send(self, fromAcct, toAddr, amount): "Send coins to an external addr " self.rpc.ping() if self.canMove(fromAcct, toAddr, amount): return self.rpc.con.sendfrom(fromAcct, toAddr, amount) return False
[docs]class DogeController: "RPC instance control class" def __init__(self, master): self.config = master.config self.log = master.log self.con = None self.ping()
[docs] def ping(self): "Test connection and re-establish if necessary" if not self.con: self.connect() try: self.con.getinfo() except: self.connect()
[docs] def connect(self): "Connect to RPC endpoint" self.log.info("DogeRPC: Connecting to dogecoind") self.con = AuthServiceProxy("http://%s:%s@%s:%s" % (self.config["username"], self.config["password"], self.config["host"], self.config["port"])) self.con.getinfo() self.log.info("DogeRPC: Connected to %s:%s" % (self.config["host"], self.config["port"]))