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.

206 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 coordianator (
  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. -- +migrate StatementBegin
  95. CREATE FUNCTION set_tx()
  96. RETURNS TRIGGER
  97. AS
  98. $BODY$
  99. DECLARE token_value NUMERIC := (SELECT usd FROM token WHERE token_id = NEW.token_id);
  100. BEGIN
  101. -- Validate L1/L2 constrains
  102. IF NEW.is_l1 AND (( -- L1 mandatory fields
  103. NEW.user_origin IS NULL OR
  104. NEW.from_eth_addr IS NULL OR
  105. NEW.from_bjj IS NULL OR
  106. NEW.load_amount IS NULL OR
  107. NEW.load_amount_f IS NULL
  108. ) OR (NOT NEW.user_origin AND NEW.batch_num IS NULL)) THEN -- If is Coordinator L1, must include batch_num
  109. RAISE EXCEPTION 'Invalid L1 tx.';
  110. ELSIF NOT NEW.is_l1 THEN
  111. IF NEW.fee IS NULL THEN
  112. NEW.fee = (SELECT 0);
  113. END IF;
  114. IF NEW.batch_num IS NULL OR NEW.nonce IS NULL THEN
  115. RAISE EXCEPTION 'Invalid L2 tx.';
  116. END IF;
  117. END IF;
  118. -- If is L2, add token_id
  119. IF NEW.token_id IS NULL THEN
  120. NEW."token_id" = (SELECT token_id FROM account WHERE idx = NEW."from_idx");
  121. END IF;
  122. -- Set value_usd
  123. NEW."amount_usd" = (SELECT token_value * NEW.amount_f);
  124. NEW."load_amount_usd" = (SELECT token_value * NEW.load_amount_f);
  125. IF NOT NEW.is_l1 THEN
  126. NEW."fee_usd" = (SELECT token_value * NEW.amount_f * CASE
  127. WHEN NEW.fee = 0 THEN 0
  128. WHEN NEW.fee >= 1 AND NEW.fee <= 32 THEN POWER(10,-24+(NEW.fee::float/2))
  129. WHEN NEW.fee >= 33 AND NEW.fee <= 223 THEN POWER(10,-8+(0.041666666666667*(NEW.fee::float-32)))
  130. WHEN NEW.fee >= 224 AND NEW.fee <= 255 THEN POWER(10,NEW.fee-224) END);
  131. END IF;
  132. RETURN NEW;
  133. END;
  134. $BODY$
  135. LANGUAGE plpgsql;
  136. -- +migrate StatementEnd
  137. CREATE TRIGGER trigger_set_tx BEFORE INSERT ON tx
  138. FOR EACH ROW EXECUTE PROCEDURE set_tx();
  139. -- +migrate StatementBegin
  140. CREATE FUNCTION forge_l1_user_txs()
  141. RETURNS TRIGGER
  142. AS
  143. $BODY$
  144. BEGIN
  145. IF NEW.forge_l1_txs_num IS NOT NULL THEN
  146. UPDATE tx
  147. SET batch_num = NEW.batch_num
  148. WHERE user_origin AND NEW.forge_l1_txs_num = to_forge_l1_txs_num;
  149. END IF;
  150. RETURN NEW;
  151. END;
  152. $BODY$
  153. LANGUAGE plpgsql;
  154. -- +migrate StatementEnd
  155. CREATE TRIGGER trigger_forge_l1_txs AFTER INSERT ON batch
  156. FOR EACH ROW EXECUTE PROCEDURE forge_l1_user_txs();
  157. CREATE TABLE account (
  158. idx BIGINT PRIMARY KEY,
  159. token_id INT NOT NULL REFERENCES token (token_id) ON DELETE CASCADE,
  160. batch_num BIGINT NOT NULL REFERENCES batch (batch_num) ON DELETE CASCADE,
  161. bjj BYTEA NOT NULL,
  162. eth_addr BYTEA NOT NULL
  163. );
  164. CREATE TABLE rollup_vars (
  165. eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
  166. forge_l1_timeout BYTEA NOT NULL,
  167. fee_l1_user_tx BYTEA NOT NULL,
  168. fee_add_token BYTEA NOT NULL,
  169. tokens_hez BYTEA NOT NULL,
  170. governance BYTEA NOT NULL
  171. );
  172. CREATE TABLE consensus_vars (
  173. eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
  174. slot_deadline INT NOT NULL,
  175. close_auction_slots INT NOT NULL,
  176. open_auction_slots INT NOT NULL,
  177. min_bid_slots VARCHAR(200) NOT NULL,
  178. outbidding INT NOT NULL,
  179. donation_address BYTEA NOT NULL,
  180. governance_address BYTEA NOT NULL,
  181. allocation_ratio VARCHAR(200)
  182. );
  183. -- +migrate Down
  184. DROP TABLE consensus_vars;
  185. DROP TABLE rollup_vars;
  186. DROP TABLE account;
  187. DROP TABLE l2tx;
  188. DROP TABLE l1tx;
  189. DROP TABLE token;
  190. DROP TABLE bid;
  191. DROP TABLE exit_tree;
  192. DROP TABLE batch;
  193. DROP TABLE coordianator;
  194. DROP TABLE block;