diff --git a/app/models/market_data.rb b/app/models/market_data.rb index 5d71baf9e..3e4e5cfb2 100644 --- a/app/models/market_data.rb +++ b/app/models/market_data.rb @@ -87,7 +87,7 @@ def parsed_dao def total_supply result = if current_timestamp > first_released_timestamp_may - parsed_dao.c_i - BURN_QUOTA - yesterday_treasury_amount.to_i + parsed_dao.c_i - BURN_QUOTA - (parsed_dao.s_i - unmade_dao_interests) else parsed_dao.c_i - BURN_QUOTA end @@ -95,6 +95,19 @@ def total_supply unit == "ckb" ? (result / 10**8).truncate(8) : result end + def unmade_dao_interests + @unmade_dao_interests ||= + begin + tip_dao = tip_block.dao + total = 0 + CellOutput.nervos_dao_deposit. + generated_before(tip_block.timestamp).unconsumed_at(tip_block.timestamp).find_each do |cell_output| + total += DaoCompensationCalculator.new(cell_output, tip_dao).call + end + total + end + end + def circulating_supply result = parsed_dao.c_i - parsed_dao.s_i - BURN_QUOTA - ecosystem_locked - team_locked - private_sale_locked - founding_partners_locked - foundation_reserve_locked - bug_bounty_locked @@ -154,9 +167,4 @@ def third_released_timestamp_other lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : CkbUtils.time_in_milliseconds(Time.find_zone("UTC").parse("2022-12-31")) end end - - def yesterday_treasury_amount - treasury_amounts = DailyStatistic.order(created_at_unixtimestamp: :desc).first(2).pluck(:treasury_amount) - treasury_amounts[0] == "0" ? treasury_amounts[1] : treasury_amounts[0] - end end diff --git a/test/controllers/api/v1/market_data_controller_test.rb b/test/controllers/api/v1/market_data_controller_test.rb index f4c353516..dffe36288 100644 --- a/test/controllers/api/v1/market_data_controller_test.rb +++ b/test/controllers/api/v1/market_data_controller_test.rb @@ -5,17 +5,23 @@ module V1 class MarketDataControllerTest < ActionDispatch::IntegrationTest setup do first_released_timestamp_addr = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn323t90gna20lusyshreg32qee4fhkt9jj2t6qrqzzqxzq8yqt8kmd9") - create(:lock_script, address: first_released_timestamp_addr, args: "0xacaf44faa7ff90242e3ca22a067354decb2ca4a5e803008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: first_released_timestamp_addr, args: "0xacaf44faa7ff90242e3ca22a067354decb2ca4a5e803008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") first_released_timestamp_other = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32s3y29vjv73cfm8qax220dwwmpdccl4upy4s9qzzqxzq8yqyd09am") - create(:lock_script, address: first_released_timestamp_other, args: "0x448a2b24cf4709d9c1d3294f6b9db0b718fd78125605008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: first_released_timestamp_other, args: "0x448a2b24cf4709d9c1d3294f6b9db0b718fd78125605008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") second_released_timestamp_other = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sn23uga5m8u5v87q98vr29qa8tl0ruu84gqfqzzqxzq8yqc2dxk6") - create(:lock_script, address: second_released_timestamp_other, args: "0x4d51e23b4d9f9461fc029d835141d3afef1f387aa009008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: second_released_timestamp_other, args: "0x4d51e23b4d9f9461fc029d835141d3afef1f387aa009008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") third_released_timestamp_other = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sdufwedw7a0w9dkvhpsah4mdk2gkfq63e0q6qzzqxzq8yqnqq85p") - create(:lock_script, address: third_released_timestamp_other, args: "0x3789765aef75ee2b6ccb861dbd76db291648351cbc1a008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: third_released_timestamp_other, args: "0x3789765aef75ee2b6ccb861dbd76db291648351cbc1a008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") second_released_timestamp_may = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn323crn7nscet5sfwxjkzhexymfa4zntzt8vasvqzzqxzq8yq92pgkg") - create(:lock_script, address: second_released_timestamp_may, args: "0xe073f4e18cae904b8d2b0af931369ed453589676760c008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: second_released_timestamp_may, args: "0xe073f4e18cae904b8d2b0af931369ed453589676760c008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") third_released_timestamp_may = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sl0qgva2l78fcnjt6x8kr8sln4lqs4twcpq4qzzqxzq8yq7hpadu") - create(:lock_script, address: third_released_timestamp_may, args: "0x7de0433aaff8e9c4e4bd18f619e1f9d7e08556ec0415008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: third_released_timestamp_may, args: "0x7de0433aaff8e9c4e4bd18f619e1f9d7e08556ec0415008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") CkbSync::Api.any_instance.stubs(:get_tip_header).returns( CKB::Types::BlockHeader.new( compact_target: "0x1a33cadd", @@ -29,8 +35,8 @@ class MarketDataControllerTest < ActionDispatch::IntegrationTest extra_hash: "0x0000000000000000000000000000000000000000000000000000000000000000", version: "0x0", epoch: "0x70803b9000045", - dao: "0xeeaf2fe1baa6df2e577fda67799223009ca127a6d1e30c00002dc77aa42b0007" - ) + dao: "0xeeaf2fe1baa6df2e577fda67799223009ca127a6d1e30c00002dc77aa42b0007", + ), ) create(:block, :with_block_hash, epoch: 69, timestamp: 1575090866093, dao: "0xeeaf2fe1baa6df2e577fda67799223009ca127a6d1e30c00002dc77aa42b0007") create(:daily_statistic, treasury_amount: "45507635189304330.674891957030103511696912093394364431189654516859837775", created_at_unixtimestamp: Time.current.yesterday.beginning_of_day.to_i) @@ -56,11 +62,10 @@ class MarketDataControllerTest < ActionDispatch::IntegrationTest test "should return total supply that sub treasury amount when current timestamp after first release timestamp" do MarketData.any_instance.stubs(:current_timestamp).returns(CkbUtils.time_in_milliseconds(Time.find_zone("UTC").parse("2020-06-03"))) - treasury_amount = DailyStatistic.find_by(created_at_unixtimestamp: Time.current.yesterday.beginning_of_day.to_i).treasury_amount.to_i valid_get api_v1_market_datum_url("total_supply") latest_dao = Block.recent.pick(:dao) parsed_dao = CkbUtils.parse_dao(latest_dao) - result = parsed_dao.c_i - (336 * 10**16 * 0.25).to_d - treasury_amount + result = parsed_dao.c_i - (336 * 10**16 * 0.25).to_d - parsed_dao.s_i expected_total_supply = (result / 10**8).to_s assert_equal expected_total_supply, json diff --git a/test/models/market_data_test.rb b/test/models/market_data_test.rb index 89d77898f..f73c01f6c 100644 --- a/test/models/market_data_test.rb +++ b/test/models/market_data_test.rb @@ -3,17 +3,23 @@ class MarketDataTest < ActiveSupport::TestCase setup do first_released_timestamp_addr = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn323t90gna20lusyshreg32qee4fhkt9jj2t6qrqzzqxzq8yqt8kmd9") - create(:lock_script, address: first_released_timestamp_addr, args: "0xacaf44faa7ff90242e3ca22a067354decb2ca4a5e803008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: first_released_timestamp_addr, args: "0xacaf44faa7ff90242e3ca22a067354decb2ca4a5e803008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") first_released_timestamp_other = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32s3y29vjv73cfm8qax220dwwmpdccl4upy4s9qzzqxzq8yqyd09am") - create(:lock_script, address: first_released_timestamp_other, args: "0x448a2b24cf4709d9c1d3294f6b9db0b718fd78125605008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: first_released_timestamp_other, args: "0x448a2b24cf4709d9c1d3294f6b9db0b718fd78125605008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") second_released_timestamp_other = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sn23uga5m8u5v87q98vr29qa8tl0ruu84gqfqzzqxzq8yqc2dxk6") - create(:lock_script, address: second_released_timestamp_other, args: "0x4d51e23b4d9f9461fc029d835141d3afef1f387aa009008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: second_released_timestamp_other, args: "0x4d51e23b4d9f9461fc029d835141d3afef1f387aa009008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") third_released_timestamp_other = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sdufwedw7a0w9dkvhpsah4mdk2gkfq63e0q6qzzqxzq8yqnqq85p") - create(:lock_script, address: third_released_timestamp_other, args: "0x3789765aef75ee2b6ccb861dbd76db291648351cbc1a008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: third_released_timestamp_other, args: "0x3789765aef75ee2b6ccb861dbd76db291648351cbc1a008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") second_released_timestamp_may = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn323crn7nscet5sfwxjkzhexymfa4zntzt8vasvqzzqxzq8yq92pgkg") - create(:lock_script, address: second_released_timestamp_may, args: "0xe073f4e18cae904b8d2b0af931369ed453589676760c008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: second_released_timestamp_may, args: "0xe073f4e18cae904b8d2b0af931369ed453589676760c008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") third_released_timestamp_may = create(:address, address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sl0qgva2l78fcnjt6x8kr8sln4lqs4twcpq4qzzqxzq8yq7hpadu") - create(:lock_script, address: third_released_timestamp_may, args: "0x7de0433aaff8e9c4e4bd18f619e1f9d7e08556ec0415008403080720", code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") + create(:lock_script, address: third_released_timestamp_may, args: "0x7de0433aaff8e9c4e4bd18f619e1f9d7e08556ec0415008403080720", + code_hash: "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8") CkbSync::Api.any_instance.stubs(:get_tip_header).returns( CKB::Types::BlockHeader.new( compact_target: "0x1a33cadd", @@ -27,8 +33,8 @@ class MarketDataTest < ActiveSupport::TestCase extra_hash: "0x0000000000000000000000000000000000000000000000000000000000000000", version: "0x0", epoch: "0x70803b9000045", - dao: "0xeeaf2fe1baa6df2e577fda67799223009ca127a6d1e30c00002dc77aa42b0007" - ) + dao: "0xeeaf2fe1baa6df2e577fda67799223009ca127a6d1e30c00002dc77aa42b0007", + ), ) create(:block, :with_block_hash, epoch: 69, timestamp: 1575090866093, dao: "0xeeaf2fe1baa6df2e577fda67799223009ca127a6d1e30c00002dc77aa42b0007") end @@ -44,11 +50,10 @@ class MarketDataTest < ActiveSupport::TestCase end test "total_supply should sub treasury amount when current timestamp after first release timestamp" do - daily_statistic = create(:daily_statistic, treasury_amount: "45507635189304330.674891957030103511696912093394364431189654516859837775", created_at_unixtimestamp: Time.current.yesterday.beginning_of_day.to_i) MarketData.any_instance.stubs(:current_timestamp).returns(CkbUtils.time_in_milliseconds(Time.find_zone("UTC").parse("2020-06-03"))) latest_dao = Block.recent.pick(:dao) parsed_dao = CkbUtils.parse_dao(latest_dao) - result = parsed_dao.c_i - (336 * 10**16 * 0.25).to_d - daily_statistic.treasury_amount.to_i + result = parsed_dao.c_i - (336 * 10**16 * 0.25).to_d - parsed_dao.s_i expected_circulating_supply = result / 10**8 assert_equal expected_circulating_supply, MarketData.new.send(:total_supply)