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>') link.send('usage: !addbalance [<nick>] <amount>')
return return
try: try:
units = long(float(amount)*coinspecs.atomic_units) units = StringToUnits(amount)
except Exception,e: except Exception,e:
log_error('AddBalance: error converting amount: %s' % str(e)) log_error('AddBalance: error converting amount: %s' % str(e))
link.send('usage: !addbalance [<nick>] <amount>') 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: if banned:
return False, reason return False, reason
try: try:
units = StringToUnits(amount)
amount = float(amount) amount = float(amount)
except Exception,e: except Exception,e:
return False, "Invalid amount" return False, "Invalid amount"
units=long(amount*coinspecs.atomic_units)
if units <= 0: if units <= 0:
return False, "Invalid amount" return False, "Invalid amount"
if maxbet != None and amount > maxbet: if maxbet != None and amount > maxbet:
@ -315,10 +315,9 @@ def ReserveBalance(link,cmd):
rbal=GetParam(cmd,1) rbal=GetParam(cmd,1)
if rbal: if rbal:
try: try:
rbal=float(cmd[1]) rbal=StringToUnits(cmd[1])
if rbal < 0: if rbal < 0:
raise RuntimeError('negative balance') raise RuntimeError('negative balance')
rbal = long(rbal * coinspecs.atomic_units)
except Exception,e: except Exception,e:
log_error('SetReserveBalance: invalid balance: %s' % str(e)) log_error('SetReserveBalance: invalid balance: %s' % str(e))
link.send("Invalid balance") link.send("Invalid balance")

View File

@ -555,7 +555,7 @@ def Blackjack(link,cmd):
return return
try: try:
amount=float(cmd[1]) amount=float(cmd[1])
units=long(amount*coinspecs.atomic_units) units=StringToUnits(cmd[1])
except Exception,e: except Exception,e:
link.send("%s: usage: !blackjack amount" % link.user.nick) link.send("%s: usage: !blackjack amount" % link.user.nick)
return return

View File

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

View File

@ -49,7 +49,7 @@ def Dice(link,cmd):
identity=link.identity() identity=link.identity()
try: try:
amount=float(cmd[1]) amount=float(cmd[1])
units=long(amount*coinspecs.atomic_units) units=StringToUnits(cmd[1])
multiplier = float(cmd[2]) multiplier = float(cmd[2])
overunder=GetParam(cmd,3) overunder=GetParam(cmd,3)
except Exception,e: except Exception,e:

View File

@ -63,11 +63,10 @@ def Tip(link,cmd):
identity=link.identity() identity=link.identity()
try: try:
who=cmd[1] who=cmd[1]
amount=float(cmd[2]) units=StringToUnits(cmd[2])
except Exception,e: except Exception,e:
link.send("Usage: tip nick amount") link.send("Usage: tip nick amount")
return return
units=long(amount*coinspecs.atomic_units)
if units <= 0: if units <= 0:
link.send("Invalid amount") link.send("Invalid amount")
return return
@ -105,6 +104,7 @@ def Rain(link,cmd):
try: try:
amount=float(cmd[1]) amount=float(cmd[1])
units = StringToUnits(cmd[1])
except Exception,e: except Exception,e:
link.send("Usage: rain amount [users]") link.send("Usage: rain amount [users]")
return return
@ -122,7 +122,6 @@ def Rain(link,cmd):
if users != None and users <= 0: if users != None and users <= 0:
link.send("Usage: rain amount [users]") link.send("Usage: rain amount [users]")
return return
units = long(amount * coinspecs.atomic_units)
try: try:
account = GetAccount(identity) account = GetAccount(identity)
@ -202,6 +201,7 @@ def RainActive(link,cmd):
link.send("usage: !rainactive <amount> <hours> [<minfrac>]") link.send("usage: !rainactive <amount> <hours> [<minfrac>]")
return return
try: try:
units=StringToUnits(amount)
amount=float(amount) amount=float(amount)
if amount <= 0: if amount <= 0:
raise RuntimeError("") raise RuntimeError("")
@ -227,8 +227,6 @@ def RainActive(link,cmd):
else: else:
minfrac = 0 minfrac = 0
units = long(amount * coinspecs.atomic_units)
try: try:
account = GetAccount(link) account = GetAccount(link)
balance = redis_hget("balances",account) balance = redis_hget("balances",account)

View File

@ -61,10 +61,9 @@ def Withdraw(link,cmd):
amount = GetParam(cmd,2) amount = GetParam(cmd,2)
if amount: if amount:
try: try:
famount=float(amount) amount = StringToUnits(amount)
if (famount < 0): if (amount <= 0):
raise RuntimeError("") raise RuntimeError("")
amount = long(famount * coinspecs.atomic_units)
amount += local_withdraw_fee amount += local_withdraw_fee
except Exception,e: except Exception,e:
link.send("Invalid amount") link.send("Invalid amount")

View File

@ -177,6 +177,13 @@ def TimeToString(seconds):
return "%.2f millenia" % (seconds / (3600*24*365.25 * 100)) return "%.2f millenia" % (seconds / (3600*24*365.25 * 100))
return "like, forever, dude" 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): def SendJSONRPCCommand(host,port,method,params):
try: try:
http = httplib.HTTPConnection(host,port,timeout=20) http = httplib.HTTPConnection(host,port,timeout=20)