-
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.
Merge pull request #1722 from nervosnetwork/develop
Deploy to testnet
- Loading branch information
Showing
15 changed files
with
255 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module Api | ||
module V1 | ||
class XudtsController < ApplicationController | ||
before_action :validate_query_params, only: :show | ||
before_action :validate_pagination_params, :pagination_params, only: :index | ||
|
||
def index | ||
udts = Udt.xudt | ||
|
||
if stale?(udts) | ||
expires_in 30.minutes, public: true, stale_while_revalidate: 10.minutes, stale_if_error: 10.minutes | ||
|
||
udts = sort_udts(udts).page(@page).per(@page_size).fast_page | ||
options = FastJsonapi::PaginationMetaGenerator.new( | ||
request:, | ||
records: udts, | ||
page: @page, | ||
page_size: @page_size, | ||
).call | ||
|
||
render json: UdtSerializer.new(udts, options) | ||
end | ||
end | ||
|
||
def show | ||
udt = Udt.find_by!(type_hash: params[:id], published: true) | ||
render json: UdtSerializer.new(udt) | ||
rescue ActiveRecord::RecordNotFound | ||
raise Api::V1::Exceptions::UdtNotFoundError | ||
end | ||
|
||
def download_csv | ||
args = params.permit(:id, :start_date, :end_date, :start_number, :end_number, udt: {}) | ||
file = CsvExportable::ExportUdtTransactionsJob.perform_now(args.to_h) | ||
|
||
send_data file, type: "text/csv; charset=utf-8; header=present", | ||
disposition: "attachment;filename=xudt_transactions.csv" | ||
rescue ActiveRecord::RecordNotFound | ||
raise Api::V1::Exceptions::UdtNotFoundError | ||
end | ||
|
||
private | ||
|
||
def validate_query_params | ||
validator = Validations::Udt.new(params) | ||
|
||
if validator.invalid? | ||
errors = validator.error_object[:errors] | ||
status = validator.error_object[:status] | ||
|
||
render json: errors, status: | ||
end | ||
end | ||
|
||
def pagination_params | ||
@page = params[:page] || 1 | ||
@page_size = params[:page_size] || Udt.default_per_page | ||
end | ||
|
||
def sort_udts(records) | ||
sort, order = params.fetch(:sort, "id.desc").split(".", 2) | ||
sort = | ||
case sort | ||
when "created_time" then "block_timestamp" | ||
when "transactions" then "h24_ckb_transactions_count" | ||
when "addresses_count" then "addresses_count" | ||
else "id" | ||
end | ||
|
||
if order.nil? || !order.match?(/^(asc|desc)$/i) | ||
order = "asc" | ||
end | ||
|
||
records.order("#{sort} #{order}") | ||
end | ||
end | ||
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
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
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,23 @@ | ||
require "test_helper" | ||
|
||
module Api | ||
module V1 | ||
class XudtsControllerTest < ActionDispatch::IntegrationTest | ||
test "should get success code when call show" do | ||
udt = create(:udt, :xudt, published: true) | ||
|
||
valid_get api_v1_xudt_url(udt.type_hash) | ||
|
||
assert_response :success | ||
end | ||
|
||
test "should get success code when call index" do | ||
create(:udt, :xudt, published: true) | ||
|
||
valid_get api_v1_xudts_url | ||
|
||
assert_response :success | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,5 +55,11 @@ | |
udt_id: udt.id) | ||
end | ||
end | ||
|
||
trait :xudt do | ||
udt_type { "xudt" } | ||
symbol { "Unique BBQ" } | ||
decimal { 8 } | ||
end | ||
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
Oops, something went wrong.