-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtc-exchange.sql
192 lines (163 loc) · 7.9 KB
/
btc-exchange.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
CREATE TABLE states (
device_address CHAR(33) NOT NULL PRIMARY KEY,
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_update TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
state VARCHAR(30) NOT NULL DEFAULT 'greeting',
FOREIGN KEY (device_address) REFERENCES correspondent_devices(device_address)
);
CREATE TABLE current_prices (
device_address CHAR(33) NOT NULL PRIMARY KEY,
buy_price DECIMAL(20, 10) NULL,
sell_price DECIMAL(20, 10) NULL,
FOREIGN KEY (device_address) REFERENCES correspondent_devices(device_address)
);
-- bindings
CREATE TABLE byte_buyer_bindings (
byte_buyer_binding_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
out_byteball_address CHAR(32) NOT NULL UNIQUE,
to_bitcoin_address VARCHAR(34) NOT NULL UNIQUE,
device_address CHAR(33) NOT NULL,
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (device_address) REFERENCES correspondent_devices(device_address)
);
CREATE TABLE byte_seller_bindings (
byte_seller_binding_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
to_byteball_address CHAR(32) NOT NULL UNIQUE,
out_bitcoin_address VARCHAR(34) NOT NULL UNIQUE,
device_address CHAR(33) NOT NULL,
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (device_address) REFERENCES correspondent_devices(device_address),
FOREIGN KEY (to_byteball_address) REFERENCES my_addresses(address)
);
-- deposits
CREATE TABLE byte_buyer_deposits (
byte_buyer_deposit_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
byte_buyer_binding_id INTEGER NOT NULL,
count_confirmations INT NOT NULL DEFAULT 0,
txid CHAR(64) NOT NULL,
satoshi_amount INT NOT NULL,
fee_satoshi_amount INT NULL, -- filled wnen confirmed
net_satoshi_amount INT NULL,
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
confirmation_date TIMESTAMP NULL,
UNIQUE (txid, byte_buyer_binding_id),
FOREIGN KEY (byte_buyer_binding_id) REFERENCES byte_buyer_bindings(byte_buyer_binding_id)
);
CREATE INDEX byBuyerDepositsConfirmation ON byte_buyer_deposits(confirmation_date);
CREATE TABLE byte_seller_deposits (
byte_seller_deposit_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
byte_seller_binding_id INTEGER NOT NULL,
unit CHAR(44) NOT NULL,
byte_amount INT NOT NULL,
fee_byte_amount INT NULL,
net_byte_amount INT NULL,
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
finality_date TIMESTAMP NULL,
UNIQUE (unit, byte_seller_binding_id),
FOREIGN KEY (byte_seller_binding_id) REFERENCES byte_seller_bindings(byte_seller_binding_id),
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATE INDEX bySellerDepositsFinality ON byte_seller_deposits(finality_date);
-- instant orders
-- customer gets quoted price and is instantly filled
-- the operator realays the deal to the book on his own behalf by buying or selling against pending book orders, with a margin
CREATE TABLE byte_buyer_instant_deals (
byte_buyer_instant_deal_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
byte_buyer_deposit_id INTEGER NOT NULL UNIQUE,
unit CHAR(44) NULL,
satoshi_amount INT NOT NULL,
byte_amount INT NOT NULL,
price DOUBLE NOT NULL,
match_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
execution_date TIMESTAMP NULL,
FOREIGN KEY (byte_buyer_deposit_id) REFERENCES byte_buyer_deposits(byte_buyer_deposit_id),
FOREIGN KEY (unit) REFERENCES units(unit)
);
CREATE INDEX byBuyerInstantDealsExecution ON byte_buyer_instant_deals(execution_date);
CREATE TABLE byte_seller_instant_deals (
byte_seller_instant_deal_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
byte_seller_deposit_id INTEGER NOT NULL UNIQUE,
txid CHAR(64) NULL,
satoshi_amount INT NOT NULL,
byte_amount INT NOT NULL,
price DOUBLE NOT NULL,
match_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
execution_date TIMESTAMP NULL,
FOREIGN KEY (byte_seller_deposit_id) REFERENCES byte_seller_deposits(byte_seller_deposit_id)
);
CREATE INDEX bySellerInstantDealsExecution ON byte_seller_instant_deals(execution_date);
CREATE TABLE byte_buyer_instant_deal_executions (
byte_buyer_instant_deal_id INTEGER NOT NULL PRIMARY KEY,
execution_date TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (byte_buyer_instant_deal_id) REFERENCES byte_buyer_instant_deals(byte_buyer_instant_deal_id)
);
CREATE TABLE byte_seller_instant_deal_executions (
byte_seller_instant_deal_id INTEGER NOT NULL PRIMARY KEY,
execution_date TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (byte_seller_instant_deal_id) REFERENCES byte_seller_instant_deals(byte_seller_instant_deal_id)
);
-- book orders
CREATE TABLE byte_buyer_orders (
byte_buyer_order_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
byte_buyer_deposit_id INTEGER NOT NULL,
prev_byte_buyer_order_id INTEGER NULL, -- after partial execution
device_address CHAR(33) NOT NULL,
is_active TINYINT NOT NULL DEFAULT 1,
satoshi_amount INT NOT NULL,
price DECIMAL(20, 10) NOT NULL,
unit CHAR(44) NULL,
execution_price DECIMAL(20, 10) NULL,
sold_satoshi_amount INT NULL,
byte_amount INT NULL,
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
match_date TIMESTAMP NULL,
execution_date TIMESTAMP NULL,
byte_seller_instant_deal_id INT NULL, -- if executed against instant order
FOREIGN KEY (unit) REFERENCES units(unit),
FOREIGN KEY (device_address) REFERENCES correspondent_devices(device_address),
FOREIGN KEY (byte_buyer_deposit_id) REFERENCES byte_buyer_deposits(byte_buyer_deposit_id),
FOREIGN KEY (prev_byte_buyer_order_id) REFERENCES byte_buyer_orders(byte_buyer_order_id),
FOREIGN KEY (byte_seller_instant_deal_id) REFERENCES byte_seller_instant_deals(byte_seller_instant_deal_id)
);
CREATE INDEX byBuyerOrdersDevice ON byte_buyer_orders(device_address);
CREATE INDEX byBuyerOrdersActivePrice ON byte_buyer_orders(is_active, price);
CREATE INDEX byBuyerOrdersActiveExecuted ON byte_buyer_orders(is_active, execution_date);
CREATE TABLE byte_seller_orders (
byte_seller_order_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
byte_seller_deposit_id INTEGER NOT NULL,
prev_byte_seller_order_id INTEGER NULL, -- after partial execution
device_address CHAR(33) NOT NULL,
is_active TINYINT NOT NULL DEFAULT 1,
byte_amount INT NOT NULL,
price DECIMAL(20, 10) NOT NULL,
txid CHAR(64) NULL,
execution_price DECIMAL(20, 10) NULL,
sold_byte_amount INT NULL,
satoshi_amount INT NULL,
creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
match_date TIMESTAMP NULL,
execution_date TIMESTAMP NULL,
byte_buyer_instant_deal_id INT NULL, -- if executed against instant order
FOREIGN KEY (device_address) REFERENCES correspondent_devices(device_address),
FOREIGN KEY (byte_seller_deposit_id) REFERENCES byte_seller_deposits(byte_seller_deposit_id),
FOREIGN KEY (prev_byte_seller_order_id) REFERENCES byte_seller_orders(byte_seller_order_id),
FOREIGN KEY (byte_buyer_instant_deal_id) REFERENCES byte_buyer_instant_deals(byte_buyer_instant_deal_id)
);
CREATE INDEX bySellerOrdersDevice ON byte_seller_orders(device_address);
CREATE INDEX bySellerOrdersActivePrice ON byte_seller_orders(is_active, price);
CREATE INDEX bySellerOrdersActiveExecuted ON byte_seller_orders(is_active, execution_date);
-- if executed against book order
ALTER TABLE byte_buyer_orders ADD COLUMN opposite_byte_seller_order_id INTEGER NULL REFERENCES byte_seller_orders(byte_seller_order_id); -- opposite order
ALTER TABLE byte_seller_orders ADD COLUMN opposite_byte_buyer_order_id INTEGER NULL REFERENCES byte_buyer_orders(byte_buyer_order_id); -- opposite order
CREATE TABLE byte_buyer_order_executions (
byte_buyer_order_id INTEGER NOT NULL PRIMARY KEY,
execution_date TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (byte_buyer_order_id) REFERENCES byte_buyer_orders(byte_buyer_order_id)
);
CREATE TABLE byte_seller_order_executions (
byte_seller_order_id INTEGER NOT NULL PRIMARY KEY,
execution_date TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (byte_seller_order_id) REFERENCES byte_seller_orders(byte_seller_order_id)
);