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

feat(network): add new network config builder #316

Merged
merged 11 commits into from
Nov 25, 2024

Conversation

drichar
Copy link
Collaborator

@drichar drichar commented Nov 23, 2024

Description

This PR introduces the NetworkConfigBuilder to enable customizable network configurations and support for custom networks. It provides a more flexible way to configure network settings, including CAIP-2 chain IDs, and allows applications to define their own networks beyond the default options:

  1. mainnet
  2. testnet
  3. betanet
  4. fnet
  5. localnet

Examples

Using default network configurations

const manager = new WalletManager({
  wallets: [WalletId.DEFLY, WalletId.KIBISIS],
  defaultNetwork: NetworkId.MAINNET, // property renamed from `network`
})

// Above is equivalent to:
const manager = new WalletManager({
  wallets: [WalletId.DEFLY, WalletId.KIBISIS],
  defaultNetwork: 'mainnet', // string or `NetworkId` enum both valid
  networks: new NetworkConfigBuilder().build() // replaces deprecated `algod` property
})

Adding a custom configuration

const networks = new NetworkConfigBuilder()
  .mainnet({
    algod: {
      token: process.env.MAINNET_TOKEN,
      baseServer: 'https://my-mainnet-node.com'
    }
  })
  .fnet({
    algod: {
      token: process.env.FNET_TOKEN,
      baseServer: 'https://my-fnet-node.com'
    }
  })
  .build()

const manager = new WalletManager({
  wallets: [WalletId.DEFLY, WalletId.KIBISIS],
  defaultNetwork: NetworkId.MAINNET,
  networks,
})

Adding a custom network

const networks = new NetworkConfigBuilder()
  .addNetwork('voi', {
    name: 'Voi Network',
    algod: {
      token: '',
      baseServer: 'https://mainnet-api.voi.nodely.dev',
    },
    isTestnet: false
  })
  .build()

const manager = new WalletManager({
  wallets: [WalletId.KIBISIS],
  defaultNetwork: 'voi',
  networks,
})

⚠️ BREAKING CHANGES: Network configuration now requires using NetworkConfigBuilder instead of the NetworkId-keyed mapped object from v3. While NetworkId is still exported for use with the defaultNetwork property, network configurations must be created using the new builder pattern.

Details

  • Add NetworkConfigBuilder for creating and customizing network configurations
  • Add support for CAIP-2 chain IDs in network configurations
  • Add isTestnet flag to network configurations
  • Update all wallet implementations to use new network config system
  • Add network property to wallet constructor calls
  • Update manager tests to use new network configuration approach
  • Add support for custom networks via NetworkConfigBuilder
  • Update network-related test assertions
  • Add proper genesis hash/ID handling from network configs
  • Maintain NetworkId enum for defaultNetwork configuration
  • Rename config property network to defaultNetwork
  • Rename config property algod to networks

Closes #214

Introduce `NetworkConfigBuilder` to enable customizable network configurations
and support for custom networks. This change provides a more flexible way to
configure network settings, including CAIP-2 chain IDs, and allows applications to
define their own networks beyond the default mainnet/testnet options.

- Add `NetworkConfigBuilder` for creating and customizing network configurations
- Update all wallet implementations to use new network config system
- Add support for CAIP-2 chain IDs in network configurations
- Add `isTestnet` flag to network configurations
- Update all tests to use string literals for network names
- Add `network` property to wallet provider constructor calls
- Update `WalletManager` tests to use new network configuration approach
- Add support for custom networks via `NetworkConfigBuilder`
- Update network-related test assertions
- Add proper genesis hash/ID handling from network configs
- Maintain `NetworkId` enum for defaultNetwork configuration
- Rename config property `network` to `defaultNetwork`
- Rename config property `algod` to `networks`

BREAKING CHANGE: Network configuration now requires using
`NetworkConfigBuilder` instead of the `NetworkId`-keyed mapped object from v3.
While `NetworkId` is still exported for use with the `defaultNetwork` property,
network configurations must be created using the new builder pattern.
Update framework adapters to handle the new nested algod configuration
structure and `defaultNetwork` property name change. This includes:

- Fix destructuring of algod config in React, Vue, and Solid adapters
- Update all example apps to use `defaultNetwork` instead of network
- Update test files to include networks in wallet constructor params
- Export `DEFAULT_NETWORKS` from core package
- Fix `NetworkId` type in `setActiveNetwork` method signatures

BREAKING CHANGE: The `network` property in `WalletManagerConfig`
has been renamed to `defaultNetwork` to better reflect its purpose.
Enhance the `NetworkConfigBuilder` and network config validation:
- Update builder methods to accept full network config instead of just algod config
- Add `DefaultNetworkConfig` type for partial network configs
- Add robust token validation for all possible algosdk token types
- Improve type guard to validate all network config properties
- Allow full network config customization for localnet
@drichar drichar force-pushed the feat/new-custom-network-config branch from 09f7284 to b9f7209 Compare November 24, 2024 09:08
Replace imported `DEFAULT_STATE` with inline state definition to prevent test
state pollution. This reverts a change made in 2bb91e4.

- Remove `DEFAULT_STATE` import
- Define default state directly in `beforeEach` block
- Ensure clean store initialization between tests
- Fix regression in 'updates wallets when store state changes' test
- Replace `NetworkId` type with string for `networkId` parameter
- Add network existence validation in `setActiveNetwork`
- Add tests for invalid network and custom network scenarios
- Change test script from "vitest" to "vitest run" in all packages
- Ensures tests run once and exit instead of watching by default
- Move `networkConfig` getter from `WalletConnect` to `BaseWallet`
- Make networks accessible for testing in all wallet implementations
- Replace empty networks object with `DEFAULT_NETWORKS` in Custom wallet tests
- Ensure consistent network configuration across all wallet tests
- Move DEFAULT_NETWORKS import to top of `LuteWallet` test file
- Group related imports together
- Sort type imports alphabetically
- Follow project import ordering conventions
@drichar drichar merged commit 9fc4365 into v4 Nov 25, 2024
1 check passed
@drichar drichar deleted the feat/new-custom-network-config branch November 25, 2024 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant