Skip to content

Commit

Permalink
Merge pull request #382 from shaojunda-add-income-to-transaction
Browse files Browse the repository at this point in the history
feat: add income to transaction
  • Loading branch information
shaojunda authored Aug 30, 2019
2 parents 746b3eb + a48a1a8 commit f131bed
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/address_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/ckb_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions app/serializers/ckb_transaction_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions doc/api.raml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ types:
"transaction_fee": {
type: "integer"
},
"is_cellbase": {
type: "boolean"
},
"income": {
type: "string"
},
"version": {
type: "integer"
},
Expand Down Expand Up @@ -767,6 +773,8 @@ types:
"block_timestamp": 1553068833785,
"transaction_fee": 666,
"version": 0,
"is_cellbase": true,
"income": "0.0",
"display_inputs": [
{
"from_cellbase": true,
Expand Down Expand Up @@ -863,6 +871,8 @@ types:
"block_timestamp": 1553068833785,
"transaction_fee": 666,
"version": 0,
"is_cellbase": true,
"income": "0.0",
"display_inputs": [
{
"from_cellbase": true,
Expand Down Expand Up @@ -900,6 +910,8 @@ types:
"block_timestamp": 1553068833785,
"transaction_fee": 666,
"version": 0,
"is_cellbase": true,
"income": "0.0",
"display_inputs": [
{
"from_cellbase": true,
Expand Down Expand Up @@ -1688,6 +1700,8 @@ types:
"block_timestamp": 1553068833785,
"transaction_fee": 666,
"version": 0,
"is_cellbase": true,
"income": "0.0",
"display_inputs": [
{
"from_cellbase": true,
Expand Down
20 changes: 14 additions & 6 deletions public/api_doc.html

Large diffs are not rendered by default.

34 changes: 27 additions & 7 deletions test/controllers/api/v1/address_transactions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion test/controllers/api/v1/block_transactions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion test/controllers/api/v1/ckb_transactions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f131bed

Please sign in to comment.