Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFT Database Schema #2070

Merged
merged 9 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/design/nft.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ the mirror node can be updated to add support for NFTs.

- Update `t_transaction_results` with new response codes

- Add to `token` table fields `type` (enum, values `FUNGIBLE_COMMON` and `NON_FUNGIBLE_UNIQUE`), `token_supply_type` (
- Add to `token` table fields `type` (enum, values `FUNGIBLE_COMMON` and `NON_FUNGIBLE_UNIQUE`), `supply_type` (
enum, values `INFINITE` and `FINITE`), and `max_supply` (bigint).
- Default values will be `FUNGIBLE_COMMON`, `INFINITE`, and max long, respectively.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
-------------------
-- Update and add tables for NFT support
-------------------

-- Create the enums for the new token columns
CREATE TYPE token_supply_type AS ENUM ('INFINITE', 'FINITE');
CREATE TYPE token_type AS ENUM ('FUNGIBLE_COMMON', 'NON_FUNGIBLE_UNIQUE');


-- Update the token table
alter table token
add column max_supply bigint not null default 9223372036854775807, -- max long
add column supply_type token_supply_type not null default 'INFINITE',
add column type token_type not null default 'FUNGIBLE_COMMON';

-- Create nft table
create table if not exists nft
(
account_id bigint not null,
created_timestamp bigint primary key not null,
deleted boolean default false not null,
modified_timestamp bigint not null,
metadata bytea default '' not null,
serial_number bigint not null,
token_id bigint not null
);
create unique index if not exists nft__token_id_serial_num
on nft (token_id desc, serial_number desc);
comment on table nft is 'Non-Fungible Tokens (NFTs) minted on network';

-- Create nft_transfer table
create table if not exists nft_transfer
(
consensus_timestamp bigint not null,
receiver_account_id bigint not null,
sender_account_id bigint not null,
serial_number bigint not null,
token_id bigint not null
);
create unique index if not exists nft_transfer__timestamp_token_id_serial_num
on nft_transfer (consensus_timestamp desc, token_id desc, serial_number desc);
comment on table nft_transfer is 'Crypto account nft transfers';

-- Insert new response codes
insert into t_transaction_results (result, proto_id)
values ('ACCOUNT_EXPIRED_AND_PENDING_REMOVAL', 223),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one isn't part of the NFT protobufs change, but I noticed we were missing it.

('INVALID_TOKEN_MAX_SUPPLY', 224),
('INVALID_TOKEN_NFT_SERIAL_NUMBER', 225),
('INVALID_NFT_ID', 226);
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
-- Supports mirror nodes migrated from v1.0
-------------------

-- Create enums for tables
CREATE TYPE token_supply_type AS ENUM ('INFINITE', 'FINITE');
CREATE TYPE token_type AS ENUM ('FUNGIBLE_COMMON', 'NON_FUNGIBLE_UNIQUE');

Comment on lines +6 to +9
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any opinions on creating all enums at the top vs. creating them in their corresponding table sections?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create at top in one group as you're doing so that the data types exist prior to table definitions.

-- account_balance
create table if not exists account_balance
(
Expand Down Expand Up @@ -137,6 +141,30 @@ create table if not exists live_hash
consensus_timestamp bigint not null
);

-- nft
create table if not exists nft
(
account_id bigint not null,
created_timestamp bigint primary key not null,
deleted boolean default false not null,
modified_timestamp bigint not null,
metadata bytea default '' not null,
serial_number bigint not null,
token_id bigint not null
);
comment on table nft is 'Non-Fungible Tokens (NFTs) minted on network';

-- nft_transfer
create table if not exists nft_transfer
(
consensus_timestamp bigint not null,
receiver_account_id bigint not null,
sender_account_id bigint not null,
serial_number bigint not null,
token_id bigint not null
);
comment on table nft_transfer is 'Crypto account nft transfers';

-- non_fee_transfer
create table if not exists non_fee_transfer
(
Expand Down Expand Up @@ -228,13 +256,16 @@ create table if not exists token
initial_supply bigint not null,
kyc_key bytea,
kyc_key_ed25519_hex varchar null,
max_supply bigint not null default 9223372036854775807, -- max long
modified_timestamp bigint not null,
name character varying(100) not null,
supply_key bytea,
supply_key_ed25519_hex varchar null,
supply_type token_supply_type not null default 'INFINITE',
symbol character varying(100) not null,
total_supply bigint not null default 0,
treasury_account_id bigint not null,
type token_type not null default 'FUNGIBLE_COMMON',
wipe_key bytea,
wipe_key_ed25519_hex varchar null
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ select create_hypertable('file_data', 'consensus_timestamp', chunk_time_interval
select create_hypertable('live_hash', 'consensus_timestamp', chunk_time_interval => ${chunkTimeInterval},
create_default_indexes => false, if_not_exists => true);

-- nft
select create_hypertable('nft', 'created_timestamp', chunk_time_interval => ${chunkTimeInterval},
create_default_indexes => false, if_not_exists => true);

-- nft_transfer
select create_hypertable('nft_transfer', 'consensus_timestamp', chunk_time_interval => ${chunkTimeInterval},
create_default_indexes => false, if_not_exists => true);

-- non_fee_transfer
select create_hypertable('non_fee_transfer', 'consensus_timestamp', chunk_time_interval => ${chunkTimeInterval},
create_default_indexes => false, if_not_exists => true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ alter table file_data
alter table live_hash
add primary key (consensus_timestamp);

-- nft
create unique index if not exists nft__token_id_serial_num_timestamp
on nft (token_id desc, serial_number desc, created_timestamp desc);

-- nft_transfer
create unique index if not exists nft_transfer__timestamp_token_id_serial_num
on nft_transfer (consensus_timestamp desc, token_id desc, serial_number desc);

-- non_fee_transfer
create index if not exists non_fee_transfer__consensus_timestamp
on non_fee_transfer (consensus_timestamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ select set_integer_now_func('crypto_transfer', 'latest_consensus_timestamp');
select set_integer_now_func('event_file', 'latest_consensus_timestamp');
select set_integer_now_func('file_data', 'latest_consensus_timestamp');
select set_integer_now_func('live_hash', 'latest_consensus_timestamp');
select set_integer_now_func('nft_transfer', 'latest_consensus_timestamp');
select set_integer_now_func('non_fee_transfer', 'latest_consensus_timestamp');
select set_integer_now_func('record_file', 'latest_consensus_timestamp');
select set_integer_now_func('transaction_signature', 'latest_consensus_timestamp');
Expand Down Expand Up @@ -68,6 +69,11 @@ alter table file_data
alter table live_hash
set (timescaledb.compress);

-- nft skipped as update on compressed chunk is not allowed

alter table nft_transfer
set (timescaledb.compress, timescaledb.compress_segmentby = 'token_id');

alter table non_fee_transfer
set (timescaledb.compress, timescaledb.compress_segmentby = 'entity_id');

Expand Down Expand Up @@ -112,6 +118,7 @@ select add_compression_policy('crypto_transfer', bigint '${compressionAge}');
select add_compression_policy('event_file', bigint '${compressionAge}');
select add_compression_policy('file_data', bigint '${compressionAge}');
select add_compression_policy('live_hash', bigint '${compressionAge}');
select add_compression_policy('nft_transfer', bigint '${compressionAge}');
select add_compression_policy('non_fee_transfer', bigint '${compressionAge}');
select add_compression_policy('record_file', bigint '${compressionAge}');
select add_compression_policy('token_balance', bigint '${compressionAge}');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ values ('OK', 0),
('THROTTLE_GROUP_HAS_ZERO_OPS_PER_SEC', 219),
('SUCCESS_BUT_MISSING_EXPECTED_OPERATION', 220),
('UNPARSEABLE_THROTTLE_DEFINITIONS', 221),
('INVALID_THROTTLE_DEFINITIONS', 222);
('INVALID_THROTTLE_DEFINITIONS', 222),
('ACCOUNT_EXPIRED_AND_PENDING_REMOVAL', 223),
('INVALID_TOKEN_MAX_SUPPLY', 224),
('INVALID_TOKEN_NFT_SERIAL_NUMBER', 225),
('INVALID_NFT_ID', 226);

-- t_transaction_types
insert into t_transaction_types (proto_id, name, entity_type)
Expand Down