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 mainnet #2444

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
74 changes: 36 additions & 38 deletions app/controllers/api/v2/nft/transfers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,18 @@ module V2
module NFT
class TransfersController < BaseController
def index
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
end

if collection && params[:token_id]
item = collection.items.find_by token_id: params[:token_id]
end

if item
scope = item.transfers
elsif collection
scope = collection.transfers
else
scope = TokenTransfer.all
end
scope = TokenTransfer.all

if params[:from]
scope = scope.where(from: find_address(params[:from]))
end
if params[:to]
scope = scope.where(to: find_address(params[:to]))
end
if params[:address_hash]
address = find_address(params[:address_hash])
scope = scope.where(from: address).or(scope.where(to: address))
end
if params[:transfer_action]
scope = scope.where(action: params[:transfer_action])
end
if params[:tx_hash]
scope = scope.includes(:ckb_transaction).where(ckb_transaction: { tx_hash: params[:tx_hash] })
if params[:collection_id].present?
collection = find_collection(params[:collection_id])
scope = collection.present? ? filtered_by_token_id(collection) : TokenTransfer.none
end

scope = apply_filters(scope)
scope = scope.order(transaction_id: :desc)
pagy, token_transfers = pagy(scope)

render json: {
data: token_transfers,
pagination: pagy
}
render json: { data: token_transfers, pagination: pagy }
end

def show
Expand All @@ -64,6 +32,36 @@ def download_csv

private

def find_collection(collection_id)
if /\A\d+\z/.match?(collection_id)
TokenCollection.find_by(id: collection_id)
else
TokenCollection.find_by(sn: collection_id)
end
end

def filtered_by_token_id(collection)
if params[:token_id].present?
item = collection.items.find_by(token_id: params[:token_id])
item.nil? ? TokenTransfer.none : item.transfers
else
collection.transfers
end
end

def apply_filters(scope)
scope = scope.where(from: find_address(params[:from])) if params[:from].present?
scope = scope.where(to: find_address(params[:to])) if params[:to].present?
if params[:address_hash].present?
address = find_address(params[:address_hash])
scope = scope.where(from: address).or(scope.where(to: address))
end
scope = scope.where(action: params[:transfer_action]) if params[:transfer_action].present?
scope = scope.includes(:ckb_transaction).where(ckb_transaction: { tx_hash: params[:tx_hash] }) if params[:tx_hash].present?

scope
end

def find_address(address_hash)
Address.find_by_address_hash(address_hash)
end
Expand Down
3 changes: 1 addition & 2 deletions test/controllers/api/v2/nft/transfers_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ def setup
end
test "should get index" do

get api_v2_nft_transfers_url(collection_id: @token_collection.id, token_id: @token_item.id,
get api_v2_nft_transfers_url(collection_id: @token_collection.id, token_id: @token_item.token_id,
from: @from_address.address_hash,
to: @to_address.address_hash)
assert_response :success
assert_equal 1, JSON.parse(response.body)['data'].size
puts response.body
assert_equal @token_transfer.id, JSON.parse(response.body)['data'][0]['id']
end

Expand Down