Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy to testnet #1654

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 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
Loading