Skip to content

Commit

Permalink
feat: update udt info through api (#1373)
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <mingchang555@hotmail.com>
  • Loading branch information
zmcNotafraid committed Aug 14, 2023
1 parent 1ff7417 commit 9488372
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 4 deletions.
23 changes: 22 additions & 1 deletion app/controllers/api/v1/udts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,30 @@ def index
end
end

def show
def update
udt = Udt.find_by!(type_hash: params[:id], published: true)
attrs = {
symbol: params[:symbol],
full_name: params[:full_name],
decimal: params[:decimal],
total_amount: params[:total_amount],
description: params[:description],
operator_website: params[:operator_website],
icon_file: params[:icon_file],
uan: params[:uan],
display_name: params[:display_name],
contact_info: params[:contact_info]
}
udt.update!(attrs)
render json: :ok
rescue ActiveRecord::RecordNotFound
raise Api::V1::Exceptions::UdtNotFoundError
rescue ActiveRecord::RecordInvalid => e
raise Api::V1::Exceptions::UdtInfoInvalidError.new(e)
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
Expand Down
7 changes: 7 additions & 0 deletions app/lib/api/v1/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ def initialize
super code: 1029, status: 404, title: "Script not found", detail: "Script not found", href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html"
end
end

class UdtInfoInvalidError < Error
def initialize(detail)
super code: 1030, status: 400, title: "UDT info parameters invalid", detail: detail, href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html"
end
end

end
end
end
1 change: 1 addition & 0 deletions app/models/udt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def type_script
# display_name :string
# uan :string
# h24_ckb_transactions_count :bigint default(0)
# contact_info :string
#
# Indexes
#
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
resources :block_statistics, only: :show ## TODO: unused route
resources :epoch_statistics, only: :show
resources :market_data, only: :show
resources :udts, only: %i(index show) do
resources :udts, only: %i(index show update) do
collection do
get :download_csv
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230808020637_add_contact_info_to_udts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddContactInfoToUdts < ActiveRecord::Migration[7.0]
def change
add_column :udts, :contact_info, :string
end
end
6 changes: 4 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,8 @@ CREATE TABLE public.udts (
nrc_factory_cell_id bigint,
display_name character varying,
uan character varying,
h24_ckb_transactions_count bigint DEFAULT 0
h24_ckb_transactions_count bigint DEFAULT 0,
contact_info character varying
);


Expand Down Expand Up @@ -4667,6 +4668,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20230622143339'),
('20230630112234'),
('20230711040233'),
('20230802015907');
('20230802015907'),
('20230808020637');


68 changes: 68 additions & 0 deletions test/controllers/api/v1/udts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,74 @@ class UdtsControllerTest < ActionDispatch::IntegrationTest

assert_response :success
end

test "should update udt info suceessfully" do
udt = create(:udt, published: true)

valid_put api_v1_udt_url(udt.type_hash), params: {
symbol: "GWK",
full_name: "GodwokenToken on testnet_v1",
decimal: "8",
total_amount: "100000000000",
description: "The sUDT_ERC20_Proxy of Godwoken Test Token.",
operator_website: "https://udt.coin",
icon_file: "https://img.udt.img",
uan: "GWK.gw|gb.ckb",
display_name: "GodwokenToken (via Godwoken Bridge from CKB)",
contact_info: "contact@usdt.com"
}

assert_response :success
udt.reload
assert_equal udt.symbol, "GWK"
assert_equal udt.full_name, "GodwokenToken on testnet_v1"
assert_equal udt.decimal, 8
assert_equal udt.total_amount, 100000000000
assert_equal udt.description, "The sUDT_ERC20_Proxy of Godwoken Test Token."
assert_equal udt.operator_website, "https://udt.coin"
assert_equal udt.icon_file, "https://img.udt.img"
assert_equal udt.uan, "GWK.gw|gb.ckb"
assert_equal udt.display_name, "GodwokenToken (via Godwoken Bridge from CKB)"
assert_equal udt.contact_info, "contact@usdt.com"

end

test "raise parameters error when update udt" do
udt = create(:udt, published: true)

valid_put api_v1_udt_url(udt.type_hash), params: {
symbol: "GWK",
full_name: "GodwokenToken on testnet_v1",
decimal: "8",
description: "The sUDT_ERC20_Proxy of Godwoken Test Token.",
operator_website: "https://udt.coin",
icon_file: "https://img.udt.img",
uan: "GWK.gw|gb.ckb",
display_name: "GodwokenToken (via Godwoken Bridge from CKB)"
}

assert_equal 400, response.status
assert_equal response.body, "[{\"title\":\"UDT info parameters invalid\",\"detail\":\"Validation failed: Total amount can't be blank, Total amount is not a number\",\"code\":1030,\"status\":400}]"
end

test "raise not found error when update udt" do
udt = create(:udt, published: true)

valid_put api_v1_udt_url("#{udt.type_hash}0"), params: {
symbol: "GWK",
full_name: "GodwokenToken on testnet_v1",
decimal: "8",
total_amount: "100000000",
description: "The sUDT_ERC20_Proxy of Godwoken Test Token.",
operator_website: "https://udt.coin",
icon_file: "https://img.udt.img",
uan: "GWK.gw|gb.ckb",
display_name: "GodwokenToken (via Godwoken Bridge from CKB)"
}

assert_equal 404, response.status
assert_equal response.body, "[{\"title\":\"UDT Not Found\",\"detail\":\"No UDT records found by given type hash\",\"code\":1026,\"status\":404}]"
end
end
end
end
7 changes: 7 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,13 @@ def valid_post(uri, **params)
params[:headers] = { "Content-Type": "application/vnd.api+json", "Accept": "application/vnd.api+json" }
post uri, as: :json, **params
end

def valid_put(uri, **params)
params[:params] ||= {}
params[:headers] = { "Content-Type": "application/vnd.api+json", "Accept": "application/vnd.api+json" }
put uri, as: :json, **params
end

end

module ActiveSupport
Expand Down

0 comments on commit 9488372

Please sign in to comment.