github.com/diadata-org/diadata@v1.4.593/deployments/config/pginit.sql (about)

     1  CREATE EXTENSION "pgcrypto";
     2  
     3  
     4  -- Table asset is the single source of truth for all assets handled at DIA.
     5  -- If a field is not case sensitive (such as address for Ethereum) it should
     6  -- be all lowercase for consistency reasons.
     7  -- Otherwise it must be as defined in the underlying contract.
     8  CREATE TABLE asset (
     9      asset_id UUID DEFAULT gen_random_uuid(),
    10      symbol text NOT NULL,
    11      name text NOT NULL,
    12      decimals text,
    13      blockchain text,
    14      address text NOT NULL,
    15      UNIQUE (asset_id),
    16      UNIQUE (address, blockchain)
    17  );
    18  
    19  -- Table exchangepair holds all trading pairs for the pair scrapers.
    20  -- The format has to be the same as emitted by the exchange's API in order
    21  -- for the pair scrapers to be able to scrape trading data from the API.
    22  CREATE TABLE exchangepair (
    23      exchangepair_id UUID DEFAULT gen_random_uuid(),
    24      symbol text NOT NULL,
    25      foreignname text NOT NULL,
    26      exchange text NOT NULL,
    27      UNIQUE (foreignname, exchange),
    28      -- These fields reference asset table and should be verified by pairdiscoveryservice.
    29      -- Only trades with verified pairs are processed further and thereby enter price calculation.
    30      verified boolean default false,
    31      id_quotetoken UUID REFERENCES asset(asset_id),
    32      id_basetoken UUID REFERENCES asset(asset_id)
    33  );
    34  
    35  CREATE TABLE exchangesymbol (
    36      exchangesymbol_id UUID DEFAULT gen_random_uuid(),
    37      symbol text NOT NULL,
    38      exchange text NOT NULL,
    39      UNIQUE (symbol,exchange),
    40      verified boolean default false,
    41      asset_id UUID REFERENCES asset(asset_id)
    42  );
    43  
    44  CREATE TABLE exchange (
    45      exchange_id UUID DEFAULT gen_random_uuid(),
    46      name text NOT NULL,
    47      centralized boolean default false,
    48      bridge boolean default false,
    49      contract text,
    50      blockchain text,
    51      rest_api text,
    52      ws_api text,
    53      pairs_api text,
    54      watchdog_delay numeric NOT NULL,
    55      scraper_active boolean,
    56      UNIQUE(exchange_id),
    57      UNIQUE (name)
    58  );
    59  
    60  CREATE TABLE pool (
    61      pool_id UUID DEFAULT gen_random_uuid(),
    62      exchange text NOT NULL,
    63      blockchain text NOT NULL,
    64      address text NOT NULL,
    65      UNIQUE (pool_id),
    66      UNIQUE (blockchain,address)
    67  );
    68  
    69  CREATE TABLE poolasset (
    70      poolasset_id UUID DEFAULT gen_random_uuid(),
    71      pool_id UUID REFERENCES pool(pool_id) NOT NULL,
    72      asset_id UUID REFERENCES asset(asset_id) NOT NULL, 
    73      liquidity numeric,
    74      liquidity_usd numeric,
    75      time_stamp timestamp,
    76      token_index integer,
    77      UNIQUE (poolasset_id),
    78      UNIQUE(pool_id,asset_id)
    79  );
    80  
    81  CREATE TABLE scraper_cronjob_state (
    82      scraper_cronjob_state_id UUID DEFAULT gen_random_uuid(),
    83      scraper text NOT NULL,
    84      index_type text NOT NULL,
    85      index_value numeric,
    86      UNIQUE(scraper_cronjob_state_id),
    87      UNIQUE(scraper,index_type)
    88  );
    89  
    90  CREATE TABLE chainconfig (
    91      chain_config_id UUID DEFAULT gen_random_uuid(),
    92      rpcurl text NOT NULL,
    93      wsurl text NOT NULL,
    94      chainID text NOT NULL,
    95      UNIQUE (chainID)
    96  );
    97  
    98  -- blockchain table stores all blockchains available in our databases
    99  CREATE TABLE blockchain (
   100      blockchain_id UUID DEFAULT gen_random_uuid(),
   101      name text NOT NULL,
   102      genesisdate numeric,
   103      nativetoken_id UUID REFERENCES asset(asset_id),
   104  	verificationmechanism text,
   105      chain_id text,
   106      UNIQUE(blockchain_id),
   107      UNIQUE(name)
   108  );
   109  
   110  CREATE TABLE assetvolume (
   111      asset_id UUID primary key,
   112      volume decimal,
   113      time_stamp timestamp
   114  );
   115  
   116  -- polling table stores data - required for HTTP polling
   117  CREATE TABLE polling (
   118      polling_id UUID DEFAULT gen_random_uuid(),
   119      blockchain text,
   120      contract_address text NOT NULL,
   121      page numeric DEFAULT 1,
   122      UNIQUE(blockchain, contract_address)
   123  );
   124  
   125  -- historicalquotation collects USD quotes with lower frequency
   126  -- for a selection of assets.
   127  CREATE TABLE historicalquotation (
   128      historicalquotation_id UUID DEFAULT gen_random_uuid(),
   129      asset_id UUID REFERENCES asset(asset_id) NOT NULL, 
   130      price numeric,
   131      quote_time timestamp,
   132      source text,
   133      UNIQUE(asset_id,quote_time,source),
   134      UNIQUE(historicalquotation_id)
   135  );
   136  
   137  CREATE TABLE IF NOT EXISTS scrapers (
   138      name character varying(255) NOT NULL,
   139  	conf json,
   140  	state json,
   141      CONSTRAINT pk_scrapers PRIMARY KEY(name)
   142  );
   143  
   144  CREATE TABLE blockdata (
   145      blockdata_id UUID DEFAULT gen_random_uuid(),
   146      blockchain text NOT NULL,
   147      block_number numeric NOT NULL,
   148      block_data jsonb,
   149      UNIQUE(blockchain, block_number),
   150      UNIQUE(blockdata_id)
   151  );
   152  
   153  CREATE TABLE assetpriceident (
   154      priceident_id UUID DEFAULT gen_random_uuid(),
   155      asset_id UUID REFERENCES asset(asset_id),
   156      group_id numeric NOT NULL,
   157      rank_in_group numeric NOT NULL,
   158      UNIQUE(asset_id),
   159      UNIQUE(group_id, rank_in_group)
   160  );
   161  
   162  
   163  CREATE TABLE oracleconfig (
   164      id uuid DEFAULT gen_random_uuid(),
   165      address text NOT NULL,
   166      feeder_id text NOT NULL,
   167      owner text NOT NULL,
   168      symbols text NOT NULL,
   169      chainid text NOT NULL,
   170      active boolean DEFAULT true,
   171      frequency text,
   172      sleepseconds text,
   173      deviationpermille text,
   174      blockchainnode text DEFAULT ''::text,
   175      feeder_address text,
   176      mandatory_frequency text,
   177      deleted boolean DEFAULT false,
   178      createddate timestamp without time zone DEFAULT now() NOT NULL,
   179      lastupdate timestamp without time zone,
   180      creation_block bigint,
   181      creation_block_time timestamp without time zone DEFAULT '1970-01-01 00:00:00'::timestamp without time zone,
   182      feedselection text,
   183      expired boolean DEFAULT false,
   184      expired_time timestamp without time zone DEFAULT '1970-01-01 00:00:00'::timestamp without time zone
   185  );
   186  
   187  ALTER TABLE oracleconfig  ADD COLUMN name VARCHAR(255);
   188  ALTER TABLE oracleconfig  ADD COLUMN draft boolean DEFAULT true;
   189  ALTER TABLE oracleconfig  ADD COLUMN customer_id int ;
   190  ALTER TABLE oracleconfig  ADD COLUMN billable boolean DEFAULT false ;
   191  
   192  
   193  
   194  
   195  ALTER TABLE oracleconfig  ADD COLUMN ecosystem boolean DEFAULT false;
   196  
   197  
   198  
   199  ALTER TABLE oracleconfig
   200  ADD CONSTRAINT unique_customer_chainid_address 
   201  UNIQUE (customer_id, chainid, address);
   202  
   203  
   204  
   205  ALTER TABLE oracleconfig
   206  ADD CONSTRAINT unique_feeder_id UNIQUE (feeder_id);
   207  
   208  
   209  -- CREATE TABLE oracleconfig (
   210  --     id UUID DEFAULT gen_random_uuid(),
   211  --     address text NOT NULL,
   212  --     feeder_id text NOT NULL,
   213  --     owner text NOT NULL,
   214  --     symbols text NOT NULL,
   215  --     feeder_address text NOT NULL,
   216  --     chainID text NOT NULL,
   217  --     active  boolean default true,
   218  --     deleted  boolean default false,
   219  --     frequency text ,
   220  --     sleepseconds text,
   221  --     deviationpermille text,
   222  --     blockchainnode text,
   223  --     mandatory_frequency text,
   224  --     createddate TIMESTAMP NOT NULL DEFAULT NOW(),
   225  --     lastupdate TIMESTAMP NOT NULL,
   226  --     UNIQUE (id),
   227  --     UNIQUE (feeder_id)
   228  -- );
   229  
   230  -- ALTER TABLE oracleconfig  ADD COLUMN creation_block_time TIMESTAMP DEFAULT 'epoch'::timestamp;
   231  -- ALTER TABLE oracleconfig  ADD COLUMN feedSelection TEXT ;
   232  -- ALTER TABLE oracleconfig  ADD COLUMN expired boolean default false ;
   233  -- ALTER TABLE oracleconfig  ADD COLUMN expired_time TIMESTAMP DEFAULT 'epoch'::timestamp;
   234  
   235  
   236  
   237  
   238  CREATE TABLE feederresource (
   239      id  SERIAL PRIMARY KEY,
   240      owner text NOT NULL,
   241      total numeric NOT NULL,
   242      UNIQUE (id),
   243      UNIQUE (owner)
   244  );
   245  
   246  CREATE TABLE asset_list (
   247      id SERIAL PRIMARY KEY,
   248      asset_name VARCHAR(255) NOT NULL,
   249      custom_name VARCHAR(255),
   250      symbol VARCHAR(50),
   251      methodology TEXT,
   252      list_name TEXT
   253      
   254  );
   255  
   256  CREATE TABLE exchange_list (
   257      id SERIAL PRIMARY KEY,
   258      name VARCHAR(255) NOT NULL,
   259      asset_id INT REFERENCES asset_list(id) ON DELETE CASCADE
   260  );
   261  
   262  CREATE TABLE exchange_pairs (
   263      id SERIAL PRIMARY KEY,
   264      exchange_id INT REFERENCES exchange_list(id) ON DELETE CASCADE,
   265      pair VARCHAR(255) NOT NULL
   266  );
   267  
   268  
   269  
   270  
   271  
   272  
   273  
   274   CREATE TABLE customers (
   275      customer_id SERIAL PRIMARY KEY,
   276      email VARCHAR(255) NOT NULL,
   277      account_creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
   278      customer_plan INTEGER REFERENCES plans(plan_id) ON DELETE SET NULL,
   279      deployed_oracles INTEGER DEFAULT 0,
   280      payment_status VARCHAR(50),
   281      last_payment TIMESTAMP,
   282      payment_source VARCHAR(255),
   283      number_of_data_feeds INTEGER DEFAULT 0,
   284      active BOOLEAN DEFAULT TRUE
   285  );
   286  
   287  
   288  
   289  ALTER TABLE customers ADD COLUMN name VARCHAR(255);
   290  ALTER TABLE customers ADD COLUMN payer_address text;
   291  
   292  
   293  
   294  CREATE TABLE wallet_public_keys (
   295      key_id SERIAL PRIMARY KEY,
   296      customer_id INTEGER REFERENCES customers(customer_id) ON DELETE CASCADE,
   297      public_key TEXT NOT NULL,
   298      access_level VARCHAR(50) NOT NULL DEFAULT 'read_write',
   299      UNIQUE (public_key),
   300      CONSTRAINT check_access_level CHECK (access_level IN ('read', 'read_write'))
   301  
   302  );
   303  
   304  
   305  
   306  ALTER TABLE wallet_public_keys ADD COLUMN username VARCHAR(255) UNIQUE;
   307  
   308  CREATE TABLE wallet_public_keys_temp (
   309      key_id SERIAL PRIMARY KEY,
   310      customer_id INTEGER REFERENCES customers(customer_id) ON DELETE CASCADE,
   311      public_key TEXT NOT NULL,
   312      access_level VARCHAR(50) NOT NULL DEFAULT 'read_write',
   313      username VARCHAR(255)
   314   );
   315  
   316   ALTER TABLE wallet_public_keys_temp add invitor TEXT;
   317  
   318   ALTER TABLE wallet_public_keys_temp
   319  ADD CONSTRAINT unique_customer_public_key UNIQUE (customer_id, public_key);
   320  
   321  
   322  CREATE TABLE transfer_created (
   323      event VARCHAR(50),
   324      transaction VARCHAR(50),
   325      network_id INT,
   326      network_name VARCHAR(50),
   327      contract_address VARCHAR(42),
   328      email VARCHAR(255),
   329      company TEXT,
   330      parent VARCHAR(50),
   331      id UUID,
   332      invoice_id VARCHAR(50),
   333      bill_date TIMESTAMP,
   334      to_address VARCHAR(42),
   335      from_address VARCHAR(42),
   336      token_symbol VARCHAR(10),
   337      token_address VARCHAR(42),
   338      payment_type VARCHAR(50),
   339      usd BOOLEAN,
   340      amount NUMERIC(10, 2),
   341      item VARCHAR(255),
   342      item_id INT,
   343      source VARCHAR(50),
   344      batch_id UUID,
   345      transfer_id UUID,
   346      ref_id VARCHAR(255),
   347      agreement_id UUID
   348  );
   349  
   350  CREATE TABLE loop_payment_transfer_processed (
   351      event               VARCHAR(255) NOT NULL,
   352      transaction         VARCHAR(255) NOT NULL,
   353      network_id          INTEGER NOT NULL,
   354      network_name        VARCHAR(255) NOT NULL,
   355      contract_address    VARCHAR(255) NOT NULL,
   356      email               VARCHAR(255),
   357      company             VARCHAR(255),
   358      parent              VARCHAR(255),
   359      transfer_id         VARCHAR(255) NOT NULL,
   360      success             BOOLEAN NOT NULL,
   361      payment_token_address VARCHAR(255),
   362      payment_token_symbol VARCHAR(255),
   363      end_user            VARCHAR(255),
   364      reason              VARCHAR(255),
   365      invoice_id          VARCHAR(255),
   366      amount_paid         DOUBLE PRECISION,
   367      agreement_id        VARCHAR(255),
   368      ref_id              VARCHAR(255),
   369      batch_id            VARCHAR(255),
   370      usd_amount          VARCHAR(255)
   371  );
   372  
   373  CREATE TABLE loop_payment_responses (
   374      id SERIAL PRIMARY KEY,
   375      event TEXT,
   376      transaction TEXT,
   377      network_id INT,
   378      network_name TEXT,
   379      contract_address TEXT,
   380      email TEXT,
   381      company TEXT,
   382      parent TEXT,
   383      subscriber TEXT,
   384      item TEXT,
   385      item_id TEXT,
   386      agreement_id TEXT,
   387      agreement_amount TEXT,
   388      frequency_number INT,
   389      frequency_unit TEXT,
   390      add_on_agreements TEXT,
   391      add_on_items TEXT,
   392      add_on_item_ids TEXT,
   393      add_on_total_amount TEXT,
   394      payment_token_symbol TEXT,
   395      payment_token_address TEXT,
   396      event_date INT,
   397      ref_id TEXT,
   398      invoice_id TEXT,
   399      metadata JSONB DEFAULT '{}'::jsonb
   400  );
   401  
   402  
   403  CREATE TABLE plans (
   404      plan_id SERIAL PRIMARY KEY,
   405      plan_name VARCHAR(50) NOT NULL UNIQUE,
   406      plan_description TEXT,
   407      plan_price NUMERIC(10, 2) NOT NULL,
   408      plan_features TEXT
   409  );
   410  
   411  ALTER TABLE plans ADD COLUMN total_feeds integer default 3;
   412  ALTER TABLE plans ADD COLUMN total_oracles integer default 3;
   413  
   414  
   415  
   416  
   417  
   418  INSERT INTO  "plans"("plan_id","plan_name","plan_description","plan_price","plan_features","total_feeds")
   419  VALUES
   420  (1,E'Plan 2',E'default',0,E'desc',10);
   421  
   422  INSERT INTO  "plans"("plan_id","plan_name","plan_description","plan_price","plan_features","total_feeds")
   423  VALUES
   424  (2,E'Plan 1',E'default',0,E'desc',3);
   425