local userKey = KEYS[1]
local receiptType = tonumber(ARGV[1])
local amount = tonumber(ARGV[2])

if amount <= 0 then
  return -3
end

local balance = redis.call('GET', userKey)

if balance == false or balance == nil or balance == "null" then
  return -2
end

local balanceAmount = tonumber(balance)

if receiptType == 0 then
  return redis.call('INCRBY', userKey, amount)
end

if receiptType == 1 then
  if amount > balanceAmount then
    return -1
  end
  return redis.call('DECRBY', userKey, amount)
end

return -3
