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 testnet #1619

Merged
merged 2 commits into from
Feb 6, 2024
Merged
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
2 changes: 2 additions & 0 deletions app/controllers/api/v1/udts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def update
raise Api::V1::Exceptions::TokenExpiredError
rescue UdtVerification::TokenNotMatchError
raise Api::V1::Exceptions::TokenNotMatchError
rescue UdtVerification::TokenNotExistError
raise Api::V1::Exceptions::TokenNotExistError
end

def show
Expand Down
10 changes: 8 additions & 2 deletions app/lib/api/v1/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ def initialize

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"
super code: 1030, status: 400, title: "UDT info parameters invalid", detail:, href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html"
end
end

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

Expand Down Expand Up @@ -228,6 +228,12 @@ def initialize
super code: 1036, status: 400, title: "Token sent too frequently", detail: "", href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html"
end
end

class TokenNotExistError < Error
def initialize
super code: 1037, status: 400, title: "Token is required when you update udt info", detail: "", href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html"
end
end
end
end
end
8 changes: 5 additions & 3 deletions app/models/udt_verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ class UdtVerification < ApplicationRecord
class TokenExpiredError < StandardError; end
class TokenNotMatchError < StandardError; end
class TokenSentTooFrequentlyError < StandardError; end
class TokenNotExistError < StandardError; end

belongs_to :udt

def refresh_token!(ip)
raise TokenSentTooFrequentlyError if sent_at.present? && self.sent_at + SENT_FREQUENCY_MINUTES.minutes > Time.now
raise TokenSentTooFrequentlyError if sent_at.present? && sent_at + SENT_FREQUENCY_MINUTES.minutes > Time.now

self.token = rand(999999).to_s.rjust(6, "0")
self.sent_at = Time.now
self.last_ip = ip
self.save!
save!
end

def validate_token!(token_params)
raise TokenExpiredError if self.sent_at + KEEP_ALIVE_MINUTES.minutes < Time.now
raise TokenNotExistError if token_params.blank?
raise TokenExpiredError if sent_at + KEEP_ALIVE_MINUTES.minutes < Time.now
raise TokenNotMatchError if token != token_params.to_i
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/serializers/ckb_transactions_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class CkbTransactionsSerializer
end

attribute :display_inputs_count do |object|
object.display_inputs.count
object.is_cellbase ? 1 : object.cell_inputs.count
end

attribute :display_outputs_count do |object|
object.display_outputs.count
object.outputs.count
end

attribute :display_inputs do |object, params|
Expand Down
13 changes: 13 additions & 0 deletions test/controllers/api/v1/udts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,19 @@ class UdtsControllerTest < ActionDispatch::IntegrationTest
assert_equal true, udt.reload.published
assert_equal "[email protected]", udt.reload.email
end

test "should raise token not exist error when update udt but token not passed" do
udt = create(:udt, email: "[email protected]")
create(:udt_verification, udt:)
valid_put api_v1_udt_url("#{udt.type_hash}"), params: {
symbol: "GWK",
full_name: "GodwokenToken on testnet_v1",
}

assert_equal 400, response.status
assert_equal [{ "title" => "Token is required when you update udt info", "detail" => "", "code" => 1037, "status" => 400 }],
JSON.parse(response.body)
end
end
end
end
Loading