Skip to content

Commit

Permalink
Merge pull request #1964 from nervosnetwork/testnet
Browse files Browse the repository at this point in the history
Deploy to mainnet
  • Loading branch information
zmcNotafraid authored Jun 20, 2024
2 parents 1381de1 + a9a5474 commit e68ad6f
Show file tree
Hide file tree
Showing 31 changed files with 288 additions and 154 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ SECRET_KEY_BASE=""

# -------------------------------- Bitcoin segment --------------------------------
BITCOIN_NODE_URL=""
BITCOIN_SIGNET_NODE_URL=""
BITCOIN_SIGNET_USER=""
BITCOIN_SIGNET_PASS=""

# Dynamic CORS configuration
PARTNER_DOMAINS="/localhost:\d*/"
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
16 changes: 5 additions & 11 deletions app/controllers/api/v2/bitcoin_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ def query
not_cached = cache_keys - res.keys
to_cache = {}

raw_transactions = ->(txids) do
Bitcoin::Rpc.instance.batch_get_raw_transactions(txids)
end

if not_cached.present?
get_raw_transactions(not_cached).each do |tx|
raw_transactions.call(not_cached).each do |tx|
next if tx.dig("error").present?

txid = tx.dig("result", "txid")
Expand All @@ -25,16 +29,6 @@ def query
Rails.logger.error "get raw transactions(#{params[:txids]}) failed: #{e.message}"
render json: {}, status: :not_found
end

private

def get_raw_transactions(txids)
payload = txids.map.with_index do |txid, index|
{ jsonrpc: "1.0", id: index + 1, method: "getrawtransaction", params: [txid, 2] }
end
response = HTTP.timeout(10).post(ENV["BITCOIN_NODE_URL"], json: payload)
JSON.parse(response.to_s)
end
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/api/v2/ckb_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ def rgb_digest
bitcoin_vouts: { ckb_transaction_id: @ckb_transaction.id },
)
op_return = @ckb_transaction.bitcoin_vouts.find_by(op_return: true)
leap_direction = @ckb_transaction.leap_direction
transfer_step = @ckb_transaction.transfer_step

if op_return && bitcoin_transaction
txid = bitcoin_transaction.txid
commitment = op_return.commitment
confirmations = bitcoin_transaction.confirmations
leap_direction = @ckb_transaction.leap_direction
transfer_step = @ckb_transaction.transfer_step

calculated_commitment = begin
CkbUtils.calculate_commitment(@ckb_transaction)
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")
rescue StandardError => e
Rails.logger.error "get #{txid} confirmations faild: #{e.message}"
0
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
20 changes: 18 additions & 2 deletions app/models/suggest_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def query_methods
method(:find_type_script_by_code_hash),
method(:find_lock_script_by_code_hash),
method(:find_bitcoin_transaction_by_txid),
method(:find_nft_collections_by_sn),
]
end

Expand All @@ -87,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 All @@ -97,7 +98,17 @@ def find_cached_address

def find_udt_by_type_hash
udt = Udt.find_by(type_hash: query_key, published: true)
UdtSerializer.new(udt) if udt.present?
return unless udt.present?

if udt.spore_cell?
type_script = TypeScript.find_by(script_hash: query_key)
return unless type_script

token_item = TokenItem.find_by(type_script:)
TokenItemSerializer.new(token_item) if token_item.present?
else
UdtSerializer.new(udt)
end
end

def find_type_script_by_type_id
Expand Down Expand Up @@ -127,6 +138,11 @@ def find_udts_by_name_or_symbol
UdtSerializer.new(udts) if udts.present?
end

def find_nft_collections_by_sn
token_collections = TokenCollection.where(sn: query_key)
TokenCollectionSerializer.new(token_collections) if token_collections.present?
end

def find_nft_collections_by_name
token_collections = TokenCollection.where("LOWER(name) LIKE LOWER(:query_key)", query_key: "%#{query_key}%")
TokenCollectionSerializer.new(token_collections) if token_collections.present?
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)
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
Loading

0 comments on commit e68ad6f

Please sign in to comment.