Templates/Finance

Crypto wallet database schema

Users' wallets holding balances in assets, with on-chain transactions.

5 tables, 5 relationships

Opens as a new diagram you can change. Free.

Tables

users

  • iduuid not null
  • emailtext not null
  • created_attimestamptz not null

crypto_assets

  • iduuid not null
  • symboltext not null
  • nametext not null
  • decimalsinteger not null
  • created_attimestamptz not null

wallets

  • iduuid not null
  • user_id> usersuuid not null
  • addresstext not null
  • networktext not null
  • created_attimestamptz not null

balances

  • iduuid not null
  • wallet_id> walletsuuid not null
  • asset_id> crypto_assetsuuid not null
  • amountnumeric(36,18) not null
  • created_attimestamptz not null

chain_transactions

  • iduuid not null
  • wallet_id> walletsuuid not null
  • asset_id> crypto_assetsuuid not null
  • tx_hashtext not null
  • amountnumeric(36,18) not null
  • confirmed_attimestamptz
  • created_attimestamptz not null

As PostgreSQL

The same diagram exports to MySQL, MariaDB, SQLite, SQL Server and Oracle, and to Prisma, Drizzle, SQLAlchemy and Convex models.

CREATE TABLE "balances" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "wallet_id" UUID NOT NULL,
  "asset_id" UUID NOT NULL,
  "amount" NUMERIC(36,18) DEFAULT 0 NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "chain_transactions" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "wallet_id" UUID NOT NULL,
  "asset_id" UUID NOT NULL,
  "tx_hash" TEXT NOT NULL UNIQUE,
  "amount" NUMERIC(36,18) NOT NULL,
  "confirmed_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "crypto_assets" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "symbol" TEXT NOT NULL UNIQUE,
  "name" TEXT NOT NULL,
  "decimals" INTEGER NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "users" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "email" TEXT NOT NULL UNIQUE,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "wallets" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL,
  "address" TEXT NOT NULL UNIQUE,
  "network" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "wallets" ADD CONSTRAINT "fk_wallets_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "balances" ADD CONSTRAINT "fk_balances_wallet_id" FOREIGN KEY ("wallet_id") REFERENCES "wallets" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "balances" ADD CONSTRAINT "fk_balances_asset_id" FOREIGN KEY ("asset_id") REFERENCES "crypto_assets" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "chain_transactions" ADD CONSTRAINT "fk_chain_transactions_wallet_id" FOREIGN KEY ("wallet_id") REFERENCES "wallets" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "chain_transactions" ADD CONSTRAINT "fk_chain_transactions_asset_id" FOREIGN KEY ("asset_id") REFERENCES "crypto_assets" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

More Finance templates

Start with one diagram, free

One diagram with two branches, no card needed. Upgrade when your team needs more.