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

Issue 508 13 #1595

Merged
merged 3 commits into from
Jan 19, 2024
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
12 changes: 8 additions & 4 deletions app/controllers/api/v1/omiga_inscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ def index
end

def show
udt = Udt.find_by!(type_hash: params[:id])
render json: UdtSerializer.new(udt)
rescue ActiveRecord::RecordNotFound
raise Api::V1::Exceptions::UdtNotFoundError
udt = Udt.joins(:omiga_inscription_info).where(
"udts.type_hash = ? or omiga_inscription_infos.type_hash = ?", params[:id], params[:id]
).first
if udt.nil?
raise Api::V1::Exceptions::UdtNotFoundError
else
render json: UdtSerializer.new(udt)
end
end

def download_csv
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module CsvExportable
class ExportOmigaInscriptionTransactionsJob < BaseExporter
def perform(args)
udt = Udt.find_by!(type_hash: args[:id], published: true)
udt = Udt.joins(:omiga_inscription_info).where(
"udts.type_hash = ? or omiga_inscription_infos.type_hash = ?", args[:id], args[:id]
).first
ckb_transactions = udt.ckb_transactions

if args[:start_date].present?
Expand Down
52 changes: 38 additions & 14 deletions app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,14 @@ def update_or_create_udt_accounts!(local_block)
end

def udt_type(cell_type)
cell_type == "udt" ? "sudt" : cell_type
case cell_type
when "udt"
"sudt"
when "omiga_inscription_info"
"omiga_inscription"
else
cell_type
end
end

def udt_account_amount(udt_type, type_hash, address)
Expand Down Expand Up @@ -657,18 +664,31 @@ def update_block_info!(local_block)

def build_udts!(local_block, outputs, outputs_data)
udts_attributes = Set.new
omiga_inscription_udt_attributes = Set.new

outputs.each do |tx_index, items|
items.each_with_index do |output, index|
cell_type = cell_type(output.type,outputs_data[tx_index][index])
if cell_type == "omiga_inscription_info"
info = CkbUtils.parse_omiga_inscription_info(outputs_data[tx_index][index])
OmigaInscriptionInfo.upsert(info.merge(output.type.to_h),
unique_by: :udt_hash)
end
cell_type = cell_type(output.type, outputs_data[tx_index][index])
next unless cell_type.in?(%w(udt m_nft_token nrc_721_token spore_cell
omiga_inscription))
omiga_inscription_info omiga_inscription))

type_hash =
if cell_type == "omiga_inscription_info"
info = CkbUtils.parse_omiga_inscription_info(outputs_data[tx_index][index])
OmigaInscriptionInfo.upsert(
info.merge(output.type.to_h,
type_hash: output.type.compute_hash), unique_by: :udt_hash
)
info[:udt_hash]
else
output.type.compute_hash
end

if cell_type == "omiga_inscription"
omiga_inscription_udt_attributes << { type_hash:,
code_hash: output.type.code_hash, hash_type: output.type.hash_type, args: output.type.args }
end

type_hash = output.type.compute_hash
unless Udt.where(type_hash:).exists?
nft_token_attr = { full_name: nil, icon_file: nil,
published: false, symbol: nil, decimal: nil, nrc_factory_cell_id: nil }
Expand Down Expand Up @@ -728,11 +748,11 @@ def build_udts!(local_block, outputs, outputs_data)
end
nft_token_attr[:published] = true
end
if cell_type == "omiga_inscription"
info = OmigaInscriptionInfo.find_by!(udt_hash: type_hash)
nft_token_attr[:full_name] = info.name
nft_token_attr[:symbol] = info.symbol
nft_token_attr[:decimal] = info.decimal
if cell_type == "omiga_inscription_info"
info = CkbUtils.parse_omiga_inscription_info(outputs_data[tx_index][index])
nft_token_attr[:full_name] = info[:name]
nft_token_attr[:symbol] = info[:symbol]
nft_token_attr[:decimal] = info[:decimal]
nft_token_attr[:published] = true
end
# fill issuer_address after publish the token
Expand All @@ -758,6 +778,10 @@ def build_udts!(local_block, outputs, outputs_data)
unique_by: :udt_hash)
end
end

if omiga_inscription_udt_attributes.present?
Udt.upsert_all(omiga_inscription_udt_attributes, unique_by: :type_hash)
end
end

def update_ckb_txs_rel_and_fee(
Expand Down
1 change: 1 addition & 0 deletions app/models/omiga_inscription_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class OmigaInscriptionInfo < ApplicationRecord
# udt_id :bigint
# created_at :datetime not null
# updated_at :datetime not null
# type_hash :binary
#
# Indexes
#
Expand Down
6 changes: 6 additions & 0 deletions app/serializers/udt_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ class UdtSerializer
} do |object|
object.omiga_inscription_info.args
end

attribute :info_type_hash, if: Proc.new { |record, _params|
record.udt_type == "omiga_inscription"
} do |object|
object.omiga_inscription_info.type_hash
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTypeHashToOmigaInscriptionInfo < ActiveRecord::Migration[7.0]
def change
add_column :omiga_inscription_infos, :type_hash, :binary
end
end
6 changes: 4 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,8 @@ CREATE TABLE public.omiga_inscription_infos (
mint_status integer,
udt_id bigint,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
updated_at timestamp(6) without time zone NOT NULL,
type_hash bytea
);


Expand Down Expand Up @@ -4816,6 +4817,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20230918033957'),
('20231017074221'),
('20231218082938'),
('20240107100346');
('20240107100346'),
('20240118103947');


16 changes: 15 additions & 1 deletion test/controllers/api/v1/omiga_inscriptions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,21 @@ class OmigaInscriptionsControllerTest < ActionDispatch::IntegrationTest
assert_equal %w(
symbol full_name display_name uan total_amount addresses_count
decimal icon_file h24_ckb_transactions_count created_at description
published type_hash type_script issuer_address mint_status mint_limit expected_supply inscription_info_id udt_type
published type_hash type_script issuer_address mint_status mint_limit expected_supply inscription_info_id udt_type info_type_hash
).sort,
response_udt["attributes"].keys.sort
end

test "should contain right keys in the serialized object when query info type hash" do
udt = create(:udt, :omiga_inscription)

valid_get api_v1_omiga_inscription_url(udt.omiga_inscription_info.type_hash)

response_udt = json["data"]
assert_equal %w(
symbol full_name display_name uan total_amount addresses_count
decimal icon_file h24_ckb_transactions_count created_at description
published type_hash type_script issuer_address mint_status mint_limit expected_supply inscription_info_id udt_type info_type_hash
).sort,
response_udt["attributes"].keys.sort
end
Expand Down
1 change: 1 addition & 0 deletions test/factories/udt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
code_hash: "0x50fdea2d0030a8d0b3d69f883b471cab2a29cae6f01923f19cecac0f27fdaaa6",
hash_type: "type",
args: "0xcd89d8f36593a9a82501c024c5cdc4877ca11c5b3d5831b3e78334aecb978f0d",
type_hash: "0x5cfcab1fc499de7d33265b04d2de9cf2f91cc7c7a578642993b0912b31b6cf39",
decimal: udt.decimal,
name: udt.full_name,
symbol: udt.symbol,
Expand Down
Loading