You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

529 lines
18 KiB

  1. -- +migrate Up
  2. -- History
  3. CREATE TABLE block (
  4. eth_block_num BIGINT PRIMARY KEY,
  5. timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  6. hash BYTEA NOT NULL
  7. );
  8. CREATE TABLE coordinator (
  9. bidder_addr BYTEA NOT NULL,
  10. forger_addr BYTEA NOT NULL,
  11. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  12. url VARCHAR(200) NOT NULL,
  13. PRIMARY KEY (bidder_addr, eth_block_num)
  14. );
  15. CREATE TABLE batch (
  16. batch_num BIGINT PRIMARY KEY,
  17. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  18. forger_addr BYTEA NOT NULL, -- fake foreign key for coordinator
  19. fees_collected BYTEA NOT NULL,
  20. state_root BYTEA NOT NULL,
  21. num_accounts BIGINT NOT NULL,
  22. exit_root BYTEA NOT NULL,
  23. forge_l1_txs_num BIGINT,
  24. slot_num BIGINT NOT NULL,
  25. total_fees_usd NUMERIC
  26. );
  27. CREATE TABLE bid (
  28. slot_num BIGINT NOT NULL,
  29. bid_value BYTEA NOT NULL,
  30. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  31. bidder_addr BYTEA NOT NULL, -- fake foreign key for coordinator
  32. PRIMARY KEY (slot_num, bid_value)
  33. );
  34. CREATE TABLE token (
  35. token_id INT PRIMARY KEY,
  36. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  37. eth_addr BYTEA UNIQUE NOT NULL,
  38. name VARCHAR(20) NOT NULL,
  39. symbol VARCHAR(10) NOT NULL,
  40. decimals INT NOT NULL,
  41. usd NUMERIC,
  42. usd_update TIMESTAMP WITHOUT TIME ZONE
  43. );
  44. CREATE TABLE account (
  45. idx BIGINT PRIMARY KEY,
  46. token_id INT NOT NULL REFERENCES token (token_id) ON DELETE CASCADE,
  47. batch_num BIGINT NOT NULL REFERENCES batch (batch_num) ON DELETE CASCADE,
  48. bjj BYTEA NOT NULL,
  49. eth_addr BYTEA NOT NULL
  50. );
  51. CREATE TABLE exit_tree (
  52. item_id SERIAL PRIMARY KEY,
  53. batch_num BIGINT REFERENCES batch (batch_num) ON DELETE CASCADE,
  54. account_idx BIGINT REFERENCES account (idx) ON DELETE CASCADE,
  55. merkle_proof BYTEA NOT NULL,
  56. balance BYTEA NOT NULL,
  57. instant_withdrawn BIGINT REFERENCES batch (batch_num) ON DELETE SET NULL,
  58. delayed_withdraw_request BIGINT REFERENCES batch (batch_num) ON DELETE SET NULL,
  59. delayed_withdrawn BIGINT REFERENCES batch (batch_num) ON DELETE SET NULL
  60. );
  61. -- +migrate StatementBegin
  62. CREATE FUNCTION set_token_usd_update()
  63. RETURNS TRIGGER
  64. AS
  65. $BODY$
  66. BEGIN
  67. IF NEW."usd" IS NOT NULL AND NEW."usd_update" IS NULL THEN
  68. NEW."usd_update" = timezone('utc', now());
  69. END IF;
  70. RETURN NEW;
  71. END;
  72. $BODY$
  73. LANGUAGE plpgsql;
  74. -- +migrate StatementEnd
  75. CREATE TRIGGER trigger_token_usd_update BEFORE UPDATE OR INSERT ON token
  76. FOR EACH ROW EXECUTE PROCEDURE set_token_usd_update();
  77. CREATE SEQUENCE tx_item_id;
  78. CREATE TABLE tx (
  79. -- Generic TX
  80. item_id INTEGER PRIMARY KEY DEFAULT nextval('tx_item_id'),
  81. is_l1 BOOLEAN NOT NULL,
  82. id BYTEA,
  83. type VARCHAR(40) NOT NULL,
  84. position INT NOT NULL,
  85. from_idx BIGINT,
  86. to_idx BIGINT NOT NULL,
  87. amount BYTEA NOT NULL,
  88. amount_f NUMERIC NOT NULL,
  89. token_id INT NOT NULL REFERENCES token (token_id),
  90. amount_usd NUMERIC, -- Value of the amount in USD at the moment the tx was inserted in the DB
  91. batch_num BIGINT REFERENCES batch (batch_num) ON DELETE SET NULL, -- Can be NULL in the case of L1 txs that are on the queue but not forged yet.
  92. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  93. -- L1
  94. to_forge_l1_txs_num BIGINT,
  95. user_origin BOOLEAN,
  96. from_eth_addr BYTEA,
  97. from_bjj BYTEA,
  98. load_amount BYTEA,
  99. load_amount_f NUMERIC,
  100. load_amount_usd NUMERIC,
  101. -- L2
  102. fee INT,
  103. fee_usd NUMERIC,
  104. nonce BIGINT
  105. );
  106. -- +migrate StatementBegin
  107. CREATE FUNCTION fee_percentage(NUMERIC)
  108. RETURNS NUMERIC
  109. AS
  110. $BODY$
  111. DECLARE perc NUMERIC;
  112. BEGIN
  113. SELECT CASE
  114. WHEN $1 = 0 THEN 0
  115. WHEN $1 = 1 THEN 3.162278e-24
  116. WHEN $1 = 2 THEN 1.000000e-23
  117. WHEN $1 = 3 THEN 3.162278e-23
  118. WHEN $1 = 4 THEN 1.000000e-22
  119. WHEN $1 = 5 THEN 3.162278e-22
  120. WHEN $1 = 6 THEN 1.000000e-21
  121. WHEN $1 = 7 THEN 3.162278e-21
  122. WHEN $1 = 8 THEN 1.000000e-20
  123. WHEN $1 = 9 THEN 3.162278e-20
  124. WHEN $1 = 10 THEN 1.000000e-19
  125. WHEN $1 = 11 THEN 3.162278e-19
  126. WHEN $1 = 12 THEN 1.000000e-18
  127. WHEN $1 = 13 THEN 3.162278e-18
  128. WHEN $1 = 14 THEN 1.000000e-17
  129. WHEN $1 = 15 THEN 3.162278e-17
  130. WHEN $1 = 16 THEN 1.000000e-16
  131. WHEN $1 = 17 THEN 3.162278e-16
  132. WHEN $1 = 18 THEN 1.000000e-15
  133. WHEN $1 = 19 THEN 3.162278e-15
  134. WHEN $1 = 20 THEN 1.000000e-14
  135. WHEN $1 = 21 THEN 3.162278e-14
  136. WHEN $1 = 22 THEN 1.000000e-13
  137. WHEN $1 = 23 THEN 3.162278e-13
  138. WHEN $1 = 24 THEN 1.000000e-12
  139. WHEN $1 = 25 THEN 3.162278e-12
  140. WHEN $1 = 26 THEN 1.000000e-11
  141. WHEN $1 = 27 THEN 3.162278e-11
  142. WHEN $1 = 28 THEN 1.000000e-10
  143. WHEN $1 = 29 THEN 3.162278e-10
  144. WHEN $1 = 30 THEN 1.000000e-09
  145. WHEN $1 = 31 THEN 3.162278e-09
  146. WHEN $1 = 32 THEN 1.000000e-08
  147. WHEN $1 = 33 THEN 1.100694e-08
  148. WHEN $1 = 34 THEN 1.211528e-08
  149. WHEN $1 = 35 THEN 1.333521e-08
  150. WHEN $1 = 36 THEN 1.467799e-08
  151. WHEN $1 = 37 THEN 1.615598e-08
  152. WHEN $1 = 38 THEN 1.778279e-08
  153. WHEN $1 = 39 THEN 1.957342e-08
  154. WHEN $1 = 40 THEN 2.154435e-08
  155. WHEN $1 = 41 THEN 2.371374e-08
  156. WHEN $1 = 42 THEN 2.610157e-08
  157. WHEN $1 = 43 THEN 2.872985e-08
  158. WHEN $1 = 44 THEN 3.162278e-08
  159. WHEN $1 = 45 THEN 3.480701e-08
  160. WHEN $1 = 46 THEN 3.831187e-08
  161. WHEN $1 = 47 THEN 4.216965e-08
  162. WHEN $1 = 48 THEN 4.641589e-08
  163. WHEN $1 = 49 THEN 5.108970e-08
  164. WHEN $1 = 50 THEN 5.623413e-08
  165. WHEN $1 = 51 THEN 6.189658e-08
  166. WHEN $1 = 52 THEN 6.812921e-08
  167. WHEN $1 = 53 THEN 7.498942e-08
  168. WHEN $1 = 54 THEN 8.254042e-08
  169. WHEN $1 = 55 THEN 9.085176e-08
  170. WHEN $1 = 56 THEN 1.000000e-07
  171. WHEN $1 = 57 THEN 1.100694e-07
  172. WHEN $1 = 58 THEN 1.211528e-07
  173. WHEN $1 = 59 THEN 1.333521e-07
  174. WHEN $1 = 60 THEN 1.467799e-07
  175. WHEN $1 = 61 THEN 1.615598e-07
  176. WHEN $1 = 62 THEN 1.778279e-07
  177. WHEN $1 = 63 THEN 1.957342e-07
  178. WHEN $1 = 64 THEN 2.154435e-07
  179. WHEN $1 = 65 THEN 2.371374e-07
  180. WHEN $1 = 66 THEN 2.610157e-07
  181. WHEN $1 = 67 THEN 2.872985e-07
  182. WHEN $1 = 68 THEN 3.162278e-07
  183. WHEN $1 = 69 THEN 3.480701e-07
  184. WHEN $1 = 70 THEN 3.831187e-07
  185. WHEN $1 = 71 THEN 4.216965e-07
  186. WHEN $1 = 72 THEN 4.641589e-07
  187. WHEN $1 = 73 THEN 5.108970e-07
  188. WHEN $1 = 74 THEN 5.623413e-07
  189. WHEN $1 = 75 THEN 6.189658e-07
  190. WHEN $1 = 76 THEN 6.812921e-07
  191. WHEN $1 = 77 THEN 7.498942e-07
  192. WHEN $1 = 78 THEN 8.254042e-07
  193. WHEN $1 = 79 THEN 9.085176e-07
  194. WHEN $1 = 80 THEN 1.000000e-06
  195. WHEN $1 = 81 THEN 1.100694e-06
  196. WHEN $1 = 82 THEN 1.211528e-06
  197. WHEN $1 = 83 THEN 1.333521e-06
  198. WHEN $1 = 84 THEN 1.467799e-06
  199. WHEN $1 = 85 THEN 1.615598e-06
  200. WHEN $1 = 86 THEN 1.778279e-06
  201. WHEN $1 = 87 THEN 1.957342e-06
  202. WHEN $1 = 88 THEN 2.154435e-06
  203. WHEN $1 = 89 THEN 2.371374e-06
  204. WHEN $1 = 90 THEN 2.610157e-06
  205. WHEN $1 = 91 THEN 2.872985e-06
  206. WHEN $1 = 92 THEN 3.162278e-06
  207. WHEN $1 = 93 THEN 3.480701e-06
  208. WHEN $1 = 94 THEN 3.831187e-06
  209. WHEN $1 = 95 THEN 4.216965e-06
  210. WHEN $1 = 96 THEN 4.641589e-06
  211. WHEN $1 = 97 THEN 5.108970e-06
  212. WHEN $1 = 98 THEN 5.623413e-06
  213. WHEN $1 = 99 THEN 6.189658e-06
  214. WHEN $1 = 100 THEN 6.812921e-06
  215. WHEN $1 = 101 THEN 7.498942e-06
  216. WHEN $1 = 102 THEN 8.254042e-06
  217. WHEN $1 = 103 THEN 9.085176e-06
  218. WHEN $1 = 104 THEN 1.000000e-05
  219. WHEN $1 = 105 THEN 1.100694e-05
  220. WHEN $1 = 106 THEN 1.211528e-05
  221. WHEN $1 = 107 THEN 1.333521e-05
  222. WHEN $1 = 108 THEN 1.467799e-05
  223. WHEN $1 = 109 THEN 1.615598e-05
  224. WHEN $1 = 110 THEN 1.778279e-05
  225. WHEN $1 = 111 THEN 1.957342e-05
  226. WHEN $1 = 112 THEN 2.154435e-05
  227. WHEN $1 = 113 THEN 2.371374e-05
  228. WHEN $1 = 114 THEN 2.610157e-05
  229. WHEN $1 = 115 THEN 2.872985e-05
  230. WHEN $1 = 116 THEN 3.162278e-05
  231. WHEN $1 = 117 THEN 3.480701e-05
  232. WHEN $1 = 118 THEN 3.831187e-05
  233. WHEN $1 = 119 THEN 4.216965e-05
  234. WHEN $1 = 120 THEN 4.641589e-05
  235. WHEN $1 = 121 THEN 5.108970e-05
  236. WHEN $1 = 122 THEN 5.623413e-05
  237. WHEN $1 = 123 THEN 6.189658e-05
  238. WHEN $1 = 124 THEN 6.812921e-05
  239. WHEN $1 = 125 THEN 7.498942e-05
  240. WHEN $1 = 126 THEN 8.254042e-05
  241. WHEN $1 = 127 THEN 9.085176e-05
  242. WHEN $1 = 128 THEN 1.000000e-04
  243. WHEN $1 = 129 THEN 1.100694e-04
  244. WHEN $1 = 130 THEN 1.211528e-04
  245. WHEN $1 = 131 THEN 1.333521e-04
  246. WHEN $1 = 132 THEN 1.467799e-04
  247. WHEN $1 = 133 THEN 1.615598e-04
  248. WHEN $1 = 134 THEN 1.778279e-04
  249. WHEN $1 = 135 THEN 1.957342e-04
  250. WHEN $1 = 136 THEN 2.154435e-04
  251. WHEN $1 = 137 THEN 2.371374e-04
  252. WHEN $1 = 138 THEN 2.610157e-04
  253. WHEN $1 = 139 THEN 2.872985e-04
  254. WHEN $1 = 140 THEN 3.162278e-04
  255. WHEN $1 = 141 THEN 3.480701e-04
  256. WHEN $1 = 142 THEN 3.831187e-04
  257. WHEN $1 = 143 THEN 4.216965e-04
  258. WHEN $1 = 144 THEN 4.641589e-04
  259. WHEN $1 = 145 THEN 5.108970e-04
  260. WHEN $1 = 146 THEN 5.623413e-04
  261. WHEN $1 = 147 THEN 6.189658e-04
  262. WHEN $1 = 148 THEN 6.812921e-04
  263. WHEN $1 = 149 THEN 7.498942e-04
  264. WHEN $1 = 150 THEN 8.254042e-04
  265. WHEN $1 = 151 THEN 9.085176e-04
  266. WHEN $1 = 152 THEN 1.000000e-03
  267. WHEN $1 = 153 THEN 1.100694e-03
  268. WHEN $1 = 154 THEN 1.211528e-03
  269. WHEN $1 = 155 THEN 1.333521e-03
  270. WHEN $1 = 156 THEN 1.467799e-03
  271. WHEN $1 = 157 THEN 1.615598e-03
  272. WHEN $1 = 158 THEN 1.778279e-03
  273. WHEN $1 = 159 THEN 1.957342e-03
  274. WHEN $1 = 160 THEN 2.154435e-03
  275. WHEN $1 = 161 THEN 2.371374e-03
  276. WHEN $1 = 162 THEN 2.610157e-03
  277. WHEN $1 = 163 THEN 2.872985e-03
  278. WHEN $1 = 164 THEN 3.162278e-03
  279. WHEN $1 = 165 THEN 3.480701e-03
  280. WHEN $1 = 166 THEN 3.831187e-03
  281. WHEN $1 = 167 THEN 4.216965e-03
  282. WHEN $1 = 168 THEN 4.641589e-03
  283. WHEN $1 = 169 THEN 5.108970e-03
  284. WHEN $1 = 170 THEN 5.623413e-03
  285. WHEN $1 = 171 THEN 6.189658e-03
  286. WHEN $1 = 172 THEN 6.812921e-03
  287. WHEN $1 = 173 THEN 7.498942e-03
  288. WHEN $1 = 174 THEN 8.254042e-03
  289. WHEN $1 = 175 THEN 9.085176e-03
  290. WHEN $1 = 176 THEN 1.000000e-02
  291. WHEN $1 = 177 THEN 1.100694e-02
  292. WHEN $1 = 178 THEN 1.211528e-02
  293. WHEN $1 = 179 THEN 1.333521e-02
  294. WHEN $1 = 180 THEN 1.467799e-02
  295. WHEN $1 = 181 THEN 1.615598e-02
  296. WHEN $1 = 182 THEN 1.778279e-02
  297. WHEN $1 = 183 THEN 1.957342e-02
  298. WHEN $1 = 184 THEN 2.154435e-02
  299. WHEN $1 = 185 THEN 2.371374e-02
  300. WHEN $1 = 186 THEN 2.610157e-02
  301. WHEN $1 = 187 THEN 2.872985e-02
  302. WHEN $1 = 188 THEN 3.162278e-02
  303. WHEN $1 = 189 THEN 3.480701e-02
  304. WHEN $1 = 190 THEN 3.831187e-02
  305. WHEN $1 = 191 THEN 4.216965e-02
  306. WHEN $1 = 192 THEN 4.641589e-02
  307. WHEN $1 = 193 THEN 5.108970e-02
  308. WHEN $1 = 194 THEN 5.623413e-02
  309. WHEN $1 = 195 THEN 6.189658e-02
  310. WHEN $1 = 196 THEN 6.812921e-02
  311. WHEN $1 = 197 THEN 7.498942e-02
  312. WHEN $1 = 198 THEN 8.254042e-02
  313. WHEN $1 = 199 THEN 9.085176e-02
  314. WHEN $1 = 200 THEN 1.000000e-01
  315. WHEN $1 = 201 THEN 1.100694e-01
  316. WHEN $1 = 202 THEN 1.211528e-01
  317. WHEN $1 = 203 THEN 1.333521e-01
  318. WHEN $1 = 204 THEN 1.467799e-01
  319. WHEN $1 = 205 THEN 1.615598e-01
  320. WHEN $1 = 206 THEN 1.778279e-01
  321. WHEN $1 = 207 THEN 1.957342e-01
  322. WHEN $1 = 208 THEN 2.154435e-01
  323. WHEN $1 = 209 THEN 2.371374e-01
  324. WHEN $1 = 210 THEN 2.610157e-01
  325. WHEN $1 = 211 THEN 2.872985e-01
  326. WHEN $1 = 212 THEN 3.162278e-01
  327. WHEN $1 = 213 THEN 3.480701e-01
  328. WHEN $1 = 214 THEN 3.831187e-01
  329. WHEN $1 = 215 THEN 4.216965e-01
  330. WHEN $1 = 216 THEN 4.641589e-01
  331. WHEN $1 = 217 THEN 5.108970e-01
  332. WHEN $1 = 218 THEN 5.623413e-01
  333. WHEN $1 = 219 THEN 6.189658e-01
  334. WHEN $1 = 220 THEN 6.812921e-01
  335. WHEN $1 = 221 THEN 7.498942e-01
  336. WHEN $1 = 222 THEN 8.254042e-01
  337. WHEN $1 = 223 THEN 9.085176e-01
  338. WHEN $1 = 224 THEN 1.000000e+00
  339. WHEN $1 = 225 THEN 1.000000e+01
  340. WHEN $1 = 226 THEN 1.000000e+02
  341. WHEN $1 = 227 THEN 1.000000e+03
  342. WHEN $1 = 228 THEN 1.000000e+04
  343. WHEN $1 = 229 THEN 1.000000e+05
  344. WHEN $1 = 230 THEN 1.000000e+06
  345. WHEN $1 = 231 THEN 1.000000e+07
  346. WHEN $1 = 232 THEN 1.000000e+08
  347. WHEN $1 = 233 THEN 1.000000e+09
  348. WHEN $1 = 234 THEN 1.000000e+10
  349. WHEN $1 = 235 THEN 1.000000e+11
  350. WHEN $1 = 236 THEN 1.000000e+12
  351. WHEN $1 = 237 THEN 1.000000e+13
  352. WHEN $1 = 238 THEN 1.000000e+14
  353. WHEN $1 = 239 THEN 1.000000e+15
  354. WHEN $1 = 240 THEN 1.000000e+16
  355. WHEN $1 = 241 THEN 1.000000e+17
  356. WHEN $1 = 242 THEN 1.000000e+18
  357. WHEN $1 = 243 THEN 1.000000e+19
  358. WHEN $1 = 244 THEN 1.000000e+20
  359. WHEN $1 = 245 THEN 1.000000e+21
  360. WHEN $1 = 246 THEN 1.000000e+22
  361. WHEN $1 = 247 THEN 1.000000e+23
  362. WHEN $1 = 248 THEN 1.000000e+24
  363. WHEN $1 = 249 THEN 1.000000e+25
  364. WHEN $1 = 250 THEN 1.000000e+26
  365. WHEN $1 = 251 THEN 1.000000e+27
  366. WHEN $1 = 252 THEN 1.000000e+28
  367. WHEN $1 = 253 THEN 1.000000e+29
  368. WHEN $1 = 254 THEN 1.000000e+30
  369. WHEN $1 = 255 THEN 1.000000e+31
  370. END INTO perc;
  371. RETURN perc;
  372. END;
  373. $BODY$
  374. LANGUAGE plpgsql;
  375. -- +migrate StatementEnd
  376. -- +migrate StatementBegin
  377. CREATE FUNCTION set_tx()
  378. RETURNS TRIGGER
  379. AS
  380. $BODY$
  381. DECLARE
  382. _value NUMERIC;
  383. _usd_update TIMESTAMP;
  384. _tx_timestamp TIMESTAMP;
  385. BEGIN
  386. -- Validate L1/L2 constrains
  387. IF NEW.is_l1 AND (( -- L1 mandatory fields
  388. NEW.user_origin IS NULL OR
  389. NEW.from_eth_addr IS NULL OR
  390. NEW.from_bjj IS NULL OR
  391. NEW.load_amount IS NULL OR
  392. NEW.load_amount_f IS NULL
  393. ) OR (NOT NEW.user_origin AND NEW.batch_num IS NULL)) THEN -- If is Coordinator L1, must include batch_num
  394. RAISE EXCEPTION 'Invalid L1 tx.';
  395. ELSIF NOT NEW.is_l1 THEN
  396. IF NEW.fee IS NULL THEN
  397. NEW.fee = (SELECT 0);
  398. END IF;
  399. IF NEW.batch_num IS NULL OR NEW.nonce IS NULL THEN
  400. RAISE EXCEPTION 'Invalid L2 tx.';
  401. END IF;
  402. END IF;
  403. -- If is L2, add token_id
  404. IF NOT NEW.is_l1 THEN
  405. NEW."token_id" = (SELECT token_id FROM account WHERE idx = NEW."from_idx");
  406. END IF;
  407. -- Set value_usd
  408. SELECT INTO _value, _usd_update, _tx_timestamp
  409. usd / POWER(10, decimals), usd_update, timestamp FROM token INNER JOIN block on token.eth_block_num = block.eth_block_num WHERE token_id = NEW.token_id;
  410. IF _tx_timestamp - interval '24 hours' < _usd_update AND _tx_timestamp + interval '24 hours' > _usd_update THEN
  411. NEW."amount_usd" = (SELECT _value * NEW.amount_f);
  412. IF NOT NEW.is_l1 THEN
  413. NEW."fee_usd" = (SELECT NEW."amount_usd" * fee_percentage(NEW.fee::NUMERIC));
  414. ELSE
  415. NEW."load_amount_usd" = (SELECT _value * NEW.load_amount_f);
  416. END IF;
  417. END IF;
  418. RETURN NEW;
  419. END;
  420. $BODY$
  421. LANGUAGE plpgsql;
  422. -- +migrate StatementEnd
  423. CREATE TRIGGER trigger_set_tx BEFORE INSERT ON tx
  424. FOR EACH ROW EXECUTE PROCEDURE set_tx();
  425. -- +migrate StatementBegin
  426. CREATE FUNCTION forge_l1_user_txs()
  427. RETURNS TRIGGER
  428. AS
  429. $BODY$
  430. BEGIN
  431. IF NEW.forge_l1_txs_num IS NOT NULL THEN
  432. UPDATE tx
  433. SET item_id = nextval('tx_item_id'), batch_num = NEW.batch_num
  434. WHERE id IN (
  435. SELECT id FROM tx
  436. WHERE user_origin AND NEW.forge_l1_txs_num = to_forge_l1_txs_num
  437. ORDER BY position
  438. FOR UPDATE
  439. );
  440. END IF;
  441. RETURN NEW;
  442. END;
  443. $BODY$
  444. LANGUAGE plpgsql;
  445. -- +migrate StatementEnd
  446. CREATE TRIGGER trigger_forge_l1_txs AFTER INSERT ON batch
  447. FOR EACH ROW EXECUTE PROCEDURE forge_l1_user_txs();
  448. CREATE TABLE rollup_vars (
  449. eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
  450. forge_l1_timeout BYTEA NOT NULL,
  451. fee_l1_user_tx BYTEA NOT NULL,
  452. fee_add_token BYTEA NOT NULL,
  453. tokens_hez BYTEA NOT NULL,
  454. governance BYTEA NOT NULL
  455. );
  456. CREATE TABLE consensus_vars (
  457. eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
  458. slot_deadline INT NOT NULL,
  459. close_auction_slots INT NOT NULL,
  460. open_auction_slots INT NOT NULL,
  461. min_bid_slots VARCHAR(200) NOT NULL,
  462. outbidding INT NOT NULL,
  463. donation_address BYTEA NOT NULL,
  464. governance_address BYTEA NOT NULL,
  465. allocation_ratio VARCHAR(200)
  466. );
  467. -- L2
  468. CREATE TABLE tx_pool (
  469. tx_id BYTEA PRIMARY KEY,
  470. from_idx BIGINT NOT NULL,
  471. to_idx BIGINT,
  472. to_eth_addr BYTEA,
  473. to_bjj BYTEA,
  474. token_id INT NOT NULL REFERENCES token (token_id) ON DELETE CASCADE,
  475. amount BYTEA NOT NULL,
  476. amount_f NUMERIC NOT NULL,
  477. fee SMALLINT NOT NULL,
  478. nonce BIGINT NOT NULL,
  479. state CHAR(4) NOT NULL,
  480. signature BYTEA NOT NULL,
  481. timestamp TIMESTAMP WITHOUT TIME ZONE DEFAULT timezone('utc', now()),
  482. batch_num BIGINT,
  483. rq_from_idx BIGINT,
  484. rq_to_idx BIGINT,
  485. rq_to_eth_addr BYTEA,
  486. rq_to_bjj BYTEA,
  487. rq_token_id INT,
  488. rq_amount BYTEA,
  489. rq_fee SMALLINT,
  490. rq_nonce BIGINT,
  491. tx_type VARCHAR(40) NOT NULL
  492. );
  493. CREATE TABLE account_creation_auth (
  494. eth_addr BYTEA PRIMARY KEY,
  495. bjj BYTEA NOT NULL,
  496. signature BYTEA NOT NULL,
  497. timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT timezone('utc', now())
  498. );
  499. -- +migrate Down
  500. DROP TABLE account_creation_auth;
  501. DROP TABLE tx_pool;
  502. DROP TABLE consensus_vars;
  503. DROP TABLE rollup_vars;
  504. DROP TABLE account;
  505. DROP TABLE tx;
  506. DROP TABLE token;
  507. DROP TABLE bid;
  508. DROP TABLE exit_tree;
  509. DROP TABLE batch;
  510. DROP TABLE coordinator;
  511. DROP TABLE block;