Skip to content

Commit

Permalink
chore: Fix amount rendering for balance/amounts
Browse files Browse the repository at this point in the history
This fixes the rendering for Balance, CryptoAmount, and FiatAmount, to
print out the BigDecimals as a float.

They were erroneously printing the `amount` as an integer, which removes
the precision unnecessarily.
  • Loading branch information
alex-stone committed Nov 26, 2024
1 parent cd1391e commit 31bd6f8
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/coinbase/balance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def initialize(amount:, asset:, asset_id: nil)
# Returns a string representation of the Balance.
# @return [String] a string representation of the Balance
def to_s
"Coinbase::Balance{amount: '#{amount.to_i}', asset_id: '#{asset_id}'}"
Coinbase.pretty_print_object(self.class, amount: amount.to_s('F'), asset_id: asset_id)
end

# Same as to_s.
Expand Down
2 changes: 1 addition & 1 deletion lib/coinbase/crypto_amount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def to_atomic_amount
# Returns a string representation of the CryptoAmount.
# @return [String] a string representation of the CryptoAmount
def to_s
Coinbase.pretty_print_object(self.class, amount: amount.to_i, asset_id: asset_id)
Coinbase.pretty_print_object(self.class, amount: amount.to_s('F'), asset_id: asset_id)
end

# Same as to_s.
Expand Down
2 changes: 1 addition & 1 deletion lib/coinbase/fiat_amount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize(amount:, currency:)
# Returns a string representation of the FiatAmount.
# @return [String] a string representation of the FiatAmount
def to_s
Coinbase.pretty_print_object(self.class, amount: amount.to_i, currency: currency)
Coinbase.pretty_print_object(self.class, amount: amount.to_s('F'), currency: currency)
end

# Same as to_s.
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/coinbase/balance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@
describe '#inspect' do
subject(:balance) { described_class.new(amount: amount, asset: asset) }

let(:amount) { BigDecimal('123.0') }
let(:amount) { BigDecimal('123.456') }
let(:asset) { Coinbase::Asset.from_model(eth_asset) }

it 'includes balance details' do
expect(balance.inspect).to include('123', 'eth')
expect(balance.inspect).to include('123.456', 'eth')
end

it 'returns the same value as to_s' do
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/coinbase/crypto_amount_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@
describe '#inspect' do
subject(:crypto_amount) { described_class.new(amount: amount, asset: asset) }

let(:amount) { BigDecimal('123.0') }
let(:amount) { BigDecimal('123.456') }
let(:asset) { Coinbase::Asset.from_model(eth_asset) }

it 'includes crypto_amount details' do
expect(crypto_amount.inspect).to include('123', 'eth')
expect(crypto_amount.inspect).to include('123.456', 'eth')
end

it 'returns the same value as to_s' do
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/coinbase/fiat_amount_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
describe '#inspect' do
subject(:fiat_amount) { described_class.new(amount: amount, currency: currency) }

let(:amount) { BigDecimal('123.0') }
let(:amount) { BigDecimal('123.456') }

it 'includes fiat_amount details' do
expect(fiat_amount.inspect).to include('123', 'usd')
expect(fiat_amount.inspect).to include('123.456', 'usd')
end

it 'returns the same value as to_s' do
Expand Down

0 comments on commit 31bd6f8

Please sign in to comment.