Skip to content

chore: remove duplicate imports. #3306

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

Merged
merged 1 commit into from
May 6, 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
24 changes: 11 additions & 13 deletions backend/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ import (
"time"

"github.com/BitBoxSwiss/bitbox-wallet-app/backend/accounts"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/accounts/types"
accountsTypes "github.com/BitBoxSwiss/bitbox-wallet-app/backend/accounts/types"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/bitsurance"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
coinpkg "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think in case of dups, you should keep the named one and remove the unnamed one. The reason for naming these imports is to avoid clashes or generic names. func foo(coin coin.Coin) is no good 😅

Not sure what the idiomatic way is, as I assume renaming imports is not very idiomatic. I am afraid it might be one letter var name like func foo(c coin.Coin), but I hate short names with a passion.

Copy link
Collaborator Author

@bznein bznein Apr 24, 2025

Choose a reason for hiding this comment

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

I think in case of dups, you should keep the named one and remove the unnamed one. The reason for naming these imports is to avoid clashes or generic names. func foo(coin coin.Coin) is no good 😅
Not sure what the idiomatic way is, as I assume renaming imports is not very idiomatic.

Exactly, according to this: https://google.github.io/styleguide/go/decisions#import-renaming imports shouldn't be renamed unless there are good reasons

The reason for naming these imports is to avoid clashes or generic names. func foo(coin coin.Coin) is no good 😅

I don't think this is solved by renaming the package import:
func foo (coin coinpkg.Coin) doesn't look much better imho. In general having a type Coin in a package coin will inevitably leads us to situations like these.

Copy link
Contributor

Choose a reason for hiding this comment

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

func foo (coin coinpkg.Coin) doesn't look much better imho

I should have been more specific. It's not the optics, it's about the name shadowing. Inside the foo function you can't use the coin package anymore b/c it's shadowed by the coin argument. It's a bit silly that vars and packages live in the same namespace in Go, but since it is, we should not shadow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd personally argue that it is better to rename the argument rather than the package (smaller scope, and it's easier to find a more meaningful name for an arg rather than a pkg) but I will not fight over it :D I'll make the changes!

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not a fight 😄 I think your point makes sense and I'm okay with it too.

"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/config"
Expand Down Expand Up @@ -253,7 +251,7 @@ func (backend *Backend) accountFiatBalance(account accounts.Interface, fiat stri
return nil, err
}

coinDecimals := coin.DecimalsExp(account.Coin())
coinDecimals := coinpkg.DecimalsExp(account.Coin())
price, err := backend.RatesUpdater().LatestPriceForPair(account.Coin().Unit(false), fiat)
if err != nil {
return nil, err
Expand Down Expand Up @@ -357,9 +355,9 @@ func (backend *Backend) LookupInsuredAccounts(accountCode accountsTypes.Code) ([
if !ok {
frontendConfig = make(map[string]interface{})
}
canceledAccounts, ok := frontendConfig["bitsuranceNotifyCancellation"].([]types.Code)
canceledAccounts, ok := frontendConfig["bitsuranceNotifyCancellation"].([]accountsTypes.Code)
if !ok {
frontendConfig["bitsuranceNotifyCancellation"] = []types.Code{bitsuranceAccount.AccountCode}
frontendConfig["bitsuranceNotifyCancellation"] = []accountsTypes.Code{bitsuranceAccount.AccountCode}
} else {
canceledAccounts = append(canceledAccounts, bitsuranceAccount.AccountCode)
frontendConfig["bitsuranceNotifyCancellation"] = canceledAccounts
Expand Down Expand Up @@ -413,12 +411,12 @@ func (backend *Backend) createAndPersistAccountConfig(
if err != nil {
return "", err
}
coin, err := backend.Coin(coinCode)
accountCoin, err := backend.Coin(coinCode)
if err != nil {
return "", err
}
if name == "" {
name = defaultAccountName(coin, accountNumber)
name = defaultAccountName(accountCoin, accountNumber)
}

// v0 prefix: in case this code turns out to be not unique in the future, we can switch to 'v1-'
Expand All @@ -438,7 +436,7 @@ func (backend *Backend) createAndPersistAccountConfig(
if coinCode == coinpkg.CodeBTC {
bip44Coin = hardenedKeystart
}
return accountCode, backend.persistBTCAccountConfig(keystore, coin,
return accountCode, backend.persistBTCAccountConfig(keystore, accountCoin,
accountCode,
hiddenBecauseUnused,
name,
Expand All @@ -455,7 +453,7 @@ func (backend *Backend) createAndPersistAccountConfig(
if coinCode == coinpkg.CodeLTC {
bip44Coin = 2 + hardenedKeystart
}
return accountCode, backend.persistBTCAccountConfig(keystore, coin,
return accountCode, backend.persistBTCAccountConfig(keystore, accountCoin,
accountCode,
hiddenBecauseUnused,
name,
Expand All @@ -471,7 +469,7 @@ func (backend *Backend) createAndPersistAccountConfig(
bip44Coin = "60'"
}
return accountCode, backend.persistETHAccountConfig(
keystore, coin, accountCode, hiddenBecauseUnused,
keystore, accountCoin, accountCode, hiddenBecauseUnused,
// TODO: Use []uint32 instead of a string keypath
fmt.Sprintf("m/44'/%s/0'/0/%d", bip44Coin, accountNumber),
name,
Expand Down Expand Up @@ -1254,11 +1252,11 @@ func (backend *Backend) maybeAddP2TR(keystore keystore.Keystore, accounts []*con
if account.CoinCode == coinpkg.CodeBTC ||
account.CoinCode == coinpkg.CodeTBTC ||
account.CoinCode == coinpkg.CodeRBTC {
coin, err := backend.Coin(account.CoinCode)
accountCoin, err := backend.Coin(account.CoinCode)
if err != nil {
return err
}
if keystore.SupportsAccount(coin, signing.ScriptTypeP2TR) &&
if keystore.SupportsAccount(accountCoin, signing.ScriptTypeP2TR) &&
account.SigningConfigurations.FindScriptType(signing.ScriptTypeP2TR) == -1 {
rootFingerprint, err := backend.keystore.RootFingerprint()
if err != nil {
Expand All @@ -1276,7 +1274,7 @@ func (backend *Backend) maybeAddP2TR(keystore keystore.Keystore, accounts []*con
86+hdkeychain.HardenedKeyStart,
bip44Coin,
uint32(accountNumber)+hdkeychain.HardenedKeyStart)
extendedPublicKey, err := keystore.ExtendedPublicKey(coin, keypath)
extendedPublicKey, err := keystore.ExtendedPublicKey(accountCoin, keypath)
if err != nil {
return err
}
Expand Down
13 changes: 6 additions & 7 deletions backend/coins/eth/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"strings"

"github.com/BitBoxSwiss/bitbox-wallet-app/backend/accounts"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
coinpkg "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth/erc20"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth/rpcclient"
Expand All @@ -45,7 +44,7 @@ type TransactionsSource interface {
type Coin struct {
observable.Implementation
client rpcclient.Interface
code coin.Code
code coinpkg.Code
name string
unit string
feeUnit string
Expand All @@ -65,7 +64,7 @@ type Coin struct {
// For erc20 tokens, provide erc20Token using NewERC20Token() (otherwise keep nil).
func NewCoin(
client rpcclient.Interface,
code coin.Code,
code coinpkg.Code,
name string,
unit string,
feeUnit string,
Expand Down Expand Up @@ -117,7 +116,7 @@ func (coin *Coin) Name() string {
}

// Code implements coin.Coin.
func (coin *Coin) Code() coin.Code {
func (coin *Coin) Code() coinpkg.Code {
return coin.code
}

Expand Down Expand Up @@ -151,21 +150,21 @@ func (coin *Coin) unitFactor(isFee bool) *big.Int {
}

// FormatAmount implements coin.Coin.
func (coin *Coin) FormatAmount(amount coin.Amount, isFee bool) string {
func (coin *Coin) FormatAmount(amount coinpkg.Amount, isFee bool) string {
factor := coin.unitFactor(isFee)
s := new(big.Rat).SetFrac(amount.BigInt(), factor).FloatString(18)
return strings.TrimRight(strings.TrimRight(s, "0"), ".")
}

// ToUnit implements coin.Coin.
func (coin *Coin) ToUnit(amount coin.Amount, isFee bool) float64 {
func (coin *Coin) ToUnit(amount coinpkg.Amount, isFee bool) float64 {
factor := coin.unitFactor(isFee)
result, _ := new(big.Rat).SetFrac(amount.BigInt(), factor).Float64()
return result
}

// SetAmount implements coin.Coin.
func (coin *Coin) SetAmount(amount *big.Rat, isFee bool) coin.Amount {
func (coin *Coin) SetAmount(amount *big.Rat, isFee bool) coinpkg.Amount {
factor := coin.unitFactor(isFee)
weiAmount := new(big.Rat).Mul(amount, new(big.Rat).SetInt(factor))
intWeiAmount, _ := new(big.Int).SetString(weiAmount.FloatString(0), 0)
Expand Down
3 changes: 1 addition & 2 deletions backend/devices/bitbox02/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package bitbox02

import (
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/devices/device/event"
deviceevent "github.com/BitBoxSwiss/bitbox-wallet-app/backend/devices/device/event"
keystoreInterface "github.com/BitBoxSwiss/bitbox-wallet-app/backend/keystore"
"github.com/BitBoxSwiss/bitbox-wallet-app/util/logging"
Expand Down Expand Up @@ -133,7 +132,7 @@ func (device *Device) Keystore() keystoreInterface.Keystore {
}

// SetOnEvent implements device.Device.
func (device *Device) SetOnEvent(onEvent func(event.Event, interface{})) {
func (device *Device) SetOnEvent(onEvent func(deviceevent.Event, interface{})) {
}

// Reset factory resets the device.
Expand Down
33 changes: 16 additions & 17 deletions backend/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc"
accountHandlers "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/handlers"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/util"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
coinpkg "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/coin"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth"
"github.com/BitBoxSwiss/bitbox-wallet-app/backend/config"
Expand Down Expand Up @@ -703,17 +702,17 @@ func (handlers *Handlers) postBtcFormatUnit(r *http.Request) interface{} {
// getAccountsBalanceHandler returns the balance of all the accounts, grouped by keystore and coin.
func (handlers *Handlers) getAccountsBalance(*http.Request) interface{} {
type response struct {
Success bool `json:"success"`
Balance map[string]map[coin.Code]accountHandlers.FormattedAmount `json:"balance,omitempty"`
Success bool `json:"success"`
Balance map[string]map[coinpkg.Code]accountHandlers.FormattedAmount `json:"balance,omitempty"`
}
totalAmount := make(map[string]map[coin.Code]accountHandlers.FormattedAmount)
totalAmount := make(map[string]map[coinpkg.Code]accountHandlers.FormattedAmount)
accountsByKeystore, err := handlers.backend.AccountsByKeystore()
if err != nil {
return response{Success: false}
}
for rootFingerprint, accountList := range accountsByKeystore {
totalPerCoin := make(map[coin.Code]*big.Int)
conversionsPerCoin := make(map[coin.Code]map[string]string)
totalPerCoin := make(map[coinpkg.Code]*big.Int)
conversionsPerCoin := make(map[coinpkg.Code]map[string]string)
for _, account := range accountList {
if account.Config().Config.Inactive || account.Config().Config.HiddenBecauseUnused {
continue
Expand All @@ -738,22 +737,22 @@ func (handlers *Handlers) getAccountsBalance(*http.Request) interface{} {
totalPerCoin[coinCode] = new(big.Int).Add(totalPerCoin[coinCode], amount.BigInt())
}

conversionsPerCoin[coinCode] = coin.Conversions(
coin.NewAmount(totalPerCoin[coinCode]),
conversionsPerCoin[coinCode] = coinpkg.Conversions(
coinpkg.NewAmount(totalPerCoin[coinCode]),
account.Coin(),
false,
account.Config().RateUpdater,
util.FormatBtcAsSat(handlers.backend.Config().AppConfig().Backend.BtcUnit))
}

totalAmount[rootFingerprint] = make(map[coin.Code]accountHandlers.FormattedAmount)
totalAmount[rootFingerprint] = make(map[coinpkg.Code]accountHandlers.FormattedAmount)
for k, v := range totalPerCoin {
currentCoin, err := handlers.backend.Coin(k)
if err != nil {
return response{Success: false}
}
totalAmount[rootFingerprint][k] = accountHandlers.FormattedAmount{
Amount: currentCoin.FormatAmount(coin.NewAmount(v), false),
Amount: currentCoin.FormatAmount(coinpkg.NewAmount(v), false),
Unit: currentCoin.GetFormatUnit(false),
Conversions: conversionsPerCoin[k],
}
Expand All @@ -767,7 +766,7 @@ func (handlers *Handlers) getAccountsBalance(*http.Request) interface{} {
}

type coinFormattedAmount struct {
CoinCode coin.Code `json:"coinCode"`
CoinCode coinpkg.Code `json:"coinCode"`
CoinName string `json:"coinName"`
FormattedAmount accountHandlers.FormattedAmount `json:"formattedAmount"`
}
Expand All @@ -779,8 +778,8 @@ func (handlers *Handlers) getCoinsTotalBalance(_ *http.Request) interface{} {
CoinsTotalBalance []coinFormattedAmount `json:"coinsTotalBalance,omitempty"`
}
var coinFormattedAmounts []coinFormattedAmount
var sortedCoins []coin.Code
totalCoinsBalances := make(map[coin.Code]*big.Int)
var sortedCoins []coinpkg.Code
totalCoinsBalances := make(map[coinpkg.Code]*big.Int)

for _, account := range handlers.backend.Accounts() {
if account.Config().Config.Inactive || account.Config().Config.HiddenBecauseUnused {
Expand Down Expand Up @@ -817,10 +816,10 @@ func (handlers *Handlers) getCoinsTotalBalance(_ *http.Request) interface{} {
CoinCode: coinCode,
CoinName: currentCoin.Name(),
FormattedAmount: accountHandlers.FormattedAmount{
Amount: currentCoin.FormatAmount(coin.NewAmount(totalCoinsBalances[coinCode]), false),
Amount: currentCoin.FormatAmount(coinpkg.NewAmount(totalCoinsBalances[coinCode]), false),
Unit: currentCoin.GetFormatUnit(false),
Conversions: coin.Conversions(
coin.NewAmount(totalCoinsBalances[coinCode]),
Conversions: coinpkg.Conversions(
coinpkg.NewAmount(totalCoinsBalances[coinCode]),
currentCoin,
false,
handlers.backend.RatesUpdater(),
Expand Down Expand Up @@ -1037,7 +1036,7 @@ func (handlers *Handlers) getConvertFromFiat(r *http.Request) interface{} {
}

rate := handlers.backend.RatesUpdater().LatestPrice()[unit][from]
result := coin.NewAmountFromInt64(0)
result := coinpkg.NewAmountFromInt64(0)
if rate != 0.0 {
amountRat := new(big.Rat).Quo(fiatRat, new(big.Rat).SetFloat64(rate))
result = currentCoin.SetAmount(amountRat, false)
Expand Down