-
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 #1347 from nervosnetwork/testnet
Deploy to mainnet
- Loading branch information
Showing
16 changed files
with
453 additions
and
265 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
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,80 @@ | ||
module CsvExportable | ||
class BaseExporter < ApplicationJob | ||
def perform(*) | ||
raise NotImplementedError | ||
end | ||
|
||
def generate_csv(header, rows) | ||
CSV.generate do |csv| | ||
csv << header | ||
rows.each { |row| csv << row } | ||
end | ||
end | ||
|
||
def generate_row(*) | ||
raise NotImplementedError | ||
end | ||
|
||
def attributes_for_udt_cell(udt_cell) | ||
udt_info = Udt.find_by(type_hash: udt_cell.type_hash, published: true) | ||
CkbUtils.hash_value_to_s( | ||
udt_info: { | ||
symbol: udt_info&.symbol, | ||
amount: udt_cell.udt_amount, | ||
decimal: udt_info&.decimal, | ||
uan: udt_info&.uan, | ||
type_hash: udt_cell.type_hash | ||
} | ||
) | ||
end | ||
|
||
def capacity_units(cell) | ||
units = ["CKB"] | ||
if cell[:udt_info] | ||
units << (cell[:udt_info][:uan].presence || cell[:udt_info][:symbol]) | ||
end | ||
|
||
units | ||
end | ||
|
||
def cell_capacity(cell, unit) | ||
return nil unless cell | ||
|
||
if unit == "CKB" | ||
byte = CkbUtils.shannon_to_byte(BigDecimal(cell[:capacity])) | ||
return byte.to_s("F") | ||
end | ||
|
||
if cell[:udt_info] && cell[:udt_info][:type_hash].present? | ||
return parse_udt_amount(cell[:udt_info][:amount], cell[:udt_info][:decimal]) | ||
end | ||
end | ||
|
||
def datetime_utc(timestamp) | ||
Time.at((timestamp / 1000).to_i).in_time_zone("UTC").strftime("%Y-%m-%d %H:%M:%S") | ||
end | ||
|
||
def parse_transaction_fee(fee) | ||
CkbUtils.shannon_to_byte(BigDecimal(fee)) | ||
end | ||
|
||
def parse_udt_amount(amount, decimal) | ||
decimal_int = decimal.to_i | ||
amount_big_decimal = BigDecimal(amount) | ||
result = amount_big_decimal / (BigDecimal(10)**decimal_int) | ||
|
||
if decimal_int > 20 | ||
return "#{result.round(20).to_s('F')}..." | ||
end | ||
|
||
if result.to_s.length >= 16 || result < BigDecimal("0.000001") | ||
return result.round(decimal_int).to_s("F") | ||
end | ||
|
||
return result.to_s("F") | ||
rescue StandardError => e | ||
puts "udt amount parse failed: #{e.message}" | ||
return "0" | ||
end | ||
end | ||
end |
107 changes: 107 additions & 0 deletions
107
app/jobs/csv_exportable/export_address_transactions_job.rb
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,107 @@ | ||
module CsvExportable | ||
class ExportAddressTransactionsJob < BaseExporter | ||
def perform(args) | ||
tx_ids = AccountBook.joins(:ckb_transaction). | ||
where(address_id: args[:address_id]). | ||
order(ckb_transaction_id: :asc). | ||
limit(5000) | ||
|
||
if args[:start_date].present? | ||
start_date = DateTime.strptime(args[:start_date], "%Y-%m-%d").to_time.to_i * 1000 | ||
tx_ids = tx_ids.where("ckb_transactions.block_timestamp >= ?", start_date) | ||
end | ||
|
||
if args[:end_date].present? | ||
end_date = DateTime.strptime(args[:end_date], "%Y-%m-%d").to_time.to_i * 1000 | ||
tx_ids = tx_ids.where("ckb_transactions.block_timestamp <= ?", end_date) | ||
end | ||
|
||
if args[:start_number].present? | ||
tx_ids = tx_ids.where("ckb_transactions.block_number >= ?", args[:start_number]) | ||
end | ||
|
||
if args[:end_number].present? | ||
tx_ids = tx_ids.where("ckb_transactions.block_number <= ?", args[:end_number]) | ||
end | ||
|
||
ckb_transactions = CkbTransaction.includes(:inputs, :outputs). | ||
select(:id, :tx_hash, :transaction_fee, :block_id, :block_number, :block_timestamp, :updated_at). | ||
where(id: tx_ids.pluck(:ckb_transaction_id)) | ||
|
||
rows = [] | ||
ckb_transactions.find_in_batches(batch_size: 1000, order: :desc) do |transactions| | ||
transactions.each do |transaction| | ||
row = generate_row(transaction, args[:address_id]) | ||
next if row.blank? | ||
|
||
rows += row | ||
end | ||
end | ||
|
||
header = [ | ||
"Txn hash", "Blockno", "UnixTimestamp", "Token", "Method", "Token In", | ||
"Token Out", "Token Balance Change", "TxnFee(CKB)", "date(UTC)" | ||
] | ||
|
||
generate_csv(header, rows) | ||
end | ||
|
||
def generate_row(transaction, address_id) | ||
inputs = simple_display_inputs(transaction, address_id) | ||
outputs = simple_display_outputs(transaction, address_id) | ||
datetime = datetime_utc(transaction.block_timestamp) | ||
|
||
rows = [] | ||
max = [inputs.size, outputs.size].max | ||
(0..max - 1).each do |i| | ||
units = capacity_units(outputs[i] || inputs[i]) | ||
units.each do |unit| | ||
token_in = cell_capacity(inputs[i], unit) | ||
token_out = cell_capacity(outputs[i], unit) | ||
balance_change = token_out.to_f - token_in.to_f | ||
method = balance_change.positive? ? "PAYMENT RECEIVED" : "PAYMENT SENT" | ||
fee = parse_transaction_fee(transaction.transaction_fee) | ||
|
||
rows << [ | ||
transaction.tx_hash, | ||
transaction.block_number, | ||
transaction.block_timestamp, | ||
unit, | ||
method, | ||
(token_in || "/"), | ||
(token_out || "/"), | ||
balance_change, | ||
(unit == "CKB" ? fee : "/"), | ||
datetime | ||
] | ||
end | ||
end | ||
|
||
rows | ||
end | ||
|
||
def simple_display_inputs(transaction, address_id) | ||
previous_cell_outputs = transaction.inputs.where(address_id: address_id).order(id: :asc) | ||
previous_cell_outputs.map do |cell_output| | ||
display_input = { capacity: cell_output.capacity } | ||
if cell_output.udt? | ||
display_input.merge!(attributes_for_udt_cell(cell_output)) | ||
end | ||
|
||
CkbUtils.hash_value_to_s(display_input) | ||
end | ||
end | ||
|
||
def simple_display_outputs(transaction, address_id) | ||
cell_outputs = transaction.outputs.where(address_id: address_id).order(id: :asc) | ||
cell_outputs.map do |cell_output| | ||
display_output = { capacity: cell_output.capacity } | ||
if cell_output.udt? | ||
display_output.merge!(attributes_for_udt_cell(cell_output)) | ||
end | ||
|
||
CkbUtils.hash_value_to_s(display_output) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.