diff --git a/app/controllers/api/v1/contract_transactions_controller.rb b/app/controllers/api/v1/contract_transactions_controller.rb index 49a8eeab5..b2a9a38ac 100644 --- a/app/controllers/api/v1/contract_transactions_controller.rb +++ b/app/controllers/api/v1/contract_transactions_controller.rb @@ -4,7 +4,7 @@ class ContractTransactionsController < ApplicationController before_action :validate_pagination_params, :pagination_params def show - raise Api::V1::Exceptions::ContractNotFoundError if params[:id] != "dao" + raise Api::V1::Exceptions::ContractNotFoundError if params[:id] != DaoContract::CONTRACT_NAME dao_contract = DaoContract.default_contract ckb_transactions = dao_contract.ckb_transactions.distinct.recent.page(@page).per(@page_size) options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call diff --git a/app/controllers/api/v1/contracts_controller.rb b/app/controllers/api/v1/contracts_controller.rb index 76dae0573..61d3a706a 100644 --- a/app/controllers/api/v1/contracts_controller.rb +++ b/app/controllers/api/v1/contracts_controller.rb @@ -2,7 +2,7 @@ module Api module V1 class ContractsController < ApplicationController def show - raise Api::V1::Exceptions::ContractNotFoundError if params[:id] != "dao" + raise Api::V1::Exceptions::ContractNotFoundError if params[:id] != DaoContract::CONTRACT_NAME render json: DaoContractSerializer.new(DaoContract.default_contract) end diff --git a/app/models/address.rb b/app/models/address.rb index f5ad553de..e20a21978 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -5,7 +5,7 @@ class Address < ApplicationRecord has_many :cell_outputs has_many :account_books has_many :ckb_transactions, through: :account_books - validates :balance, :cell_consumed, :ckb_transactions_count, :subsidy, :dao_deposit, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true + validates :balance, :cell_consumed, :ckb_transactions_count, :interest, :dao_deposit, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true after_commit :flush_cache @@ -24,7 +24,7 @@ def self.find_address!(query_key) end def self.cached_find(query_key) - Rails.cache.fetch([name, query_key], race_condition_ttl: 3.seconds) do + Rails.cache.realize([name, query_key], race_condition_ttl: 3.seconds) do if QueryKeyUtils.valid_hex?(query_key) find_by(lock_hash: query_key) else @@ -34,7 +34,7 @@ def self.cached_find(query_key) end def cached_lock_script - Rails.cache.fetch([self.class.name, "lock_script", lock_hash], race_condition_ttl: 3.seconds) do + Rails.cache.realize([self.class.name, "lock_script", lock_hash], race_condition_ttl: 3.seconds) do lock_script.to_node_lock end end @@ -59,7 +59,7 @@ def flush_cache # lock_hash :binary # pending_reward_blocks_count :integer default(0) # dao_deposit :decimal(30, ) default(0) -# subsidy :decimal(30, ) default(0) +# interest :decimal(30, ) default(0) # # Indexes # diff --git a/app/models/block.rb b/app/models/block.rb index 977c9fa10..81a7de651 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -59,7 +59,7 @@ def self.find_block!(query_key) end def self.cached_find(query_key) - Rails.cache.fetch([name, query_key], race_condition_ttl: 3.seconds) do + Rails.cache.realize([name, query_key], race_condition_ttl: 3.seconds) do if QueryKeyUtils.valid_hex?(query_key) block = where(block_hash: query_key).first else diff --git a/app/models/cell_input.rb b/app/models/cell_input.rb index a48afce72..dcd9286ec 100644 --- a/app/models/cell_input.rb +++ b/app/models/cell_input.rb @@ -5,13 +5,13 @@ class CellInput < ApplicationRecord after_commit :flush_cache def find_lock_script! - Rails.cache.fetch(["CellInput", id, "lock_script"], race_condition_ttl: 3.seconds) do + Rails.cache.realize(["CellInput", id, "lock_script"], race_condition_ttl: 3.seconds) do previous_cell_output!.lock_script end end def find_type_script! - Rails.cache.fetch(["CellInput", id, "type_script"], race_condition_ttl: 3.seconds) do + Rails.cache.realize(["CellInput", id, "type_script"], race_condition_ttl: 3.seconds) do previous_cell_output!.type_script end end @@ -30,7 +30,7 @@ def previous_cell_output end def self.cached_find(id) - Rails.cache.fetch([name, id], race_condition_ttl: 3.seconds) { find(id) } + Rails.cache.realize([name, id], race_condition_ttl: 3.seconds) { find(id) } end def flush_cache @@ -46,7 +46,7 @@ def previous_cell_output! tx_hash = previous_output["tx_hash"] cell_index = previous_output["index"].to_i - Rails.cache.fetch("previous_cell_output/#{tx_hash}/#{cell_index}", race_condition_ttl: 3.seconds) do + Rails.cache.realize("previous_cell_output/#{tx_hash}/#{cell_index}", race_condition_ttl: 3.seconds) do CellOutput.find_by!(tx_hash: tx_hash, cell_index: cell_index) end end diff --git a/app/models/cell_output.rb b/app/models/cell_output.rb index 4ad5e3be2..c0d78af6d 100644 --- a/app/models/cell_output.rb +++ b/app/models/cell_output.rb @@ -2,7 +2,7 @@ class CellOutput < ApplicationRecord SYSTEM_TX_HASH = "0x0000000000000000000000000000000000000000000000000000000000000000".freeze enum status: { live: 0, dead: 1 } - enum cell_type: { normal: 0, dao: 1 } + enum cell_type: { normal: 0, nervos_dao_deposit: 1, nervos_dao_withdrawing: 2 } belongs_to :ckb_transaction belongs_to :generated_by, class_name: "CkbTransaction" diff --git a/app/models/ckb_sync/node_data_processor.rb b/app/models/ckb_sync/node_data_processor.rb index 5dc2da1df..cc13d1f3c 100644 --- a/app/models/ckb_sync/node_data_processor.rb +++ b/app/models/ckb_sync/node_data_processor.rb @@ -59,7 +59,7 @@ def update_dao_contract_related_info(local_block) process_deposit_to_dao(dao_contract, dao_events) process_new_dao_depositor(dao_contract, dao_events) process_withdraw_from_dao(dao_contract, dao_events) - process_issue_subsidy(dao_contract, dao_events) + process_issue_interest(dao_contract, dao_events) process_take_away_all_deposit(dao_contract, dao_events) end @@ -71,12 +71,12 @@ def process_take_away_all_deposit(dao_contract, dao_events) end end - def process_issue_subsidy(dao_contract, dao_events) - issue_subsidy_dao_events = dao_events.where(event_type: "issue_subsidy") - issue_subsidy_dao_events.each do |event| - dao_contract.increment!(:subsidy_granted, event.value) + def process_issue_interest(dao_contract, dao_events) + issue_interest_dao_events = dao_events.where(event_type: "issue_interest") + issue_interest_dao_events.each do |event| + dao_contract.increment!(:interest_granted, event.value) address = event.address - address.increment!(:subsidy, event.value) + address.increment!(:interest, event.value) event.processed! end end @@ -170,7 +170,7 @@ def revert_dao_contract_related_operations(dao_contract, dao_events) revert_deposit_to_dao(dao_contract, dao_events) revert_new_dao_depositor(dao_contract, dao_events) revert_withdraw_from_dao(dao_contract, dao_events) - revert_issue_subsidy(dao_contract, dao_events) + revert_issue_interest(dao_contract, dao_events) revert_take_away_all_deposit(dao_contract, dao_events) end @@ -182,12 +182,12 @@ def revert_take_away_all_deposit(dao_contract, dao_events) end end - def revert_issue_subsidy(dao_contract, dao_events) - issue_subsidy_dao_events = dao_events.where(event_type: "issue_subsidy") - issue_subsidy_dao_events.each do |event| - dao_contract.decrement!(:subsidy_granted, event.value) + def revert_issue_interest(dao_contract, dao_events) + issue_interest_dao_events = dao_events.where(event_type: "issue_interest") + issue_interest_dao_events.each do |event| + dao_contract.decrement!(:interest_granted, event.value) address = event.address - address.decrement!(:subsidy, event.value) + address.decrement!(:interest, event.value) event.reverted! end end @@ -374,7 +374,7 @@ def build_cell_outputs(node_outputs, ckb_transaction, addresses, outputs_data, o end def build_deposit_dao_events(address, cell_output, ckb_transaction, new_dao_depositor_events) - if cell_output.dao? + if cell_output.nervos_dao_deposit? dao_contract = DaoContract.find_or_create_by(id: 1) ckb_transaction.dao_events.build(block: ckb_transaction.block, address_id: address.id, event_type: "deposit_to_dao", value: cell_output.capacity, contract_id: dao_contract.id) if address.dao_deposit.zero? && !new_dao_depositor_events.key?(address.id) @@ -384,14 +384,12 @@ def build_deposit_dao_events(address, cell_output, ckb_transaction, new_dao_depo end def build_withdraw_dao_events(address_id, ckb_transaction_id, local_block, previous_cell_output) - if previous_cell_output.dao? + if previous_cell_output.nervos_dao_withdrawing? withdraw_amount = previous_cell_output.capacity ckb_transaction = CkbTransaction.find(ckb_transaction_id) ckb_transaction.dao_events.create!(block: local_block, address_id: address_id, event_type: "withdraw_from_dao", value: withdraw_amount, contract_id: DaoContract.default_contract.id) - header_deps = ckb_transaction.header_deps - witnesses = ckb_transaction.witnesses - subsidy = CkbUtils.dao_subsidy(previous_cell_output, header_deps, witnesses) - ckb_transaction.dao_events.create!(block: local_block, address_id: address_id, event_type: "issue_subsidy", value: subsidy, contract_id: DaoContract.default_contract.id) + interest = CkbUtils.dao_interest(previous_cell_output) + ckb_transaction.dao_events.create!(block: local_block, address_id: address_id, event_type: "issue_interest", value: interest, contract_id: DaoContract.default_contract.id) address = Address.find(address_id) if (address.dao_deposit - withdraw_amount).zero? ckb_transaction.dao_events.create!(block: local_block, address_id: address_id, event_type: "take_away_all_deposit", value: 1, contract_id: DaoContract.default_contract.id) @@ -399,10 +397,14 @@ def build_withdraw_dao_events(address_id, ckb_transaction_id, local_block, previ end end - def cell_type(type_script) - return "normal" if type_script.blank? + def cell_type(type_script, output_data) + return "normal" unless [ENV["DAO_CODE_HASH"], ENV["DAO_TYPE_HASH"]].include?(type_script&.code_hash) - [ENV["DAO_CODE_HASH"], ENV["DAO_TYPE_HASH"]].include?(type_script.code_hash) ? "dao" : "normal" + if output_data == CKB::Utils.bin_to_hex("\x00" * 8) + "nervos_dao_deposit" + else + "nervos_dao_withdrawing" + end end def build_cell_output(ckb_transaction, output, address, cell_index, output_data) @@ -414,7 +416,7 @@ def build_cell_output(ckb_transaction, output, address, cell_index, output_data) tx_hash: ckb_transaction.tx_hash, cell_index: cell_index, generated_by: ckb_transaction, - cell_type: cell_type(output.type) + cell_type: cell_type(output.type, output_data) ) end diff --git a/app/models/ckb_transaction.rb b/app/models/ckb_transaction.rb index 4856d0bc7..8159278f6 100644 --- a/app/models/ckb_transaction.rb +++ b/app/models/ckb_transaction.rb @@ -21,7 +21,7 @@ class CkbTransaction < ApplicationRecord after_commit :flush_cache def self.cached_find(query_key) - Rails.cache.fetch([name, query_key], race_condition_ttl: 3.seconds) do + Rails.cache.realize([name, query_key], race_condition_ttl: 3.seconds) do find_by(tx_hash: query_key) end end @@ -51,18 +51,18 @@ def income(address) end def dao_transaction? - inputs.dao.present? || outputs.dao.present? + inputs.where(cell_type: %w(nervos_dao_deposit nervos_dao_withdrawing)) end private def normal_tx_display_outputs(previews) - Rails.cache.fetch("normal_tx_display_outputs_previews_#{previews}_#{id}", race_condition_ttl: 3.seconds) do + Rails.cache.realize("normal_tx_display_outputs_previews_#{previews}_#{id}", race_condition_ttl: 3.seconds) do cell_outputs_for_display = previews ? outputs.limit(10) : outputs cell_outputs_for_display.order(:id).map do |output| consumed_tx_hash = output.live? ? nil : output.consumed_by.tx_hash display_output = { id: output.id, capacity: output.capacity, address_hash: output.address_hash, status: output.status, consumed_tx_hash: consumed_tx_hash, cell_type: output.cell_type } - display_output.merge!({ dao_type_hash: ENV["DAO_TYPE_HASH"] }) if output.dao? + display_output.merge!({ dao_type_hash: ENV["DAO_TYPE_HASH"] }) unless output.normal? display_output end @@ -79,12 +79,12 @@ def cellbase_display_outputs end def normal_tx_display_inputs(previews) - Rails.cache.fetch("normal_tx_display_inputs_previews_#{previews}_#{id}", race_condition_ttl: 3.seconds) do + Rails.cache.realize("normal_tx_display_inputs_previews_#{previews}_#{id}", race_condition_ttl: 3.seconds) do cell_inputs_for_display = previews ? cell_inputs.limit(10) : cell_inputs cell_inputs_for_display.order(:id).map do |cell_input| previous_cell_output = cell_input.previous_cell_output display_input = { id: previous_cell_output.id, from_cellbase: false, capacity: previous_cell_output.capacity, address_hash: previous_cell_output.address_hash, generated_tx_hash: previous_cell_output.generated_by.tx_hash, cell_type: previous_cell_output.cell_type } - display_input.merge!(attributes_for_dao_input(previous_cell_output)) if previous_cell_output.dao? + display_input.merge!(attributes_for_dao_input(previous_cell_output)) if previous_cell_output.nervos_dao_withdrawing? display_input end @@ -92,14 +92,13 @@ def normal_tx_display_inputs(previews) end def attributes_for_dao_input(input) - witness = witnesses[input.cell_index] - header_deps_index = CKB::Utils.bin_to_hex(CKB::Utils.hex_to_bin(witness)[-8..-1]).hex - withdraw_block_hash = header_deps[header_deps_index] - started_block_number = Block.find(input.block_id).number + withdraw_block_hash = input.block.block_hash + nervos_dao_withdrawing_cell_generated_tx = input.generated_by + started_block_number = Block.find(nervos_dao_withdrawing_cell_generated_tx.block.id).number ended_block_number = Block.find_by(block_hash: withdraw_block_hash).number - subsidy = CkbUtils.dao_subsidy(input, header_deps, witnesses) + interest = CkbUtils.dao_interest(input) - { started_block_number: started_block_number, ended_block_number: ended_block_number, subsidy: subsidy, dao_type_hash: ENV["DAO_TYPE_HASH"] } + { started_block_number: started_block_number, ended_block_number: ended_block_number, interest: interest, dao_type_hash: ENV["DAO_TYPE_HASH"] } end def cellbase_display_inputs diff --git a/app/models/dao_contract.rb b/app/models/dao_contract.rb index dbc331f9a..e6b857c6d 100644 --- a/app/models/dao_contract.rb +++ b/app/models/dao_contract.rb @@ -1,12 +1,13 @@ class DaoContract < ApplicationRecord - validates :total_deposit, :subsidy_granted, :deposit_transactions_count, :withdraw_transactions_count, :depositors_count, :total_depositors_count, presence: true, numericality: { greater_than_or_equal_to: 0 } + validates :total_deposit, :interest_granted, :deposit_transactions_count, :withdraw_transactions_count, :depositors_count, :total_depositors_count, presence: true, numericality: { greater_than_or_equal_to: 0 } + CONTRACT_NAME = "nervos_dao" def self.default_contract find_or_create_by(id: 1) end def ckb_transactions - ckb_transaction_ids = CellOutput.dao.select("ckb_transaction_id") + ckb_transaction_ids = CellOutput.where(cell_type: %w(nervos_dao_deposit nervos_dao_withdrawing)).select("ckb_transaction_id") CkbTransaction.where(id: ckb_transaction_ids) end end @@ -17,7 +18,7 @@ def ckb_transactions # # id :bigint not null, primary key # total_deposit :decimal(30, ) default(0) -# subsidy_granted :decimal(30, ) default(0) +# interest_granted :decimal(30, ) default(0) # deposit_transactions_count :bigint default(0) # withdraw_transactions_count :bigint default(0) # depositors_count :integer default(0) diff --git a/app/models/dao_event.rb b/app/models/dao_event.rb index 53c90fa5d..98cdd2380 100644 --- a/app/models/dao_event.rb +++ b/app/models/dao_event.rb @@ -1,5 +1,5 @@ class DaoEvent < ApplicationRecord - enum event_type: { deposit_to_dao: 0, new_dao_depositor: 1, withdraw_from_dao: 2, issue_subsidy: 3, take_away_all_deposit: 4 } + enum event_type: { deposit_to_dao: 0, new_dao_depositor: 1, withdraw_from_dao: 2, issue_interest: 3, take_away_all_deposit: 4 } enum status: { pending: 0, processed: 1, reverted: 2 } validates :value, presence: true, numericality: { greater_than_or_equal_to: 0 } diff --git a/app/models/miner_ranking.rb b/app/models/miner_ranking.rb index 9c03d9689..f31840b09 100644 --- a/app/models/miner_ranking.rb +++ b/app/models/miner_ranking.rb @@ -9,7 +9,7 @@ def id def ranking(limit = nil) limit = limit.presence || DEFAULT_LIMIT - Rails.cache.fetch("miner_ranking", expires_in: 1.hour, race_condition_ttl: 10.seconds) do + Rails.cache.realize("miner_ranking", expires_in: 1.hour, race_condition_ttl: 10.seconds) do ranking_infos.take(limit) end end diff --git a/app/models/net_info.rb b/app/models/net_info.rb index 847bb174e..e36f662d0 100644 --- a/app/models/net_info.rb +++ b/app/models/net_info.rb @@ -20,7 +20,7 @@ def version end def local_node_info - Rails.cache.fetch("local_node_info") do + Rails.cache.realize("local_node_info") do CkbSync::Api.instance.local_node_info end end diff --git a/app/models/statistic_info_chart.rb b/app/models/statistic_info_chart.rb index 214caa1d1..06444d079 100644 --- a/app/models/statistic_info_chart.rb +++ b/app/models/statistic_info_chart.rb @@ -9,7 +9,7 @@ def id def hash_rate to = Rails.cache.read("hash_rate_to") - Rails.cache.fetch("hash_rate_chart_data_#{to}")&.uniq || [] + Rails.cache.realize("hash_rate_chart_data_#{to}")&.uniq || [] end def uncle_rate @@ -23,7 +23,7 @@ def uncle_rate def difficulty current_epoch_number = CkbSync::Api.instance.get_current_epoch.number - Rails.cache.fetch("statistic_info_difficulty_#{current_epoch_number}", expires_in: 10.minutes, race_condition_ttl: 10.seconds) do + Rails.cache.realize("statistic_info_difficulty_#{current_epoch_number}", expires_in: 10.minutes, race_condition_ttl: 10.seconds) do from = Block.where(epoch: 0).recent.first&.number.to_i to = Block.maximum(:number).to_i hash_rate_block_numbers = (from + 1).step(to, 100).to_a @@ -37,8 +37,8 @@ def difficulty def calculate_hash_rate max_block_number = Block.maximum(:number).to_i - from = Rails.cache.fetch("hash_rate_from") { last_epoch0_block_number } - to = Rails.cache.fetch("hash_rate_to") { max_block_number } + from = Rails.cache.realize("hash_rate_from") { last_epoch0_block_number } + to = Rails.cache.realize("hash_rate_to") { max_block_number } epoch_first_block_numbers = Block.order(:epoch, :timestamp).select("distinct on (epoch) number").to_a.pluck(:number) result = diff --git a/app/presenters/address_presenter.rb b/app/presenters/address_presenter.rb index d58998b81..0bdfa2c58 100644 --- a/app/presenters/address_presenter.rb +++ b/app/presenters/address_presenter.rb @@ -15,6 +15,14 @@ def balance object.reduce(0) { |sum, addr| sum + addr.balance.to_i } end + def dao_deposit + object.reduce(0) { |sum, addr| sum + addr.dao_deposit.to_i } + end + + def interest + object.reduce(0) { |sum, addr| sum + addr.interest.to_i } + end + def lock_script object.first.cached_lock_script end @@ -34,7 +42,7 @@ def ckb_transactions def ckb_dao_transactions address_ids = object.pluck(:id) - ckb_transaction_ids = CellOutput.where(address_id: address_ids).dao.select("ckb_transaction_id") + ckb_transaction_ids = CellOutput.where(address_id: address_ids).where(cell_type: %w(nervos_dao_deposit nervos_dao_withdrawing)).select("ckb_transaction_id") CkbTransaction.where(id: ckb_transaction_ids) end diff --git a/app/serializers/address_serializer.rb b/app/serializers/address_serializer.rb index 95d310688..3ab195bb1 100644 --- a/app/serializers/address_serializer.rb +++ b/app/serializers/address_serializer.rb @@ -11,4 +11,10 @@ class AddressSerializer attribute :pending_reward_blocks_count do |object| object.pending_reward_blocks_count.to_s end + attribute :dao_deposit do |object| + object.dao_deposit.to_s + end + attribute :interest do |object| + object.interest.to_s + end end diff --git a/app/serializers/dao_contract_serializer.rb b/app/serializers/dao_contract_serializer.rb index f52640449..e2faa3779 100644 --- a/app/serializers/dao_contract_serializer.rb +++ b/app/serializers/dao_contract_serializer.rb @@ -1,6 +1,9 @@ class DaoContractSerializer include FastJsonapi::ObjectSerializer - set_type :dao - attributes :total_deposit, :subsidy_granted, :deposit_transactions_count, :withdraw_transactions_count, + set_type :nervos_dao + attributes :total_deposit, :interest_granted, :deposit_transactions_count, :withdraw_transactions_count, :depositors_count, :total_depositors_count + attribute :dao_type_hash do + ENV["DAO_TYPE_HASH"] + end end diff --git a/app/serializers/lock_hash_serializer.rb b/app/serializers/lock_hash_serializer.rb index 2082a4883..b86ffed49 100644 --- a/app/serializers/lock_hash_serializer.rb +++ b/app/serializers/lock_hash_serializer.rb @@ -16,4 +16,10 @@ class LockHashSerializer attribute :balance do |object| object.balance.to_s end + attribute :dao_deposit do |object| + object.dao_deposit.to_s + end + attribute :interest do |object| + object.interest.to_s + end end diff --git a/app/utils/ckb_utils.rb b/app/utils/ckb_utils.rb index 34786cf31..dcb7f4558 100644 --- a/app/utils/ckb_utils.rb +++ b/app/utils/ckb_utils.rb @@ -160,7 +160,7 @@ def self.parse_epoch_info(header) end def self.ckb_transaction_fee(ckb_transaction, input_capacities, output_capacities) - if ckb_transaction.inputs.dao.present? + if ckb_transaction.inputs.where(cell_type: "nervos_dao_withdrawing").present? dao_withdraw_tx_fee(ckb_transaction) else normal_tx_fee(input_capacities, output_capacities) @@ -235,20 +235,18 @@ def self.normal_tx_fee(input_capacities, output_capacities) end def self.dao_withdraw_tx_fee(ckb_transaction) - dao_cells = ckb_transaction.inputs.dao - witnesses = ckb_transaction.witnesses - header_deps = ckb_transaction.header_deps - interests = dao_cells.reduce(0) { |memo, dao_cell| memo + dao_subsidy(dao_cell, header_deps, witnesses) } + nervos_dao_withdrawing_cells = ckb_transaction.inputs.nervos_dao_withdrawing + interests = nervos_dao_withdrawing_cells.reduce(0) { |memo, nervos_dao_withdrawing_cell| memo + dao_interest(nervos_dao_withdrawing_cell) } ckb_transaction.inputs.sum(:capacity) + interests - ckb_transaction.outputs.sum(:capacity) end - def self.dao_subsidy(dao_cell, header_deps, witnesses) - witness = witnesses[dao_cell.cell_index] - header_deps_index = CKB::Utils.bin_to_hex(CKB::Utils.hex_to_bin(witness)[-8..-1]).hex - block_hash = header_deps[header_deps_index] - out_point = CKB::Types::OutPoint.new(tx_hash: dao_cell.tx_hash, index: dao_cell.cell_index) - CkbSync::Api.instance.calculate_dao_maximum_withdraw(out_point, block_hash).hex - dao_cell.capacity.to_i + def self.dao_interest(nervos_dao_withdrawing_cell) + nervos_dao_withdrawing_cell_generated_tx = nervos_dao_withdrawing_cell.generated_by + nervos_dao_deposit_cell = nervos_dao_withdrawing_cell_generated_tx.inputs.where(cell_index: nervos_dao_withdrawing_cell.cell_index).first + deposit_out_point = CKB::Types::OutPoint.new(tx_hash: nervos_dao_deposit_cell.tx_hash, index: nervos_dao_deposit_cell.cell_index) + withdrawing_dao_cell_block_hash = nervos_dao_withdrawing_cell.block.block_hash + CkbSync::Api.instance.calculate_dao_maximum_withdraw(deposit_out_point, withdrawing_dao_cell_block_hash).hex - nervos_dao_deposit_cell.capacity.to_i rescue CKB::RPCError 0 end diff --git a/config/initializers/monkey_patches.rb b/config/initializers/monkey_patches.rb new file mode 100644 index 000000000..b25a25f9a --- /dev/null +++ b/config/initializers/monkey_patches.rb @@ -0,0 +1,9 @@ +module CacheRealizer + def realize(key, *args, &block) + fetch(key, *args, &block).tap do |result| + delete(key) if result.nil? + end + end +end + +Rails.cache.extend(CacheRealizer) \ No newline at end of file diff --git a/db/migrate/20191108023656_rename_subsidy_to_interest.rb b/db/migrate/20191108023656_rename_subsidy_to_interest.rb new file mode 100644 index 000000000..4dcd854ae --- /dev/null +++ b/db/migrate/20191108023656_rename_subsidy_to_interest.rb @@ -0,0 +1,6 @@ +class RenameSubsidyToInterest < ActiveRecord::Migration[6.0] + def change + rename_column :addresses, :subsidy, :interest + rename_column :dao_contracts, :subsidy_granted, :interest_granted + end +end diff --git a/db/schema.rb b/db/schema.rb index e160b55f4..d555a699b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_10_09_083202) do +ActiveRecord::Schema.define(version: 2019_11_08_023656) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -34,7 +34,7 @@ t.binary "lock_hash" t.integer "pending_reward_blocks_count", default: 0 t.decimal "dao_deposit", precision: 30, default: "0" - t.decimal "subsidy", precision: 30, default: "0" + t.decimal "interest", precision: 30, default: "0" t.index ["address_hash"], name: "index_addresses_on_address_hash" t.index ["lock_hash"], name: "index_addresses_on_lock_hash", unique: true end @@ -136,7 +136,7 @@ create_table "dao_contracts", force: :cascade do |t| t.decimal "total_deposit", precision: 30, default: "0" - t.decimal "subsidy_granted", precision: 30, default: "0" + t.decimal "interest_granted", precision: 30, default: "0" t.bigint "deposit_transactions_count", default: 0 t.bigint "withdraw_transactions_count", default: 0 t.integer "depositors_count", default: 0 diff --git a/doc/api.raml b/doc/api.raml index 55c9984bc..a6a9f7bab 100644 --- a/doc/api.raml +++ b/doc/api.raml @@ -102,6 +102,12 @@ types: "lock_hash": { "type": script }, + "dao_deposit": { + "type": string + }, + "interest": { + "type": string + }, "pending_reward_blocks_count": { "type": integer } @@ -185,7 +191,7 @@ types: type: integer, required: false }, - "subsidy": { + "interest": { type: integer, required: false }, @@ -883,7 +889,7 @@ types: "generated_tx_hash": "0x3abd21e6e51674bb961bb4c5f3cee9faa5da30e64be10628dc1cef292cbae321", "started_block_number": 10, "ended_block_number": 15, - "subsidy": 100000, + "interest": 100000, "cell_type": "dao", "dao_type_hash": "0xa20df8e80518e9b2eabc1a0efb0ebe1de83f8df9c867edf99d0c5895654fcde1", } @@ -1544,6 +1550,8 @@ types: "args": [], "code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001" }, + "dao_deposit": "10000", + "interest": "100", "pending_reward_blocks_count": 3 } } @@ -1929,7 +1937,9 @@ types: "lock_script": { "args": [], "code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001" - } + }, + "dao_deposit": "10000", + "interest": "100" } } } @@ -2130,7 +2140,7 @@ types: "type": "dao_contract", "attributes": { "total_deposit": 1000000, - "subsidy_granted": 1000, + "interest_granted": 1000, "deposit_transactions_count": 10, "withdraw_transactions_count": 1, "depositors_count": 3, diff --git a/public/api_doc.html b/public/api_doc.html index 733a595b5..8746cb476 100644 --- a/public/api_doc.html +++ b/public/api_doc.html @@ -321,7 +321,7 @@ } ] } -

/transactions

get

Returns a transaction by transaction hash

/transactions

get

Returns a transaction by transaction hash

/address_transactions

get

Returns transactions related to an address

/address_transactions

get

Returns transactions related to an address

/block_transactions

get

Returns transactions in the block

/block_transactions

get

Returns transactions in the block

/addresses

get

Returns address info

/addresses

get

Returns address info

/contract_transactions

get

Returns transactions under the contract

/contract_transactions

get

Returns transactions under the contract

/dao_contract_transactions

get

Returns the transaction related to dao contract by given transaction hash

/dao_contract_transactions

get

Returns the transaction related to dao contract by given transaction hash

/address_dao_transactions

get

Returns transactions under the address by given address hash

/address_dao_transactions

get

Returns transactions under the address by given address hash