From ac1677c6fa446899d94561a478802db972fa11c0 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Thu, 29 Aug 2019 12:59:50 +0800 Subject: [PATCH 1/3] test: should return correct income --- .../address_transactions_controller_test.rb | 34 +++++++++++++++---- .../v1/block_transactions_controller_test.rb | 10 +++++- .../v1/ckb_transactions_controller_test.rb | 11 +++++- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/test/controllers/api/v1/address_transactions_controller_test.rb b/test/controllers/api/v1/address_transactions_controller_test.rb index c2359d3bb..cc28bf7fc 100644 --- a/test/controllers/api/v1/address_transactions_controller_test.rb +++ b/test/controllers/api/v1/address_transactions_controller_test.rb @@ -74,7 +74,7 @@ class AddressTransactionsControllerTest < ActionDispatch::IntegrationTest options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: page, page_size: page_size).call - assert_equal CkbTransactionSerializer.new(ckb_transactions, options).serialized_json, response.body + assert_equal CkbTransactionSerializer.new(ckb_transactions, options.merge({ params: { previews: true, address: address } })).serialized_json, response.body end test "should return corresponding ckb transactions with given lock hash" do @@ -87,7 +87,7 @@ class AddressTransactionsControllerTest < ActionDispatch::IntegrationTest options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: page, page_size: page_size).call - assert_equal CkbTransactionSerializer.new(ckb_transactions, options).serialized_json, response.body + assert_equal CkbTransactionSerializer.new(ckb_transactions, options.merge({ params: { previews: true, address: address } })).serialized_json, response.body end test "should contain right keys in the serialized object when call show" do @@ -97,7 +97,27 @@ class AddressTransactionsControllerTest < ActionDispatch::IntegrationTest response_tx_transaction = json["data"].first - assert_equal %w(block_number transaction_hash block_timestamp transaction_fee version display_inputs display_outputs is_cellbase).sort, response_tx_transaction["attributes"].keys.sort + assert_equal %w(block_number transaction_hash block_timestamp transaction_fee version display_inputs display_outputs is_cellbase income).sort, response_tx_transaction["attributes"].keys.sort + end + + test "should return correct income" do + address = create(:address) + + block = create(:block, :with_block_hash) + generated_ckb_transaction = create(:ckb_transaction, block: block, block_timestamp: "1567131126594") + create(:cell_output, capacity: 10**8 * 8, ckb_transaction: generated_ckb_transaction, block: generated_ckb_transaction.block, tx_hash: generated_ckb_transaction.tx_hash, cell_index: 0, generated_by: generated_ckb_transaction, address: address) + consumed_ckb_transaction = create(:ckb_transaction, block: block, block_timestamp: "1567131126595") + + generated_ckb_transaction1 = create(:ckb_transaction, block: block, block_timestamp: "1567131126596") + create(:cell_output, capacity: 10**8 * 8, ckb_transaction: generated_ckb_transaction1, block: generated_ckb_transaction1.block, tx_hash: generated_ckb_transaction1.tx_hash, cell_index: 0, generated_by: generated_ckb_transaction1, address: address) + create(:cell_output, capacity: 10**8 * 6, ckb_transaction: consumed_ckb_transaction, block: consumed_ckb_transaction.block, tx_hash: consumed_ckb_transaction.tx_hash, cell_index: 0, generated_by: generated_ckb_transaction, consumed_by: consumed_ckb_transaction, address: address) + address.ckb_transactions << [generated_ckb_transaction1, consumed_ckb_transaction, generated_ckb_transaction] + + valid_get api_v1_address_transaction_url(address.address_hash) + + expected_incomes = address.ckb_transactions.recent.distinct.map { |transaction| transaction.outputs.sum(:capacity) - transaction.inputs.sum(:capacity) }.map(&:to_i) + + assert_equal expected_incomes, json["data"].map { |transaction| transaction["attributes"]["income"].to_i } end test "should return error object when no records found by id" do @@ -158,7 +178,7 @@ class AddressTransactionsControllerTest < ActionDispatch::IntegrationTest valid_get api_v1_address_transaction_url(address.address_hash), params: { page: page } options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: address_ckb_transactions, page: page, page_size: page_size).call - response_transaction = CkbTransactionSerializer.new(address_ckb_transactions, options).serialized_json + response_transaction = CkbTransactionSerializer.new(address_ckb_transactions, options.merge({ params: { previews: true, address: address } })).serialized_json assert_equal response_transaction, response.body assert_equal page_size, json["data"].size @@ -173,7 +193,7 @@ class AddressTransactionsControllerTest < ActionDispatch::IntegrationTest valid_get api_v1_address_transaction_url(address.address_hash), params: { page_size: page_size } options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: address_ckb_transactions, page: page, page_size: page_size).call - response_transaction = CkbTransactionSerializer.new(address_ckb_transactions, options).serialized_json + response_transaction = CkbTransactionSerializer.new(address_ckb_transactions, options.merge({ params: { previews: true, address: address } })).serialized_json assert_equal response_transaction, response.body assert_equal page_size, json["data"].size @@ -187,7 +207,7 @@ class AddressTransactionsControllerTest < ActionDispatch::IntegrationTest valid_get api_v1_address_transaction_url(address.address_hash), params: { page: page, page_size: page_size } options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: address_ckb_transactions, page: page, page_size: page_size).call - response_transaction = CkbTransactionSerializer.new(address_ckb_transactions, options).serialized_json + response_transaction = CkbTransactionSerializer.new(address_ckb_transactions, options.merge({ params: { previews: true, address: address } })).serialized_json assert_equal response_transaction, response.body end @@ -201,7 +221,7 @@ class AddressTransactionsControllerTest < ActionDispatch::IntegrationTest valid_get api_v1_address_transaction_url(address.address_hash), params: { page: page, page_size: page_size } options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: address_ckb_transactions, page: page, page_size: page_size).call - response_transaction = CkbTransactionSerializer.new(address_ckb_transactions, options).serialized_json + response_transaction = CkbTransactionSerializer.new(address_ckb_transactions, options.merge({ params: { previews: true, address: address } })).serialized_json assert_equal [], json["data"] assert_equal response_transaction, response.body diff --git a/test/controllers/api/v1/block_transactions_controller_test.rb b/test/controllers/api/v1/block_transactions_controller_test.rb index e4264d216..b87802b69 100644 --- a/test/controllers/api/v1/block_transactions_controller_test.rb +++ b/test/controllers/api/v1/block_transactions_controller_test.rb @@ -92,7 +92,15 @@ class BlockTransactionsControllerTest < ActionDispatch::IntegrationTest response_tx_transaction = json["data"].first - assert_equal %w(block_number transaction_hash block_timestamp transaction_fee version display_inputs display_outputs is_cellbase).sort, response_tx_transaction["attributes"].keys.sort + assert_equal %w(block_number transaction_hash block_timestamp transaction_fee version display_inputs display_outputs is_cellbase income).sort, response_tx_transaction["attributes"].keys.sort + end + + test "returned income should be null" do + block = create(:block, :with_ckb_transactions) + + valid_get api_v1_block_transaction_url(block.block_hash) + + assert_nil json["data"].map { |transaction| transaction.dig("attributes", "income") }.uniq.first end test "should return error object when no records found by id" do diff --git a/test/controllers/api/v1/ckb_transactions_controller_test.rb b/test/controllers/api/v1/ckb_transactions_controller_test.rb index 98cb89868..611c3a736 100644 --- a/test/controllers/api/v1/ckb_transactions_controller_test.rb +++ b/test/controllers/api/v1/ckb_transactions_controller_test.rb @@ -97,7 +97,16 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest valid_get api_v1_ckb_transaction_url(ckb_transaction.tx_hash) response_tx_transaction = json["data"] - assert_equal %w(block_number transaction_hash block_timestamp transaction_fee version display_inputs display_outputs is_cellbase).sort, response_tx_transaction["attributes"].keys.sort + assert_equal %w(block_number transaction_hash block_timestamp transaction_fee version display_inputs display_outputs is_cellbase income).sort, response_tx_transaction["attributes"].keys.sort + end + + test "returned income should be null" do + prepare_node_data(8) + ckb_transaction = CkbTransaction.last + + valid_get api_v1_ckb_transaction_url(ckb_transaction.tx_hash) + + assert_nil json["data"].dig("attributes", "income") end test "should return all display_inputs" do From 1e6b3f32aa02ab261577d2cee8d5f2298289ed40 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Thu, 29 Aug 2019 13:00:35 +0800 Subject: [PATCH 2/3] feat: implement income --- app/controllers/api/v1/address_transactions_controller.rb | 2 +- app/models/ckb_transaction.rb | 4 ++++ app/serializers/ckb_transaction_serializer.rb | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/address_transactions_controller.rb b/app/controllers/api/v1/address_transactions_controller.rb index 9491eb498..f98dd646b 100644 --- a/app/controllers/api/v1/address_transactions_controller.rb +++ b/app/controllers/api/v1/address_transactions_controller.rb @@ -10,7 +10,7 @@ def show ckb_transactions = presented_address.ckb_transactions.recent.distinct.page(@page).per(@page_size) options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call - render json: CkbTransactionSerializer.new(ckb_transactions, options.merge({ params: { previews: true } })) + render json: CkbTransactionSerializer.new(ckb_transactions, options.merge({ params: { previews: true, address: address } })) end private diff --git a/app/models/ckb_transaction.rb b/app/models/ckb_transaction.rb index c0f683177..c85ceb963 100644 --- a/app/models/ckb_transaction.rb +++ b/app/models/ckb_transaction.rb @@ -45,6 +45,10 @@ def display_outputs(previews: false) end end + def income(address) + outputs.where(address: address).sum(:capacity) - inputs.where(address: address).sum(:capacity) + end + private def normal_tx_display_outputs(previews) diff --git a/app/serializers/ckb_transaction_serializer.rb b/app/serializers/ckb_transaction_serializer.rb index 5ca002463..7e6f6fe26 100644 --- a/app/serializers/ckb_transaction_serializer.rb +++ b/app/serializers/ckb_transaction_serializer.rb @@ -12,4 +12,8 @@ class CkbTransactionSerializer attribute :display_outputs do |object, params| params && params[:previews] ? object.display_outputs(previews: true) : object.display_outputs end + + attribute :income do |object, params| + params && params[:previews] && params[:address] ? object.income(params[:address]) : nil + end end From a48a1a8d5164db920feec95e9de4c93289acf03d Mon Sep 17 00:00:00 2001 From: shaojunda Date: Fri, 30 Aug 2019 10:59:25 +0800 Subject: [PATCH 3/3] feat: add income to api doc --- doc/api.raml | 14 ++++++++++++++ public/api_doc.html | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/doc/api.raml b/doc/api.raml index 434144ac5..8a1fa60d4 100644 --- a/doc/api.raml +++ b/doc/api.raml @@ -51,6 +51,12 @@ types: "transaction_fee": { type: "integer" }, + "is_cellbase": { + type: "boolean" + }, + "income": { + type: "string" + }, "version": { type: "integer" }, @@ -767,6 +773,8 @@ types: "block_timestamp": 1553068833785, "transaction_fee": 666, "version": 0, + "is_cellbase": true, + "income": "0.0", "display_inputs": [ { "from_cellbase": true, @@ -863,6 +871,8 @@ types: "block_timestamp": 1553068833785, "transaction_fee": 666, "version": 0, + "is_cellbase": true, + "income": "0.0", "display_inputs": [ { "from_cellbase": true, @@ -900,6 +910,8 @@ types: "block_timestamp": 1553068833785, "transaction_fee": 666, "version": 0, + "is_cellbase": true, + "income": "0.0", "display_inputs": [ { "from_cellbase": true, @@ -1688,6 +1700,8 @@ types: "block_timestamp": 1553068833785, "transaction_fee": 666, "version": 0, + "is_cellbase": true, + "income": "0.0", "display_inputs": [ { "from_cellbase": true, diff --git a/public/api_doc.html b/public/api_doc.html index 95a2e5f59..3dec3b4ff 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