pyircbot/pyircbot/modules/DogeRPC.py

81 lines
2.5 KiB
Python
Raw Normal View History

2014-01-07 10:22:21 -08:00
#!/usr/bin/env python
2014-10-02 18:14:42 -07:00
"""
.. module:: DogeRPC
:synopsis: Provides a service for interacting with dogecoind.
2014-10-02 18:14:42 -07:00
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
2017-01-01 14:59:01 -08:00
from pyircbot.modulebase import ModuleBase
2014-01-07 10:22:21 -08:00
from bitcoinrpc.authproxy import AuthServiceProxy
2017-01-01 14:59:01 -08:00
2014-01-07 10:22:21 -08:00
class DogeRPC(ModuleBase):
def __init__(self, bot, moduleName):
2017-01-01 14:59:01 -08:00
ModuleBase.__init__(self, bot, moduleName)
self.services = ["dogerpc"]
self.rpc = DogeController(self)
2017-01-01 14:59:01 -08:00
def getBal(self, acct):
"Get a balance of a local address or an account "
return self.getAcctBal(acct)
2017-01-01 14:59:01 -08:00
def getAcctAddr(self, acct):
"Returns the address for an account. creates if necessary "
self.rpc.ping()
addrs = self.rpc.con.getaddressesbyaccount(acct)
2017-01-01 14:59:01 -08:00
if len(addrs) == 0:
return self.rpc.con.getnewaddress(acct)
return addrs[0]
2017-01-01 14:59:01 -08:00
def getAcctBal(self, acct):
"Returns an account's balance"
self.rpc.ping()
return float(self.rpc.con.getbalance(acct))
2017-01-01 14:59:01 -08:00
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
2017-01-01 14:59:01 -08:00
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
2017-01-01 14:59:01 -08:00
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
2014-01-07 10:22:21 -08:00
2017-01-01 14:59:01 -08:00
2014-01-07 10:22:21 -08:00
class DogeController:
"RPC instance control class"
def __init__(self, master):
self.config = master.config
self.log = master.log
self.con = None
self.ping()
2017-01-01 14:59:01 -08:00
def ping(self):
"Test connection and re-establish if necessary"
if not self.con:
self.connect()
try:
self.con.getinfo()
except:
self.connect()
2017-01-01 14:59:01 -08:00
def connect(self):
"Connect to RPC endpoint"
2016-11-06 15:00:15 -08:00
self.log.info("DogeRPC: Connecting to dogecoind")
2017-01-01 14:59:01 -08:00
self.con = AuthServiceProxy("http://%s:%s@%s:%s" % (self.config["username"], self.config["password"],
self.config["host"], self.config["port"]))
self.con.getinfo()
2016-11-06 15:00:15 -08:00
self.log.info("DogeRPC: Connected to %s:%s" % (self.config["host"], self.config["port"]))