Skip to content

Commit

Permalink
Issue 508 14 (#1600)
Browse files Browse the repository at this point in the history
* feat: record pre udt_hash to new rebase start omiga inscription

Signed-off-by: Miles Zhang <[email protected]>

* feat: adjust omiga_inscription api to handle with same info type hash

Signed-off-by: Miles Zhang <[email protected]>

* test: fix test

Signed-off-by: Miles Zhang <[email protected]>

---------

Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid authored Jan 22, 2024
1 parent f07b2e8 commit d8c479b
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 28 deletions.
26 changes: 21 additions & 5 deletions app/controllers/api/v1/omiga_inscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ class OmigaInscriptionsController < ApplicationController
only: :index

def index
udts = Udt.omiga_inscription
pre_udt_hashes = OmigaInscriptionInfo.where.not(pre_udt_hash: nil).pluck(:pre_udt_hash)
udts =
if pre_udt_hashes.present?
Udt.joins(:omiga_inscription_info).where.not(
"omiga_inscription_infos.mint_status = 1 and omiga_inscription_infos.udt_hash IN (?)", pre_udt_hashes
)
else
Udt.omiga_inscription
end

if stale?(udts)
udts = sort_udts(udts).page(@page).per(@page_size).fast_page
Expand All @@ -22,9 +30,17 @@ def index
end

def show
udt = Udt.joins(:omiga_inscription_info).where(
"udts.type_hash = ? or omiga_inscription_infos.type_hash = ?", params[:id], params[:id]
).first
udt =
if params[:status] == "closed"
Udt.joins(:omiga_inscription_info).where(
"omiga_inscription_infos.type_hash = ? and omiga_inscription_infos.mint_status = 1", params[:id]
).first
else
Udt.joins(:omiga_inscription_info).where(
"udts.type_hash = ? or omiga_inscription_infos.type_hash = ?", params[:id], params[:id]
).order("id DESC").first
end

if udt.nil?
raise Api::V1::Exceptions::UdtNotFoundError
else
Expand All @@ -34,7 +50,7 @@ def show

def download_csv
args = params.permit(:id, :start_date, :end_date, :start_number,
:end_number, udt: {})
:end_number, :status, udt: {})
file = CsvExportable::ExportOmigaInscriptionTransactionsJob.perform_now(args.to_h)

send_data file, type: "text/csv; charset=utf-8; header=present",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
module CsvExportable
class ExportOmigaInscriptionTransactionsJob < BaseExporter
def perform(args)
udt = Udt.joins(:omiga_inscription_info).where(
"udts.type_hash = ? or omiga_inscription_infos.type_hash = ?", args[:id], args[:id]
).first
udt =
if args[:status] == "closed"
Udt.joins(:omiga_inscription_info).where(
"omiga_inscription_infos.type_hash = ? and omiga_inscription_infos.mint_status = 1", args[:id]
).first
else
Udt.joins(:omiga_inscription_info).where(
"udts.type_hash = ? or omiga_inscription_infos.type_hash = ?", args[:id], args[:id]
).order("id DESC").first
end
ckb_transactions = udt.ckb_transactions

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

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

def udt_account_amount(udt_type, type_hash, address)
Expand Down Expand Up @@ -629,16 +622,24 @@ def build_udts!(local_block, outputs, outputs_data)
next unless cell_type.in?(%w(udt m_nft_token nrc_721_token spore_cell
omiga_inscription_info omiga_inscription))

type_hash =
type_hash, parsed_udt_type =
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_type_hash = output.type.compute_hash
attrs = info.merge(output.type.to_h, type_hash: info_type_hash)
pre_closed_info = OmigaInscriptionInfo.find_by(
type_hash: info_type_hash, mint_status: :closed,
)
info[:udt_hash]
attrs =
if pre_closed_info
attrs.merge(pre_udt_hash: pre_closed_info.udt_hash)
else
attrs
end
OmigaInscriptionInfo.upsert(attrs, unique_by: :udt_hash)
[info[:udt_hash], "omiga_inscription"]
else
output.type.compute_hash
[output.type.compute_hash, udt_type(cell_type)]
end

if cell_type == "omiga_inscription"
Expand Down Expand Up @@ -714,7 +715,7 @@ def build_udts!(local_block, outputs, outputs_data)
end
# fill issuer_address after publish the token
udts_attributes << {
type_hash:, udt_type: udt_type(cell_type), block_timestamp: local_block.timestamp, args: output.type.args,
type_hash:, udt_type: parsed_udt_type, block_timestamp: local_block.timestamp, args: output.type.args,
code_hash: output.type.code_hash, hash_type: output.type.hash_type
}.merge(nft_token_attr)
end
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 @@ -23,6 +23,7 @@ class OmigaInscriptionInfo < ApplicationRecord
# created_at :datetime not null
# updated_at :datetime not null
# type_hash :binary
# pre_udt_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 @@ -49,4 +49,10 @@ class UdtSerializer
} do |object|
object.omiga_inscription_info.type_hash
end

attribute :pre_udt_hash, if: Proc.new { |record, _params|
record.udt_type == "omiga_inscription"
} do |object|
object.omiga_inscription_info.pre_udt_hash
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPreUdtHashToOmigaInscriptionInfo < ActiveRecord::Migration[7.0]
def change
add_column :omiga_inscription_infos, :pre_udt_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 @@ -1669,7 +1669,8 @@ CREATE TABLE public.omiga_inscription_infos (
udt_id bigint,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL,
type_hash bytea
type_hash bytea,
pre_udt_hash bytea
);


Expand Down Expand Up @@ -4818,6 +4819,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20231017074221'),
('20231218082938'),
('20240107100346'),
('20240118103947');
('20240118103947'),
('20240119131328');


42 changes: 40 additions & 2 deletions test/controllers/api/v1/omiga_inscriptions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@ class OmigaInscriptionsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end

test "should return pre udt when call show" do
udt = create(:udt, :omiga_inscription)
udt.omiga_inscription_info.update(mint_status: :closed)
new_udt = create(:udt, udt_type: :omiga_inscription)
info = create(:omiga_inscription_info, udt_id: new_udt.id,
mint_status: :rebase_start, pre_udt_hash: udt.omiga_inscription_info.udt_hash, udt_hash: "0x#{SecureRandom.hex(32)}")

valid_get api_v1_omiga_inscription_url(info.type_hash, status: "closed")
assert_equal udt.type_hash,
JSON.parse(response.body)["data"]["attributes"]["type_hash"]
end

test "should return current rebase_start udt when call show" do
udt = create(:udt, :omiga_inscription,
block_timestamp: (Time.now - 10.minutes).to_i * 1000)
udt.omiga_inscription_info.update(mint_status: :closed)
new_udt = create(:udt, udt_type: :omiga_inscription,
block_timestamp: Time.now.to_i * 1000)
info = create(:omiga_inscription_info, udt_id: new_udt.id,
mint_status: :rebase_start, pre_udt_hash: udt.omiga_inscription_info.udt_hash, udt_hash: "0x#{SecureRandom.hex(32)}")

valid_get api_v1_omiga_inscription_url(info.type_hash)
assert_equal new_udt.type_hash,
JSON.parse(response.body)["data"]["attributes"]["type_hash"]
end

test "should set right content type when call show" do
udt = create(:udt, :omiga_inscription)

Expand Down Expand Up @@ -46,7 +72,7 @@ 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 info_type_hash
published type_hash type_script issuer_address mint_status mint_limit expected_supply inscription_info_id udt_type pre_udt_hash info_type_hash
).sort,
response_udt["attributes"].keys.sort
end
Expand All @@ -60,7 +86,7 @@ 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 info_type_hash
published type_hash type_script issuer_address mint_status mint_limit expected_supply inscription_info_id udt_type pre_udt_hash info_type_hash
).sort,
response_udt["attributes"].keys.sort
end
Expand Down Expand Up @@ -89,6 +115,18 @@ class OmigaInscriptionsControllerTest < ActionDispatch::IntegrationTest
assert_equal 2, json["data"].length
end

test "should return rebase_start omiga_inscription udts" do
udt = create(:udt, :omiga_inscription)
udt.omiga_inscription_info.update(mint_status: :closed)
new_udt = create(:udt, udt_type: :omiga_inscription)
create(:omiga_inscription_info, udt_id: new_udt.id,
mint_status: :rebase_start, pre_udt_hash: udt.omiga_inscription_info.udt_hash, udt_hash: "0x#{SecureRandom.hex(32)}")

valid_get api_v1_omiga_inscriptions_url

assert_equal 1, json["data"].length
end

test "should sorted by mint_status asc when sort param is mint_status" do
page = 1
page_size = 5
Expand Down
14 changes: 13 additions & 1 deletion test/factories/omiga_inscription_info.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
FactoryBot.define do
factory :omiga_inscription_info do
udt_hash { "0x#{SecureRandom.hex(32)}" }
udt_hash do
"0x5fa66c8d5f43914f85d3083e0529931883a5b0a14282f891201069f1b5067908"
end
code_hash do
"0x50fdea2d0030a8d0b3d69f883b471cab2a29cae6f01923f19cecac0f27fdaaa6"
end
hash_type { "type" }
args do
"0xcd89d8f36593a9a82501c024c5cdc4877ca11c5b3d5831b3e78334aecb978f0d"
end
type_hash do
"0x5cfcab1fc499de7d33265b04d2de9cf2f91cc7c7a578642993b0912b31b6cf39"
end
end
end
47 changes: 47 additions & 0 deletions test/models/ckb_sync/node_data_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4101,6 +4101,53 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
end
end

test "change omiga inscription to rebase_start" do
CkbSync::Api.any_instance.stubs(:mode).returns("testnet")
CkbSync::Api.any_instance.stubs(:xudt_code_hash).returns("0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb")
CkbSync::Api.any_instance.stubs(:omiga_inscription_info_code_hash).returns("0x50fdea2d0030a8d0b3d69f883b471cab2a29cae6f01923f19cecac0f27fdaaa6")

VCR.use_cassette("blocks/33") do
node_block = CkbSync::Api.instance.get_block_by_number(33)
block1 = create(:block, :with_block_hash,
number: node_block.header.number - 1)
tx1 = create(:ckb_transaction, block: block1,
tx_hash: "0x3e89753ebca825e1504498eb18b56576d5b7eff59fe033346a10ab9e8ca359a4")
input_address1 = create(:address)
address1_lock = create(:lock_script, address_id: input_address1.id,
args: "0x#{SecureRandom.hex(20)}",
code_hash: Settings.secp_cell_type_hash,
hash_type: "type")
output1 = create(:cell_output, ckb_transaction: tx1,
block: block1, capacity: 50000000 * 10**8,
tx_hash: tx1.tx_hash,
cell_index: 1,
address: input_address1,
cell_type: "omiga_inscription_info",
lock_script_id: address1_lock.id,
type_script_id: nil)

udt = create(:udt, code_hash: "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb", hash_type: "type", args: "0x9709d30fc21348ae1d28a197310a80aec3b8cdb5c93814d5e240f9fba85b76af",
type_hash: "0x5fa66c8d5f43914f85d3083e0529931883a5b0a14282f891201069f1b5067908", udt_type: "omiga_inscription")
info = create(:omiga_inscription_info,
code_hash: "0x50fdea2d0030a8d0b3d69f883b471cab2a29cae6f01923f19cecac0f27fdaaa6",
hash_type: "type",
args: "0xcd89d8f36593a9a82501c024c5cdc4877ca11c5b3d5831b3e78334aecb978f0d",
type_hash: "0x5cfcab1fc499de7d33265b04d2de9cf2f91cc7c7a578642993b0912b31b6cf39",
decimal: 0.8e1,
name: "CKB Fist Inscription",
symbol: "CKBI",
udt_hash: "0x5fa66c8d5f43914f85d3083e0529931883a5b0a14282f891201069f1b5067908",
expected_supply: 0.21e16,
mint_limit: 0.1e12,
mint_status: "closed",
udt_id: udt.id)

node_data_processor.process_block(node_block)
assert_equal 2, Udt.count
assert_equal info.udt_hash, OmigaInscriptionInfo.last.pre_udt_hash
end
end

private

def node_data_processor
Expand Down
39 changes: 39 additions & 0 deletions vcr_fixtures/vcr_cassettes/blocks/33.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d8c479b

Please sign in to comment.