Skip to content

Commit 85a8391

Browse files
authored
feat: add Avalanche Local chain support (#315)
* feat: add Avalanche Local chain support 🔺 Generated by DevRel swarm Add support for local Avalanche network (chain ID 43112) to enable local development and testing with avalanche-cli or similar local networks. * chore: update test snapshots for Avalanche Local chain Add new exports to snapshot tests: - avalancheLocal chain - Local network IDs (P_CHAIN_LOCAL_ID, X_CHAIN_LOCAL_ID, C_CHAIN_LOCAL_ID) - LOCAL_NETWORK_ID constant - AVALANCHE_CHAIN_IDS and AVALANCHE_NETWORK_IDS maps * refactor: use AVALANCHE_CHAIN_IDS constant for chain ID checks Replace hardcoded chain ID comparisons with AVALANCHE_CHAIN_ID_VALUES array derived from the AVALANCHE_CHAIN_IDS constant. This makes it easier to add new chains without modifying multiple conditions.
1 parent 8db0e36 commit 85a8391

File tree

7 files changed

+50
-3
lines changed

7 files changed

+50
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineChain } from "viem";
2+
3+
export const avalancheLocal = /*#__PURE__*/ defineChain({
4+
id: 43_112,
5+
name: "Avalanche Local",
6+
nativeCurrency: {
7+
decimals: 18,
8+
name: "Avalanche",
9+
symbol: "AVAX",
10+
},
11+
rpcUrls: {
12+
default: { http: ["http://127.0.0.1:9650/ext/bc/C/rpc"] },
13+
},
14+
testnet: true,
15+
});

client/src/chains/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ test("exports", () => {
171171
"fantom",
172172
"avalanche",
173173
"avalancheFuji",
174+
"avalancheLocal",
174175
"fantomSonicTestnet",
175176
"fantomTestnet",
176177
"fibo",

client/src/chains/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export type {
174174
} from "viem/chains";
175175
export { avalanche } from "./avalanche.js";
176176
export { avalancheFuji } from "./avalancheFuji.js";
177+
export { avalancheLocal } from "./avalancheLocal.js";
177178
/** @deprecated Use `sonicTestnet` instead. */
178179
export {
179180
fantomSonicTestnet,

client/src/clients/createAvalancheClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
RpcSchema,
99
Transport,
1010
} from "viem";
11+
import { AVALANCHE_CHAIN_IDS } from "../methods/consts.js";
1112
import { AvalanchePublicRpcSchema } from "../methods/public/avalanchePublicRpcSchema.js";
1213
import { createAdminApiClient } from "./createAdminApiClient.js";
1314
import { createCChainClient } from "./createCChainClient.js";
@@ -23,6 +24,8 @@ import {
2324
AvalancheClientConfig,
2425
} from "./types/createAvalancheClient.js";
2526
import { createAvalancheTransportClient } from "./utils.js";
27+
28+
const AVALANCHE_CHAIN_ID_VALUES = Object.values(AVALANCHE_CHAIN_IDS) as number[];
2629
/**
2730
* Creates an Avalanche Client with a given transport configured for a Chain.
2831
*
@@ -120,7 +123,7 @@ export function createAvalancheClient<
120123
});
121124
const extendedClient = client.extend(avalanchePublicActions) as any;
122125

123-
if (chainConfig?.id !== 43_113 && chainConfig?.id !== 43_114) {
126+
if (!chainConfig?.id || !AVALANCHE_CHAIN_ID_VALUES.includes(chainConfig.id)) {
124127
return {
125128
...extendedClient,
126129
};

client/src/clients/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
} from "viem";
1010
import { commonHeaders } from "./common.js";
1111
import { AvalancheTransportConfig, ClientType } from "./types/types.js";
12+
import { AVALANCHE_CHAIN_IDS } from "../methods/consts.js";
13+
14+
const AVALANCHE_CHAIN_ID_VALUES = Object.values(AVALANCHE_CHAIN_IDS) as number[];
1215

1316
export function createAvalancheTransportClient<
1417
transport extends Transport,
@@ -94,7 +97,7 @@ function getClientURL(
9497
clientType: ClientType = "public",
9598
transportType: "http" | "webSocket" = "http"
9699
): string | undefined {
97-
if (chain?.id !== 43_113 && chain?.id !== 43_114) {
100+
if (!chain?.id || !AVALANCHE_CHAIN_ID_VALUES.includes(chain.id)) {
98101
return url ?? chain?.rpcUrls.default[transportType]?.[0];
99102
}
100103

client/src/methods/consts.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1+
// Chain IDs for easy reference
2+
export const AVALANCHE_CHAIN_IDS = {
3+
MAINNET: 43114,
4+
FUJI: 43113,
5+
LOCAL: 43112,
6+
} as const;
7+
8+
// Network IDs (different from chain IDs)
9+
export const AVALANCHE_NETWORK_IDS = {
10+
MAINNET: 1,
11+
FUJI: 5,
12+
LOCAL: 1337,
13+
} as const;
14+
115
export const P_CHAIN_MAINNET_ID = '11111111111111111111111111111111LpoYY';
216
export const P_CHAIN_FUJI_ID = '11111111111111111111111111111111LpoYY';
17+
export const P_CHAIN_LOCAL_ID = '11111111111111111111111111111111LpoYY';
318
export const P_CHAIN_ALIAS = 'P';
419

520
export const X_CHAIN_MAINNET_ID = '2oYMBNV4eNHyqk2fjjV5nVQLDbtmNJzq5s3qs3Lo6ftnC6FByM';
621
export const X_CHAIN_FUJI_ID = '2JVSBoinj9C2J33VntvzYtVJNZdN2NKiwwKjcumHUWEb5DbBrm';
22+
export const X_CHAIN_LOCAL_ID = '2eNy1mUFdmaxXNj1eQHUe7Np4gju9sJsEtWQ4MX3ToiNKuADed';
723
export const X_CHAIN_ALIAS = 'X';
824

925
export const C_CHAIN_MAINNET_ID = '2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5';
1026
export const C_CHAIN_FUJI_ID = 'yH8D7ThNJkxmtkuv2jgBa4P1Rn3Qpr4pPr7QYNfcdoS6k6HWp';
27+
export const C_CHAIN_LOCAL_ID = '2CA6j5zYzasynPsFeNoqWkmTCt3VScMvXUZHbfDJ8k3oGzAPtU';
1128
export const C_CHAIN_ALIAS = 'C';
1229

1330
export const MAINNET_NETWORK_ID = 1;
14-
export const TESTNET_NETWORK_ID = 5;
31+
export const TESTNET_NETWORK_ID = 5;
32+
export const LOCAL_NETWORK_ID = 1337;

client/src/methods/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,23 @@ test("exports", () => {
110110
"watchEvent",
111111
"watchPendingTransactions",
112112
"writeContract",
113+
"AVALANCHE_CHAIN_IDS",
114+
"AVALANCHE_NETWORK_IDS",
113115
"P_CHAIN_MAINNET_ID",
114116
"P_CHAIN_FUJI_ID",
117+
"P_CHAIN_LOCAL_ID",
115118
"P_CHAIN_ALIAS",
116119
"X_CHAIN_MAINNET_ID",
117120
"X_CHAIN_FUJI_ID",
121+
"X_CHAIN_LOCAL_ID",
118122
"X_CHAIN_ALIAS",
119123
"C_CHAIN_MAINNET_ID",
120124
"C_CHAIN_FUJI_ID",
125+
"C_CHAIN_LOCAL_ID",
121126
"C_CHAIN_ALIAS",
122127
"MAINNET_NETWORK_ID",
123128
"TESTNET_NETWORK_ID",
129+
"LOCAL_NETWORK_ID",
124130
]
125131
`);
126132
});

0 commit comments

Comments
 (0)