Skip to content

Commit

Permalink
Merge pull request #1652 from nervosnetwork/testnet
Browse files Browse the repository at this point in the history
Deploy to mainnet
  • Loading branch information
zmcNotafraid authored Feb 29, 2024
2 parents 3f8ae04 + 45034de commit c2ffdbe
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 48 deletions.
28 changes: 18 additions & 10 deletions app/controllers/api/v2/nft/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ def index
scope = scope.where(owner_id: @owner.id)
end
scope = scope.where(collection_id: @collection.id) if @collection
scope = scope.where(collection:{standard: params[:standard]}) if params[:standard]
scope = scope.where(collection: { standard: params[:standard] }) if params[:standard]
scope = scope.where(token_id: params[:token_id]) if params[:token_id]
scope = scope.order(token_id: :asc)
pagy, items = pagy(scope)
items = items.map do |i|
j = i.as_json
j['collection'] = i.collection.as_json
j["collection"] = i.collection.as_json
j
end
render json: {
data: items,
pagination: pagy_metadata(pagy)
pagination: pagy_metadata(pagy),
}
end

Expand All @@ -29,7 +29,8 @@ def show
return head(:not_found)
end

item = @collection.items.find_by token_id: params[:id]
token_id = parse_hex_token_id(params[:id])
item = @collection.items.find_by(token_id:)
if item
render json: item.as_json.merge(collection: item.collection.as_json)
else
Expand All @@ -39,14 +40,21 @@ def show

protected


def find_collection
if params[:collection_id].present?
if /\A\d+\z/.match?(params[:collection_id])
@collection = TokenCollection.find params[:collection_id]
else
@collection = TokenCollection.find_by_sn params[:collection_id]
end
@collection = if /\A\d+\z/.match?(params[:collection_id])
TokenCollection.find params[:collection_id]
else
TokenCollection.find_by_sn params[:collection_id]
end
end
end

def parse_hex_token_id(hex_id)
if hex_id.start_with?("0x")
hex_id.hex
else
hex_id
end
end
end
Expand Down
20 changes: 11 additions & 9 deletions app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -631,24 +631,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, parsed_udt_type =
type_hash, parsed_udt_type, published =
if cell_type == "omiga_inscription_info"
info = CkbUtils.parse_omiga_inscription_info(outputs_data[tx_index][index])
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(
pre_closed_info = OmigaInscriptionInfo.includes(:udt).find_by(
type_hash: info_type_hash, mint_status: :closed,
)
attrs =
attrs, published =
if pre_closed_info
attrs.merge(pre_udt_hash: pre_closed_info.udt_hash)
[attrs.merge(pre_udt_hash: pre_closed_info.udt_hash), pre_closed_info.udt&.published == true]
else
attrs
[attrs, false]
end
OmigaInscriptionInfo.upsert(attrs, unique_by: :udt_hash)
[info[:udt_hash], "omiga_inscription"]
[info[:udt_hash], "omiga_inscription", published]
else
[output.type.compute_hash, udt_type(cell_type)]
[output.type.compute_hash, udt_type(cell_type), false]
end

if cell_type == "omiga_inscription"
Expand Down Expand Up @@ -683,7 +683,7 @@ def build_udts!(local_block, outputs, outputs_data)
if parsed_spore_cell[:cluster_id].present?
binary_hashes = CkbUtils.hexes_to_bins_sql(CkbSync::Api.instance.spore_cluster_code_hashes)
spore_cluster_type_ids = TypeScript.where("code_hash IN (#{binary_hashes})").where(hash_type: "data1",
args: parsed_spore_cell[:cluster_id]).pluck(:id)
args: parsed_spore_cell[:cluster_id]).pluck(:id)

spore_cluster_cell = CellOutput.live.where(type_script_id: spore_cluster_type_ids).last
parsed_cluster_data = CkbUtils.parse_spore_cluster_data(spore_cluster_cell.data)
Expand Down Expand Up @@ -712,7 +712,9 @@ def build_udts!(local_block, outputs, outputs_data)
nft_token_attr[:full_name] = info[:name]
nft_token_attr[:symbol] = info[:symbol]
nft_token_attr[:decimal] = info[:decimal]
nft_token_attr[:published] = true
if published || !Udt.where(symbol: info[:symbol].strip, udt_type: :omiga_inscription).exists?
nft_token_attr[:published] = true
end
end
# fill issuer_address after publish the token
udts_attributes << {
Expand Down
39 changes: 22 additions & 17 deletions app/models/token_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TokenCollection < ApplicationRecord
validates :sn, uniqueness: true, presence: true

def self.find_by_sn(sn)
c = find_by sn: sn
c = find_by(sn:)
return c if c

c = find_by_type_hash(sn)
Expand All @@ -24,18 +24,19 @@ def self.find_by_type_hash(type_hash)
TokenCollection.find_by! type_script_id: ts.id
end

def as_json(options = {})
def as_json(_options = {})
{
id: id,
standard: standard,
name: name,
description: description,
icon_url: icon_url,
id:,
standard:,
name:,
description:,
icon_url:,
creator: creator&.address_hash || "",
items_count: items_count,
holders_count: holders_count,
h24_ckb_transactions_count: h24_ckb_transactions_count,
type_script: type_script&.as_json
items_count:,
holders_count:,
h24_ckb_transactions_count:,
type_script: type_script&.as_json,
sn:,
}
end

Expand Down Expand Up @@ -75,12 +76,12 @@ def update_udt_info
Udt.where(
code_hash: ts.code_hash,
hash_type: ts.hash_type,
args: ts.args
args: ts.args,
).update_all(
symbol: symbol,
symbol:,
full_name: name,
description: description,
icon_file: icon_url
description:,
icon_file: icon_url,
)
end
end
Expand All @@ -94,7 +95,11 @@ def self.update_cell
# removed the wrong token collections
def self.remove_corrupted
where(standard: "nrc721").where(type_script_id: nil).or(where(creator_id: nil)).find_each do |tc|
tc.update_info rescue nil
begin
tc.update_info
rescue StandardError
nil
end

if tc.cell.blank?
tc.destroy
Expand All @@ -113,7 +118,7 @@ def self.fix_sn

begin
tc2.items.update_all collection_id: tc.id
rescue
rescue StandardError
puts "destroy all items"
end
tc2.destroy!
Expand Down
1 change: 1 addition & 0 deletions app/workers/token_transfer_detect_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def find_or_create_spore_collection(_cell, type_script)
args: parsed_spore_cell[:cluster_id])
TokenCollection.find_or_create_by(
standard: "spore",
type_script_id: spore_cluster_type.id,
sn: spore_cluster_type.script_hash,
description: "Only for no cluster spore cell",
)
Expand Down
20 changes: 18 additions & 2 deletions test/controllers/api/v2/nft/items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def setup

test "should get index with collection sn" do
sn = "iam-an-sn"
token_collection = create :token_collection, name: "token1", sn: sn
token_collection = create(:token_collection, name: "token1", sn:)
address = create :address, is_depositor: true

create :token_item, name: "item1", collection_id: token_collection.id, owner_id: address.id
Expand Down Expand Up @@ -50,7 +50,7 @@ def setup
test "should return spore cell" do
token_collection = create :token_collection, name: "token1", standard: "spore"
address = create :address, is_depositor: true
my_token_id = 100
my_token_id = 244995949481600724545646750271542270961771653267601098727781219042501243997
cell = create :cell_output, :with_full_transaction
create(:cell_datum, cell_output: cell)
create :token_item, name: "item1", collection_id: token_collection.id, owner_id: address.id,
Expand All @@ -62,6 +62,22 @@ def setup
assert_equal JSON.parse(response.body)["standard"], "spore"
assert_not_nil JSON.parse(response.body)["cell"]["data"]
end

test "should return spore cell when pass hex token_id" do
token_collection = create :token_collection, name: "token1", standard: "spore"
address = create :address, is_depositor: true
my_token_id = "0x008aa9acd3bd41c6e5d051d3cea822772249f2945179dcd4bf97259c474ab45d"
cell = create :cell_output, :with_full_transaction
create(:cell_datum, cell_output: cell)
create :token_item, name: "item1", collection_id: token_collection.id, owner_id: address.id,
token_id: my_token_id.hex, cell_id: cell.id

get api_v2_nft_collection_item_url(id: my_token_id, collection_id: token_collection.id)

assert_response :success
assert_equal JSON.parse(response.body)["standard"], "spore"
assert_not_nil JSON.parse(response.body)["cell"]["data"]
end
end
end
end
23 changes: 13 additions & 10 deletions test/models/ckb_sync/node_data_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1034,15 +1034,15 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
Address.find_or_create_address(lock1, node_block.header.timestamp)
Address.find_or_create_address(lock2, node_block.header.timestamp)
300.times do |i|
if i % 2 == 0
node_block.transactions.first.outputs << CKB::Types::Output.new(
capacity: 30000 * 10**8, lock: lock1,
)
else
node_block.transactions.first.outputs << CKB::Types::Output.new(
capacity: 40000 * 10**8, lock: lock2,
)
end
node_block.transactions.first.outputs << if i % 2 == 0
CKB::Types::Output.new(
capacity: 30000 * 10**8, lock: lock1,
)
else
CKB::Types::Output.new(
capacity: 40000 * 10**8, lock: lock2,
)
end
node_block.transactions.first.outputs_data << "0x"
end
new_local_block = node_data_processor.process_block(node_block)
Expand Down Expand Up @@ -4043,8 +4043,10 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
assert_equal info.mint_limit, 0.1e12
assert_equal info.mint_status, "minting"
assert_equal info.udt_id, Udt.first.id
udt = Udt.first
assert_equal "0x5fa66c8d5f43914f85d3083e0529931883a5b0a14282f891201069f1b5067908",
Udt.first.type_hash
udt.type_hash
assert_equal true, udt.published
end
end

Expand Down Expand Up @@ -4145,6 +4147,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
node_data_processor.process_block(node_block)
assert_equal 2, Udt.count
assert_equal info.udt_hash, OmigaInscriptionInfo.last.pre_udt_hash
assert_equal true, Udt.last.published
end
end

Expand Down

0 comments on commit c2ffdbe

Please sign in to comment.