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.

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