From b1f3b2cd3305c24ca797819286791b65875ca857 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Tue, 24 Oct 2023 15:38:28 +0800 Subject: [PATCH 1/4] chore: make put method cors (#1486) Signed-off-by: Miles Zhang --- config/initializers/cors.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/config/initializers/cors.rb b/config/initializers/cors.rb index fd8e1a3f5..e673c5386 100644 --- a/config/initializers/cors.rb +++ b/config/initializers/cors.rb @@ -18,13 +18,13 @@ Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins "https://explorer.nervos.org", - "https://explorer-testnet.nervos.org", - "https://aggron.explorer.nervos.org", - "https://pudge.explorer.nervos.org", - "https://staging.explorer.nervos.org", - /\Ahttps:\/\/ckb-explorer-.*-magickbase.vercel.app\z/, - "http://localhost:3000", - (ENV["STAGING_DOMAIN"]).to_s - resource "*", headers: :any, methods: [:get, :post, :head, :options] + "https://explorer-testnet.nervos.org", + "https://aggron.explorer.nervos.org", + "https://pudge.explorer.nervos.org", + "https://staging.explorer.nervos.org", + /\Ahttps:\/\/ckb-explorer-.*-magickbase.vercel.app\z/, + "http://localhost:3000", + (ENV["STAGING_DOMAIN"]).to_s + resource "*", headers: :any, methods: [:get, :post, :put, :head, :options] end end From 70f3d613097a83665a6e5ea581c976158ef8428e Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 30 Oct 2023 10:44:47 +0800 Subject: [PATCH 2/4] feat: query udt by name or symbol (#1488) Signed-off-by: Miles Zhang --- .../api/v1/udt_queries_controller.rb | 11 +++ app/models/udt.rb | 4 ++ config/routes.rb | 2 + .../api/v1/udt_queries_controller_test.rb | 69 +++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 app/controllers/api/v1/udt_queries_controller.rb create mode 100644 test/controllers/api/v1/udt_queries_controller_test.rb diff --git a/app/controllers/api/v1/udt_queries_controller.rb b/app/controllers/api/v1/udt_queries_controller.rb new file mode 100644 index 000000000..cdd934e39 --- /dev/null +++ b/app/controllers/api/v1/udt_queries_controller.rb @@ -0,0 +1,11 @@ +module Api + module V1 + class UdtQueriesController < ApplicationController + def index + udts = Udt.query_by_name_or_symbl(params[:q].downcase) + + render json: UdtSerializer.new(udts, { fields: { udt: [:full_name, :symbol, :type_hash, :icon_file] } }) + end + end + end +end diff --git a/app/models/udt.rb b/app/models/udt.rb index 0b73b73b5..2d9645aad 100644 --- a/app/models/udt.rb +++ b/app/models/udt.rb @@ -14,6 +14,10 @@ class Udt < ApplicationRecord validates :total_amount, numericality: { greater_than_or_equal_to: 0 } validates :email, format: { with: /\A(.+)@(.+)\z/, message: "Not a valid email" }, allow_nil: true + scope :query_by_name_or_symbl, ->(search) { + where("lower(full_name) LIKE ? or lower(symbol) LIKE ?", "%#{search}%", "%#{search}%") + } + attribute :code_hash, :ckb_hash has_and_belongs_to_many :ckb_transactions, join_table: :udt_transactions diff --git a/config/routes.rb b/config/routes.rb index fe4a1de51..8b2f4784e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -34,6 +34,8 @@ resources :cell_output_type_scripts, only: :show resources :cell_output_data, only: :show resources :suggest_queries, only: :index + resources :udt_queries, only: :index + resources :statistics, only: %i(index show) resources :statistics, only: %i(index show) resources :nets, only: %i(index show) resources :statistic_info_charts, only: :index diff --git a/test/controllers/api/v1/udt_queries_controller_test.rb b/test/controllers/api/v1/udt_queries_controller_test.rb new file mode 100644 index 000000000..1e9f82ef0 --- /dev/null +++ b/test/controllers/api/v1/udt_queries_controller_test.rb @@ -0,0 +1,69 @@ +require "test_helper" + +module Api + module V1 + class UdtQueriesControllerTest < ActionDispatch::IntegrationTest + test "should return empty array" do + valid_get api_v1_udt_queries_url, params: { q: "CKB" } + + response_json = { data: [] }.to_json + + assert_response :success + assert_equal "application/vnd.api+json", response.media_type + assert_equal response_json, response.body + end + + test "should query by symbol" do + udt = create(:udt, full_name: "Nervos Token", symbol: "CKB") + + valid_get api_v1_udt_queries_url, params: { q: "CKB" } + + response_json = UdtSerializer.new([udt], + { + fields: { + udt: [ + :full_name, :symbol, :type_hash, + :icon_file + ] } }).serialized_json + + assert_response :success + assert_equal response_json, response.body + end + + test "should query by name" do + udt = create(:udt, full_name: "Nervos Token", symbol: "CKB") + + valid_get api_v1_udt_queries_url, params: { q: "nervos" } + + response_json = UdtSerializer.new([udt], + { + fields: { + udt: [ + :full_name, :symbol, :type_hash, + :icon_file + ] } }).serialized_json + + assert_response :success + assert_equal response_json, response.body + end + + test "should query by symbol and name" do + udt1 = create(:udt, full_name: "Nervos Token", symbol: "CKB") + udt2 = create(:udt, full_name: "Nervos CKB", symbol: "NCKB") + + valid_get api_v1_udt_queries_url, params: { q: "CKB" } + + response_json = UdtSerializer.new([udt1, udt2], + { + fields: { + udt: [ + :full_name, :symbol, :type_hash, + :icon_file + ] } }).serialized_json + + assert_response :success + assert_equal response_json, response.body + end + end + end +end From 896fa7d68ec8409d5c2b2c96a958c6b5ed72905d Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Wed, 1 Nov 2023 18:09:50 +0800 Subject: [PATCH 3/4] feat: add cache for daily statistic data (#1489) * feat: add cache for daily statistic data Signed-off-by: Miles Zhang * test: fix test Signed-off-by: Miles Zhang --------- Signed-off-by: Miles Zhang --- app/controllers/api/v1/daily_statistics_controller.rb | 4 +++- app/controllers/api/v1/monetary_data_controller.rb | 2 ++ app/models/daily_statistic.rb | 2 +- test/models/daily_statistic_test.rb | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/daily_statistics_controller.rb b/app/controllers/api/v1/daily_statistics_controller.rb index 8a092ba33..4c6294c74 100644 --- a/app/controllers/api/v1/daily_statistics_controller.rb +++ b/app/controllers/api/v1/daily_statistics_controller.rb @@ -6,7 +6,9 @@ class DailyStatisticsController < ApplicationController def show daily_statistics = DailyStatistic.order(created_at_unixtimestamp: :asc).valid_indicators - render json: rendered_json(daily_statistics) + if stale?(daily_statistics, public: true) + render json: rendered_json(daily_statistics) + end end private diff --git a/app/controllers/api/v1/monetary_data_controller.rb b/app/controllers/api/v1/monetary_data_controller.rb index 0a75148ef..cd4e7719a 100644 --- a/app/controllers/api/v1/monetary_data_controller.rb +++ b/app/controllers/api/v1/monetary_data_controller.rb @@ -4,6 +4,8 @@ class MonetaryDataController < ApplicationController before_action :validate_query_params, only: :show def show + expires_in 1.hour, public: true, stale_while_revalidate: 10.minutes, stale_if_error: 1.hour + monetary_data = MonetaryData.new render json: MonetaryDataSerializer.new(monetary_data, params: { indicator: params[:id] }) diff --git a/app/models/daily_statistic.rb b/app/models/daily_statistic.rb index 0087fccc4..d1aceed13 100644 --- a/app/models/daily_statistic.rb +++ b/app/models/daily_statistic.rb @@ -12,7 +12,7 @@ class DailyStatistic < ApplicationRecord attr_accessor :from_scratch - scope :valid_indicators, -> { select(VALID_INDICATORS - %w(burnt liquidity created_at updated_at) + %w(id)) } + scope :valid_indicators, -> { select(VALID_INDICATORS - %w(burnt liquidity created_at) + %w(id updated_at)) } scope :recent, -> { order("created_at_unixtimestamp desc nulls last") } scope :recent_year, -> { where("created_at_unixtimestamp >= ? and created_at_unixtimestamp < ?", Time.current.beginning_of_year.to_i, Time.current.to_i) diff --git a/test/models/daily_statistic_test.rb b/test/models/daily_statistic_test.rb index ba4764f28..d6fdd79cd 100644 --- a/test/models/daily_statistic_test.rb +++ b/test/models/daily_statistic_test.rb @@ -4,6 +4,6 @@ class DailyStatisticTest < ActiveSupport::TestCase test "valid_indicators should only return valid indicators" do create(:daily_statistic) attrs = DailyStatistic.valid_indicators.first.attribute_names + %w(burnt liquidity) - assert_equal (DailyStatistic::VALID_INDICATORS + %w(id)).sort, attrs.sort + assert_equal (DailyStatistic::VALID_INDICATORS + %w(id updated_at)).sort, attrs.sort end end From 67bfec139d6942e729d9871d0eeaf492760796fa Mon Sep 17 00:00:00 2001 From: NanZhang Date: Mon, 6 Nov 2023 11:43:37 +0800 Subject: [PATCH 4/4] chore: log transaction deletion chain (#1491) --- app/models/ckb_transaction.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/models/ckb_transaction.rb b/app/models/ckb_transaction.rb index 376fad136..47a1e860c 100644 --- a/app/models/ckb_transaction.rb +++ b/app/models/ckb_transaction.rb @@ -52,6 +52,7 @@ class CkbTransaction < ApplicationRecord after_commit :flush_cache before_destroy :recover_dead_cell + before_destroy :log_deletion_chain def self.cached_find(query_key) Rails.cache.realize([name, query_key], race_condition_ttl: 3.seconds) do @@ -434,6 +435,12 @@ def cellbase_display_inputs def recover_dead_cell inputs.update_all(status: "live", consumed_by_id: nil, consumed_block_timestamp: nil) end + + def log_deletion_chain + unless tx_pending? + Rails.logger.info "Deleting #{self.class.name} with ID #{id} using #{caller}: #{as_json}" + end + end end # == Schema Information