Skip to content

Commit

Permalink
Remove unneccessary code in TxBuilder#collect_colored_outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Nov 29, 2023
1 parent d01e3fc commit aff6091
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 3 additions & 6 deletions lib/glueby/contract/tx_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def create_multi_transfer_tx(color_id:, sender:, receivers:, fee_estimator: FeeE

amount = receivers.reduce(0) { |sum, r| sum + r[:amount].to_i }
utxos = sender.internal_wallet.list_unspent(color_id, only_finalized)
sum_token, outputs = collect_colored_outputs(utxos, color_id, amount)
sum_token, outputs = collect_colored_outputs(utxos, amount)
fill_input(tx, outputs)

receivers.each do |r|
Expand Down Expand Up @@ -248,7 +248,7 @@ def create_burn_tx(color_id:, sender:, amount: 0, fee_estimator: FeeEstimator::F
tx = Tapyrus::Tx.new

utxos = sender.internal_wallet.list_unspent(color_id, only_finalized)
sum_token, outputs = collect_colored_outputs(utxos, color_id, amount)
sum_token, outputs = collect_colored_outputs(utxos, amount)
fill_input(tx, outputs)

fill_change_token(tx, sender, sum_token - amount, color_id) if amount.positive?
Expand Down Expand Up @@ -319,12 +319,9 @@ def fill_change_token(tx, wallet, change, color_id)
# Returns the set of utxos that satisfies the specified amount and has the specified color_id.
# if amount is not specified or 0, return all utxos with color_id
# @param results [Array] response of Glueby::Internal::Wallet#list_unspent
# @param color_id [Tapyrus::Color::ColorIdentifier] color identifier
# @param amount [Integer]
def collect_colored_outputs(results, color_id, amount = 0)
def collect_colored_outputs(results, amount = 0)
results = results.inject([0, []]) do |sum, output|
next sum unless output[:color_id] == color_id.to_hex

new_sum = sum[0] + output[:amount]
new_outputs = sum[1] << output
return [new_sum, new_outputs] if new_sum >= amount && amount.positive?
Expand Down
24 changes: 18 additions & 6 deletions spec/glueby/contract/tx_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,16 @@ class TxBuilderMock
let(:receiver_address) { wallet.internal_wallet.receive_address }
let(:script_pubkey) { Tapyrus::Script.parse_from_addr(receiver_address).add_color(color_id).to_hex }
let(:amount) { 100_001 }


before do
allow(internal_wallet).to receive(:list_unspent).with(color_id, true)
.and_return(unspents.select { |utxo| utxo[:color_id] == 'c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3' })
end
it { expect(subject.inputs.size).to eq 3 }
it { expect(subject.inputs[0].out_point.txid).to eq '100c4dc65ea4af8abb9e345b3d4cdcc548bb5e1cdb1cb3042c840e147da72fa2' }
it { expect(subject.inputs[0].out_point.index).to eq 0 }
it { expect(subject.inputs[1].out_point.txid).to eq 'a3f20bc94c8d77c35ba1770116d2b34375475a4194d15f76442636e9f77d50d9' }
it { expect(subject.inputs[1].out_point.index).to eq 2 }
it { expect(subject.inputs[2].out_point.txid).to eq '5c3d79041ff4974282b8ab72517d2ef15d8b6273cb80a01077145afb3d5e7cc5' }
it { expect(subject.inputs[2].out_point.index).to eq 0 }
it { expect(subject.outputs.size).to eq 3 }
it { expect(subject.outputs[0].value).to eq 100_001 }
it { expect(subject.outputs[0].colored?).to be_truthy }
Expand Down Expand Up @@ -423,6 +425,11 @@ class TxBuilderMock
]
end

before do
allow(internal_wallet).to receive(:list_unspent).with(color_id, true)
.and_return(unspents.select { |utxo| utxo[:color_id] == 'c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3' })
end

it { expect(subject.inputs.size).to eq 3 }
it { expect(subject.inputs[0].out_point.txid).to eq '100c4dc65ea4af8abb9e345b3d4cdcc548bb5e1cdb1cb3042c840e147da72fa2' }
it { expect(subject.inputs[0].out_point.index).to eq 0 }
Expand Down Expand Up @@ -479,6 +486,12 @@ class TxBuilderMock
let(:amount) { 50_000 }
let(:fee_estimator) { Glueby::Contract::FeeEstimator::Fixed.new }


before do
allow(internal_wallet).to receive(:list_unspent).with(color_id, true)
.and_return(unspents.select { |utxo| utxo[:color_id] == 'c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3' })
end

it { expect(subject.inputs.size).to eq 2 }
it { expect(subject.inputs[0].out_point.txid).to eq '100c4dc65ea4af8abb9e345b3d4cdcc548bb5e1cdb1cb3042c840e147da72fa2' }
it { expect(subject.inputs[0].out_point.index).to eq 0 }
Expand Down Expand Up @@ -641,11 +654,10 @@ class TxBuilderMock
end

describe '#collect_colored_outputs' do
subject { mock.collect_colored_outputs(results, color_id, amount) }
subject { mock.collect_colored_outputs(results, amount) }

let(:results) { unspents }
let(:results) { unspents.select { |utxo| utxo[:color_id] == 'c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3' } }
let(:amount) { 50_000 }
let(:color_id) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3'.htb) }

it { expect(subject[0]).to eq 100_000 }
it { expect(subject[1].size).to eq 1 }
Expand Down

0 comments on commit aff6091

Please sign in to comment.