Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for non-checksummed address #236

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed
- Fixed a bug where non-checksummed asset IDs were throwing an error.

## [0.14.0] - 2025-01-14

### Added
Expand Down
21 changes: 13 additions & 8 deletions lib/coinbase/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ def from_model(asset_model, asset_id: nil)

# Handle the non-primary denomination case at the asset level.
# TODO: Push this logic down to the backend.
if asset_id && Coinbase.to_sym(asset_id) != Coinbase.to_sym(asset_model.asset_id)
case asset_id
when :gwei
decimals = GWEI_DECIMALS
when :wei
decimals = 0
else
raise ArgumentError, "Unsupported asset ID: #{asset_id}"
if asset_id && asset_model.asset_id
normalized_asset_id = asset_id.downcase
normalized_asset_model_id = asset_model.asset_id.downcase

if Coinbase.to_sym(normalized_asset_id) != Coinbase.to_sym(normalized_asset_model_id)
case normalized_asset_id
when :gwei
decimals = GWEI_DECIMALS
when :wei
decimals = 0
else
raise ArgumentError, "Unsupported asset ID: #{asset_id}"
end
end
end

Expand Down
21 changes: 21 additions & 0 deletions spec/unit/coinbase/asset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@
expect(asset.address_id).to eq(contract_address)
end
end

context 'when the asset_id is non-checksummed' do
subject(:asset) { described_class.from_model(asset_model, asset_id: lowercase_address) }

let(:checksummed_address) { '0x8309fbdF021eDF768DC13195741940ba544dEa98' }
let(:lowercase_address) { checksummed_address.downcase }
let(:asset_model) { build(:asset_model, asset_id: checksummed_address) }

it 'handles case-insensitive address matching' do
expect(asset.asset_id).to eq(lowercase_address)
end
end

context 'when the asset_id is checksummed' do
let(:contract_address) { '0x8309fbdF021eDF768DC13195741940ba544dEa98' }
let(:asset_model) { build(:asset_model, network_id, :usdc, contract_address: contract_address) }

it 'sets the address_id as the checksummed address' do
expect(asset.address_id).to eq(contract_address)
end
end
end

describe '.primary_denomination' do
Expand Down
Loading