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

Refactor method names and where asset, balance, and import logic lives #41

Merged
merged 12 commits into from May 13, 2024

Conversation

alex-stone
Copy link
Contributor

@alex-stone alex-stone commented May 10, 2024

What changed? Why?

This renames some methods to be more idiomatic, e.g. address.list_balances => address.balances.

This also moves the responsibility of some logic into the class that best represents the domain of that behavior.

  • Wallet Import
    • user.import_wallet => Coinbase::Wallet.import (the helper still exists, but logic belongs to Wallet).
  • Balance Map
    • Coinbase.to_balance_map => Coinbase::BalanceMap.from_balances
  • Asset denomination handling
    • Logic existed at the wallet and address layer now belongs to Asset.
  • Coinbase::Balance handles parsing client balances and converting them to the write asset using the asset class methods.
  • address.list_transfer_ids => address.transfers.
  • Change ID methods to not stutter
    • user.user_id => user.id
    • wallet.wallet_id => wallet.id
    • address.address_id => address.id

TODO:

  • Move asset and amount manipulation to the backend. Supporting gwei and wei and taking an argument to indicate if we're passing whole or atomic units will allow us to not have to do any of this logic client side.
  • Move wallet creation logic out of Wallet.new so we can leverage wallets in a state where we do not need a seed to be imported.
    • This enables us to add user.wallets or Coinbase::Wallet.list that returns a set of wallets

Qualified Impact

This contains some breaking changes from v0.0.3.

This migrates the get_balance* calls to balance* to be more inline
with standard ruby method naming, i.e. the method name should reflect
what is being returned v.s. what the method is doing.
This removes the reference to the unused FaucetLimitReached error
and updates docs to refer to the correct FaucetLimitReachedError
This enables you to import a wallet with the export data directly.
This migrates the balance map parsing off of the top-level Coinbase
class and onto the Coinbase::BalanceMap class as a class method.

This also exposes a `Coinbase::Balance` object for simple parsing
of Coinbase::Client::Balances.
Comment on lines -68 to -73
when :eth
amount / BigDecimal(Coinbase::WEI_PER_ETHER.to_s)
when :gwei
amount / BigDecimal(Coinbase::GWEI_PER_ETHER.to_s)
when :usdc
amount / BigDecimal(Coinbase::ATOMIC_UNITS_PER_USDC.to_s)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not handling weth properly here. Moving this logic to Coinbase::Asset will allow us to more easily support more assets until we push this to the backend.

@@ -84,54 +117,31 @@ def default_address
# Returns the Address with the given ID.
# @param address_id [String] The ID of the Address to retrieve
# @return [Address] The Address
def get_address(address_id)
def address(address_id)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one feels a bit weird to me since we're already exposing addresses.

In what flow will someone have an on-chain address string that is associated with a wallet, where they already have the wallet in memory, and do not have a reference to the address object?

I suspect it might be more likely that they have an address without a wallet and want to be able to search for it and return it if the user/project owns the wallet and address?

# frozen_string_literal: true

module Coinbase
# A representation of an Balance.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of a Balance or of an Asset Balance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference in your opinion there? We can clarify it is an amount of a specific asset?

This makes it so that our fetches of the resource ID doesn't stutter
with the class name.

```ruby
wallet = Wallet.import(...)
puts wallet.id
```
This consolidates the transforms around assets and balances to
their respective classes, and moves them out of wallet and address.

This also adds testing and fixes the handling for fetching balances
of `weth` using `get_address_balance` and `get_wallet_balance` which
was not handled properly.

The `list_address_balances` and `list_wallet_balances` handling properly
handled `weth`.
This makes it so that we return address.transfers instead of
address.list_transfer_ids.
@@ -35,45 +35,32 @@ def wallet_id

# Returns the Address ID.
# @return [String] The Address ID
def address_id
def id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we do the same for transfer_id?

@@ -15,7 +15,7 @@ def initialize(model)

# Returns the User ID.
# @return [String] the User ID
def user_id
def id
@model.id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also update reference in to_s

@@ -163,7 +163,7 @@ The below code demonstrates how to re-instantiate a Wallet from the data export.
```ruby
# The Wallet can be re-instantiated using the exported data.
# w4 will be equivalent to w3.
w4 = u.import_wallet(data)
w4 = Coinbase::Wallet.import(data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update line 174 to w5 = wallets[w3.id]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the README / example something that is actually evaluated?

This updates all model's fields and adds specs that test the
inspect function to ensure we're evaluating those functions in
test.
@alex-stone alex-stone requested a review from jazz-cb May 13, 2024 13:04
@yuga-cb yuga-cb merged commit 3f8661a into v0.0.4 May 13, 2024
4 checks passed
@yuga-cb yuga-cb deleted the stone/refactor branch May 13, 2024 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants