Skip to content

Commit c3e9096

Browse files
authored
Merge pull request #2559 from nervosnetwork/develop
Deploy to testnet
2 parents 039bea8 + cfce57b commit c3e9096

File tree

8 files changed

+347
-273
lines changed

8 files changed

+347
-273
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
module Api
2+
module V1
3+
class FungibleTokensController < ApplicationController
4+
before_action :validate_query_params, only: :show
5+
before_action :validate_pagination_params, :pagination_params, only: :index
6+
7+
def index
8+
scope = Udt.includes(:xudt_tag).where(published: true, udt_type: %i[ssri xudt xudt_compatible sudt])
9+
10+
if params[:tags].present?
11+
tags = parse_tags
12+
if params[:union].present?
13+
scope = scope.joins(:xudt_tag).where("xudt_tags.tags && ARRAY[?]::varchar[]", tags).select("udts.*") unless tags.empty?
14+
else
15+
scope = scope.joins(:xudt_tag).where("xudt_tags.tags @> array[?]::varchar[]", tags).select("udts.*") unless tags.empty?
16+
end
17+
end
18+
19+
if stale?(scope)
20+
expires_in 1.minute, public: true
21+
22+
udts = sort_udts(scope).page(@page).per(@page_size).fast_page
23+
options = FastJsonapi::PaginationMetaGenerator.new(
24+
request:,
25+
records: udts,
26+
page: @page,
27+
page_size: @page_size,
28+
).call
29+
30+
render json: UdtSerializer.new(udts, options)
31+
end
32+
end
33+
34+
def show
35+
udt = Udt.find_by!(type_hash: params[:id], published: true)
36+
render json: UdtSerializer.new(udt)
37+
rescue ActiveRecord::RecordNotFound
38+
raise Api::V1::Exceptions::UdtNotFoundError
39+
end
40+
41+
def download_csv
42+
args = params.permit(:id, :start_date, :end_date, :start_number, :end_number, udt: {})
43+
file = CsvExportable::ExportUdtTransactionsJob.perform_now(args.to_h)
44+
45+
send_data file, type: "text/csv; charset=utf-8; header=present",
46+
disposition: "attachment;filename=udt_transactions.csv"
47+
rescue ActiveRecord::RecordNotFound
48+
raise Api::V1::Exceptions::UdtNotFoundError
49+
end
50+
51+
private
52+
53+
def validate_query_params
54+
validator = Validations::Udt.new(params)
55+
56+
if validator.invalid?
57+
errors = validator.error_object[:errors]
58+
status = validator.error_object[:status]
59+
60+
render json: errors, status:
61+
end
62+
end
63+
64+
def pagination_params
65+
@page = params[:page] || 1
66+
@page_size = params[:page_size] || Udt.default_per_page
67+
end
68+
69+
def sort_udts(records)
70+
sort, order = params.fetch(:sort, "id.desc").split(".", 2)
71+
sort =
72+
case sort
73+
when "created_time" then "block_timestamp"
74+
when "transactions" then "h24_ckb_transactions_count"
75+
when "addresses_count" then "addresses_count"
76+
else "id"
77+
end
78+
79+
if order.nil? || !order.match?(/^(asc|desc)$/i)
80+
order = "asc"
81+
end
82+
83+
records.order("#{sort} #{order}").order("full_name ASC, id ASC")
84+
end
85+
86+
def parse_tags
87+
tags = params[:tags].split(",")
88+
tags & XudtTag::VALID_TAGS
89+
end
90+
end
91+
end
92+
end

app/jobs/revert_block_job.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def update_address_balance_and_ckb_transactions_count(local_tip_block)
4848
address.update!(attrs)
4949
else
5050
address.live_cells_count = address.cell_outputs.live.count
51-
address.ckb_transactions_count = AccountBook.where(address_id: address.id).count
51+
address.ckb_transactions_count = AccountBook.tx_committed.where(address_id: address.id).count
5252
address.dao_transactions_count = DaoEvent.processed.where(address_id: address.id).distinct(:ckb_transaction_id).count
5353
address.cal_balance!
5454
address.save!
@@ -184,7 +184,7 @@ def revert_deposit_to_dao(dao_events)
184184
end
185185

186186
upsert_data = address_attrs.values
187-
address_ids = address_attrs.values.map { |hash| hash[:id] }
187+
address_ids = address_attrs.values.pluck(:id)
188188
Address.upsert_all(upsert_data, unique_by: :id) if upsert_data.present?
189189
Address.where(id: address_ids, dao_deposit: 0).update_all(is_depositor: false)
190190

app/models/ckb_sync/new_node_data_processor.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def update_addresses_info(addrs_change, local_block, refresh_balance)
498498
if addr.last_updated_block_number.nil?
499499
addr.last_updated_block_number = local_block.number
500500
addr.live_cells_count = addr.cell_outputs.live.count
501-
addr.ckb_transactions_count = AccountBook.where(address_id: addr.id).count
501+
addr.ckb_transactions_count = AccountBook.tx_committed.where(address_id: addr.id).count
502502
addr.dao_transactions_count = DaoEvent.processed.where(address_id: addr.id).distinct(:ckb_transaction_id).count
503503
addr.cal_balance!
504504
addr.save!
@@ -1444,17 +1444,6 @@ def forked?(target_block, local_tip_block)
14441444
target_block.header.parent_hash != local_tip_block.block_hash
14451445
end
14461446

1447-
def update_address_balance_and_ckb_transactions_count(local_tip_block)
1448-
local_tip_block.contained_addresses.each do |address|
1449-
address.live_cells_count = address.cell_outputs.live.count
1450-
# address.ckb_transactions_count = address.custom_ckb_transactions.count
1451-
address.ckb_transactions_count = AccountBook.where(address_id: address.id).count
1452-
address.dao_transactions_count = DaoEvent.processed.where(address_id: address.id).distinct(:ckb_transaction_id).count
1453-
address.cal_balance!
1454-
address.save!
1455-
end
1456-
end
1457-
14581447
def update_nrc_factory_cell_info(type_script, output_data)
14591448
factory_cell = NrcFactoryCell.find_or_create_by(
14601449
code_hash: type_script.code_hash,

app/models/udt.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def holders_count
4848
end
4949

5050
def ssri_contract_outpoint
51-
return unless udt_type == "ssri"
51+
return unless udt_type == "ssri" && SsriContract.find_by(code_hash: code_hash, hash_type: hash_type)
5252

5353
cell = SsriContract.find_by(code_hash: code_hash, hash_type: hash_type).contract.deployed_cell_output
5454
{ tx_hash: cell.tx_hash, cell_index: cell.cell_index }

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
get :download_csv, on: :collection
6262
get :snapshot, on: :collection
6363
end
64+
resources :fungible_tokens, only: %i(index show) do
65+
get :download_csv, on: :collection
66+
end
6467
resources :omiga_inscriptions, only: %i(index show) do
6568
get :download_csv, on: :collection
6669
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "test_helper"
2+
3+
module Api
4+
module V1
5+
class FungibleTokensControllerTest < ActionDispatch::IntegrationTest
6+
setup do
7+
create(:udt, published: true, udt_type: "xudt_compatible")
8+
create(:udt, published: true, udt_type: "xudt")
9+
@ssri = create(:udt, published: true, udt_type: "ssri")
10+
create(:udt, published: true, udt_type: "sudt")
11+
end
12+
13+
test "should return all udts" do
14+
valid_get api_v1_fungible_tokens_url
15+
16+
assert_response :success
17+
assert 4, json["data"].length
18+
end
19+
20+
test "should return ssri udt" do
21+
valid_get api_v1_fungible_token_url(@ssri.type_hash)
22+
23+
assert_response :success
24+
assert "ssri", json["data"]["udt_type"]
25+
end
26+
end
27+
end
28+
end

test/factories/udt.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
transaction = create(:ckb_transaction, block:,
2424
contained_udt_ids: [udt.id], tags: ["udt"], tx_index: i * 2)
2525
transaction1 = create(:ckb_transaction, block:,
26-
contained_udt_ids: [udt.id], tags: ["udt"], tx_index: i * 2 + 1)
26+
contained_udt_ids: [udt.id], tags: ["udt"], tx_index: (i * 2) + 1)
2727
cell_output = create(:cell_output, block:,
2828
ckb_transaction: transaction,
2929
consumed_by: transaction1,

0 commit comments

Comments
 (0)