Skip to content

Commit

Permalink
feat: adjust xudt and nft tag logic (#2084)
Browse files Browse the repository at this point in the history
* feat: adjust xudt and nft tag logic

Signed-off-by: Miles Zhang <mingchang555@hotmail.com>

* fix: lp token issuer address logic

Signed-off-by: Miles Zhang <mingchang555@hotmail.com>

* feat: nft tag renmae rgbpp-compatible to rgb++

Signed-off-by: Miles Zhang <mingchang555@hotmail.com>

* fix: typo

Signed-off-by: Miles Zhang <mingchang555@hotmail.com>

* test: fix xudt_tag worker test

Signed-off-by: Miles Zhang <mingchang555@hotmail.com>

* feat:  nft name out-of-length tag max 60 characters

Signed-off-by: Miles Zhang <mingchang555@hotmail.com>

---------

Signed-off-by: Miles Zhang <mingchang555@hotmail.com>
  • Loading branch information
zmcNotafraid committed Aug 2, 2024
1 parent d532a81 commit 8922012
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 45 deletions.
4 changes: 3 additions & 1 deletion app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,9 @@ def build_udts!(local_block, outputs, outputs_data)
nft_token_attr[:decimal] = info[:decimal]
nft_token_attr[:published] = true
when "xudt", "xudt_compatible"
issuer_address = Address.find_by(lock_hash: output.type.args[0..65])&.address_hash
if output.type.args.length == 66
issuer_address = Address.find_by(lock_hash: output.type.args[0..65])&.address_hash
end
items.each_with_index do |output, index|
if output.type&.code_hash == CkbSync::Api.instance.unique_cell_code_hash
info = CkbUtils.parse_unique_cell(outputs_data[tx_index][index])
Expand Down
2 changes: 1 addition & 1 deletion app/models/token_collection.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class TokenCollection < ApplicationRecord
VALID_TAGS = ["invalid", "suspicious", "out-of-length-range", "rgbpp-compatible", "layer-1-asset", "duplicate", "layer-2-asset"]
VALID_TAGS = ["invalid", "suspicious", "out-of-length-range", "rgb++", "layer-1-asset", "layer-2-asset"]

enum standard: { cota: "cota", spore: "spore", m_nft: "m_nft", nrc721: "nrc721" }

Expand Down
2 changes: 1 addition & 1 deletion app/models/xudt_tag.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class XudtTag < ApplicationRecord
belongs_to :udt

VALID_TAGS = ["unnamed", "invalid", "suspicious", "out-of-length-range", "rgbpp-compatible", "layer-1-asset", "supply-limited", "duplicate", "layer-2-asset", "supply-unlimited"]
VALID_TAGS = ["unnamed", "invalid", "suspicious", "out-of-length-range", "rgb++", "layer-1-asset", "supply-limited", "utility", "layer-2-asset", "supply-unlimited"]
end

# == Schema Information
Expand Down
14 changes: 4 additions & 10 deletions app/workers/token_collection_tag_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@ def mark_tags(token_collection)
["suspicious"]
elsif out_of_length?(token_collection.name)
["out-of-length-range"]
elsif first_token_collection?(token_collection.name, token_collection.block_timestamp, token_collection.standard)
if rgbpp_lock?(token_collection.creator.address_hash)
["rgbpp-compatible", "layer-1-asset"]
else
["rgbpp-compatible", "layer-2-asset"]
end
elsif rgbpp_lock?(token_collection.creator.address_hash)
["duplicate", "layer-1-asset"]
["rgb++", "layer-1-asset"]
else
["duplicate", "layer-2-asset"]
["rgb++", "layer-2-asset"]
end
end

Expand All @@ -39,11 +33,11 @@ def invalid_char?(name)
end

def invisible_char?(name)
(name =~ /^[\x21-\x7E]+$/).nil?
(name =~ /^[\x21-\x7E]+(?:\s[\x21-\x7E]+)?$/).nil?
end

def out_of_length?(name)
name.length > 255
name.length > 60
end

def first_token_collection?(name, block_timestamp, standard)
Expand Down
21 changes: 12 additions & 9 deletions app/workers/xudt_tag_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def perform
attrs =
udts.map do |udt|
tags = mark_tags(udt)
tags << "rgb++" if udt.xudt? && !tags.include?("rgb++")
{ udt_id: udt.id, udt_type_hash: udt.type_hash, tags: }
end

Expand All @@ -23,16 +24,14 @@ def mark_tags(udt)
["suspicious"]
elsif out_of_length?(udt.symbol)
["out-of-length-range"]
elsif first_xudt?(udt.symbol, udt.block_timestamp)
if rgbpp_lock?(udt.issuer_address)
["rgbpp-compatible", "layer-1-asset", "supply-limited"]
else
["rgbpp-compatible", "layer-2-asset", "supply-unlimited"]
end
elsif utility_lp_token?(udt.args)
["utility"]
elsif !first_xudt?(udt.symbol, udt.block_timestamp)
["suspicious"]
elsif rgbpp_lock?(udt.issuer_address)
["duplicate", "layer-1-asset", "supply-limited"]
["rgb++", "layer-1-asset", "supply-limited"]
else
["duplicate", "layer-2-asset", "supply-unlimited"]
["rgb++", "layer-2-asset", "supply-unlimited"]
end
end

Expand All @@ -41,7 +40,7 @@ def invalid_char?(symbol)
end

def invisible_char?(symbol)
(symbol =~ /^[\x21-\x7E]+$/).nil?
(symbol =~ /^[\x21-\x7E]+(?:\s[\x21-\x7E]+)?$/).nil?
end

def out_of_length?(symbol)
Expand All @@ -57,6 +56,10 @@ def rgbpp_lock?(issuer_address)
issuer_address.present? && CkbSync::Api.instance.rgbpp_code_hash.include?(address_code_hash)
end

def utility_lp_token?(args)
args.length == 74
end

## TODO: current no this condition
def omni_lock_with_supply_mode?(issuer_address); end
end
10 changes: 5 additions & 5 deletions test/controllers/api/v2/nft/collections_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class NFT::CollectionsControllerTest < ActionDispatch::IntegrationTest
end

test "should filter by union tags" do
create :token_collection, name: "token1", tags: ["layer-1-asset", "rgbpp-compatible"]
create :token_collection, name: "token2", tags: ["layer-1-asset", "rgbpp-compatible"]
create :token_collection, name: "token1", tags: ["layer-1-asset", "rgb++"]
create :token_collection, name: "token2", tags: ["layer-1-asset", "rgb++"]

get api_v2_nft_collections_url, params: { tags: "layer-1-asset,rgbpp-compatible", union: true }
get api_v2_nft_collections_url, params: { tags: "layer-1-asset,rgb++", union: true }
assert_response :success
assert_equal JSON.parse(response.body)["data"].size, 2
end

test "should filter by tags but not match" do
create :token_collection, name: "token1", tags: ["layer-1-asset", "rgbpp-compatible"]
create :token_collection, name: "token2", tags: ["layer-1-asset", "rgbpp-compatible"]
create :token_collection, name: "token1", tags: ["layer-1-asset", "rgb++"]
create :token_collection, name: "token2", tags: ["layer-1-asset", "rgb++"]

get api_v2_nft_collections_url, params: { tags: "layer-1-asset,invalid" }
assert_response :success
Expand Down
13 changes: 5 additions & 8 deletions test/workers/token_collection_tag_worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@ class TokenCollectionTagWorkerTest < ActiveJob::TestCase
end

test "add suspicious tag to token_collection" do
create(:token_collection, name: "CK BB", cell_id: @cell.id, creator_id: @address.id)
create(:token_collection, name: "CK BB", cell_id: @cell.id, creator_id: @address.id)
TokenCollectionTagWorker.new.perform
assert_equal ["suspicious"], TokenCollection.last.tags
end

test "add out-of-length-range tag to token_collection" do
create(:token_collection, name: "C" * 256, cell_id: @cell.id, creator_id: @address.id)
create(:token_collection, name: "C" * 66, cell_id: @cell.id, creator_id: @address.id)
TokenCollectionTagWorker.new.perform
assert_equal ["out-of-length-range"], TokenCollection.last.tags
end

test "add duplicate tag to token_collection" do
create(:token_collection, name: "CKBNFT", cell_id: @cell.id, creator_id: @address.id, block_timestamp: 1.hour.ago.to_i, tags: ["rgbpp-compatible", "layer-1-asset"])
new_tx = create(:ckb_transaction)
new_cell = create(:cell_output, address_id: @address.id, ckb_transaction_id: new_tx.id, tx_hash: new_tx.tx_hash)
create(:token_collection, name: "CKBNFT", cell_id: new_cell.id, creator_id: @address.id, block_timestamp: Time.now.to_i)
test "add rgb++ tag to token_collection" do
create(:token_collection, name: "CKBNFT", cell_id: @cell.id, creator_id: @address.id, block_timestamp: 1.hour.ago.to_i)
TokenCollectionTagWorker.new.perform
assert_equal ["duplicate", "layer-1-asset"], TokenCollection.last.tags
assert_equal ["rgb++", "layer-1-asset"], TokenCollection.last.tags
end
end
28 changes: 18 additions & 10 deletions test/workers/xudt_tag_worker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class XudtTagWorkerTest < ActiveJob::TestCase
assert_changes -> { XudtTag.count }, from: 0, to: 1 do
XudtTagWorker.new.perform
end
assert_equal ["unnamed"], XudtTag.last.tags
assert_equal ["unnamed", "rgb++"], XudtTag.last.tags
end

test "insert to xudt_tags successfully" do
Expand All @@ -28,40 +28,48 @@ class XudtTagWorkerTest < ActiveJob::TestCase
assert_changes -> { XudtTag.count }, from: 1, to: 2 do
XudtTagWorker.new.perform
end
assert_equal ["rgbpp-compatible", "layer-1-asset", "supply-limited"], XudtTag.last.tags
assert_equal ["rgb++", "layer-1-asset", "supply-limited"], XudtTag.last.tags
end

test "insert invalid tag" do
create(:udt, :xudt, symbol: "ü")
assert_changes -> { XudtTag.count }, from: 0, to: 1 do
XudtTagWorker.new.perform
end
assert_equal ["invalid"], XudtTag.last.tags
assert_equal ["invalid", "rgb++"], XudtTag.last.tags
end

test "insert suspicious tag" do
create(:udt, :xudt, symbol: "CK BB")
create(:udt, :xudt, symbol: "CK BB")
assert_changes -> { XudtTag.count }, from: 0, to: 1 do
XudtTagWorker.new.perform
end
assert_equal ["suspicious"], XudtTag.last.tags
assert_equal ["suspicious", "rgb++"], XudtTag.last.tags
end

test "insert out-of-length-range tag" do
create(:udt, :xudt, symbol: "CKBBBB")
create(:udt, :xudt, symbol: "CK BBBB")
assert_changes -> { XudtTag.count }, from: 0, to: 1 do
XudtTagWorker.new.perform
end
assert_equal ["out-of-length-range", "rgb++"], XudtTag.last.tags
end

test "insert utility tag" do
create(:udt, :xudt, symbol: "CKBBB", args: "0xdd6faefeffaa7d2a7b2e6890713c5fa4bbf378add1cfc1b27672a50a6ad3e83500000040")
assert_changes -> { XudtTag.count }, from: 0, to: 1 do
XudtTagWorker.new.perform
end
assert_equal ["out-of-length-range"], XudtTag.last.tags
assert_equal ["utility", "rgb++"], XudtTag.last.tags
end

test "insert duplicate tag" do
test "insert suspicious tag when not lp token but duplicate" do
udt = create(:udt, :xudt, symbol: "CKBBB", block_timestamp: 1.day.ago.to_i * 1000)
create(:xudt_tag, udt_id: udt.id, udt_type_hash: udt.type_hash, tags: ["rgbpp-compatible", "layer-1-asset", "supply-limited"])
create(:xudt_tag, udt_id: udt.id, udt_type_hash: udt.type_hash, tags: ["rgb++", "layer-1-asset", "supply-limited"])
create(:udt, :xudt, symbol: "ckbbb", block_timestamp: Time.now.to_i * 1000, issuer_address: @address.address_hash)
assert_changes -> { XudtTag.count }, from: 1, to: 2 do
XudtTagWorker.new.perform
end
assert_equal ["duplicate", "layer-1-asset", "supply-limited"], XudtTag.last.tags
assert_equal ["suspicious", "rgb++"], XudtTag.last.tags
end
end

0 comments on commit 8922012

Please sign in to comment.