CREATE TYPE "router_v1.7"."status_code" AS ENUM('VALID', 'NOT_ENOUGH_LIQUIDITY');--> statement-breakpoint
CREATE TABLE "router_v1.7"."chains" (
	"chain_id" bigint NOT NULL,
	"block_number" bigint NOT NULL,
	"epoch" numeric(78, 0) DEFAULT '0' NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."collectors" (
	"chain_id" bigint NOT NULL,
	"name" text NOT NULL,
	"block_number" bigint NOT NULL,
	"epoch" numeric(78, 0) DEFAULT '0' NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."consumed_per_user_and_nonce" (
	"id" varchar(255) PRIMARY KEY NOT NULL,
	"chain_id" bigint NOT NULL,
	"offering" varchar(42) NOT NULL,
	"nonce" bigint NOT NULL,
	"consumed" numeric(78, 0) NOT NULL,
	"block_number" bigint NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."consumed_events" (
	"event_id" varchar(128) PRIMARY KEY NOT NULL,
	"chain_id" bigint NOT NULL,
	"maker" varchar(42) NOT NULL,
	"group" varchar(66) NOT NULL,
	"amount" numeric(78, 0) NOT NULL,
	"block_number" bigint NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."groups" (
	"chain_id" bigint NOT NULL,
	"maker" varchar(42) NOT NULL,
	"group" varchar(66) NOT NULL,
	"consumed" numeric(78, 0) NOT NULL,
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "groups_pk" PRIMARY KEY("chain_id","maker","group")
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."liquidity_links" (
	"parent_pool_id" varchar(255) NOT NULL,
	"child_pool_id" varchar(255) NOT NULL,
	"priority" integer NOT NULL,
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "liquidity_links_pk" PRIMARY KEY("parent_pool_id","priority")
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."liquidity_pools" (
	"id" varchar(255) PRIMARY KEY NOT NULL,
	"amount" numeric(78, 0) NOT NULL,
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."obligation_collaterals" (
	"obligation_id" varchar(66) NOT NULL,
	"asset" varchar(42) NOT NULL,
	"oracle" varchar(42) NOT NULL,
	"lltv" bigint NOT NULL,
	CONSTRAINT "obligation_collaterals_pk" PRIMARY KEY("obligation_id","asset")
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."obligation_collaterals_v2" (
	"obligation_id" varchar(66) NOT NULL,
	"asset" varchar(42) NOT NULL,
	"oracle_chain_id" bigint NOT NULL,
	"oracle_address" varchar(42) NOT NULL,
	"lltv" bigint NOT NULL,
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "obligation_collaterals_v2_pk" PRIMARY KEY("obligation_id","asset")
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."obligations" (
	"obligation_id" varchar(66) PRIMARY KEY NOT NULL,
	"chain_id" bigint NOT NULL,
	"loan_token" varchar(42) NOT NULL,
	"maturity" integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."offer_liquidity_pools" (
	"offer_hash" varchar(66) NOT NULL,
	"pool_id" varchar(255) NOT NULL,
	"amount" numeric(78, 0) NOT NULL,
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "offer_liquidity_pools_pk" PRIMARY KEY("offer_hash","pool_id")
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."offer_status" (
	"offer_hash" varchar(66) PRIMARY KEY NOT NULL,
	"status_id" serial NOT NULL,
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."offers" (
	"hash" varchar(66) PRIMARY KEY NOT NULL,
	"obligation_id" varchar(66) NOT NULL,
	"offering" varchar(42) NOT NULL,
	"assets" numeric(78, 0) NOT NULL,
	"rate" bigint NOT NULL,
	"maturity" integer NOT NULL,
	"expiry" integer NOT NULL,
	"start" integer NOT NULL,
	"nonce" bigint NOT NULL,
	"buy" boolean NOT NULL,
	"chain_id" bigint NOT NULL,
	"loan_token" varchar(42) NOT NULL,
	"callback_address" varchar(42) NOT NULL,
	"callback_data" text NOT NULL,
	"callback_gas_limit" bigint NOT NULL,
	"signature" varchar(132),
	"created_at" timestamp DEFAULT now() NOT NULL,
	"block_number" bigint NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."offers_v2" (
	"hash" varchar(66) PRIMARY KEY NOT NULL,
	"obligation_id" varchar(66) NOT NULL,
	"assets" numeric(78, 0) NOT NULL,
	"rate" numeric(78, 0) NOT NULL,
	"maturity" integer NOT NULL,
	"expiry" integer NOT NULL,
	"start" integer NOT NULL,
	"group_chain_id" bigint NOT NULL,
	"group_maker" varchar(42) NOT NULL,
	"group_group" varchar(66) NOT NULL,
	"nonce" varchar(66) NOT NULL,
	"buy" boolean NOT NULL,
	"callback_address" varchar(42) NOT NULL,
	"callback_data" text NOT NULL,
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."oracles" (
	"chain_id" bigint NOT NULL,
	"address" varchar(42) NOT NULL,
	"block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "oracles_pk" PRIMARY KEY("chain_id","address")
);
--> statement-breakpoint
CREATE TABLE "router_v1.7"."status" (
	"id" serial PRIMARY KEY NOT NULL,
	"code" "router_v1.7"."status_code",
	CONSTRAINT "status_code_unique" UNIQUE("code")
);
--> statement-breakpoint
ALTER TABLE "router_v1.7"."consumed_events" ADD CONSTRAINT "consumed_events_groups_fk" FOREIGN KEY ("chain_id","maker","group") REFERENCES "router_v1.7"."groups"("chain_id","maker","group") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."liquidity_links" ADD CONSTRAINT "liquidity_links_parent_pool_id_liquidity_pools_id_fk" FOREIGN KEY ("parent_pool_id") REFERENCES "router_v1.7"."liquidity_pools"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."liquidity_links" ADD CONSTRAINT "liquidity_links_child_pool_id_liquidity_pools_id_fk" FOREIGN KEY ("child_pool_id") REFERENCES "router_v1.7"."liquidity_pools"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."obligation_collaterals" ADD CONSTRAINT "obligation_collaterals_obligation_id_obligations_obligation_id_fk" FOREIGN KEY ("obligation_id") REFERENCES "router_v1.7"."obligations"("obligation_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."obligation_collaterals_v2" ADD CONSTRAINT "obligation_collaterals_v2_obligation_id_obligations_obligation_id_fk" FOREIGN KEY ("obligation_id") REFERENCES "router_v1.7"."obligations"("obligation_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."obligation_collaterals_v2" ADD CONSTRAINT "obligation_collaterals_v2_oracles_fk" FOREIGN KEY ("oracle_chain_id","oracle_address") REFERENCES "router_v1.7"."oracles"("chain_id","address") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."offer_liquidity_pools" ADD CONSTRAINT "offer_liquidity_pools_offer_hash_offers_hash_fk" FOREIGN KEY ("offer_hash") REFERENCES "router_v1.7"."offers"("hash") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."offer_liquidity_pools" ADD CONSTRAINT "offer_liquidity_pools_pool_id_liquidity_pools_id_fk" FOREIGN KEY ("pool_id") REFERENCES "router_v1.7"."liquidity_pools"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."offer_status" ADD CONSTRAINT "offer_status_offer_hash_offers_v2_hash_fk" FOREIGN KEY ("offer_hash") REFERENCES "router_v1.7"."offers_v2"("hash") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."offer_status" ADD CONSTRAINT "offer_status_status_id_status_id_fk" FOREIGN KEY ("status_id") REFERENCES "router_v1.7"."status"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."offers" ADD CONSTRAINT "offers_obligation_id_obligations_obligation_id_fk" FOREIGN KEY ("obligation_id") REFERENCES "router_v1.7"."obligations"("obligation_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."offers_v2" ADD CONSTRAINT "offers_v2_obligation_id_obligations_obligation_id_fk" FOREIGN KEY ("obligation_id") REFERENCES "router_v1.7"."obligations"("obligation_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "router_v1.7"."offers_v2" ADD CONSTRAINT "offers_v2_groups_fk" FOREIGN KEY ("group_chain_id","group_maker","group_group") REFERENCES "router_v1.7"."groups"("chain_id","maker","group") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "chains_id_epoch_idx" ON "router_v1.7"."chains" USING btree ("chain_id","epoch");--> statement-breakpoint
CREATE UNIQUE INDEX "collectors_chain_name_epoch_idx" ON "router_v1.7"."collectors" USING btree ("chain_id","name","epoch");--> statement-breakpoint
CREATE INDEX "consumed_per_user_and_nonce_chain_id_offering_nonce_block_number_idx" ON "router_v1.7"."consumed_per_user_and_nonce" USING btree ("chain_id","offering","nonce","block_number" desc);--> statement-breakpoint
CREATE INDEX "consumed_events_group_idx" ON "router_v1.7"."consumed_events" USING btree ("chain_id","maker","group");--> statement-breakpoint
CREATE INDEX "consumed_events_block_number_idx" ON "router_v1.7"."consumed_events" USING btree ("block_number");--> statement-breakpoint
CREATE INDEX "groups_chain_id_maker_group_consumed_idx" ON "router_v1.7"."groups" USING btree ("chain_id","maker","group","consumed");--> statement-breakpoint
CREATE INDEX "liquidity_links_parent_pool_id_idx" ON "router_v1.7"."liquidity_links" USING btree ("parent_pool_id");--> statement-breakpoint
CREATE INDEX "liquidity_links_child_pool_id_idx" ON "router_v1.7"."liquidity_links" USING btree ("child_pool_id");--> statement-breakpoint
CREATE INDEX "obligation_collaterals_obligation_id_idx" ON "router_v1.7"."obligation_collaterals" USING btree ("obligation_id");--> statement-breakpoint
CREATE INDEX "obligation_collaterals_v2_obligation_id_idx" ON "router_v1.7"."obligation_collaterals_v2" USING btree ("obligation_id");--> statement-breakpoint
CREATE INDEX "obligation_collaterals_v2_oracle_fk_idx" ON "router_v1.7"."obligation_collaterals_v2" USING btree ("oracle_chain_id","oracle_address");--> statement-breakpoint
CREATE INDEX "offer_liquidity_pools_pool_id_idx" ON "router_v1.7"."offer_liquidity_pools" USING btree ("pool_id");--> statement-breakpoint
CREATE INDEX "offer_status_status_hash_idx" ON "router_v1.7"."offer_status" USING btree ("status_id","offer_hash");--> statement-breakpoint
CREATE INDEX "offers_obligation_id_idx" ON "router_v1.7"."offers" USING btree ("obligation_id");--> statement-breakpoint
CREATE INDEX "offers_offering_idx" ON "router_v1.7"."offers" USING btree ("offering");--> statement-breakpoint
CREATE INDEX "offers_buy_idx" ON "router_v1.7"."offers" USING btree ("buy");--> statement-breakpoint
CREATE INDEX "offers_chain_id_idx" ON "router_v1.7"."offers" USING btree ("chain_id");--> statement-breakpoint
CREATE INDEX "offers_loan_token_idx" ON "router_v1.7"."offers" USING btree ("loan_token");--> statement-breakpoint
CREATE INDEX "offers_maturity_idx" ON "router_v1.7"."offers" USING btree ("maturity");--> statement-breakpoint
CREATE INDEX "offers_expiry_idx" ON "router_v1.7"."offers" USING btree ("expiry");--> statement-breakpoint
CREATE INDEX "offers_rate_idx" ON "router_v1.7"."offers" USING btree ("rate");--> statement-breakpoint
CREATE INDEX "offers_assets_idx" ON "router_v1.7"."offers" USING btree ("assets");--> statement-breakpoint
CREATE INDEX "offers_created_at_idx" ON "router_v1.7"."offers" USING btree ("created_at");--> statement-breakpoint
CREATE INDEX "offers_block_number_idx" ON "router_v1.7"."offers" USING btree ("block_number");--> statement-breakpoint
CREATE INDEX "offers_rate_hash_idx" ON "router_v1.7"."offers" USING btree ("rate","hash");--> statement-breakpoint
CREATE INDEX "offers_maturity_hash_idx" ON "router_v1.7"."offers" USING btree ("maturity","hash");--> statement-breakpoint
CREATE INDEX "offers_expiry_hash_idx" ON "router_v1.7"."offers" USING btree ("expiry","hash");--> statement-breakpoint
CREATE INDEX "offers_assets_hash_idx" ON "router_v1.7"."offers" USING btree ("assets","hash");--> statement-breakpoint
CREATE INDEX "offers_rate_created_at_assets_hash_idx" ON "router_v1.7"."offers" USING btree ("rate","created_at" asc,"assets" desc,"hash" asc);--> statement-breakpoint
CREATE INDEX "offers_v2_group_fk_idx" ON "router_v1.7"."offers_v2" USING btree ("group_chain_id","group_maker","group_group");--> statement-breakpoint
CREATE INDEX "offers_v2_group_winner_sell_side_idx" ON "router_v1.7"."offers_v2" USING btree ("group_chain_id","group_maker","group_group","rate" desc,"block_number" asc,"assets" desc,"hash" asc);--> statement-breakpoint
CREATE INDEX "offers_v2_group_winner_buy_side_idx" ON "router_v1.7"."offers_v2" USING btree ("group_chain_id","group_maker","group_group","rate" asc,"block_number" asc,"assets" desc,"hash" asc);--> statement-breakpoint
CREATE INDEX "offers_v2_obligation_id_side_idx" ON "router_v1.7"."offers_v2" USING btree ("obligation_id","buy");