From 8ff9f7cd884f2c650cfaec580d8b0007ca34b28a Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 29 Jul 2024 09:55:26 +0800 Subject: [PATCH] feat: add collection info for nft (#2100) * feat: add collection info for nft Signed-off-by: Miles Zhang * feat: support spore_cluster for extra_info Signed-off-by: Miles Zhang * test: fix test Signed-off-by: Miles Zhang --------- Signed-off-by: Miles Zhang --- .../concerns/cell_outputs/extra_info.rb | 32 ++++++++++++++++++- app/serializers/cell_output_serializer.rb | 2 ++ app/workers/update_udt_info_worker.rb | 2 +- test/models/ckb_transaction_test.rb | 15 +++++---- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/app/models/concerns/cell_outputs/extra_info.rb b/app/models/concerns/cell_outputs/extra_info.rb index 3c7a61320..60e7755b2 100644 --- a/app/models/concerns/cell_outputs/extra_info.rb +++ b/app/models/concerns/cell_outputs/extra_info.rb @@ -25,7 +25,7 @@ def m_nft_info value = { issuer_name: CkbUtils.parse_issuer_data(data).info["name"] } when "m_nft_class" parsed_data = CkbUtils.parse_token_class_data(data) - value = { class_name: parsed_data.name, total: parsed_data.total } + value = { class_name: parsed_data.name, total: parsed_data.total, type_hash: } when "m_nft_token" # issuer_id size is 20 bytes, class_id size is 4 bytes m_nft_class_type = TypeScript.where( @@ -40,6 +40,9 @@ def m_nft_info class_name: parsed_class_data.name, token_id: type_script.args[50..-1], total: parsed_class_data.total, + collection: { + type_hash: m_nft_class_type.script_hash, + }, } else value = { class_name: "", token_id: nil, total: "" } @@ -51,6 +54,30 @@ def m_nft_info CkbUtils.hash_value_to_s(value) end + def dob_info + return unless cell_type.in?(%w(spore_cluster spore_cell did_cell)) + + case cell_type + when "spore_cluster" + tc = TokenCollection.find_by(sn: type_hash) + value = { cluster_name: tc&.name, published: !tc.nil?, type_hash: } + else + ti = TokenItem.find_by(type_script_id:) + coll = ti.collection + + value = { + cluster_name: coll&.name, + token_id: ti.token_id, + collection: { + type_hash: coll&.sn, + }, + published: true, + } + end + + CkbUtils.hash_value_to_s(value) + end + def nrc_721_nft_info return unless cell_type.in?(%w(nrc_721_token nrc_721_factory)) @@ -79,6 +106,9 @@ def nrc_721_nft_info decimal: udt_account.decimal, type_hash:, published: true, + collection: { + type_hash: factory_cell&.type_script&.script_hash, + }, } else raise "invalid cell type" diff --git a/app/serializers/cell_output_serializer.rb b/app/serializers/cell_output_serializer.rb index 70c0e2238..a7f1a626b 100644 --- a/app/serializers/cell_output_serializer.rb +++ b/app/serializers/cell_output_serializer.rb @@ -45,6 +45,8 @@ class CellOutputSerializer object.m_nft_info.merge!(type: "m_nft") when "nrc_721_token", "nrc_721_factory" object.nrc_721_nft_info.merge!(type: "nrc_721") + when "spore_cluster", "spore_cell", "did_cell" + object.dob_info.merge!(type: "dob") when "omiga_inscription_info", "omiga_inscription" object.omiga_inscription_info.merge!(type: "omiga_inscription") end diff --git a/app/workers/update_udt_info_worker.rb b/app/workers/update_udt_info_worker.rb index 9b40651bb..60a4b9038 100644 --- a/app/workers/update_udt_info_worker.rb +++ b/app/workers/update_udt_info_worker.rb @@ -37,7 +37,7 @@ def perform(block_number) } # generate udt holder allocation - if udt.xudt_compatible? || udt.xudt? || xudt.omiga_inscription? + if udt.xudt_compatible? || udt.xudt? || udt.omiga_inscription? $redis.sadd("udt_holder_allocation", type_hash) end end diff --git a/test/models/ckb_transaction_test.rb b/test/models/ckb_transaction_test.rb index 415c5d381..e7c5a1eb6 100644 --- a/test/models/ckb_transaction_test.rb +++ b/test/models/ckb_transaction_test.rb @@ -477,7 +477,7 @@ class CkbTransactionTest < ActiveSupport::TestCase id from_cellbase capacity address_hash generated_tx_hash cell_index cell_type since extra_info ).sort - expected_m_nft_attributes = %i(class_name total).sort + expected_m_nft_attributes = %i(class_name total type_hash).sort expected_display_input = CkbUtils.hash_value_to_s( id: m_nft_cell_output.id, from_cellbase: false, @@ -538,7 +538,7 @@ class CkbTransactionTest < ActiveSupport::TestCase id from_cellbase capacity address_hash generated_tx_hash cell_index cell_type since extra_info ).sort - expected_m_nft_attributes = %i(class_name token_id total).sort + expected_m_nft_attributes = %i(class_name token_id total collection).sort expected_display_input = CkbUtils.hash_value_to_s( id: m_nft_cell_output.id, from_cellbase: false, @@ -604,7 +604,7 @@ class CkbTransactionTest < ActiveSupport::TestCase id capacity address_hash status consumed_tx_hash cell_type extra_info ).sort - expected_m_nft_attributes = %i(class_name total).sort + expected_m_nft_attributes = %i(class_name total type_hash).sort expected_display_output = CkbUtils.hash_value_to_s( id: m_nft_cell_output.id, capacity: m_nft_cell_output.capacity, @@ -687,13 +687,14 @@ class CkbTransactionTest < ActiveSupport::TestCase block: nrc_721_token_output_block) nrc_factory_cell = create(:nrc_factory_cell, - code_hash: "0x00000000000000000000000000000000000000000000000000545950455f4944013620e2ced53373c5b55c5cef79b7fd0a875c60a70382a9e9664fe28e0bb345ab22c70f8e24a90dcccc7eb1ea669ac6cfecab095a1886af01d71612fdb3c836c8", - args: "0x3620e2ced53373c5b55c5cef79b7fd0a875c60a70382a9e9664fe28e0bb345ab", + code_hash: "0xf31f9c3241043976c93db9f203d765ce2c1d96465c918b93fbb117cd35bcfb98", + args: "0x47a6262383ad9b266c6fae7c460dc39261e4f914e77467426809ce0fd607baec", verified: true) nrc_721_factory_type_script = create(:type_script, code_hash: nrc_factory_cell.code_hash, hash_type: "type", - args: nrc_factory_cell.args) + args: nrc_factory_cell.args, + script_hash: "0xcf4f2494ea1fbebcd07a7445415a0e8b36be214af6257065888dae0ce1434434") nrc_721_factory_cell_output = create(:cell_output, block: nrc_721_token_output_block, ckb_transaction: nrc_721_token_output_transaction, @@ -729,7 +730,7 @@ class CkbTransactionTest < ActiveSupport::TestCase symbol: "TTF", amount: "", decimal: "", type_hash: "0x", published: true } token_info = { - symbol: "TTF", amount: udt_account.nft_token_id, decimal: "6", type_hash: "0x", published: true + symbol: "TTF", amount: udt_account.nft_token_id, decimal: "6", type_hash: "0x", published: true, collection: { type_hash: "0xcf4f2494ea1fbebcd07a7445415a0e8b36be214af6257065888dae0ce1434434" } } display_outputs = nrc_721_token_output_transaction.display_outputs assert_equal factory_info.to_a,