Use decimal functions to read amounts/units from string inputs

This commit is contained in:
moneromooo 2015-02-02 12:22:20 +00:00
parent 8a81397a84
commit d455985552
8 changed files with 18 additions and 16 deletions

View file

@ -137,7 +137,7 @@ def AddBalance(link,cmd):
link.send('usage: !addbalance [<nick>] <amount>')
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 [<nick>] <amount>')

View file

@ -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")

View file

@ -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

View file

@ -273,11 +273,10 @@ def Bet(link,cmd):
link.send('usage: !bet [<event name>] <outcome> <amount>')
return
try:
amount = float(amount)
units = StringToUnits(amount)
except Exception,e:
link.send('usage: !bet [<event name>] <outcome> <amount>')
return
units = long(amount*coinspecs.atomic_units)
if units <= 0:
link.send("Invalid amount")
return

View file

@ -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:

View file

@ -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 <amount> <hours> [<minfrac>]")
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)

View file

@ -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")

View file

@ -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)