Skip to content

Commit

Permalink
Merge pull request #1970 from nervosnetwork/develop
Browse files Browse the repository at this point in the history
Deploy to testnet
  • Loading branch information
zmcNotafraid authored Jun 20, 2024
2 parents 5abf3d1 + 1a70ae0 commit a9a5474
Show file tree
Hide file tree
Showing 25 changed files with 144 additions and 117 deletions.
32 changes: 20 additions & 12 deletions app/controllers/api/v1/udts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,26 @@ def index

def update
udt = Udt.find_by!(type_hash: params[:id])
attrs = {
symbol: params[:symbol],
full_name: params[:full_name],
decimal: params[:decimal],
description: params[:description],
operator_website: params[:operator_website],
icon_file: params[:icon_file],
uan: params[:uan],
display_name: params[:display_name],
email: params[:email],
published: true,
}
attrs =
if udt.udt_type == "xudt"
{
description: params[:description],
operator_website: params[:operator_website],
icon_file: params[:icon_file],
email: params[:email],
}
else
{
symbol: params[:symbol],
full_name: params[:full_name],
decimal: params[:decimal],
description: params[:description],
operator_website: params[:operator_website],
icon_file: params[:icon_file],
email: params[:email],
published: true,
}
end
if udt.email.blank?
raise Api::V1::Exceptions::UdtInfoInvalidError.new("Email can't be blank") if params[:email].blank?

Expand Down
2 changes: 0 additions & 2 deletions app/controllers/concerns/cell_data_comparator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def diff_udt_cells(inputs, outputs)
udt_infos[c.type_hash] = {
symbol: info&.symbol,
decimal: info&.decimal,
display_name: info&.display_name,
type_hash: c.type_hash,
uan: info&.uan,
}
end

Expand Down
28 changes: 17 additions & 11 deletions app/jobs/csv_exportable/base_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ def attributes_for_udt_cell(udt_cell)
decimal: udt_info&.decimal,
type_hash: udt_cell.type_hash,
published: !!udt_info&.published,
display_name: udt_info&.display_name,
uan: udt_info&.uan
}

return { udt_info: info }
{ udt_info: info }
end

def token_unit(cell)
Expand Down Expand Up @@ -64,10 +62,10 @@ def parse_udt_amount(amount, decimal)
return result.round(decimal_int).to_s("F")
end

return result.to_s("F")
result.to_s("F")
rescue StandardError => e
puts "udt amount parse failed: #{e.message}"
return "0"
"0"
end

def transfer_method(amount_in, amount_out)
Expand All @@ -91,10 +89,18 @@ def build_ckb_data(input, output)
capacity_diff = (capacity_out.to_d - capacity_in.to_d).abs

{
token_in: (CkbUtils.shannon_to_byte(capacity_in) rescue "/"),
token_out: (CkbUtils.shannon_to_byte(capacity_out) rescue "/"),
token_in: begin
CkbUtils.shannon_to_byte(capacity_in)
rescue StandardError
"/"
end,
token_out: begin
CkbUtils.shannon_to_byte(capacity_out)
rescue StandardError
"/"
end,
balance_diff: CkbUtils.shannon_to_byte(capacity_diff),
method: method
method:,
}
end

Expand All @@ -111,22 +117,22 @@ def build_udt_data(input, output)
token_in: amount_in.nil? ? "/" : parse_udt_amount(amount_in, decimal),
token_out: amount_out.nil? ? "/" : parse_udt_amount(amount_out, decimal),
balance_diff: parse_udt_amount(amount_diff, decimal),
method: method
method:,
}
else
{
token_in: amount_in.nil? ? "/" : "#{amount_in} (raw)",
token_out: amount_out.nil? ? "/" : "#{amount_out} (raw)",
balance_diff: "#{amount_diff} (raw)",
method: method
method:,
}
end
end

def parse_udt_token(input, output)
udt_info = output&.dig(:udt_info) || input&.dig(:udt_info)
if udt_info[:published]
udt_info[:uan].presence || udt_info[:symbol]
udt_info[:symbol]
else
type_hash = udt_info[:type_hash]
"Unknown Token ##{type_hash[-4..]}"
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/csv_exportable/export_udt_transactions_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def generate_row(transaction, udt)
rows = []
unit =
if udt.published
udt.uan.presence || udt.symbol
udt.symbol
else
type_hash = udt.type_hash
"Unknown Token ##{type_hash[-4..]}"
Expand All @@ -71,7 +71,7 @@ def generate_row(transaction, udt)
unit,
data[:balance_diff],
address_hash,
datetime
datetime,
]
end

Expand Down
7 changes: 1 addition & 6 deletions app/jobs/import_rgbpp_cell_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,13 @@ def build_transaction!(raw_tx, ckb_tx)
tx = BitcoinTransaction.find_by(txid: raw_tx["txid"])
return tx if tx

# raw transactions may not include the block hash
if raw_tx["blockhash"].present?
block_header = rpc.getblockheader(raw_tx["blockhash"])
end

created_at = Time.at((ckb_tx.block_timestamp / 1000).to_i).in_time_zone
BitcoinTransaction.create!(
txid: raw_tx["txid"],
tx_hash: raw_tx["hash"],
time: raw_tx["time"],
block_hash: raw_tx["blockhash"],
block_height: block_header&.dig("result", "height") || 0,
block_height: 0,
created_at:,
)
end
Expand Down
32 changes: 8 additions & 24 deletions app/models/bitcoin_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,20 @@ class BitcoinTransaction < ApplicationRecord
has_many :bitcoin_transfers

def confirmations
tip_block_height =
Rails.cache.fetch("tip_block_height", expires_in: 5.minutes) do
chain_info = Bitcoin::Rpc.instance.getblockchaininfo
chain_info.dig("result", "headers")
rescue StandardError => e
Rails.logger.error "get tip block faild: #{e.message}"
nil
end

return 0 unless tip_block_height

refresh_block_height! if block_hash.blank?
block_height == 0 ? 0 : tip_block_height - block_height
Rails.cache.fetch("#{txid}/confirmations", expires_in: 30.seconds) do
rpc = Bitcoin::Rpc.instance
raw_transaction = rpc.getrawtransaction(txid, 2)
raw_transaction.dig("result", "confirmations")

Check warning on line 9 in app/models/bitcoin_transaction.rb

View check run for this annotation

Codecov / codecov/patch

app/models/bitcoin_transaction.rb#L6-L9

Added lines #L6 - L9 were not covered by tests
rescue StandardError => e
Rails.logger.error "get #{txid} confirmations faild: #{e.message}"
0

Check warning on line 12 in app/models/bitcoin_transaction.rb

View check run for this annotation

Codecov / codecov/patch

app/models/bitcoin_transaction.rb#L11-L12

Added lines #L11 - L12 were not covered by tests
end
end

def ckb_transaction_hash
ckb_transaction = bitcoin_transfers&.take&.ckb_transaction
return ckb_transaction.tx_hash if ckb_transaction
end

def refresh_block_height!
rpc = Bitcoin::Rpc.instance
raw_transaction = rpc.getrawtransaction(txid, 2)
block_hash = raw_transaction.dig("result", "blockhash")
block_header = rpc.getblockheader(block_hash)
block_height = block_header.dig("result", "height")
update(block_hash:, block_height:)
rescue StandardError => e
Rails.logger.error "refresh block height error: #{e.message}"
end
end

# == Schema Information
Expand Down
10 changes: 2 additions & 8 deletions app/models/concerns/cell_outputs/extra_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ def udt_info
decimal: udt_info&.decimal,
type_hash:,
published: !!udt_info&.published,
display_name: udt_info&.display_name,
uan: udt_info&.uan,
)
end

Expand Down Expand Up @@ -70,8 +68,6 @@ def nrc_721_nft_info
decimal: "",
type_hash:,
published: true,
display_name: factory_cell&.name,
uan: "",
}
when "nrc_721_token"
udt = Udt.find_by(type_hash:)
Expand All @@ -83,8 +79,6 @@ def nrc_721_nft_info
decimal: udt_account.decimal,
type_hash:,
published: true,
display_name: udt_account.full_name,
uan: "",
}
else
raise "invalid cell type"
Expand Down Expand Up @@ -130,7 +124,7 @@ def cota_registry_info
code_hash = CkbSync::Api.instance.cota_registry_code_hash
CkbUtils.hash_value_to_s(
symbol: "", amount: udt_amount, decimal: "", type_hash:,
published: "true", display_name: "", uan: "", code_hash:
published: "true", code_hash:
)
end

Expand All @@ -140,7 +134,7 @@ def cota_regular_info
code_hash = CkbSync::Api.instance.cota_regular_code_hash
CkbUtils.hash_value_to_s(
symbol: "", amount: udt_amount, decimal: "", type_hash:,
published: "true", display_name: "", uan: "", code_hash:
published: "true", code_hash:
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/suggest_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def find_ckb_transaction_by_hash

def find_address_by_lock_hash
address = Address.cached_find(query_key)
LockHashSerializer.new(address) if address.present?
AddressSerializer.new(address) if address.present?
end

def find_cached_address
Expand Down
7 changes: 7 additions & 0 deletions app/models/udt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ def type_script
hash_type:,
}
end

def holder_allocation
allocation = Rails.cache.read("udt_holders/#{type_hash}")
return unless allocation

CkbUtils.hash_value_to_s(allocation)

Check warning on line 49 in app/models/udt.rb

View check run for this annotation

Codecov / codecov/patch

app/models/udt.rb#L49

Added line #L49 was not covered by tests
end
end

# == Schema Information
Expand Down
1 change: 0 additions & 1 deletion app/models/udt_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class UdtAccount < ApplicationRecord
validates :decimal,
numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 39 }, allow_nil: true
validates :amount, numericality: { greater_than_or_equal_to: 0 }
delegate :display_name, :uan, to: :udt

attribute :code_hash, :ckb_hash

Expand Down
2 changes: 0 additions & 2 deletions app/serializers/address_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class AddressSerializer
type_hash: udt_account.type_hash,
udt_icon_file: udt_account.udt_icon_file,
udt_type: udt_account.udt_type,
display_name: udt_account.display_name,
uan: udt_account.uan,
udt_type_script: udt_account.udt&.type_script,
}
elsif udt_account.udt_type.in?(["xudt", "xudt_compatible"])
Expand Down
5 changes: 4 additions & 1 deletion app/serializers/udt_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class UdtSerializer
include FastJsonapi::ObjectSerializer

attributes :symbol, :full_name, :icon_file, :published, :description,
:type_hash, :type_script, :issuer_address, :display_name, :uan, :udt_type, :operator_website
:type_hash, :type_script, :issuer_address, :udt_type, :operator_website

attribute :email do |object|
object.email&.sub(/\A(..)(.*)@(.*)(..)\z/) do
Expand All @@ -16,6 +16,9 @@ class UdtSerializer
attribute :addresses_count do |object|
object.addresses_count.to_s
end
attribute :holder_allocation do |object|
object.holder_allocation
end
attribute :decimal do |object|
object.decimal.to_s
end
Expand Down
16 changes: 7 additions & 9 deletions app/services/portfolios/udt_accounts_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(user)
end

def sudt_accounts(published = true)
udt_accounts = UdtAccount.sudt.where(address_id: user.address_ids, published: published)
udt_accounts = UdtAccount.sudt.where(address_id: user.address_ids, published:)

Check warning on line 10 in app/services/portfolios/udt_accounts_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/services/portfolios/udt_accounts_statistic.rb#L10

Added line #L10 was not covered by tests
grouped_accounts =
udt_accounts.group_by(&:type_hash).transform_values do |accounts|
total_amount = accounts.reduce(0) { |sum, account| sum + account.amount }
Expand All @@ -18,8 +18,6 @@ def sudt_accounts(published = true)
type_hash: accounts[0].type_hash,
udt_icon_file: accounts[0].udt_icon_file,
udt_type: accounts[0].udt_type,
display_name: accounts[0].display_name,
uan: accounts[0].uan
}
end

Expand All @@ -45,10 +43,10 @@ def nft_accounts
amount: udt_account.amount.to_s,
type_hash: udt_account.type_hash,
collection: {
type_hash: coll&.type_script&.script_hash
type_hash: coll&.type_script&.script_hash,

Check warning on line 46 in app/services/portfolios/udt_accounts_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/services/portfolios/udt_accounts_statistic.rb#L46

Added line #L46 was not covered by tests
},
udt_icon_file: udt_account.udt_icon_file,
udt_type: udt_account.udt_type
udt_type: udt_account.udt_type,

Check warning on line 49 in app/services/portfolios/udt_accounts_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/services/portfolios/udt_accounts_statistic.rb#L49

Added line #L49 was not covered by tests
}
when "nrc_721_token"
udt = udt_account.udt
Expand All @@ -59,10 +57,10 @@ def nft_accounts
amount: udt_account.nft_token_id.to_s,
type_hash: udt_account.type_hash,
collection: {
type_hash: coll&.type_script&.script_hash
type_hash: coll&.type_script&.script_hash,

Check warning on line 60 in app/services/portfolios/udt_accounts_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/services/portfolios/udt_accounts_statistic.rb#L60

Added line #L60 was not covered by tests
},
udt_icon_file: "#{udt_account.udt.nrc_factory_cell&.base_token_uri}/#{udt_account.nft_token_id}",
udt_type: udt_account.udt_type
udt_type: udt_account.udt_type,

Check warning on line 63 in app/services/portfolios/udt_accounts_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/services/portfolios/udt_accounts_statistic.rb#L63

Added line #L63 was not covered by tests
}
when "spore_cell"
ts = TypeScript.where(script_hash: udt_account.type_hash).first
Expand All @@ -76,10 +74,10 @@ def nft_accounts
amount: udt_account.nft_token_id.to_s,
type_hash: udt_account.type_hash,
collection: {
type_hash: coll&.type_script&.script_hash
type_hash: coll&.type_script&.script_hash,

Check warning on line 77 in app/services/portfolios/udt_accounts_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/services/portfolios/udt_accounts_statistic.rb#L77

Added line #L77 was not covered by tests
},
udt_icon_file: data,
udt_type: udt_account.udt_type
udt_type: udt_account.udt_type,

Check warning on line 80 in app/services/portfolios/udt_accounts_statistic.rb

View check run for this annotation

Codecov / codecov/patch

app/services/portfolios/udt_accounts_statistic.rb#L80

Added line #L80 was not covered by tests
}
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/utils/ckb_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -651,11 +651,11 @@ def self.parse_omiga_inscription_data(hex_data)
end

def self.is_rgbpp_lock_cell?(lock_script)
lock_script.code_hash == CkbSync::Api.instance.rgbpp_code_hash && lock_script.hash_type == "type"
CkbSync::Api.instance.rgbpp_code_hash.include?(lock_script.code_hash) && lock_script.hash_type == "type"

Check warning on line 654 in app/utils/ckb_utils.rb

View check run for this annotation

Codecov / codecov/patch

app/utils/ckb_utils.rb#L654

Added line #L654 was not covered by tests
end

def self.is_btc_time_lock_cell?(lock_script)
lock_script.code_hash == CkbSync::Api.instance.btc_time_code_hash && lock_script.hash_type == "type"
CkbSync::Api.instance.btc_time_code_hash.include?(lock_script.code_hash) && lock_script.hash_type == "type"

Check warning on line 658 in app/utils/ckb_utils.rb

View check run for this annotation

Codecov / codecov/patch

app/utils/ckb_utils.rb#L658

Added line #L658 was not covered by tests
end

def self.parse_btc_time_lock_cell(args)
Expand Down
Loading

0 comments on commit a9a5474

Please sign in to comment.