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.

209 lines
6.5 KiB

  1. -- +migrate Up
  2. CREATE TABLE block (
  3. eth_block_num BIGINT PRIMARY KEY,
  4. timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  5. hash BYTEA NOT NULL
  6. );
  7. CREATE TABLE coordinator (
  8. forger_addr BYTEA NOT NULL,
  9. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  10. withdraw_addr BYTEA NOT NULL,
  11. url VARCHAR(200) NOT NULL,
  12. PRIMARY KEY (forger_addr, eth_block_num)
  13. );
  14. CREATE TABLE batch (
  15. batch_num BIGINT PRIMARY KEY,
  16. eth_block_num BIGINT REFERENCES block (eth_block_num) ON DELETE CASCADE,
  17. forger_addr BYTEA NOT NULL, -- fake foreign key for coordinator
  18. fees_collected BYTEA NOT NULL,
  19. state_root BYTEA NOT NULL,
  20. num_accounts BIGINT NOT NULL,
  21. exit_root BYTEA NOT NULL,
  22. forge_l1_txs_num BIGINT,
  23. slot_num BIGINT NOT NULL
  24. );
  25. CREATE TABLE exit_tree (
  26. batch_num BIGINT REFERENCES batch (batch_num) ON DELETE CASCADE,
  27. account_idx BIGINT,
  28. merkle_proof BYTEA NOT NULL,
  29. balance BYTEA NOT NULL,
  30. instant_withdrawn BIGINT REFERENCES batch (batch_num) ON DELETE SET NULL,
  31. delayed_withdraw_request BIGINT REFERENCES batch (batch_num) ON DELETE SET NULL,
  32. delayed_withdrawn BIGINT REFERENCES batch (batch_num) ON DELETE SET NULL,
  33. PRIMARY KEY (batch_num, account_idx)
  34. );
  35. CREATE TABLE bid (
  36. slot_num BIGINT NOT NULL,
  37. bid_value BYTEA NOT NULL,
  38. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  39. forger_addr BYTEA NOT NULL, -- fake foreign key for coordinator
  40. PRIMARY KEY (slot_num, bid_value)
  41. );
  42. CREATE TABLE token (
  43. token_id INT PRIMARY KEY,
  44. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  45. eth_addr BYTEA UNIQUE NOT NULL,
  46. name VARCHAR(20) NOT NULL,
  47. symbol VARCHAR(10) NOT NULL,
  48. decimals INT NOT NULL,
  49. usd NUMERIC,
  50. usd_update TIMESTAMP
  51. );
  52. -- +migrate StatementBegin
  53. CREATE FUNCTION set_token_usd_update()
  54. RETURNS TRIGGER
  55. AS
  56. $BODY$
  57. BEGIN
  58. IF NEW."usd" IS NOT NULL AND NEW."usd_update" IS NULL THEN
  59. NEW."usd_update" = timezone('utc', now());
  60. END IF;
  61. RETURN NEW;
  62. END;
  63. $BODY$
  64. LANGUAGE plpgsql;
  65. -- +migrate StatementEnd
  66. CREATE TRIGGER trigger_token_usd_update BEFORE UPDATE OR INSERT ON token
  67. FOR EACH ROW EXECUTE PROCEDURE set_token_usd_update();
  68. CREATE TABLE tx (
  69. -- Generic TX
  70. is_l1 BOOLEAN NOT NULL,
  71. id BYTEA PRIMARY KEY,
  72. type VARCHAR(40) NOT NULL,
  73. position INT NOT NULL,
  74. from_idx BIGINT NOT NULL,
  75. to_idx BIGINT NOT NULL,
  76. amount BYTEA NOT NULL,
  77. amount_f NUMERIC NOT NULL,
  78. token_id INT NOT NULL REFERENCES token (token_id),
  79. amount_usd NUMERIC, -- Value of the amount in USD at the moment the tx was inserted in the DB
  80. 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.
  81. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  82. -- L1
  83. to_forge_l1_txs_num BIGINT,
  84. user_origin BOOLEAN,
  85. from_eth_addr BYTEA,
  86. from_bjj BYTEA,
  87. load_amount BYTEA,
  88. load_amount_f NUMERIC,
  89. load_amount_usd NUMERIC,
  90. -- L2
  91. fee INT,
  92. fee_usd NUMERIC,
  93. nonce BIGINT
  94. );
  95. CREATE INDEX tx_order ON tx (batch_num, position);
  96. -- +migrate StatementBegin
  97. CREATE FUNCTION set_tx()
  98. RETURNS TRIGGER
  99. AS
  100. $BODY$
  101. DECLARE token_value NUMERIC := (SELECT usd FROM token WHERE token_id = NEW.token_id);
  102. BEGIN
  103. -- Validate L1/L2 constrains
  104. IF NEW.is_l1 AND (( -- L1 mandatory fields
  105. NEW.user_origin IS NULL OR
  106. NEW.from_eth_addr IS NULL OR
  107. NEW.from_bjj IS NULL OR
  108. NEW.load_amount IS NULL OR
  109. NEW.load_amount_f IS NULL
  110. ) OR (NOT NEW.user_origin AND NEW.batch_num IS NULL)) THEN -- If is Coordinator L1, must include batch_num
  111. RAISE EXCEPTION 'Invalid L1 tx.';
  112. ELSIF NOT NEW.is_l1 THEN
  113. IF NEW.fee IS NULL THEN
  114. NEW.fee = (SELECT 0);
  115. END IF;
  116. IF NEW.batch_num IS NULL OR NEW.nonce IS NULL THEN
  117. RAISE EXCEPTION 'Invalid L2 tx.';
  118. END IF;
  119. END IF;
  120. -- If is L2, add token_id
  121. IF NEW.token_id IS NULL THEN
  122. NEW."token_id" = (SELECT token_id FROM account WHERE idx = NEW."from_idx");
  123. END IF;
  124. -- Set value_usd
  125. NEW."amount_usd" = (SELECT token_value * NEW.amount_f);
  126. NEW."load_amount_usd" = (SELECT token_value * NEW.load_amount_f);
  127. IF NOT NEW.is_l1 THEN
  128. NEW."fee_usd" = (SELECT token_value * NEW.amount_f * CASE
  129. WHEN NEW.fee = 0 THEN 0
  130. WHEN NEW.fee >= 1 AND NEW.fee <= 32 THEN POWER(10,-24+(NEW.fee::float/2))
  131. WHEN NEW.fee >= 33 AND NEW.fee <= 223 THEN POWER(10,-8+(0.041666666666667*(NEW.fee::float-32)))
  132. WHEN NEW.fee >= 224 AND NEW.fee <= 255 THEN POWER(10,NEW.fee-224) END);
  133. END IF;
  134. RETURN NEW;
  135. END;
  136. $BODY$
  137. LANGUAGE plpgsql;
  138. -- +migrate StatementEnd
  139. CREATE TRIGGER trigger_set_tx BEFORE INSERT ON tx
  140. FOR EACH ROW EXECUTE PROCEDURE set_tx();
  141. -- +migrate StatementBegin
  142. CREATE FUNCTION forge_l1_user_txs()
  143. RETURNS TRIGGER
  144. AS
  145. $BODY$
  146. BEGIN
  147. IF NEW.forge_l1_txs_num IS NOT NULL THEN
  148. UPDATE tx
  149. SET batch_num = NEW.batch_num
  150. WHERE user_origin AND NEW.forge_l1_txs_num = to_forge_l1_txs_num;
  151. END IF;
  152. RETURN NEW;
  153. END;
  154. $BODY$
  155. LANGUAGE plpgsql;
  156. -- +migrate StatementEnd
  157. CREATE TRIGGER trigger_forge_l1_txs AFTER INSERT ON batch
  158. FOR EACH ROW EXECUTE PROCEDURE forge_l1_user_txs();
  159. CREATE TABLE account (
  160. idx BIGINT PRIMARY KEY,
  161. token_id INT NOT NULL REFERENCES token (token_id) ON DELETE CASCADE,
  162. batch_num BIGINT NOT NULL REFERENCES batch (batch_num) ON DELETE CASCADE,
  163. bjj BYTEA NOT NULL,
  164. eth_addr BYTEA NOT NULL
  165. );
  166. CREATE TABLE rollup_vars (
  167. eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
  168. forge_l1_timeout BYTEA NOT NULL,
  169. fee_l1_user_tx BYTEA NOT NULL,
  170. fee_add_token BYTEA NOT NULL,
  171. tokens_hez BYTEA NOT NULL,
  172. governance BYTEA NOT NULL
  173. );
  174. CREATE TABLE consensus_vars (
  175. eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
  176. slot_deadline INT NOT NULL,
  177. close_auction_slots INT NOT NULL,
  178. open_auction_slots INT NOT NULL,
  179. min_bid_slots VARCHAR(200) NOT NULL,
  180. outbidding INT NOT NULL,
  181. donation_address BYTEA NOT NULL,
  182. governance_address BYTEA NOT NULL,
  183. allocation_ratio VARCHAR(200)
  184. );
  185. -- +migrate Down
  186. DROP TABLE consensus_vars;
  187. DROP TABLE rollup_vars;
  188. DROP TABLE account;
  189. DROP TABLE tx;
  190. DROP TABLE token;
  191. DROP TABLE bid;
  192. DROP TABLE exit_tree;
  193. DROP TABLE batch;
  194. DROP TABLE coordinator;
  195. DROP TABLE block;