CREATE TABLE "offers_v1.1"."available_liquidity_pools" (
	"id" varchar(255) PRIMARY KEY NOT NULL,
	"amount" numeric(78, 0) NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "offers_v1.1"."available_liquidity_queues" (
	"id" varchar(255) PRIMARY KEY NOT NULL,
	"available_liquidity_pool_id" varchar(255) NOT NULL,
	"index" integer NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "offers_v1.1"."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,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "offers_v1.1"."indexing_progress" (
	"chain_id" bigint NOT NULL,
	"event_type" text NOT NULL,
	"latest_block_number" bigint NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "offers_v1.1"."offer_collaterals" (
	"id" serial PRIMARY KEY NOT NULL,
	"offer_hash" varchar(66) NOT NULL,
	"asset" varchar(42) NOT NULL,
	"oracle" varchar(42) NOT NULL,
	"lltv" bigint NOT NULL
);
--> statement-breakpoint
CREATE TABLE "offers_v1.1"."offer_status" (
	"id" serial PRIMARY KEY NOT NULL,
	"offer_hash" varchar(66) NOT NULL,
	"status" text NOT NULL,
	"metadata" jsonb,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "offers_v1.1"."offers" (
	"hash" varchar(66) PRIMARY KEY 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
);
--> statement-breakpoint
CREATE TABLE "offers_v1.1"."user_positions" (
	"id" varchar(255) PRIMARY KEY NOT NULL,
	"available_liquidity_queue_id" varchar(255) NOT NULL,
	"amount" numeric(78, 0) NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "offers_v1.1"."available_liquidity_queues" ADD CONSTRAINT "available_liquidity_queues_available_liquidity_pool_id_available_liquidity_pools_id_fk" FOREIGN KEY ("available_liquidity_pool_id") REFERENCES "offers_v1.1"."available_liquidity_pools"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "offers_v1.1"."offer_collaterals" ADD CONSTRAINT "offer_collaterals_offer_hash_offers_hash_fk" FOREIGN KEY ("offer_hash") REFERENCES "offers_v1.1"."offers"("hash") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "offers_v1.1"."offer_status" ADD CONSTRAINT "offer_status_offer_hash_offers_hash_fk" FOREIGN KEY ("offer_hash") REFERENCES "offers_v1.1"."offers"("hash") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "offers_v1.1"."user_positions" ADD CONSTRAINT "user_positions_available_liquidity_queue_id_available_liquidity_queues_id_fk" FOREIGN KEY ("available_liquidity_queue_id") REFERENCES "offers_v1.1"."available_liquidity_queues"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "consumed_per_user_and_nonce_chain_id_offering_nonce_created_at_idx" ON "offers_v1.1"."consumed_per_user_and_nonce" USING btree ("chain_id","offering","nonce","created_at" desc);--> statement-breakpoint
CREATE UNIQUE INDEX "indexing_progress_chain_event_unique" ON "offers_v1.1"."indexing_progress" USING btree ("chain_id","event_type");--> statement-breakpoint
CREATE INDEX "offer_collaterals_offer_hash_idx" ON "offers_v1.1"."offer_collaterals" USING btree ("offer_hash");--> statement-breakpoint
CREATE INDEX "offer_collaterals_asset_idx" ON "offers_v1.1"."offer_collaterals" USING btree ("asset");--> statement-breakpoint
CREATE INDEX "offer_collaterals_oracle_idx" ON "offers_v1.1"."offer_collaterals" USING btree ("oracle");--> statement-breakpoint
CREATE INDEX "offer_collaterals_tuple_idx" ON "offers_v1.1"."offer_collaterals" USING btree ("asset","oracle","lltv");--> statement-breakpoint
CREATE INDEX "offer_status_offer_hash_created_at_idx" ON "offers_v1.1"."offer_status" USING btree ("offer_hash","created_at" desc);--> statement-breakpoint
CREATE INDEX "offer_status_status_idx" ON "offers_v1.1"."offer_status" USING btree ("status");--> statement-breakpoint
CREATE INDEX "offers_offering_idx" ON "offers_v1.1"."offers" USING btree ("offering");--> statement-breakpoint
CREATE INDEX "offers_buy_idx" ON "offers_v1.1"."offers" USING btree ("buy");--> statement-breakpoint
CREATE INDEX "offers_chain_id_idx" ON "offers_v1.1"."offers" USING btree ("chain_id");--> statement-breakpoint
CREATE INDEX "offers_loan_token_idx" ON "offers_v1.1"."offers" USING btree ("loan_token");--> statement-breakpoint
CREATE INDEX "offers_maturity_idx" ON "offers_v1.1"."offers" USING btree ("maturity");--> statement-breakpoint
CREATE INDEX "offers_expiry_idx" ON "offers_v1.1"."offers" USING btree ("expiry");--> statement-breakpoint
CREATE INDEX "offers_rate_idx" ON "offers_v1.1"."offers" USING btree ("rate");--> statement-breakpoint
CREATE INDEX "offers_assets_idx" ON "offers_v1.1"."offers" USING btree ("assets");--> statement-breakpoint
CREATE INDEX "offers_rate_hash_idx" ON "offers_v1.1"."offers" USING btree ("rate","hash");--> statement-breakpoint
CREATE INDEX "offers_maturity_hash_idx" ON "offers_v1.1"."offers" USING btree ("maturity","hash");--> statement-breakpoint
CREATE INDEX "offers_expiry_hash_idx" ON "offers_v1.1"."offers" USING btree ("expiry","hash");--> statement-breakpoint
CREATE INDEX "offers_assets_hash_idx" ON "offers_v1.1"."offers" USING btree ("assets","hash");