diff --git a/tipbot.py b/tipbot.py index 4333da5..74bfcf4 100644 --- a/tipbot.py +++ b/tipbot.py @@ -137,7 +137,7 @@ def AddBalance(link,cmd): link.send('usage: !addbalance [] ') return try: - units = long(float(amount)*coinspecs.atomic_units) + units = StringToUnits(amount) except Exception,e: log_error('AddBalance: error converting amount: %s' % str(e)) link.send('usage: !addbalance [] ') diff --git a/tipbot/betutils.py b/tipbot/betutils.py index 8c297c3..8ef597f 100644 --- a/tipbot/betutils.py +++ b/tipbot/betutils.py @@ -38,10 +38,10 @@ def IsBetValid(link,amount,minbet,maxbet,potential_loss,max_loss,max_loss_ratio) if banned: return False, reason try: + units = StringToUnits(amount) amount = float(amount) except Exception,e: return False, "Invalid amount" - units=long(amount*coinspecs.atomic_units) if units <= 0: return False, "Invalid amount" if maxbet != None and amount > maxbet: @@ -315,10 +315,9 @@ def ReserveBalance(link,cmd): rbal=GetParam(cmd,1) if rbal: try: - rbal=float(cmd[1]) + rbal=StringToUnits(cmd[1]) if rbal < 0: raise RuntimeError('negative balance') - rbal = long(rbal * coinspecs.atomic_units) except Exception,e: log_error('SetReserveBalance: invalid balance: %s' % str(e)) link.send("Invalid balance") diff --git a/tipbot/modules/blackjack.py b/tipbot/modules/blackjack.py index 0b12b5c..ae34702 100644 --- a/tipbot/modules/blackjack.py +++ b/tipbot/modules/blackjack.py @@ -555,7 +555,7 @@ def Blackjack(link,cmd): return try: amount=float(cmd[1]) - units=long(amount*coinspecs.atomic_units) + units=StringToUnits(cmd[1]) except Exception,e: link.send("%s: usage: !blackjack amount" % link.user.nick) return diff --git a/tipbot/modules/bookie.py b/tipbot/modules/bookie.py index e587079..f055cc8 100644 --- a/tipbot/modules/bookie.py +++ b/tipbot/modules/bookie.py @@ -273,11 +273,10 @@ def Bet(link,cmd): link.send('usage: !bet [] ') return try: - amount = float(amount) + units = StringToUnits(amount) except Exception,e: link.send('usage: !bet [] ') return - units = long(amount*coinspecs.atomic_units) if units <= 0: link.send("Invalid amount") return diff --git a/tipbot/modules/dice.py b/tipbot/modules/dice.py index 636da0f..eeae8a3 100644 --- a/tipbot/modules/dice.py +++ b/tipbot/modules/dice.py @@ -49,7 +49,7 @@ def Dice(link,cmd): identity=link.identity() try: amount=float(cmd[1]) - units=long(amount*coinspecs.atomic_units) + units=StringToUnits(cmd[1]) multiplier = float(cmd[2]) overunder=GetParam(cmd,3) except Exception,e: diff --git a/tipbot/modules/tipping.py b/tipbot/modules/tipping.py index dd4d994..6257efe 100644 --- a/tipbot/modules/tipping.py +++ b/tipbot/modules/tipping.py @@ -63,11 +63,10 @@ def Tip(link,cmd): identity=link.identity() try: who=cmd[1] - amount=float(cmd[2]) + units=StringToUnits(cmd[2]) except Exception,e: link.send("Usage: tip nick amount") return - units=long(amount*coinspecs.atomic_units) if units <= 0: link.send("Invalid amount") return @@ -105,6 +104,7 @@ def Rain(link,cmd): try: amount=float(cmd[1]) + units = StringToUnits(cmd[1]) except Exception,e: link.send("Usage: rain amount [users]") return @@ -122,7 +122,6 @@ def Rain(link,cmd): if users != None and users <= 0: link.send("Usage: rain amount [users]") return - units = long(amount * coinspecs.atomic_units) try: account = GetAccount(identity) @@ -202,6 +201,7 @@ def RainActive(link,cmd): link.send("usage: !rainactive []") return try: + units=StringToUnits(amount) amount=float(amount) if amount <= 0: raise RuntimeError("") @@ -227,8 +227,6 @@ def RainActive(link,cmd): else: minfrac = 0 - units = long(amount * coinspecs.atomic_units) - try: account = GetAccount(link) balance = redis_hget("balances",account) diff --git a/tipbot/modules/withdraw.py b/tipbot/modules/withdraw.py index 73a3393..8737c1d 100644 --- a/tipbot/modules/withdraw.py +++ b/tipbot/modules/withdraw.py @@ -61,10 +61,9 @@ def Withdraw(link,cmd): amount = GetParam(cmd,2) if amount: try: - famount=float(amount) - if (famount < 0): + amount = StringToUnits(amount) + if (amount <= 0): raise RuntimeError("") - amount = long(famount * coinspecs.atomic_units) amount += local_withdraw_fee except Exception,e: link.send("Invalid amount") diff --git a/tipbot/utils.py b/tipbot/utils.py index 48106af..da1f350 100644 --- a/tipbot/utils.py +++ b/tipbot/utils.py @@ -177,6 +177,13 @@ def TimeToString(seconds): return "%.2f millenia" % (seconds / (3600*24*365.25 * 100)) return "like, forever, dude" +def StringToUnits(s): + try: + return long(Decimal(s)*long(coinspecs.atomic_units)) + except Exception,e: + log_error('Failed to convert %s to units: %s' % (s,str(e))) + raise + def SendJSONRPCCommand(host,port,method,params): try: http = httplib.HTTPConnection(host,port,timeout=20)