fix integer overflow

This commit is contained in:
keyan 2022-02-26 15:42:38 -06:00
parent bc1c45e7bf
commit 3fb7ab9cd7
7 changed files with 88 additions and 12 deletions

View File

@ -532,7 +532,7 @@ export default {
const checkSats = async () => { const checkSats = async () => {
// check if the user has the funds to run for the first minute // check if the user has the funds to run for the first minute
const minuteMsats = maxBid * 1000 / 30 / 24 / 60 const minuteMsats = maxBid / 1296
const user = models.user.findUnique({ where: { id: me.id } }) const user = models.user.findUnique({ where: { id: me.id } })
if (user.msats < minuteMsats) { if (user.msats < minuteMsats) {
throw new UserInputError('insufficient funds') throw new UserInputError('insufficient funds')

View File

@ -159,7 +159,7 @@ export default {
if (me?.id !== user.id) { if (me?.id !== user.id) {
return 0 return 0
} }
return Math.floor(user.msats / 1000) return Math.floor(user.msats / 1000.0)
}, },
bio: async (user, args, { models }) => { bio: async (user, args, { models }) => {
return getItem(user, { id: user.bioId }, { models }) return getItem(user, { id: user.bioId }, { models })

View File

@ -46,7 +46,6 @@ export default gql`
tipDefault: Int! tipDefault: Int!
bio: Item bio: Item
sats: Int! sats: Int!
msats: Int!
upvotePopover: Boolean! upvotePopover: Boolean!
tipPopover: Boolean! tipPopover: Boolean!
} }

View File

@ -0,0 +1,33 @@
-- AlterTable
ALTER TABLE "Item" RENAME COLUMN "noSatsAt" TO "statusUpdatedAt";
-- charge the user for the auction item
CREATE OR REPLACE FUNCTION run_auction(item_id INTEGER) RETURNS void AS $$
DECLARE
bid INTEGER;
user_id INTEGER;
user_msats INTEGER;
item_status "Status";
BEGIN
PERFORM ASSERT_SERIALIZED();
-- extract data we need
SELECT ("maxBid" * 1000 / 30 / 24 / 60), "userId", status INTO bid, user_id, item_status FROM "Item" WHERE id = item_id;
SELECT msats INTO user_msats FROM users WHERE id = user_id;
-- check if user wallet has enough sats
IF bid > user_msats THEN
-- if not, set status = NOSATS and statusUpdatedAt to now_utc if not already set
IF item_status <> 'NOSATS' THEN
UPDATE "Item" SET status = 'NOSATS', "statusUpdatedAt" = now_utc() WHERE id = item_id;
END IF;
ELSE
-- if so, deduct from user
UPDATE users SET msats = msats - bid WHERE id = user_id;
-- update item status = ACTIVE and statusUpdatedAt = null if NOSATS
IF item_status = 'NOSATS' THEN
UPDATE "Item" SET status = 'ACTIVE', "statusUpdatedAt" = now_utc() WHERE id = item_id;
END IF;
END IF;
END;
$$ LANGUAGE plpgsql;

View File

@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "users" ALTER COLUMN "msats" SET DEFAULT 0,
ALTER COLUMN "msats" SET DATA TYPE BIGINT;

View File

@ -0,0 +1,41 @@
/*
Warnings:
- You are about to alter the column `msats` on the `users` table. The data in that column could be lost. The data in that column will be cast from `BigInt` to `Integer`.
*/
-- AlterTable
ALTER TABLE "users" ALTER COLUMN "msats" SET DEFAULT 0,
ALTER COLUMN "msats" SET DATA TYPE INTEGER;
-- charge the user for the auction item
CREATE OR REPLACE FUNCTION run_auction(item_id INTEGER) RETURNS void AS $$
DECLARE
bid INTEGER;
user_id INTEGER;
user_msats INTEGER;
item_status "Status";
BEGIN
PERFORM ASSERT_SERIALIZED();
-- extract data we need
SELECT ("maxBid" * 5 / 216), "userId", status INTO bid, user_id, item_status FROM "Item" WHERE id = item_id;
SELECT msats INTO user_msats FROM users WHERE id = user_id;
-- check if user wallet has enough sats
IF bid > user_msats THEN
-- if not, set status = NOSATS and statusUpdatedAt to now_utc if not already set
IF item_status <> 'NOSATS' THEN
UPDATE "Item" SET status = 'NOSATS', "statusUpdatedAt" = now_utc() WHERE id = item_id;
END IF;
ELSE
-- if so, deduct from user
UPDATE users SET msats = msats - bid WHERE id = user_id;
-- update item status = ACTIVE and statusUpdatedAt = null if NOSATS
IF item_status = 'NOSATS' THEN
UPDATE "Item" SET status = 'ACTIVE', "statusUpdatedAt" = now_utc() WHERE id = item_id;
END IF;
END IF;
END;
$$ LANGUAGE plpgsql;

View File

@ -113,7 +113,7 @@ model Item {
maxSalary Int? maxSalary Int?
maxBid Int? maxBid Int?
status Status @default(ACTIVE) status Status @default(ACTIVE)
noSatsAt DateTime? statusUpdatedAt DateTime?
location String? location String?
latitude Float? latitude Float?
longitude Float? longitude Float?