Skip to content

Commit

Permalink
Merge pull request #1395 from nervosnetwork/testnet
Browse files Browse the repository at this point in the history
Deploy to mainnet
  • Loading branch information
zmcNotafraid committed Aug 31, 2023
2 parents 17a8cd5 + bb754f7 commit 5618021
Show file tree
Hide file tree
Showing 84 changed files with 941 additions and 333 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ jobs:
image-name: '${{ steps.docker_build.outputs.imageFullName }}'
image-tag: '${{ steps.docker_build.outputs.tags }}'
steps:
- name: Free Disk Space (Ubuntu)
uses: jlumbroso/free-disk-space@main
with:
# this might remove tools that are actually needed,
# if set to "true" but frees about 6 GB
tool-cache: false

# all of these default to true, but feel free to set to
# "false" if necessary for your workflow
android: true
dotnet: true
haskell: true
large-packages: true
docker-images: true
swap-storage: true
- uses: actions/checkout@v3
with:
fetch-depth: 0
Expand Down
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ gem "dotenv-rails"
gem "config"

# CKB SDK
gem "ckb-sdk-ruby", github: "ShiningRay/ckb-sdk-ruby", require: "ckb", branch: "move_to_-rbsecp256k1"
# gem "ckb-sdk-ruby", git: "https://github.com/nervosnetwork/ckb-sdk-ruby.git", require: "ckb", tag: "v0.103.0"
gem "ckb-sdk-ruby", git: "https://github.com/nervosnetwork/ckb-sdk-ruby.git", require: "ckb"

# Redis
gem "hiredis" # , "~> 0.6.3"
Expand Down
23 changes: 11 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
GIT
remote: https://github.com/ShiningRay/ckb-sdk-ruby.git
revision: abf9f2f43eead30b85addfd00eda6e0903c1b3de
branch: move_to_-rbsecp256k1
specs:
ckb-sdk-ruby (0.103.0)
net-http-persistent (~> 4.0.1)
rbnacl (~> 7.1.1)
rbsecp256k1 (~> 5.1.1)

GIT
remote: https://github.com/dougmrqs/sidekiq-statistic.git
revision: 590a190fcc8020b4fa05f400d51f6980514f25dd
Expand All @@ -17,6 +7,15 @@ GIT
sidekiq (>= 5.0)
tilt (~> 2.0)

GIT
remote: https://github.com/nervosnetwork/ckb-sdk-ruby.git
revision: 1790c72fb886bda3e30936a73bc3d1cbcee6c91f
specs:
ckb-sdk-ruby (0.103.0)
net-http-persistent (~> 4.0.1)
rbnacl (~> 7.1.1)
rbsecp256k1 (~> 5.1.1)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -284,7 +283,7 @@ GEM
mocha (2.0.2)
ruby2_keywords (>= 0.0.5)
msgpack (1.6.0)
net-http-persistent (4.0.1)
net-http-persistent (4.0.2)
connection_pool (~> 2.2)
net-imap (0.3.1)
net-protocol
Expand All @@ -310,7 +309,7 @@ GEM
parser (3.2.2.0)
ast (~> 2.4.1)
pg (1.4.5)
pkg-config (1.5.1)
pkg-config (1.5.2)
protocol-hpack (1.4.2)
protocol-http (0.24.0)
protocol-http1 (0.14.6)
Expand Down
1 change: 0 additions & 1 deletion app/controllers/api/v1/address_transactions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "csv"
module Api
module V1
class AddressTransactionsController < ApplicationController
Expand Down
1 change: 0 additions & 1 deletion app/controllers/api/v1/blocks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "csv"
module Api
module V1
class BlocksController < ApplicationController
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/contract_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def show
address = Address.find_address!(params[:address_hash])
raise Api::V1::Exceptions::AddressNotFoundError if address.is_a?(NullAddress)

ckb_transactions = ckb_transactions.includes(:contained_dao_addresses).
where(address_dao_transactions: { address_id: address.id })
ckb_transactions = ckb_transactions.joins(:account_books).
where(account_books: { address_id: address.id })
end

ckb_transactions = ckb_transactions.page(@page).per(@page_size).fast_page
Expand Down
24 changes: 22 additions & 2 deletions app/controllers/api/v1/udts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "csv"
module Api
module V1
class UdtsController < ApplicationController
Expand All @@ -23,9 +22,30 @@ def index
end
end

def show
def update
udt = Udt.find_by!(type_hash: params[:id], published: true)
attrs = {
symbol: params[:symbol],
full_name: params[:full_name],
decimal: params[:decimal],
total_amount: params[:total_amount],
description: params[:description],
operator_website: params[:operator_website],
icon_file: params[:icon_file],
uan: params[:uan],
display_name: params[:display_name],
contact_info: params[:contact_info]
}
udt.update!(attrs)
render json: :ok
rescue ActiveRecord::RecordNotFound
raise Api::V1::Exceptions::UdtNotFoundError
rescue ActiveRecord::RecordInvalid => e
raise Api::V1::Exceptions::UdtInfoInvalidError.new(e)
end

def show
udt = Udt.find_by!(type_hash: params[:id], published: true)
render json: UdtSerializer.new(udt)
rescue ActiveRecord::RecordNotFound
raise Api::V1::Exceptions::UdtNotFoundError
Expand Down
161 changes: 76 additions & 85 deletions app/controllers/api/v2/ckb_transactions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,103 +1,94 @@
module Api
module V2
class CkbTransactionsController < ApplicationController
before_action :find_transaction, only: :details
before_action :set_page_and_page_size, only: :details

class CkbTransactionsController < BaseController
# transaction lite info
def details
capacities = {}
@ckb_transaction.display_inputs.select{ |e| e[:cell_type] == 'normal' }.each {|input|
capacities[input[:address_hash]] ||= 0
capacities[input[:address_hash]] -= input[:capacity].to_d
}
ckb_transaction = CkbTransaction.where(tx_hash: params[:id]).order(tx_status: :desc).first
head :not_found and return if ckb_transaction.blank?

@ckb_transaction.display_outputs.select{ |e| e[:cell_type] == 'normal' }.each {|output|
capacities[output[:address_hash]] ||= 0
capacities[output[:address_hash]] += output[:capacity].to_d
}
json = capacities.map { |address, value|
{
address: address,
transfers: [
{
asset: "CKB",
capacity: value,
token_name: "CKB",
entity_type: "CKB",
transfer_type: "ordinary_transfer"
}
]
}
}
expires_in 10.seconds, public: true, must_revalidate: true

input_capacities = build_cell_capacities(ckb_transaction.display_inputs)
output_capacities = build_cell_capacities(ckb_transaction.display_outputs)
transfers = build_transfers(input_capacities, output_capacities)

render json: {data: json}
render json: { data: transfers }
end

private
def find_transaction
@ckb_transaction = CkbTransaction.find_by(tx_hash: params[:id])
end

def set_page_and_page_size
@page = params[:page] || 1
@page_size = params[:page_size] || 10
end
def build_cell_capacities(outputs)
cell_capacities = Hash.new { |hash, key| hash[key] = {} }
outputs.each do |output|
parsed_output = JSON.parse(output.to_json, object_class: OpenStruct)
next if parsed_output.from_cellbase

def get_transaction_content address_ids, cell_outputs
transaction_data = []
transfers = []
address_ids.each do |address_id|
cell_outputs.where(address_id: address_id.address_id).each do |cell_output|
entity_type = "CKB"
transfer_type = "ordinary_transfer"
if cell_output.nervos_dao_deposit?
transfer_type = "nervos_dao_deposit"
elsif cell_output.nervos_dao_withdrawing?
transfer_type = "nervos_dao_withdrawing"
interest_data = get_nervos_dao_withdrawing_data(cell_output)
transfer.merge(interest_data)
elsif cell_output.cell_type.in?(%w(nrc_721_token nrc_721_factory))
entity_type = "nft"
transfer_type = "nft_transfer"
elsif cell_output.cell_type.in?(%w(m_nft_issuer m_nft_class m_nft_token))
transfer_type = "nft_mint"
entity_type = "nft"
nft_token = "NFT" # token 缩写
nft_id = "001" # NFT ID
end
transfer = {
asset: "unknown #62bc",
capacity: cell_output.capacity.to_s,
entity_type: entity_type,
transfer_type: transfer_type,
unit = token_unit(parsed_output)
address = parsed_output.address_hash
udt_info = parsed_output.udt_info

if (cell_capacity = cell_capacities[[address, unit]]).blank?
capacity = unit == "CKB" ? parsed_output.capacity.to_f : 0.0
cell_capacity = {
capacity: capacity,
cell_type: parsed_output.cell_type,
udt_info: {
symbol: udt_info&.symbol,
amount: udt_info&.amount.to_f,
decimal: udt_info&.decimal,
type_hash: udt_info&.type_hash,
published: !!udt_info&.published,
display_name: udt_info&.display_name,
uan: udt_info&.uan
}
}
transfers.push(transfer)
elsif unit == "CKB"
cell_capacity[:capacity] += parsed_output.capacity.to_f
else
cell_capacity[:udt_info][:amount] += udt_info.amount.to_f
end
address = Address.find address_id.address_id
data = {
address: address.address_hash,
transfers: transfers
}
transaction_data.push(data)

cell_capacities[[address, unit]] = cell_capacity
end
return transaction_data

cell_capacities
end

def get_nervos_dao_withdrawing_data
nervos_dao_deposit_cell = @transaction.cell_inputs.order(:id)[cell_output.cell_index].previous_cell_output
compensation_started_block = Block.find(nervos_dao_deposit_cell.block.id)
compensation_ended_block = Block.select(:number, :timestamp).find(@transaction.block_id)
interest = CkbUtils.dao_interest(cell_output)
interest_data = {
compensation_started_block_number: compensation_started_block.number.to_s,
compensation_ended_block_number: compensation_ended_block.number.to_s,
compensation_started_timestamp: compensation_started_block.timestamp.to_s,
compensation_ended_timestamp: compensation_ended_block.timestamp.to_s,
interest: interest,
locked_until_block_timestamp: @transaction.block.timestamp,
locked_until_block_number: @transaction.block.number,
}
return interest_data
def build_transfers(input_capacities, output_capacities)
capacities = Hash.new { |hash, key| hash[key] = [] }
keys = input_capacities.keys | output_capacities.keys
keys.each do |key|
address_hash, unit = key
input = input_capacities[key]
output = output_capacities[key]

# There may be keys in both input_capacities and output_capacities that do not exist
cell_type = output[:cell_type] || input[:cell_type]
capacity_change = output[:capacity].to_f - input[:capacity].to_f

transfer = { capacity: capacity_change, cell_type: cell_type }
if unit != "CKB"
output_amount = output[:udt_info] ? output[:udt_info][:amount] : 0.0
input_amount = input[:udt_info] ? input[:udt_info][:amount] : 0.0
amount_change = output_amount - input_amount
transfer[:udt_info] = output[:udt_info] || input[:udt_info]
transfer[:udt_info][:amount] = amount_change
end

capacities[address_hash] << CkbUtils.hash_value_to_s(transfer)
end

capacities.map do |address, value|
{ address: address, transfers: value }
end
end

def token_unit(cell)
if (udt_info = cell.udt_info).present?
udt_info.type_hash
else
"CKB"
end
end
end
end
Expand Down
29 changes: 23 additions & 6 deletions app/controllers/api/v2/nft/transfers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "csv"
module Api
module V2
module NFT
Expand All @@ -24,11 +23,23 @@ def index
scope = TokenTransfer.all
end

from = Address.find_by_address_hash(params[:from]) if params[:from]
to = Address.find_by_address_hash(params[:to]) if params[:to]
scope = scope.where(from: from) if from
scope = scope.where(to: to) if to
scope = scope.where(action: params[:transfer_action]) if params[:transfer_action]
if params[:from]
scope = scope.where(from: find_address(params[:from]))
end
if params[:to]
scope = scope.where(to: find_address(params[:to]))
end
if params[:address_hash]
address = find_address(params[:address_hash])
scope = scope.where(from: address).or(scope.where(to: address))
end
if params[:transfer_action]
scope = scope.where(action: params[:transfer_action])
end
if params[:tx_hash]
scope = scope.includes(:ckb_transaction).where(ckb_transaction: { tx_hash: params[:tx_hash] })
end

scope = scope.order(transaction_id: :desc)
pagy, token_transfers = pagy(scope)

Expand All @@ -50,6 +61,12 @@ def download_csv
send_data file, type: "text/csv; charset=utf-8; header=present",
disposition: "attachment;filename=token_transfers.csv"
end

private

def find_address(address_hash)
Address.find_by_address_hash(address_hash)
end
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions app/controllers/api/v2/pending_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ class PendingTransactionsController < BaseController
before_action :set_page_and_page_size

def index
pending_transactions = CkbTransaction.tx_pending
pending_transactions = CkbTransaction.tx_pending.includes(:cell_inputs).
where.not(cell_inputs: { previous_cell_output_id: nil, from_cell_base: false })

if stale?(pending_transactions)
expires_in 10.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds

total_count = pending_transactions.count
pending_transactions = sort_transactions(pending_transactions).
page(@page).per(@page_size).fast_page
page(@page).per(@page_size)

json = {
data: pending_transactions.map do |tx|
Expand All @@ -28,6 +29,7 @@ def index
page_size: @page_size.to_i
}
}

render json: json
end
end
Expand Down Expand Up @@ -59,7 +61,7 @@ def sort_transactions(records)
order = "asc"
end

records.order("#{sort} #{order} NULLS LAST")
records.order("ckb_transactions.#{sort} #{order} NULLS LAST")
end
end
end
Expand Down
Loading

0 comments on commit 5618021

Please sign in to comment.