diff --git a/backend/services/auth-service/prisma/migrations/20260111000000_init/migration.sql b/backend/services/auth-service/prisma/migrations/20260111000000_init/migration.sql new file mode 100644 index 00000000..57d34ce1 --- /dev/null +++ b/backend/services/auth-service/prisma/migrations/20260111000000_init/migration.sql @@ -0,0 +1,237 @@ +-- CreateEnum +CREATE TYPE "UserStatus" AS ENUM ('ACTIVE', 'DISABLED', 'DELETED'); + +-- CreateEnum +CREATE TYPE "KycStatus" AS ENUM ('PENDING', 'SUBMITTED', 'VERIFIED', 'REJECTED'); + +-- CreateEnum +CREATE TYPE "SmsVerificationType" AS ENUM ('REGISTER', 'LOGIN', 'RESET_PASSWORD', 'CHANGE_PHONE'); + +-- CreateEnum +CREATE TYPE "SmsStatus" AS ENUM ('PENDING', 'SENT', 'DELIVERED', 'FAILED'); + +-- CreateEnum +CREATE TYPE "LoginType" AS ENUM ('PASSWORD', 'SMS_CODE', 'LEGACY_MIGRATE'); + +-- CreateEnum +CREATE TYPE "OutboxStatus" AS ENUM ('PENDING', 'PUBLISHED', 'FAILED'); + +-- CreateTable +CREATE TABLE "users" ( + "id" BIGSERIAL NOT NULL, + "phone" TEXT NOT NULL, + "password_hash" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "status" "UserStatus" NOT NULL DEFAULT 'ACTIVE', + "kycStatus" "KycStatus" NOT NULL DEFAULT 'PENDING', + "real_name" TEXT, + "id_card_no" TEXT, + "id_card_front" TEXT, + "id_card_back" TEXT, + "kyc_submitted_at" TIMESTAMP(3), + "kyc_verified_at" TIMESTAMP(3), + "kyc_reject_reason" TEXT, + "login_fail_count" INTEGER NOT NULL DEFAULT 0, + "locked_until" TIMESTAMP(3), + "last_login_at" TIMESTAMP(3), + "last_login_ip" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "users_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_legacy_users" ( + "id" BIGSERIAL NOT NULL, + "legacy_id" BIGINT NOT NULL, + "account_sequence" TEXT NOT NULL, + "phone" TEXT NOT NULL, + "password_hash" TEXT NOT NULL, + "status" TEXT NOT NULL, + "legacy_created_at" TIMESTAMP(3) NOT NULL, + "migrated_to_v2" BOOLEAN NOT NULL DEFAULT false, + "migrated_at" TIMESTAMP(3), + "source_sequence_num" BIGINT NOT NULL, + "synced_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "synced_legacy_users_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "refresh_tokens" ( + "id" BIGSERIAL NOT NULL, + "user_id" BIGINT NOT NULL, + "token" TEXT NOT NULL, + "device_info" TEXT, + "ip_address" TEXT, + "expires_at" TIMESTAMP(3) NOT NULL, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "revoked_at" TIMESTAMP(3), + + CONSTRAINT "refresh_tokens_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "sms_verifications" ( + "id" BIGSERIAL NOT NULL, + "phone" TEXT NOT NULL, + "code" TEXT NOT NULL, + "type" "SmsVerificationType" NOT NULL, + "expires_at" TIMESTAMP(3) NOT NULL, + "verified_at" TIMESTAMP(3), + "attempts" INTEGER NOT NULL DEFAULT 0, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "sms_verifications_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "sms_logs" ( + "id" BIGSERIAL NOT NULL, + "user_id" BIGINT, + "phone" TEXT NOT NULL, + "type" "SmsVerificationType" NOT NULL, + "content" TEXT, + "status" "SmsStatus" NOT NULL DEFAULT 'PENDING', + "provider" TEXT, + "provider_id" TEXT, + "error_msg" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "sms_logs_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "login_logs" ( + "id" BIGSERIAL NOT NULL, + "user_id" BIGINT, + "phone" TEXT NOT NULL, + "type" "LoginType" NOT NULL, + "success" BOOLEAN NOT NULL, + "fail_reason" TEXT, + "ip_address" TEXT, + "user_agent" TEXT, + "device_info" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "login_logs_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "daily_sequence_counters" ( + "id" BIGSERIAL NOT NULL, + "date_key" TEXT NOT NULL, + "last_seq" INTEGER NOT NULL DEFAULT 0, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "daily_sequence_counters_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "outbox_events" ( + "id" BIGSERIAL NOT NULL, + "aggregate_type" TEXT NOT NULL, + "aggregate_id" TEXT NOT NULL, + "event_type" TEXT NOT NULL, + "payload" JSONB NOT NULL, + "topic" TEXT NOT NULL, + "key" TEXT NOT NULL, + "status" "OutboxStatus" NOT NULL DEFAULT 'PENDING', + "retry_count" INTEGER NOT NULL DEFAULT 0, + "max_retries" INTEGER NOT NULL DEFAULT 3, + "last_error" TEXT, + "published_at" TIMESTAMP(3), + "next_retry_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "outbox_events_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "users_phone_key" ON "users"("phone"); + +-- CreateIndex +CREATE UNIQUE INDEX "users_account_sequence_key" ON "users"("account_sequence"); + +-- CreateIndex +CREATE INDEX "users_phone_idx" ON "users"("phone"); + +-- CreateIndex +CREATE INDEX "users_account_sequence_idx" ON "users"("account_sequence"); + +-- CreateIndex +CREATE INDEX "users_status_idx" ON "users"("status"); + +-- CreateIndex +CREATE INDEX "users_kycStatus_idx" ON "users"("kycStatus"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_legacy_users_legacy_id_key" ON "synced_legacy_users"("legacy_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_legacy_users_account_sequence_key" ON "synced_legacy_users"("account_sequence"); + +-- CreateIndex +CREATE INDEX "synced_legacy_users_phone_idx" ON "synced_legacy_users"("phone"); + +-- CreateIndex +CREATE INDEX "synced_legacy_users_account_sequence_idx" ON "synced_legacy_users"("account_sequence"); + +-- CreateIndex +CREATE INDEX "synced_legacy_users_migrated_to_v2_idx" ON "synced_legacy_users"("migrated_to_v2"); + +-- CreateIndex +CREATE UNIQUE INDEX "refresh_tokens_token_key" ON "refresh_tokens"("token"); + +-- CreateIndex +CREATE INDEX "refresh_tokens_user_id_idx" ON "refresh_tokens"("user_id"); + +-- CreateIndex +CREATE INDEX "refresh_tokens_token_idx" ON "refresh_tokens"("token"); + +-- CreateIndex +CREATE INDEX "refresh_tokens_expires_at_idx" ON "refresh_tokens"("expires_at"); + +-- CreateIndex +CREATE INDEX "sms_verifications_phone_type_idx" ON "sms_verifications"("phone", "type"); + +-- CreateIndex +CREATE INDEX "sms_verifications_expires_at_idx" ON "sms_verifications"("expires_at"); + +-- CreateIndex +CREATE INDEX "sms_logs_phone_idx" ON "sms_logs"("phone"); + +-- CreateIndex +CREATE INDEX "sms_logs_user_id_idx" ON "sms_logs"("user_id"); + +-- CreateIndex +CREATE INDEX "sms_logs_created_at_idx" ON "sms_logs"("created_at"); + +-- CreateIndex +CREATE INDEX "login_logs_user_id_idx" ON "login_logs"("user_id"); + +-- CreateIndex +CREATE INDEX "login_logs_phone_idx" ON "login_logs"("phone"); + +-- CreateIndex +CREATE INDEX "login_logs_created_at_idx" ON "login_logs"("created_at"); + +-- CreateIndex +CREATE UNIQUE INDEX "daily_sequence_counters_date_key_key" ON "daily_sequence_counters"("date_key"); + +-- CreateIndex +CREATE INDEX "outbox_events_status_idx" ON "outbox_events"("status"); + +-- CreateIndex +CREATE INDEX "outbox_events_next_retry_at_idx" ON "outbox_events"("next_retry_at"); + +-- AddForeignKey +ALTER TABLE "refresh_tokens" ADD CONSTRAINT "refresh_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "sms_logs" ADD CONSTRAINT "sms_logs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "login_logs" ADD CONSTRAINT "login_logs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; + diff --git a/backend/services/mining-admin-service/prisma/migrations/20260111000000_init/migration.sql b/backend/services/mining-admin-service/prisma/migrations/20260111000000_init/migration.sql new file mode 100644 index 00000000..5e34ec03 --- /dev/null +++ b/backend/services/mining-admin-service/prisma/migrations/20260111000000_init/migration.sql @@ -0,0 +1,749 @@ +-- CreateTable +CREATE TABLE "admin_users" ( + "id" TEXT NOT NULL, + "username" TEXT NOT NULL, + "password" TEXT NOT NULL, + "name" TEXT NOT NULL, + "role" TEXT NOT NULL, + "status" TEXT NOT NULL DEFAULT 'ACTIVE', + "lastLoginAt" TIMESTAMP(3), + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "admin_users_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "system_configs" ( + "id" TEXT NOT NULL, + "category" TEXT NOT NULL, + "key" TEXT NOT NULL, + "value" TEXT NOT NULL, + "description" TEXT, + "isPublic" BOOLEAN NOT NULL DEFAULT false, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "system_configs_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "system_accounts" ( + "id" TEXT NOT NULL, + "accountType" TEXT NOT NULL, + "name" TEXT NOT NULL, + "description" TEXT, + "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "system_accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "initialization_records" ( + "id" TEXT NOT NULL, + "type" TEXT NOT NULL, + "status" TEXT NOT NULL, + "config" JSONB NOT NULL, + "executedBy" TEXT NOT NULL, + "executedAt" TIMESTAMP(3), + "errorMessage" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "initialization_records_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "audit_logs" ( + "id" TEXT NOT NULL, + "adminId" TEXT NOT NULL, + "action" TEXT NOT NULL, + "resource" TEXT NOT NULL, + "resourceId" TEXT, + "oldValue" JSONB, + "newValue" JSONB, + "ipAddress" TEXT, + "userAgent" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "audit_logs_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "daily_reports" ( + "id" TEXT NOT NULL, + "reportDate" DATE NOT NULL, + "totalUsers" INTEGER NOT NULL DEFAULT 0, + "newUsers" INTEGER NOT NULL DEFAULT 0, + "activeUsers" INTEGER NOT NULL DEFAULT 0, + "totalAdoptions" INTEGER NOT NULL DEFAULT 0, + "newAdoptions" INTEGER NOT NULL DEFAULT 0, + "totalTrees" INTEGER NOT NULL DEFAULT 0, + "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0, + "contributionGrowth" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalDistributed" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalBurned" DECIMAL(30,8) NOT NULL DEFAULT 0, + "tradingVolume" DECIMAL(30,8) NOT NULL DEFAULT 0, + "tradingAmount" DECIMAL(30,8) NOT NULL DEFAULT 0, + "tradeCount" INTEGER NOT NULL DEFAULT 0, + "openPrice" DECIMAL(30,18) NOT NULL DEFAULT 1, + "closePrice" DECIMAL(30,18) NOT NULL DEFAULT 1, + "highPrice" DECIMAL(30,18) NOT NULL DEFAULT 1, + "lowPrice" DECIMAL(30,18) NOT NULL DEFAULT 1, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "daily_reports_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_users" ( + "id" TEXT NOT NULL, + "originalUserId" TEXT NOT NULL, + "accountSequence" TEXT NOT NULL, + "phone" TEXT NOT NULL, + "status" TEXT NOT NULL, + "kycStatus" TEXT NOT NULL, + "realName" TEXT, + "isLegacyUser" BOOLEAN NOT NULL DEFAULT false, + "createdAt" TIMESTAMP(3) NOT NULL, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_users_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_contribution_accounts" ( + "id" TEXT NOT NULL, + "accountSequence" TEXT NOT NULL, + "personalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0, + "teamLevelContribution" DECIMAL(30,8) NOT NULL DEFAULT 0, + "teamBonusContribution" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0, + "effectiveContribution" DECIMAL(30,8) NOT NULL DEFAULT 0, + "hasAdopted" BOOLEAN NOT NULL DEFAULT false, + "directReferralCount" INTEGER NOT NULL DEFAULT 0, + "unlockedLevelDepth" INTEGER NOT NULL DEFAULT 0, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_contribution_accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_mining_accounts" ( + "id" TEXT NOT NULL, + "accountSequence" TEXT NOT NULL, + "totalMined" DECIMAL(30,8) NOT NULL DEFAULT 0, + "availableBalance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozenBalance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_mining_accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_trading_accounts" ( + "id" TEXT NOT NULL, + "accountSequence" TEXT NOT NULL, + "shareBalance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "cashBalance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozenShares" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozenCash" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalBought" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalSold" DECIMAL(30,8) NOT NULL DEFAULT 0, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_trading_accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_mining_configs" ( + "id" TEXT NOT NULL, + "totalShares" DECIMAL(30,8) NOT NULL, + "distributionPool" DECIMAL(30,8) NOT NULL, + "remainingDistribution" DECIMAL(30,8) NOT NULL, + "halvingPeriodYears" INTEGER NOT NULL, + "currentEra" INTEGER NOT NULL DEFAULT 1, + "minuteDistribution" DECIMAL(30,8) NOT NULL, + "isActive" BOOLEAN NOT NULL DEFAULT false, + "activatedAt" TIMESTAMP(3), + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_mining_configs_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_daily_mining_stats" ( + "id" TEXT NOT NULL, + "statDate" DATE NOT NULL, + "totalContribution" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalDistributed" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalBurned" DECIMAL(30,8) NOT NULL DEFAULT 0, + "participantCount" INTEGER NOT NULL DEFAULT 0, + "avgContributionRate" DECIMAL(30,18) NOT NULL DEFAULT 0, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_daily_mining_stats_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_day_klines" ( + "id" TEXT NOT NULL, + "klineDate" DATE NOT NULL, + "open" DECIMAL(30,18) NOT NULL, + "high" DECIMAL(30,18) NOT NULL, + "low" DECIMAL(30,18) NOT NULL, + "close" DECIMAL(30,18) NOT NULL, + "volume" DECIMAL(30,8) NOT NULL DEFAULT 0, + "amount" DECIMAL(30,8) NOT NULL DEFAULT 0, + "tradeCount" INTEGER NOT NULL DEFAULT 0, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_day_klines_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_circulation_pools" ( + "id" TEXT NOT NULL, + "totalShares" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalCash" DECIMAL(30,8) NOT NULL DEFAULT 0, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_circulation_pools_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_system_contributions" ( + "id" TEXT NOT NULL, + "accountType" TEXT NOT NULL, + "name" TEXT NOT NULL, + "contributionBalance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "contributionNeverExpires" BOOLEAN NOT NULL DEFAULT false, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_system_contributions_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "cdc_sync_progress" ( + "id" TEXT NOT NULL, + "sourceTopic" TEXT NOT NULL, + "sourceService" TEXT NOT NULL, + "lastOffset" TEXT, + "lastSequenceNum" BIGINT NOT NULL DEFAULT 0, + "lastSyncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "errorCount" INTEGER NOT NULL DEFAULT 0, + "lastError" TEXT, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "cdc_sync_progress_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "processed_events" ( + "id" TEXT NOT NULL, + "eventId" TEXT NOT NULL, + "eventType" TEXT NOT NULL, + "sourceService" TEXT NOT NULL, + "processedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "processed_events_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_provinces" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "code" TEXT NOT NULL, + "name" TEXT NOT NULL, + "status" TEXT NOT NULL DEFAULT 'ACTIVE', + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_provinces_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_cities" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "province_id" TEXT NOT NULL, + "code" TEXT NOT NULL, + "name" TEXT NOT NULL, + "status" TEXT NOT NULL DEFAULT 'ACTIVE', + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_cities_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_user_region_mappings" ( + "id" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "city_id" TEXT NOT NULL, + "assigned_at" TIMESTAMP(3) NOT NULL, + "assigned_by" TEXT, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_user_region_mappings_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_wallet_system_accounts" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "account_type" TEXT NOT NULL, + "name" TEXT NOT NULL, + "code" TEXT NOT NULL, + "province_id" TEXT, + "city_id" TEXT, + "share_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "usdt_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "green_point_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozen_share" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozen_usdt" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "blockchain_address" TEXT, + "is_active" BOOLEAN NOT NULL DEFAULT true, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_wallet_system_accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_wallet_pool_accounts" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "pool_type" TEXT NOT NULL, + "name" TEXT NOT NULL, + "balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "target_burn" DECIMAL(30,8), + "remaining_burn" DECIMAL(30,8), + "is_active" BOOLEAN NOT NULL DEFAULT true, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_wallet_pool_accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_user_wallets" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "wallet_type" TEXT NOT NULL, + "balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozen_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "is_active" BOOLEAN NOT NULL DEFAULT true, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_user_wallets_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_withdraw_requests" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "request_no" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "asset_type" TEXT NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "fee" DECIMAL(30,8) NOT NULL DEFAULT 0, + "net_amount" DECIMAL(30,8) NOT NULL, + "to_address" TEXT NOT NULL, + "status" TEXT NOT NULL, + "tx_hash" TEXT, + "block_number" BIGINT, + "confirmations" INTEGER NOT NULL DEFAULT 0, + "error_message" TEXT, + "approved_by" TEXT, + "approved_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL, + "completed_at" TIMESTAMP(3), + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_withdraw_requests_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_deposit_records" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "tx_hash" TEXT NOT NULL, + "from_address" TEXT NOT NULL, + "to_address" TEXT NOT NULL, + "asset_type" TEXT NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "block_number" BIGINT NOT NULL, + "confirmations" INTEGER NOT NULL DEFAULT 0, + "matched_account_seq" TEXT, + "is_processed" BOOLEAN NOT NULL DEFAULT false, + "processed_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_deposit_records_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_dex_swap_records" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "swap_no" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "from_asset" TEXT NOT NULL, + "to_asset" TEXT NOT NULL, + "from_amount" DECIMAL(30,8) NOT NULL, + "to_amount" DECIMAL(30,8) NOT NULL, + "exchange_rate" DECIMAL(30,18) NOT NULL, + "slippage" DECIMAL(10,4) NOT NULL DEFAULT 0, + "fee" DECIMAL(30,8) NOT NULL DEFAULT 0, + "status" TEXT NOT NULL, + "tx_hash" TEXT, + "block_number" BIGINT, + "error_message" TEXT, + "created_at" TIMESTAMP(3) NOT NULL, + "completed_at" TIMESTAMP(3), + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_dex_swap_records_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_blockchain_address_bindings" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "kava_address" TEXT NOT NULL, + "is_verified" BOOLEAN NOT NULL DEFAULT false, + "verified_at" TIMESTAMP(3), + "verification_tx_hash" TEXT, + "created_at" TIMESTAMP(3) NOT NULL, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_blockchain_address_bindings_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_black_hole_contracts" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "contract_address" TEXT NOT NULL, + "name" TEXT NOT NULL, + "total_burned" DECIMAL(30,8) NOT NULL DEFAULT 0, + "target_burn" DECIMAL(30,8) NOT NULL, + "remaining_burn" DECIMAL(30,8) NOT NULL, + "is_active" BOOLEAN NOT NULL DEFAULT true, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_black_hole_contracts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_burn_to_black_hole_records" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "black_hole_id" TEXT NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "source_type" TEXT NOT NULL, + "source_account_seq" TEXT, + "source_user_id" TEXT, + "source_pool_type" TEXT, + "tx_hash" TEXT, + "block_number" BIGINT, + "memo" TEXT, + "created_at" TIMESTAMP(3) NOT NULL, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "synced_burn_to_black_hole_records_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "synced_fee_configs" ( + "id" TEXT NOT NULL, + "original_id" TEXT NOT NULL, + "fee_type" TEXT NOT NULL, + "fee_rate" DECIMAL(10,6) NOT NULL, + "min_fee" DECIMAL(30,8) NOT NULL, + "max_fee" DECIMAL(30,8), + "headquarters_rate" DECIMAL(10,6) NOT NULL, + "operation_rate" DECIMAL(10,6) NOT NULL, + "province_rate" DECIMAL(10,6) NOT NULL, + "city_rate" DECIMAL(10,6) NOT NULL, + "is_active" BOOLEAN NOT NULL DEFAULT true, + "syncedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "synced_fee_configs_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "admin_users_username_key" ON "admin_users"("username"); + +-- CreateIndex +CREATE UNIQUE INDEX "system_configs_category_key_key" ON "system_configs"("category", "key"); + +-- CreateIndex +CREATE UNIQUE INDEX "system_accounts_accountType_key" ON "system_accounts"("accountType"); + +-- CreateIndex +CREATE INDEX "audit_logs_adminId_idx" ON "audit_logs"("adminId"); + +-- CreateIndex +CREATE INDEX "audit_logs_action_idx" ON "audit_logs"("action"); + +-- CreateIndex +CREATE INDEX "audit_logs_resource_idx" ON "audit_logs"("resource"); + +-- CreateIndex +CREATE INDEX "audit_logs_createdAt_idx" ON "audit_logs"("createdAt" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "daily_reports_reportDate_key" ON "daily_reports"("reportDate"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_users_originalUserId_key" ON "synced_users"("originalUserId"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_users_accountSequence_key" ON "synced_users"("accountSequence"); + +-- CreateIndex +CREATE INDEX "synced_users_phone_idx" ON "synced_users"("phone"); + +-- CreateIndex +CREATE INDEX "synced_users_status_idx" ON "synced_users"("status"); + +-- CreateIndex +CREATE INDEX "synced_users_kycStatus_idx" ON "synced_users"("kycStatus"); + +-- CreateIndex +CREATE INDEX "synced_users_createdAt_idx" ON "synced_users"("createdAt" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_contribution_accounts_accountSequence_key" ON "synced_contribution_accounts"("accountSequence"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_mining_accounts_accountSequence_key" ON "synced_mining_accounts"("accountSequence"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_trading_accounts_accountSequence_key" ON "synced_trading_accounts"("accountSequence"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_daily_mining_stats_statDate_key" ON "synced_daily_mining_stats"("statDate"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_day_klines_klineDate_key" ON "synced_day_klines"("klineDate"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_system_contributions_accountType_key" ON "synced_system_contributions"("accountType"); + +-- CreateIndex +CREATE UNIQUE INDEX "cdc_sync_progress_sourceTopic_key" ON "cdc_sync_progress"("sourceTopic"); + +-- CreateIndex +CREATE INDEX "cdc_sync_progress_sourceService_idx" ON "cdc_sync_progress"("sourceService"); + +-- CreateIndex +CREATE UNIQUE INDEX "processed_events_eventId_key" ON "processed_events"("eventId"); + +-- CreateIndex +CREATE INDEX "processed_events_sourceService_idx" ON "processed_events"("sourceService"); + +-- CreateIndex +CREATE INDEX "processed_events_processedAt_idx" ON "processed_events"("processedAt"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_provinces_original_id_key" ON "synced_provinces"("original_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_provinces_code_key" ON "synced_provinces"("code"); + +-- CreateIndex +CREATE INDEX "synced_provinces_code_idx" ON "synced_provinces"("code"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_cities_original_id_key" ON "synced_cities"("original_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_cities_code_key" ON "synced_cities"("code"); + +-- CreateIndex +CREATE INDEX "synced_cities_province_id_idx" ON "synced_cities"("province_id"); + +-- CreateIndex +CREATE INDEX "synced_cities_code_idx" ON "synced_cities"("code"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_user_region_mappings_account_sequence_key" ON "synced_user_region_mappings"("account_sequence"); + +-- CreateIndex +CREATE INDEX "synced_user_region_mappings_city_id_idx" ON "synced_user_region_mappings"("city_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_wallet_system_accounts_original_id_key" ON "synced_wallet_system_accounts"("original_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_wallet_system_accounts_code_key" ON "synced_wallet_system_accounts"("code"); + +-- CreateIndex +CREATE INDEX "synced_wallet_system_accounts_account_type_idx" ON "synced_wallet_system_accounts"("account_type"); + +-- CreateIndex +CREATE INDEX "synced_wallet_system_accounts_province_id_idx" ON "synced_wallet_system_accounts"("province_id"); + +-- CreateIndex +CREATE INDEX "synced_wallet_system_accounts_city_id_idx" ON "synced_wallet_system_accounts"("city_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_wallet_pool_accounts_original_id_key" ON "synced_wallet_pool_accounts"("original_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_wallet_pool_accounts_pool_type_key" ON "synced_wallet_pool_accounts"("pool_type"); + +-- CreateIndex +CREATE INDEX "synced_wallet_pool_accounts_pool_type_idx" ON "synced_wallet_pool_accounts"("pool_type"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_user_wallets_original_id_key" ON "synced_user_wallets"("original_id"); + +-- CreateIndex +CREATE INDEX "synced_user_wallets_account_sequence_idx" ON "synced_user_wallets"("account_sequence"); + +-- CreateIndex +CREATE INDEX "synced_user_wallets_wallet_type_idx" ON "synced_user_wallets"("wallet_type"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_user_wallets_account_sequence_wallet_type_key" ON "synced_user_wallets"("account_sequence", "wallet_type"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_withdraw_requests_original_id_key" ON "synced_withdraw_requests"("original_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_withdraw_requests_request_no_key" ON "synced_withdraw_requests"("request_no"); + +-- CreateIndex +CREATE INDEX "synced_withdraw_requests_account_sequence_idx" ON "synced_withdraw_requests"("account_sequence"); + +-- CreateIndex +CREATE INDEX "synced_withdraw_requests_status_idx" ON "synced_withdraw_requests"("status"); + +-- CreateIndex +CREATE INDEX "synced_withdraw_requests_tx_hash_idx" ON "synced_withdraw_requests"("tx_hash"); + +-- CreateIndex +CREATE INDEX "synced_withdraw_requests_created_at_idx" ON "synced_withdraw_requests"("created_at" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_deposit_records_original_id_key" ON "synced_deposit_records"("original_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_deposit_records_tx_hash_key" ON "synced_deposit_records"("tx_hash"); + +-- CreateIndex +CREATE INDEX "synced_deposit_records_from_address_idx" ON "synced_deposit_records"("from_address"); + +-- CreateIndex +CREATE INDEX "synced_deposit_records_to_address_idx" ON "synced_deposit_records"("to_address"); + +-- CreateIndex +CREATE INDEX "synced_deposit_records_matched_account_seq_idx" ON "synced_deposit_records"("matched_account_seq"); + +-- CreateIndex +CREATE INDEX "synced_deposit_records_is_processed_idx" ON "synced_deposit_records"("is_processed"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_dex_swap_records_original_id_key" ON "synced_dex_swap_records"("original_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_dex_swap_records_swap_no_key" ON "synced_dex_swap_records"("swap_no"); + +-- CreateIndex +CREATE INDEX "synced_dex_swap_records_account_sequence_idx" ON "synced_dex_swap_records"("account_sequence"); + +-- CreateIndex +CREATE INDEX "synced_dex_swap_records_status_idx" ON "synced_dex_swap_records"("status"); + +-- CreateIndex +CREATE INDEX "synced_dex_swap_records_tx_hash_idx" ON "synced_dex_swap_records"("tx_hash"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_blockchain_address_bindings_original_id_key" ON "synced_blockchain_address_bindings"("original_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_blockchain_address_bindings_account_sequence_key" ON "synced_blockchain_address_bindings"("account_sequence"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_blockchain_address_bindings_kava_address_key" ON "synced_blockchain_address_bindings"("kava_address"); + +-- CreateIndex +CREATE INDEX "synced_blockchain_address_bindings_kava_address_idx" ON "synced_blockchain_address_bindings"("kava_address"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_black_hole_contracts_original_id_key" ON "synced_black_hole_contracts"("original_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_black_hole_contracts_contract_address_key" ON "synced_black_hole_contracts"("contract_address"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_burn_to_black_hole_records_original_id_key" ON "synced_burn_to_black_hole_records"("original_id"); + +-- CreateIndex +CREATE INDEX "synced_burn_to_black_hole_records_black_hole_id_idx" ON "synced_burn_to_black_hole_records"("black_hole_id"); + +-- CreateIndex +CREATE INDEX "synced_burn_to_black_hole_records_source_account_seq_idx" ON "synced_burn_to_black_hole_records"("source_account_seq"); + +-- CreateIndex +CREATE INDEX "synced_burn_to_black_hole_records_tx_hash_idx" ON "synced_burn_to_black_hole_records"("tx_hash"); + +-- CreateIndex +CREATE INDEX "synced_burn_to_black_hole_records_created_at_idx" ON "synced_burn_to_black_hole_records"("created_at" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_fee_configs_original_id_key" ON "synced_fee_configs"("original_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "synced_fee_configs_fee_type_key" ON "synced_fee_configs"("fee_type"); + +-- AddForeignKey +ALTER TABLE "audit_logs" ADD CONSTRAINT "audit_logs_adminId_fkey" FOREIGN KEY ("adminId") REFERENCES "admin_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "synced_contribution_accounts" ADD CONSTRAINT "synced_contribution_accounts_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "synced_users"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "synced_mining_accounts" ADD CONSTRAINT "synced_mining_accounts_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "synced_users"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "synced_trading_accounts" ADD CONSTRAINT "synced_trading_accounts_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "synced_users"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "synced_cities" ADD CONSTRAINT "synced_cities_province_id_fkey" FOREIGN KEY ("province_id") REFERENCES "synced_provinces"("original_id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "synced_user_region_mappings" ADD CONSTRAINT "synced_user_region_mappings_city_id_fkey" FOREIGN KEY ("city_id") REFERENCES "synced_cities"("original_id") ON DELETE RESTRICT ON UPDATE CASCADE; + diff --git a/backend/services/mining-wallet-service/prisma/migrations/20260111000000_init/migration.sql b/backend/services/mining-wallet-service/prisma/migrations/20260111000000_init/migration.sql new file mode 100644 index 00000000..25c47a4a --- /dev/null +++ b/backend/services/mining-wallet-service/prisma/migrations/20260111000000_init/migration.sql @@ -0,0 +1,613 @@ +-- CreateEnum +CREATE TYPE "SystemAccountType" AS ENUM ('HEADQUARTERS', 'OPERATION', 'PROVINCE', 'CITY', 'FEE', 'HOT_WALLET', 'COLD_WALLET'); + +-- CreateEnum +CREATE TYPE "PoolAccountType" AS ENUM ('SHARE_POOL', 'BLACK_HOLE_POOL', 'CIRCULATION_POOL'); + +-- CreateEnum +CREATE TYPE "UserWalletType" AS ENUM ('CONTRIBUTION', 'TOKEN_STORAGE', 'GREEN_POINTS'); + +-- CreateEnum +CREATE TYPE "AssetType" AS ENUM ('SHARE', 'USDT', 'GREEN_POINT', 'CONTRIBUTION'); + +-- CreateEnum +CREATE TYPE "TransactionType" AS ENUM ('MINING_REWARD', 'MINING_DISTRIBUTE', 'TRANSFER_IN', 'TRANSFER_OUT', 'INTERNAL_TRANSFER', 'TRADE_BUY', 'TRADE_SELL', 'WITHDRAW', 'DEPOSIT', 'BURN', 'FREEZE', 'UNFREEZE', 'FEE_COLLECT', 'FEE_DISTRIBUTE', 'POOL_INJECT', 'POOL_EXTRACT', 'ADJUSTMENT', 'INITIAL_INJECT'); + +-- CreateEnum +CREATE TYPE "CounterpartyType" AS ENUM ('USER', 'SYSTEM_ACCOUNT', 'POOL', 'BLOCKCHAIN', 'EXTERNAL'); + +-- CreateEnum +CREATE TYPE "WithdrawStatus" AS ENUM ('PENDING', 'PROCESSING', 'CONFIRMING', 'COMPLETED', 'FAILED', 'CANCELLED'); + +-- CreateEnum +CREATE TYPE "OutboxStatus" AS ENUM ('PENDING', 'PUBLISHED', 'FAILED'); + +-- CreateTable +CREATE TABLE "provinces" ( + "id" TEXT NOT NULL, + "code" TEXT NOT NULL, + "name" TEXT NOT NULL, + "status" TEXT NOT NULL DEFAULT 'ACTIVE', + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "provinces_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "cities" ( + "id" TEXT NOT NULL, + "province_id" TEXT NOT NULL, + "code" TEXT NOT NULL, + "name" TEXT NOT NULL, + "status" TEXT NOT NULL DEFAULT 'ACTIVE', + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "cities_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "user_region_mappings" ( + "id" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "city_id" TEXT NOT NULL, + "assigned_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "assigned_by" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "user_region_mappings_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "system_accounts" ( + "id" TEXT NOT NULL, + "account_type" "SystemAccountType" NOT NULL, + "name" TEXT NOT NULL, + "code" TEXT NOT NULL, + "province_id" TEXT, + "city_id" TEXT, + "share_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "usdt_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "green_point_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozen_share" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozen_usdt" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "blockchain_address" TEXT, + "description" TEXT, + "is_active" BOOLEAN NOT NULL DEFAULT true, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "system_accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "system_account_transactions" ( + "id" TEXT NOT NULL, + "system_account_id" TEXT NOT NULL, + "transaction_type" "TransactionType" NOT NULL, + "asset_type" "AssetType" NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "balance_before" DECIMAL(30,8) NOT NULL, + "balance_after" DECIMAL(30,8) NOT NULL, + "counterparty_type" "CounterpartyType", + "counterparty_account_seq" TEXT, + "counterparty_user_id" TEXT, + "counterparty_system_id" TEXT, + "counterparty_pool_type" "PoolAccountType", + "counterparty_address" TEXT, + "reference_id" TEXT, + "reference_type" TEXT, + "tx_hash" TEXT, + "memo" TEXT, + "metadata" JSONB, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "system_account_transactions_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "pool_accounts" ( + "id" TEXT NOT NULL, + "pool_type" "PoolAccountType" NOT NULL, + "name" TEXT NOT NULL, + "balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "target_burn" DECIMAL(30,8), + "remaining_burn" DECIMAL(30,8), + "is_active" BOOLEAN NOT NULL DEFAULT true, + "description" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "pool_accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "pool_account_transactions" ( + "id" TEXT NOT NULL, + "pool_account_id" TEXT NOT NULL, + "pool_type" "PoolAccountType" NOT NULL, + "transaction_type" "TransactionType" NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "balance_before" DECIMAL(30,8) NOT NULL, + "balance_after" DECIMAL(30,8) NOT NULL, + "counterparty_type" "CounterpartyType", + "counterparty_account_seq" TEXT, + "counterparty_user_id" TEXT, + "counterparty_system_id" TEXT, + "counterparty_pool_type" "PoolAccountType", + "counterparty_address" TEXT, + "reference_id" TEXT, + "reference_type" TEXT, + "tx_hash" TEXT, + "memo" TEXT, + "metadata" JSONB, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "pool_account_transactions_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "user_wallets" ( + "id" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "wallet_type" "UserWalletType" NOT NULL, + "balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozen_balance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "is_active" BOOLEAN NOT NULL DEFAULT true, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "user_wallets_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "user_wallet_transactions" ( + "id" TEXT NOT NULL, + "user_wallet_id" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "wallet_type" "UserWalletType" NOT NULL, + "transaction_type" "TransactionType" NOT NULL, + "asset_type" "AssetType" NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "balance_before" DECIMAL(30,8) NOT NULL, + "balance_after" DECIMAL(30,8) NOT NULL, + "counterparty_type" "CounterpartyType", + "counterparty_account_seq" TEXT, + "counterparty_user_id" TEXT, + "counterparty_system_id" TEXT, + "counterparty_pool_type" "PoolAccountType", + "counterparty_address" TEXT, + "reference_id" TEXT, + "reference_type" TEXT, + "tx_hash" TEXT, + "memo" TEXT, + "metadata" JSONB, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "user_wallet_transactions_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "withdraw_requests" ( + "id" TEXT NOT NULL, + "request_no" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "asset_type" "AssetType" NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "fee" DECIMAL(30,8) NOT NULL DEFAULT 0, + "net_amount" DECIMAL(30,8) NOT NULL, + "to_address" TEXT NOT NULL, + "status" "WithdrawStatus" NOT NULL DEFAULT 'PENDING', + "tx_hash" TEXT, + "block_number" BIGINT, + "confirmations" INTEGER NOT NULL DEFAULT 0, + "error_message" TEXT, + "approved_by" TEXT, + "approved_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + "completed_at" TIMESTAMP(3), + + CONSTRAINT "withdraw_requests_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "deposit_records" ( + "id" TEXT NOT NULL, + "tx_hash" TEXT NOT NULL, + "from_address" TEXT NOT NULL, + "to_address" TEXT NOT NULL, + "asset_type" "AssetType" NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "block_number" BIGINT NOT NULL, + "confirmations" INTEGER NOT NULL DEFAULT 0, + "matched_account_seq" TEXT, + "is_processed" BOOLEAN NOT NULL DEFAULT false, + "processed_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "deposit_records_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "dex_swap_records" ( + "id" TEXT NOT NULL, + "swap_no" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "from_asset" "AssetType" NOT NULL, + "to_asset" "AssetType" NOT NULL, + "from_amount" DECIMAL(30,8) NOT NULL, + "to_amount" DECIMAL(30,8) NOT NULL, + "exchange_rate" DECIMAL(30,18) NOT NULL, + "slippage" DECIMAL(10,4) NOT NULL DEFAULT 0, + "fee" DECIMAL(30,8) NOT NULL DEFAULT 0, + "status" "WithdrawStatus" NOT NULL DEFAULT 'PENDING', + "tx_hash" TEXT, + "block_number" BIGINT, + "error_message" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + "completed_at" TIMESTAMP(3), + + CONSTRAINT "dex_swap_records_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "blockchain_address_bindings" ( + "id" TEXT NOT NULL, + "account_sequence" TEXT NOT NULL, + "kava_address" TEXT NOT NULL, + "is_verified" BOOLEAN NOT NULL DEFAULT false, + "verified_at" TIMESTAMP(3), + "verification_tx_hash" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "blockchain_address_bindings_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "black_hole_contracts" ( + "id" TEXT NOT NULL, + "contract_address" TEXT NOT NULL, + "name" TEXT NOT NULL, + "total_burned" DECIMAL(30,8) NOT NULL DEFAULT 0, + "target_burn" DECIMAL(30,8) NOT NULL, + "remaining_burn" DECIMAL(30,8) NOT NULL, + "is_active" BOOLEAN NOT NULL DEFAULT true, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "black_hole_contracts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "burn_to_black_hole_records" ( + "id" TEXT NOT NULL, + "black_hole_id" TEXT NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "source_type" "CounterpartyType" NOT NULL, + "source_account_seq" TEXT, + "source_user_id" TEXT, + "source_pool_type" "PoolAccountType", + "tx_hash" TEXT, + "block_number" BIGINT, + "memo" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "burn_to_black_hole_records_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "fee_configs" ( + "id" TEXT NOT NULL, + "fee_type" TEXT NOT NULL, + "fee_rate" DECIMAL(10,6) NOT NULL, + "min_fee" DECIMAL(30,8) NOT NULL, + "max_fee" DECIMAL(30,8), + "headquarters_rate" DECIMAL(10,6) NOT NULL, + "operation_rate" DECIMAL(10,6) NOT NULL, + "province_rate" DECIMAL(10,6) NOT NULL, + "city_rate" DECIMAL(10,6) NOT NULL, + "is_active" BOOLEAN NOT NULL DEFAULT true, + "description" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "fee_configs_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "outbox_events" ( + "id" TEXT NOT NULL, + "aggregate_type" TEXT NOT NULL, + "aggregate_id" TEXT NOT NULL, + "event_type" TEXT NOT NULL, + "payload" JSONB NOT NULL, + "topic" TEXT NOT NULL DEFAULT 'mining-wallet.events', + "key" TEXT, + "status" "OutboxStatus" NOT NULL DEFAULT 'PENDING', + "retry_count" INTEGER NOT NULL DEFAULT 0, + "max_retries" INTEGER NOT NULL DEFAULT 10, + "last_error" TEXT, + "published_at" TIMESTAMP(3), + "next_retry_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "outbox_events_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "processed_events" ( + "id" TEXT NOT NULL, + "event_id" TEXT NOT NULL, + "event_type" TEXT NOT NULL, + "source_service" TEXT NOT NULL, + "processed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "processed_events_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "audit_logs" ( + "id" TEXT NOT NULL, + "operator_id" TEXT NOT NULL, + "operator_type" TEXT NOT NULL, + "action" TEXT NOT NULL, + "resource" TEXT NOT NULL, + "resource_id" TEXT, + "old_value" JSONB, + "new_value" JSONB, + "ip_address" TEXT, + "user_agent" TEXT, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "audit_logs_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "provinces_code_key" ON "provinces"("code"); + +-- CreateIndex +CREATE INDEX "provinces_code_idx" ON "provinces"("code"); + +-- CreateIndex +CREATE UNIQUE INDEX "cities_code_key" ON "cities"("code"); + +-- CreateIndex +CREATE INDEX "cities_province_id_idx" ON "cities"("province_id"); + +-- CreateIndex +CREATE INDEX "cities_code_idx" ON "cities"("code"); + +-- CreateIndex +CREATE UNIQUE INDEX "user_region_mappings_account_sequence_key" ON "user_region_mappings"("account_sequence"); + +-- CreateIndex +CREATE INDEX "user_region_mappings_city_id_idx" ON "user_region_mappings"("city_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "system_accounts_code_key" ON "system_accounts"("code"); + +-- CreateIndex +CREATE INDEX "system_accounts_account_type_idx" ON "system_accounts"("account_type"); + +-- CreateIndex +CREATE INDEX "system_accounts_province_id_idx" ON "system_accounts"("province_id"); + +-- CreateIndex +CREATE INDEX "system_accounts_city_id_idx" ON "system_accounts"("city_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "system_accounts_account_type_province_id_city_id_key" ON "system_accounts"("account_type", "province_id", "city_id"); + +-- CreateIndex +CREATE INDEX "system_account_transactions_system_account_id_created_at_idx" ON "system_account_transactions"("system_account_id", "created_at" DESC); + +-- CreateIndex +CREATE INDEX "system_account_transactions_transaction_type_idx" ON "system_account_transactions"("transaction_type"); + +-- CreateIndex +CREATE INDEX "system_account_transactions_counterparty_account_seq_idx" ON "system_account_transactions"("counterparty_account_seq"); + +-- CreateIndex +CREATE INDEX "system_account_transactions_counterparty_user_id_idx" ON "system_account_transactions"("counterparty_user_id"); + +-- CreateIndex +CREATE INDEX "system_account_transactions_reference_id_idx" ON "system_account_transactions"("reference_id"); + +-- CreateIndex +CREATE INDEX "system_account_transactions_created_at_idx" ON "system_account_transactions"("created_at" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "pool_accounts_pool_type_key" ON "pool_accounts"("pool_type"); + +-- CreateIndex +CREATE INDEX "pool_accounts_pool_type_idx" ON "pool_accounts"("pool_type"); + +-- CreateIndex +CREATE INDEX "pool_account_transactions_pool_account_id_created_at_idx" ON "pool_account_transactions"("pool_account_id", "created_at" DESC); + +-- CreateIndex +CREATE INDEX "pool_account_transactions_pool_type_transaction_type_idx" ON "pool_account_transactions"("pool_type", "transaction_type"); + +-- CreateIndex +CREATE INDEX "pool_account_transactions_counterparty_account_seq_idx" ON "pool_account_transactions"("counterparty_account_seq"); + +-- CreateIndex +CREATE INDEX "pool_account_transactions_counterparty_user_id_idx" ON "pool_account_transactions"("counterparty_user_id"); + +-- CreateIndex +CREATE INDEX "pool_account_transactions_reference_id_idx" ON "pool_account_transactions"("reference_id"); + +-- CreateIndex +CREATE INDEX "pool_account_transactions_created_at_idx" ON "pool_account_transactions"("created_at" DESC); + +-- CreateIndex +CREATE INDEX "user_wallets_account_sequence_idx" ON "user_wallets"("account_sequence"); + +-- CreateIndex +CREATE INDEX "user_wallets_wallet_type_idx" ON "user_wallets"("wallet_type"); + +-- CreateIndex +CREATE UNIQUE INDEX "user_wallets_account_sequence_wallet_type_key" ON "user_wallets"("account_sequence", "wallet_type"); + +-- CreateIndex +CREATE INDEX "user_wallet_transactions_user_wallet_id_created_at_idx" ON "user_wallet_transactions"("user_wallet_id", "created_at" DESC); + +-- CreateIndex +CREATE INDEX "user_wallet_transactions_account_sequence_wallet_type_idx" ON "user_wallet_transactions"("account_sequence", "wallet_type"); + +-- CreateIndex +CREATE INDEX "user_wallet_transactions_transaction_type_idx" ON "user_wallet_transactions"("transaction_type"); + +-- CreateIndex +CREATE INDEX "user_wallet_transactions_counterparty_account_seq_idx" ON "user_wallet_transactions"("counterparty_account_seq"); + +-- CreateIndex +CREATE INDEX "user_wallet_transactions_counterparty_user_id_idx" ON "user_wallet_transactions"("counterparty_user_id"); + +-- CreateIndex +CREATE INDEX "user_wallet_transactions_reference_id_idx" ON "user_wallet_transactions"("reference_id"); + +-- CreateIndex +CREATE INDEX "user_wallet_transactions_created_at_idx" ON "user_wallet_transactions"("created_at" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "withdraw_requests_request_no_key" ON "withdraw_requests"("request_no"); + +-- CreateIndex +CREATE INDEX "withdraw_requests_account_sequence_idx" ON "withdraw_requests"("account_sequence"); + +-- CreateIndex +CREATE INDEX "withdraw_requests_status_idx" ON "withdraw_requests"("status"); + +-- CreateIndex +CREATE INDEX "withdraw_requests_tx_hash_idx" ON "withdraw_requests"("tx_hash"); + +-- CreateIndex +CREATE INDEX "withdraw_requests_created_at_idx" ON "withdraw_requests"("created_at" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "deposit_records_tx_hash_key" ON "deposit_records"("tx_hash"); + +-- CreateIndex +CREATE INDEX "deposit_records_from_address_idx" ON "deposit_records"("from_address"); + +-- CreateIndex +CREATE INDEX "deposit_records_to_address_idx" ON "deposit_records"("to_address"); + +-- CreateIndex +CREATE INDEX "deposit_records_matched_account_seq_idx" ON "deposit_records"("matched_account_seq"); + +-- CreateIndex +CREATE INDEX "deposit_records_is_processed_idx" ON "deposit_records"("is_processed"); + +-- CreateIndex +CREATE INDEX "deposit_records_created_at_idx" ON "deposit_records"("created_at" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "dex_swap_records_swap_no_key" ON "dex_swap_records"("swap_no"); + +-- CreateIndex +CREATE INDEX "dex_swap_records_account_sequence_idx" ON "dex_swap_records"("account_sequence"); + +-- CreateIndex +CREATE INDEX "dex_swap_records_status_idx" ON "dex_swap_records"("status"); + +-- CreateIndex +CREATE INDEX "dex_swap_records_tx_hash_idx" ON "dex_swap_records"("tx_hash"); + +-- CreateIndex +CREATE INDEX "dex_swap_records_created_at_idx" ON "dex_swap_records"("created_at" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "blockchain_address_bindings_account_sequence_key" ON "blockchain_address_bindings"("account_sequence"); + +-- CreateIndex +CREATE UNIQUE INDEX "blockchain_address_bindings_kava_address_key" ON "blockchain_address_bindings"("kava_address"); + +-- CreateIndex +CREATE INDEX "blockchain_address_bindings_kava_address_idx" ON "blockchain_address_bindings"("kava_address"); + +-- CreateIndex +CREATE UNIQUE INDEX "black_hole_contracts_contract_address_key" ON "black_hole_contracts"("contract_address"); + +-- CreateIndex +CREATE INDEX "burn_to_black_hole_records_black_hole_id_idx" ON "burn_to_black_hole_records"("black_hole_id"); + +-- CreateIndex +CREATE INDEX "burn_to_black_hole_records_source_account_seq_idx" ON "burn_to_black_hole_records"("source_account_seq"); + +-- CreateIndex +CREATE INDEX "burn_to_black_hole_records_tx_hash_idx" ON "burn_to_black_hole_records"("tx_hash"); + +-- CreateIndex +CREATE INDEX "burn_to_black_hole_records_created_at_idx" ON "burn_to_black_hole_records"("created_at" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "fee_configs_fee_type_key" ON "fee_configs"("fee_type"); + +-- CreateIndex +CREATE INDEX "outbox_events_status_idx" ON "outbox_events"("status"); + +-- CreateIndex +CREATE INDEX "outbox_events_next_retry_at_idx" ON "outbox_events"("next_retry_at"); + +-- CreateIndex +CREATE INDEX "outbox_events_created_at_idx" ON "outbox_events"("created_at"); + +-- CreateIndex +CREATE UNIQUE INDEX "processed_events_event_id_key" ON "processed_events"("event_id"); + +-- CreateIndex +CREATE INDEX "processed_events_source_service_idx" ON "processed_events"("source_service"); + +-- CreateIndex +CREATE INDEX "processed_events_processed_at_idx" ON "processed_events"("processed_at"); + +-- CreateIndex +CREATE INDEX "audit_logs_operator_id_idx" ON "audit_logs"("operator_id"); + +-- CreateIndex +CREATE INDEX "audit_logs_action_idx" ON "audit_logs"("action"); + +-- CreateIndex +CREATE INDEX "audit_logs_resource_idx" ON "audit_logs"("resource"); + +-- CreateIndex +CREATE INDEX "audit_logs_created_at_idx" ON "audit_logs"("created_at" DESC); + +-- AddForeignKey +ALTER TABLE "cities" ADD CONSTRAINT "cities_province_id_fkey" FOREIGN KEY ("province_id") REFERENCES "provinces"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "user_region_mappings" ADD CONSTRAINT "user_region_mappings_city_id_fkey" FOREIGN KEY ("city_id") REFERENCES "cities"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "system_accounts" ADD CONSTRAINT "system_accounts_province_id_fkey" FOREIGN KEY ("province_id") REFERENCES "provinces"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "system_accounts" ADD CONSTRAINT "system_accounts_city_id_fkey" FOREIGN KEY ("city_id") REFERENCES "cities"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "system_account_transactions" ADD CONSTRAINT "system_account_transactions_system_account_id_fkey" FOREIGN KEY ("system_account_id") REFERENCES "system_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "pool_account_transactions" ADD CONSTRAINT "pool_account_transactions_pool_account_id_fkey" FOREIGN KEY ("pool_account_id") REFERENCES "pool_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "user_wallet_transactions" ADD CONSTRAINT "user_wallet_transactions_user_wallet_id_fkey" FOREIGN KEY ("user_wallet_id") REFERENCES "user_wallets"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "burn_to_black_hole_records" ADD CONSTRAINT "burn_to_black_hole_records_black_hole_id_fkey" FOREIGN KEY ("black_hole_id") REFERENCES "black_hole_contracts"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + diff --git a/backend/services/trading-service/prisma/migrations/20260111000000_init/migration.sql b/backend/services/trading-service/prisma/migrations/20260111000000_init/migration.sql new file mode 100644 index 00000000..1d15f8aa --- /dev/null +++ b/backend/services/trading-service/prisma/migrations/20260111000000_init/migration.sql @@ -0,0 +1,312 @@ +-- CreateEnum +CREATE TYPE "OutboxStatus" AS ENUM ('PENDING', 'PUBLISHED', 'FAILED'); + +-- CreateTable +CREATE TABLE "trading_accounts" ( + "id" TEXT NOT NULL, + "accountSequence" TEXT NOT NULL, + "shareBalance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "cashBalance" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozenShares" DECIMAL(30,8) NOT NULL DEFAULT 0, + "frozenCash" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalBought" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalSold" DECIMAL(30,8) NOT NULL DEFAULT 0, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "trading_accounts_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "orders" ( + "id" TEXT NOT NULL, + "orderNo" TEXT NOT NULL, + "accountSequence" TEXT NOT NULL, + "type" TEXT NOT NULL, + "status" TEXT NOT NULL, + "price" DECIMAL(30,18) NOT NULL, + "quantity" DECIMAL(30,8) NOT NULL, + "filledQuantity" DECIMAL(30,8) NOT NULL DEFAULT 0, + "remainingQuantity" DECIMAL(30,8) NOT NULL, + "averagePrice" DECIMAL(30,18) NOT NULL DEFAULT 0, + "totalAmount" DECIMAL(30,8) NOT NULL DEFAULT 0, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + "cancelledAt" TIMESTAMP(3), + "completedAt" TIMESTAMP(3), + + CONSTRAINT "orders_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "trades" ( + "id" TEXT NOT NULL, + "tradeNo" TEXT NOT NULL, + "buyOrderId" TEXT NOT NULL, + "sellOrderId" TEXT NOT NULL, + "buyerSequence" TEXT NOT NULL, + "sellerSequence" TEXT NOT NULL, + "price" DECIMAL(30,18) NOT NULL, + "quantity" DECIMAL(30,8) NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "trades_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "trading_transactions" ( + "id" TEXT NOT NULL, + "accountSequence" TEXT NOT NULL, + "type" TEXT NOT NULL, + "assetType" TEXT NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "balanceBefore" DECIMAL(30,8) NOT NULL, + "balanceAfter" DECIMAL(30,8) NOT NULL, + "referenceId" TEXT, + "referenceType" TEXT, + "counterparty_type" TEXT, + "counterparty_account_seq" TEXT, + "counterparty_user_id" TEXT, + "memo" TEXT, + "description" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "trading_transactions_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "circulation_pools" ( + "id" TEXT NOT NULL, + "totalShares" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalCash" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalInflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "totalOutflow" DECIMAL(30,8) NOT NULL DEFAULT 0, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "circulation_pools_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "circulation_pool_transactions" ( + "id" TEXT NOT NULL, + "pool_id" TEXT NOT NULL, + "type" TEXT NOT NULL, + "assetType" TEXT NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "balance_before" DECIMAL(30,8) NOT NULL, + "balance_after" DECIMAL(30,8) NOT NULL, + "counterparty_type" TEXT, + "counterparty_account_seq" TEXT, + "counterparty_user_id" TEXT, + "reference_id" TEXT, + "reference_type" TEXT, + "memo" TEXT, + "metadata" JSONB, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "circulation_pool_transactions_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "pool_transactions" ( + "id" TEXT NOT NULL, + "type" TEXT NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "referenceId" TEXT, + "description" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "pool_transactions_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "minute_klines" ( + "id" TEXT NOT NULL, + "minute" TIMESTAMP(3) NOT NULL, + "open" DECIMAL(30,18) NOT NULL, + "high" DECIMAL(30,18) NOT NULL, + "low" DECIMAL(30,18) NOT NULL, + "close" DECIMAL(30,18) NOT NULL, + "volume" DECIMAL(30,8) NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "tradeCount" INTEGER NOT NULL DEFAULT 0, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "minute_klines_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "hour_klines" ( + "id" TEXT NOT NULL, + "hour" TIMESTAMP(3) NOT NULL, + "open" DECIMAL(30,18) NOT NULL, + "high" DECIMAL(30,18) NOT NULL, + "low" DECIMAL(30,18) NOT NULL, + "close" DECIMAL(30,18) NOT NULL, + "volume" DECIMAL(30,8) NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "tradeCount" INTEGER NOT NULL DEFAULT 0, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "hour_klines_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "day_klines" ( + "id" TEXT NOT NULL, + "date" DATE NOT NULL, + "open" DECIMAL(30,18) NOT NULL, + "high" DECIMAL(30,18) NOT NULL, + "low" DECIMAL(30,18) NOT NULL, + "close" DECIMAL(30,18) NOT NULL, + "volume" DECIMAL(30,8) NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "tradeCount" INTEGER NOT NULL DEFAULT 0, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "day_klines_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "transfer_records" ( + "id" TEXT NOT NULL, + "transferNo" TEXT NOT NULL, + "accountSequence" TEXT NOT NULL, + "direction" TEXT NOT NULL, + "amount" DECIMAL(30,8) NOT NULL, + "status" TEXT NOT NULL, + "miningTxId" TEXT, + "errorMessage" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "completedAt" TIMESTAMP(3), + + CONSTRAINT "transfer_records_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "outbox_events" ( + "id" TEXT NOT NULL, + "aggregate_type" TEXT NOT NULL, + "aggregate_id" TEXT NOT NULL, + "event_type" TEXT NOT NULL, + "payload" JSONB NOT NULL, + "topic" TEXT NOT NULL DEFAULT 'trading.events', + "key" TEXT, + "status" "OutboxStatus" NOT NULL DEFAULT 'PENDING', + "retry_count" INTEGER NOT NULL DEFAULT 0, + "max_retries" INTEGER NOT NULL DEFAULT 10, + "last_error" TEXT, + "published_at" TIMESTAMP(3), + "next_retry_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "outbox_events_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "trading_accounts_accountSequence_key" ON "trading_accounts"("accountSequence"); + +-- CreateIndex +CREATE UNIQUE INDEX "orders_orderNo_key" ON "orders"("orderNo"); + +-- CreateIndex +CREATE INDEX "orders_accountSequence_status_idx" ON "orders"("accountSequence", "status"); + +-- CreateIndex +CREATE INDEX "orders_type_status_price_idx" ON "orders"("type", "status", "price"); + +-- CreateIndex +CREATE INDEX "orders_createdAt_idx" ON "orders"("createdAt" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "trades_tradeNo_key" ON "trades"("tradeNo"); + +-- CreateIndex +CREATE INDEX "trades_buyerSequence_idx" ON "trades"("buyerSequence"); + +-- CreateIndex +CREATE INDEX "trades_sellerSequence_idx" ON "trades"("sellerSequence"); + +-- CreateIndex +CREATE INDEX "trades_createdAt_idx" ON "trades"("createdAt" DESC); + +-- CreateIndex +CREATE INDEX "trading_transactions_accountSequence_createdAt_idx" ON "trading_transactions"("accountSequence", "createdAt" DESC); + +-- CreateIndex +CREATE INDEX "trading_transactions_counterparty_account_seq_idx" ON "trading_transactions"("counterparty_account_seq"); + +-- CreateIndex +CREATE INDEX "trading_transactions_counterparty_user_id_idx" ON "trading_transactions"("counterparty_user_id"); + +-- CreateIndex +CREATE INDEX "circulation_pool_transactions_pool_id_created_at_idx" ON "circulation_pool_transactions"("pool_id", "created_at" DESC); + +-- CreateIndex +CREATE INDEX "circulation_pool_transactions_type_assetType_idx" ON "circulation_pool_transactions"("type", "assetType"); + +-- CreateIndex +CREATE INDEX "circulation_pool_transactions_counterparty_account_seq_idx" ON "circulation_pool_transactions"("counterparty_account_seq"); + +-- CreateIndex +CREATE INDEX "circulation_pool_transactions_counterparty_user_id_idx" ON "circulation_pool_transactions"("counterparty_user_id"); + +-- CreateIndex +CREATE INDEX "circulation_pool_transactions_reference_id_idx" ON "circulation_pool_transactions"("reference_id"); + +-- CreateIndex +CREATE INDEX "circulation_pool_transactions_created_at_idx" ON "circulation_pool_transactions"("created_at" DESC); + +-- CreateIndex +CREATE INDEX "pool_transactions_createdAt_idx" ON "pool_transactions"("createdAt" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "minute_klines_minute_key" ON "minute_klines"("minute"); + +-- CreateIndex +CREATE INDEX "minute_klines_minute_idx" ON "minute_klines"("minute" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "hour_klines_hour_key" ON "hour_klines"("hour"); + +-- CreateIndex +CREATE INDEX "hour_klines_hour_idx" ON "hour_klines"("hour" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "day_klines_date_key" ON "day_klines"("date"); + +-- CreateIndex +CREATE INDEX "day_klines_date_idx" ON "day_klines"("date" DESC); + +-- CreateIndex +CREATE UNIQUE INDEX "transfer_records_transferNo_key" ON "transfer_records"("transferNo"); + +-- CreateIndex +CREATE INDEX "transfer_records_accountSequence_idx" ON "transfer_records"("accountSequence"); + +-- CreateIndex +CREATE INDEX "transfer_records_status_idx" ON "transfer_records"("status"); + +-- CreateIndex +CREATE INDEX "outbox_events_status_idx" ON "outbox_events"("status"); + +-- CreateIndex +CREATE INDEX "outbox_events_next_retry_at_idx" ON "outbox_events"("next_retry_at"); + +-- CreateIndex +CREATE INDEX "outbox_events_created_at_idx" ON "outbox_events"("created_at"); + +-- AddForeignKey +ALTER TABLE "orders" ADD CONSTRAINT "orders_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "trading_accounts"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "trades" ADD CONSTRAINT "trades_buyOrderId_fkey" FOREIGN KEY ("buyOrderId") REFERENCES "orders"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "trading_transactions" ADD CONSTRAINT "trading_transactions_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "trading_accounts"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "circulation_pool_transactions" ADD CONSTRAINT "circulation_pool_transactions_pool_id_fkey" FOREIGN KEY ("pool_id") REFERENCES "circulation_pools"("id") ON DELETE RESTRICT ON UPDATE CASCADE; +