2021-05-13 01:51:37 +00:00
|
|
|
CREATE OR REPLACE FUNCTION create_withdrawl(lnd_id TEXT, invoice TEXT, msats_amount INTEGER, msats_max_fee INTEGER, username TEXT)
|
|
|
|
RETURNS "Withdrawl"
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
|
|
|
user_id INTEGER;
|
|
|
|
user_msats INTEGER;
|
|
|
|
withdrawl "Withdrawl";
|
|
|
|
BEGIN
|
2021-05-20 01:09:32 +00:00
|
|
|
PERFORM ASSERT_SERIALIZED();
|
|
|
|
|
2021-05-13 01:51:37 +00:00
|
|
|
SELECT msats, id INTO user_msats, user_id FROM users WHERE name = username;
|
|
|
|
IF (msats_amount + msats_max_fee) > user_msats THEN
|
|
|
|
RAISE EXCEPTION 'SN_INSUFFICIENT_FUNDS';
|
|
|
|
END IF;
|
|
|
|
|
2021-05-20 01:09:32 +00:00
|
|
|
INSERT INTO "Withdrawl" (hash, bolt11, "msatsPaying", "msatsFeePaying", "userId", created_at, updated_at)
|
|
|
|
VALUES (lnd_id, invoice, msats_amount, msats_max_fee, user_id, now_utc(), now_utc()) RETURNING * INTO withdrawl;
|
2021-05-13 01:51:37 +00:00
|
|
|
|
|
|
|
UPDATE users SET msats = msats - msats_amount - msats_max_fee WHERE id = user_id;
|
|
|
|
|
|
|
|
RETURN withdrawl;
|
|
|
|
END;
|
|
|
|
$$;
|
|
|
|
|
2021-05-13 21:19:51 +00:00
|
|
|
CREATE OR REPLACE FUNCTION confirm_withdrawl(wid INTEGER, msats_paid INTEGER, msats_fee_paid INTEGER)
|
2021-05-13 01:51:37 +00:00
|
|
|
RETURNS INTEGER
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
2021-05-13 21:19:51 +00:00
|
|
|
msats_fee_paying INTEGER;
|
|
|
|
user_id INTEGER;
|
2021-05-13 01:51:37 +00:00
|
|
|
BEGIN
|
2021-05-20 01:09:32 +00:00
|
|
|
PERFORM ASSERT_SERIALIZED();
|
|
|
|
|
2021-05-13 21:19:51 +00:00
|
|
|
IF EXISTS (SELECT 1 FROM "Withdrawl" WHERE id = wid AND status IS NULL) THEN
|
2021-05-20 01:09:32 +00:00
|
|
|
UPDATE "Withdrawl"
|
|
|
|
SET status = 'CONFIRMED', "msatsPaid" = msats_paid,
|
|
|
|
"msatsFeePaid" = msats_fee_paid, updated_at = now_utc()
|
|
|
|
WHERE id = wid;
|
2021-05-13 21:19:51 +00:00
|
|
|
SELECT "msatsFeePaying", "userId" INTO msats_fee_paying, user_id FROM "Withdrawl" WHERE id = wid;
|
|
|
|
UPDATE users SET msats = msats + (msats_fee_paying - msats_fee_paid) WHERE id = user_id;
|
|
|
|
END IF;
|
2021-05-13 01:51:37 +00:00
|
|
|
|
2021-05-13 21:19:51 +00:00
|
|
|
RETURN 0;
|
2021-05-13 01:51:37 +00:00
|
|
|
END;
|
|
|
|
$$;
|
|
|
|
|
2021-05-13 21:19:51 +00:00
|
|
|
CREATE OR REPLACE FUNCTION reverse_withdrawl(wid INTEGER, wstatus "WithdrawlStatus")
|
2021-05-13 01:51:37 +00:00
|
|
|
RETURNS INTEGER
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
2021-05-13 21:19:51 +00:00
|
|
|
msats_fee_paying INTEGER;
|
|
|
|
msats_paying INTEGER;
|
|
|
|
user_id INTEGER;
|
2021-05-13 01:51:37 +00:00
|
|
|
BEGIN
|
2021-05-20 01:09:32 +00:00
|
|
|
PERFORM ASSERT_SERIALIZED();
|
|
|
|
|
2021-05-13 21:19:51 +00:00
|
|
|
IF EXISTS (SELECT 1 FROM "Withdrawl" WHERE id = wid AND status IS NULL) THEN
|
2021-05-20 01:09:32 +00:00
|
|
|
UPDATE "Withdrawl" SET status = wstatus, updated_at = now_utc() WHERE id = wid;
|
2021-05-13 21:19:51 +00:00
|
|
|
SELECT "msatsPaying", "msatsFeePaying", "userId" INTO msats_paying, msats_fee_paying, user_id FROM "Withdrawl" WHERE id = wid;
|
|
|
|
UPDATE users SET msats = msats + msats_paying + msats_fee_paying WHERE id = user_id;
|
|
|
|
END IF;
|
|
|
|
RETURN 0;
|
2021-05-13 01:51:37 +00:00
|
|
|
END;
|
|
|
|
$$;
|