Skip to content

Commit e7d94e2

Browse files
Транзакции p2p
1 parent f0ed978 commit e7d94e2

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

databases/datafarm/src/data/init.sql

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ DECLARE
7070
BEGIN
7171
FOREACH i IN ARRAY symbol_list
7272
LOOP
73-
INSERT INTO market.currencies(symbol) VALUES(CONCAT(i, 'usdt'));
73+
INSERT INTO market.currencies(symbol) VALUES(UPPER(CONCAT(i, 'usdt')));
7474
END LOOP;
7575
END $$;
7676

@@ -114,6 +114,37 @@ CREATE TABLE profile.portfolios
114114
);
115115

116116

117+
-- Тикеры для p2p-торговли
118+
CREATE TABLE p2p.exchange_currencies
119+
(
120+
ticker VARCHAR(6) PRIMARY KEY
121+
);
122+
123+
124+
-- Наполнение таблицы тикерами
125+
DO $$
126+
DECLARE
127+
currency_list VARCHAR[] := ARRAY[
128+
'btc', 'usdt'
129+
];
130+
i VARCHAR;
131+
BEGIN
132+
FOREACH i IN ARRAY currency_list
133+
LOOP
134+
INSERT INTO p2p.exchange_currencies(ticker) VALUES(UPPER(i));
135+
END LOOP;
136+
END $$;
137+
138+
139+
-- Счет для p2p-торговли
140+
CREATE TABLE p2p.wallets
141+
(
142+
fk_user_owner service.valid_email REFERENCES profile.users(email),
143+
fk_currency VARCHAR(6) REFERENCES p2p.exchange_currencies(ticker),
144+
balance NUMERIC DEFAULT 0
145+
);
146+
147+
117148
-- Эмитенты/платежные системы
118149
CREATE TABLE p2p.emitents
119150
(
@@ -164,7 +195,6 @@ CREATE TABLE p2p.offers
164195
(
165196
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
166197
action_type service.valid_action_type DEFAULT 'BUY',
167-
currency VARCHAR(4) CHECK (currency IN ('usdt', 'btc', 'eth', 'xrp')) NOT NULL,
168198
quantity NUMERIC NOT NULL,
169199
limit_min NUMERIC DEFAULT 0,
170200
limit_max NUMERIC,
@@ -173,6 +203,7 @@ CREATE TABLE p2p.offers
173203
offer_status VARCHAR(16) CHECK (offer_status IN (
174204
'ACTIVE', 'AWAITING PAYMENT', 'CLOSED'
175205
)) DEFAULT 'ACTIVE',
206+
fk_currency VARCHAR(6) REFERENCES p2p.exchange_currencies(ticker),
176207
fk_user_creator service.valid_email REFERENCES profile.users(email)
177208
);
178209

@@ -408,7 +439,7 @@ CREATE OR REPLACE PROCEDURE p2p.create_offer(
408439
input_user_creator service.valid_email
409440
) AS $$
410441
INSERT INTO p2p.offers(
411-
action_type, currency, quantity, limit_min, limit_max, comment, fk_user_creator
442+
action_type, fk_currency, quantity, limit_min, limit_max, comment, fk_user_creator
412443
)
413444
VALUES(
414445
input_action_type, input_currency, input_quantity, input_limit_min,
@@ -455,7 +486,6 @@ CREATE OR REPLACE PROCEDURE p2p.deal_payment(
455486
input_offer_id BIGINT,
456487
input_deal_id UUID
457488
) AS $$
458-
459489
-- Разница между offers.quantity и deal.quantity (процесс совершения сделки)
460490
UPDATE p2p.offers
461491
SET quantity = p2p.offers.quantity - (
@@ -468,10 +498,37 @@ CREATE OR REPLACE PROCEDURE p2p.deal_payment(
468498
-- Изменение статуса deal
469499
UPDATE p2p.deals
470500
SET deal_status = 'PAYED'
471-
WHERE id = input_deal_id
501+
WHERE id = input_deal_id;
472502
$$ LANGUAGE sql;
473503

474504

505+
-- Создание p2p-транзакции
506+
CREATE OR REPLACE PROCEDURE p2p.create_p2p_transaction(
507+
input_user_creator service.valid_email,
508+
input_user_contragent service.valid_email,
509+
input_currency VARCHAR(6),
510+
input_quantity NUMERIC
511+
) AS $$
512+
BEGIN
513+
IF (SELECT balance
514+
FROM p2p.wallets
515+
WHERE fk_currency = input_currency AND fk_user_owner = input_user_contragent
516+
) >= input_quantity THEN
517+
518+
UPDATE p2p.wallets
519+
SET balance = balance - input_quantity
520+
WHERE fk_user_owner = input_user_contragent AND fk_currency = input_currency;
521+
522+
UPDATE p2p.wallets
523+
SET balance = balance + input_quantity
524+
WHERE fk_user_owner = input_user_creator AND fk_currency = input_currency;
525+
ELSE
526+
RAISE NOTICE 'Недостаточно средств на счету';
527+
END IF;
528+
END;
529+
$$ LANGUAGE plpgsql;
530+
531+
475532
-- Смена статуса сделок спустя 15 минут бездействия
476533
CREATE OR REPLACE PROCEDURE p2p.check_deal_time(
477534
input_deal_id UUID

0 commit comments

Comments
 (0)