-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tags to token collection (#1980)
Signed-off-by: Miles Zhang <[email protected]>
- Loading branch information
1 parent
da97f96
commit c759cc0
Showing
10 changed files
with
121 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
class TokenCollectionSerializer | ||
include FastJsonapi::ObjectSerializer | ||
|
||
attributes :standard, :name, :description, :icon_url, :symbol, :sn | ||
attributes :standard, :name, :description, :icon_url, :symbol, :sn, :tags | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
class TokenCollectionTagWorker | ||
include Sidekiq::Job | ||
|
||
def perform | ||
token_collections = TokenCollection.preload(:creator).where(tags: []).where.not("name IS NULL OR name = ''").limit(100) | ||
unless token_collections.empty? | ||
attrs = | ||
token_collections.map do |token_collection| | ||
tags = mark_tags(token_collection) | ||
{ id: token_collection.id, tags: } | ||
end | ||
|
||
TokenCollection.upsert_all(attrs, unique_by: :id, on_duplicate: :update, update_only: :tags) | ||
end | ||
end | ||
|
||
def mark_tags(token_collection) | ||
if invalid_char?(token_collection.name) | ||
["invalid"] | ||
elsif invisible_char?(token_collection.name) | ||
["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"] | ||
else | ||
["duplicate", "layer-2-asset"] | ||
end | ||
end | ||
|
||
def invalid_char?(name) | ||
!name.ascii_only? | ||
end | ||
|
||
def invisible_char?(name) | ||
(name =~ /^[\x21-\x7E]+$/).nil? | ||
end | ||
|
||
def out_of_length?(name) | ||
name.length > 255 | ||
end | ||
|
||
def first_token_collection?(name, block_timestamp, standard) | ||
!TokenCollection.where(name:, standard:).where("block_timestamp < ?", block_timestamp).exists? | ||
end | ||
|
||
def rgbpp_lock?(issuer_address) | ||
address_code_hash = CkbUtils.parse_address(issuer_address).script.code_hash | ||
issuer_address.present? && CkbSync::Api.instance.rgbpp_code_hash.include?(address_code_hash) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddTagsToTokenCollection < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :token_collections, :tags, :string, array: true, default: [] | ||
add_column :token_collections, :block_timestamp, :bigint | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "test_helper" | ||
|
||
class TokenCollectionTagWorkerTest < ActiveJob::TestCase | ||
setup do | ||
@address = create(:address, address_hash: "ckb1qz7xc452rgxs5z0ks3xun46dmdp58sepg0ljtae8ck0d7nah945nvqgqqqqqqx3l3v4") | ||
tx = create(:ckb_transaction) | ||
@cell = create(:cell_output, address_id: @address.id, ckb_transaction_id: tx.id, tx_hash: tx.tx_hash) | ||
end | ||
|
||
test "add invalid tag to token_collection" do | ||
create(:token_collection, name: "ü", cell_id: @cell.id, creator_id: @address.id) | ||
TokenCollectionTagWorker.new.perform | ||
assert_equal ["invalid"], TokenCollection.last.tags | ||
end | ||
|
||
test "add suspicious tag to token_collection" do | ||
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) | ||
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) | ||
TokenCollectionTagWorker.new.perform | ||
assert_equal ["duplicate", "layer-1-asset"], TokenCollection.last.tags | ||
end | ||
end |