Files
zeekayandHanzo Dev 62d5f28c22 A chain address is built in one place
The node moved every chain route from /v1/bc/<alias> to /v1/chain/<alias>
and deleted the old prefix, so every chain call answered 404. The address
was spelled out at 112 sites across 34 files and constants.ChainAliasPrefix
was used at none of them, which is why nothing failed to compile when the
segment moved.

route.Chain(uri, alias) composes it now, and route.Alias reads one back,
replacing three hand-rolled parsers — one of which took the alias by
position in the split path and only worked because the new segment happens
to leave the component count unchanged.

TestChainAddressBuiltOnlyHere walks every .go file in the module and fails
on a chain address in a string literal, so the segment cannot drift back;
prose is free to name a route. TestGuardCatchesEachShape plants each
forbidden shape and requires the guard to find it.

The endpoint box and the eleven-chain table were the same list written
twice; there is one list, rendered once, measured rather than hand-padded.
The generated CLI reference and the faucet manifest compose through the
same function.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-30 15:05:42 -07:00

83 lines
2.2 KiB
Go

// Copyright (C) 2022-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package upgradecmd
import (
"fmt"
"math/big"
"github.com/luxfi/cli/pkg/route"
"github.com/luxfi/cli/pkg/utils"
"github.com/luxfi/constants"
"github.com/luxfi/geth/common"
"github.com/luxfi/geth/ethclient"
"github.com/luxfi/sdk/models"
"go.uber.org/zap"
)
func getCClient(apiEndpoint string, blockchainID string) (*ethclient.Client, error) {
cClient, err := ethclient.Dial(route.Chain(apiEndpoint, blockchainID) + "/rpc")
if err != nil {
return nil, err
}
return cClient, nil
}
func ensureHaveBalanceLocalNetwork(which string, addresses []common.Address, blockchainID string) error {
cClient, err := getCClient(constants.LocalAPIEndpoint, blockchainID)
if err != nil {
return err
}
for _, address := range addresses {
// we can break at the first address who has a non-zero balance
accountBalance, err := getAccountBalance(cClient, address.String())
if err != nil {
return err
}
if accountBalance > float64(0) {
return nil
}
}
return fmt.Errorf("at least one of the %s addresses requires a positive token balance", which)
}
func ensureHaveBalance(
sc *models.Sidecar,
which string,
addresses []common.Address,
) error {
if len(addresses) < 1 {
return nil
}
switch sc.VM {
case models.EVM:
// Currently only checking if admins have balance for chains deployed in Local Network
if networkData, ok := sc.Networks["Local Network"]; ok {
blockchainID := networkData.BlockchainID.String()
if err := ensureHaveBalanceLocalNetwork(which, addresses, blockchainID); err != nil {
return err
}
}
default:
app.Log.Warn("Unsupported VM type", zap.Any("vm-type", sc.VM))
}
return nil
}
func getAccountBalance(cClient *ethclient.Client, addrStr string) (float64, error) {
addr := common.HexToAddress(addrStr)
ctx, cancel := utils.GetAPIContext()
balance, err := cClient.BalanceAt(ctx, addr, nil)
defer cancel()
if err != nil {
return 0, err
}
// convert to nLux
balance = balance.Div(balance, big.NewInt(int64(constants.Lux)))
if balance.Cmp(big.NewInt(0)) == 0 {
return 0, nil
}
return float64(balance.Uint64()) / float64(constants.Lux), nil
}