Skip to content

Commit

Permalink
Enable custom networks and add avalanche testnet to all networks
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Jul 26, 2023
1 parent 93b2661 commit b253e9f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 4 additions & 0 deletions packages/network/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export const allNetworks = validateAndSortNetworks([
...networks[ChainId.HOMEVERSE_TESTNET],
...genUrls('homeverse-testnet')
},
{
...networks[ChainId.AVALANCHE_TESTNET],
...genUrls('avalanche-testnet')
},
{
...networks[ChainId.HARDHAT],
rpcUrl: 'http://localhost:8545',
Expand Down
17 changes: 8 additions & 9 deletions packages/provider/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,19 @@ export const initWallet = (

const rpcProviders: Record<number, ethers.providers.JsonRpcProvider> = {}

// We can't allow the dapp to define networks that aren't already defined in sequence.js
// we may in the future, but the issue is that wallet-webapp won't have these new networks
// either, so it will fail to connect to them.
if (config.networks?.some((n) => {
return !allNetworks.find((cn) => cn.chainId === n.chainId)
})) {
throw new Error('networks config must only contain networks defined in sequence.js')
}
// Find any new networks that aren't already defined in sequence.js
// and add them to the list of networks, (they must have a rpcUrl and chainId)
const newNetworks = (config.networks?.filter((n) => {
n.rpcUrl !== undefined &&
n.chainId !== undefined &&
!allNetworks.find((an) => an.chainId === n.chainId)
}) ?? []) as NetworkConfig[]

// Override any information about the networks using the config
const combinedNetworks = allNetworks.map((n) => {
const network = config.networks?.find((cn) => cn.chainId === n.chainId)
return network ? { ...n, ...network } : n
})
}).concat(newNetworks)

// This build a "public rpc" on demand, we build them on demand because we don't want to
// generate a bunch of providers for networks that aren't used.
Expand Down
7 changes: 4 additions & 3 deletions packages/provider/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export class SequenceProvider extends ethers.providers.BaseProvider implements I

constructor (
public readonly client: SequenceClient,
private readonly providerFor: (networkId: number) => ethers.providers.JsonRpcProvider
private readonly providerFor: (networkId: number) => ethers.providers.JsonRpcProvider,
public readonly networks: NetworkConfig[] = allNetworks
) {
// We support a lot of networks
// but we start with the default one
Expand Down Expand Up @@ -158,7 +159,7 @@ export class SequenceProvider extends ethers.providers.BaseProvider implements I
return undefined
}

const resolved = findNetworkConfig(allNetworks, chainId as ChainIdLike)
const resolved = findNetworkConfig(this.networks, chainId as ChainIdLike)

if (!resolved) {
throw new Error(`Unsupported network ${chainId}`)
Expand Down Expand Up @@ -270,7 +271,7 @@ export class SequenceProvider extends ethers.providers.BaseProvider implements I

async detectNetwork(): Promise<ethers.providers.Network> {
const chainId = this.client.getChainId()
const network = findNetworkConfig(allNetworks, chainId)
const network = findNetworkConfig(this.networks, chainId)

if (!network) {
throw new Error(`Unknown network ${chainId}`)
Expand Down

0 comments on commit b253e9f

Please sign in to comment.