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.

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