-
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 #1826 from nervosnetwork/develop
Deploy to testnet
- Loading branch information
Showing
26 changed files
with
534 additions
and
128 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
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 |
---|---|---|
@@ -1,31 +1,36 @@ | ||
module Api::V2 | ||
class StatisticsController < BaseController | ||
def transaction_fees | ||
expires_in 15.seconds, public: true | ||
stats_info = StatisticInfo.default | ||
if stale?(stats_info) | ||
expires_in 15.seconds, public: true | ||
|
||
render json: { | ||
transaction_fee_rates: stats_info.transaction_fee_rates, | ||
pending_transaction_fee_rates: stats_info.pending_transaction_fee_rates, | ||
last_n_days_transaction_fee_rates: stats_info.last_n_days_transaction_fee_rates, | ||
} | ||
render json: { | ||
transaction_fee_rates: stats_info.transaction_fee_rates, | ||
pending_transaction_fee_rates: stats_info.pending_transaction_fee_rates, | ||
last_n_days_transaction_fee_rates: stats_info.last_n_days_transaction_fee_rates, | ||
} | ||
end | ||
end | ||
|
||
def contract_resource_distributed | ||
expires_in 30.minutes, public: true | ||
contracts = Contract.filter_nil_hash_type | ||
if stale?(contracts) | ||
expires_in 30.minutes, public: true | ||
json = contracts.map do |contract| | ||
{ | ||
name: contract.name, | ||
code_hash: contract.code_hash, | ||
hash_type: contract.hash_type, | ||
tx_count: contract.ckb_transactions_count, | ||
h24_tx_count: contract.h24_ckb_transactions_count, | ||
ckb_amount: (contract.total_referring_cells_capacity / 10**8).truncate(8), | ||
address_count: contract.addresses_count, | ||
} | ||
end | ||
|
||
json = Contract.filter_nil_hash_type.map do |contract| | ||
{ | ||
name: contract.name, | ||
code_hash: contract.code_hash, | ||
hash_type: contract.hash_type, | ||
tx_count: contract.ckb_transactions_count, | ||
ckb_amount: (contract.total_referring_cells_capacity / 10**8).truncate(8), | ||
address_count: contract.addresses_count, | ||
} | ||
render json: | ||
end | ||
|
||
render json: | ||
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module CsvExportable | ||
class ExportUdtSnapshotJob < BaseExporter | ||
attr_accessor :udt, :block | ||
|
||
def perform(args) | ||
@block = Block.find_by!(number: args[:number]) | ||
@udt = Udt.published_xudt.find_by!(type_hash: args[:id]) | ||
type_script = TypeScript.find_by(@udt.type_script) | ||
|
||
condition = <<-SQL | ||
type_script_id = #{type_script.id} AND | ||
block_timestamp <= #{@block.timestamp} AND | ||
(consumed_block_timestamp > #{@block.timestamp} OR consumed_block_timestamp IS NULL) | ||
SQL | ||
snapshot = CellOutput.where(condition).group(:address_id).sum(:udt_amount) | ||
|
||
rows = [] | ||
snapshot = snapshot.reject { |_, v| v.to_f.zero? } | ||
snapshot.sort_by { |_k, v| -v }.each do |address_id, udt_amount| | ||
row = generate_row(address_id, udt_amount) | ||
next if row.blank? | ||
|
||
rows << row | ||
end | ||
|
||
header = ["Token Symbol", "Block Height", "UnixTimestamp", "date(UTC)", "Address", "Amount"] | ||
generate_csv(header, rows) | ||
end | ||
|
||
def generate_row(address_id, udt_amount) | ||
address = Address.find_by(id: address_id) | ||
return unless address | ||
|
||
address_hash = address.bitcoin_address&.address_hash || address.address_hash | ||
datetime = datetime_utc(@block.timestamp) | ||
|
||
if (decimal = @udt.decimal) | ||
[ | ||
@udt.symbol, | ||
@block.number, | ||
@block.timestamp, | ||
datetime, | ||
address_hash, | ||
parse_udt_amount(udt_amount.to_d, decimal), | ||
] | ||
else | ||
[ | ||
@udt.symbol, | ||
@block.number, | ||
@block.timestamp, | ||
datetime, | ||
address_hash, | ||
"#{udt_amount} (raw)", | ||
] | ||
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
class ImportBtcTimeCellJob < ApplicationJob | ||
queue_as :bitcoin | ||
|
||
def perform(cell_id) | ||
ApplicationRecord.transaction do | ||
cell_output = CellOutput.find_by(id: cell_id) | ||
return unless cell_output | ||
|
||
lock_script = cell_output.lock_script | ||
return unless CkbUtils.is_btc_time_lock_cell?(lock_script) | ||
|
||
parsed_args = CkbUtils.parse_btc_time_lock_cell(lock_script.args) | ||
txid = parsed_args.txid | ||
Rails.logger.info("Importing btc time cell #{cell_id} txid #{txid}") | ||
|
||
# build bitcoin transaction | ||
raw_tx = fetch_raw_transaction(txid) | ||
return unless raw_tx | ||
|
||
tx = build_transaction!(raw_tx) | ||
# build transfer | ||
BitcoinTransfer.create_with( | ||
bitcoin_transaction_id: tx.id, | ||
ckb_transaction_id: cell_output.ckb_transaction_id, | ||
lock_type: "btc_time", | ||
).find_or_create_by!( | ||
cell_output_id: cell_id, | ||
) | ||
end | ||
end | ||
|
||
def build_transaction!(raw_tx) | ||
tx = BitcoinTransaction.find_by(txid: raw_tx["txid"]) | ||
return tx if tx | ||
|
||
BitcoinTransaction.create!( | ||
txid: raw_tx["txid"], | ||
tx_hash: raw_tx["hash"], | ||
time: raw_tx["time"], | ||
block_hash: raw_tx["blockhash"], | ||
block_height: 0, | ||
) | ||
end | ||
|
||
def fetch_raw_transaction(txid) | ||
data = Rails.cache.read(txid) | ||
data ||= rpc.getrawtransaction(txid, 2) | ||
Rails.cache.write(txid, data, expires_in: 10.minutes) unless Rails.cache.exist?(txid) | ||
data["result"] | ||
rescue StandardError => e | ||
Rails.logger.error "get bitcoin raw transaction #{txid} failed: #{e}" | ||
nil | ||
end | ||
|
||
def rpc | ||
@rpc ||= Bitcoin::Rpc.instance | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class BitcoinTransfer < ApplicationRecord | ||
belongs_to :bitcoin_transaction | ||
belongs_to :ckb_transaction | ||
belongs_to :cell_output | ||
|
||
enum lock_type: { rgbpp: 0, btc_time: 1 } | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: bitcoin_transfers | ||
# | ||
# id :bigint not null, primary key | ||
# bitcoin_transaction_id :bigint | ||
# ckb_transaction_id :bigint | ||
# cell_output_id :bigint | ||
# lock_type :integer default("rgbpp") | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_bitcoin_transfers_on_bitcoin_transaction_id (bitcoin_transaction_id) | ||
# index_bitcoin_transfers_on_cell_output_id (cell_output_id) UNIQUE | ||
# index_bitcoin_transfers_on_ckb_transaction_id (ckb_transaction_id) | ||
# |
Oops, something went wrong.