Skip to content

Commit 5789937

Browse files
committed
fix: add StacksDevnet constructor, closes #1470
1 parent 913483c commit 5789937

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

packages/network/src/network.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export interface NetworkConfig {
1010
fetchFn?: FetchFn;
1111
}
1212

13-
export const StacksNetworks = ['mainnet', 'testnet'] as const;
13+
export const StacksNetworks = ['mainnet', 'testnet', 'devnet', 'mocknet'] as const;
1414
export type StacksNetworkName = (typeof StacksNetworks)[number];
1515

1616
/**
17-
* @related {@link StacksMainnet}, {@link StacksTestnet}, {@link StacksMocknet}
17+
* @related {@link StacksMainnet}, {@link StacksTestnet}, {@link StacksDevnet}, {@link StacksMocknet}
1818
*/
1919
export class StacksNetwork {
2020
version = TransactionVersion.Mainnet;
@@ -42,6 +42,10 @@ export class StacksNetwork {
4242
return new StacksMainnet();
4343
case 'testnet':
4444
return new StacksTestnet();
45+
case 'devnet':
46+
return new StacksDevnet();
47+
case 'mocknet':
48+
return new StacksMocknet();
4549
default:
4650
throw new Error(
4751
`Invalid network name provided. Must be one of the following: ${StacksNetworks.join(
@@ -133,7 +137,7 @@ export class StacksNetwork {
133137
}
134138

135139
/**
136-
* A {@link StacksNetwork} with default values for the Stacks mainnet.
140+
* A {@link StacksNetwork} with the parameters for the Stacks mainnet.
137141
* Pass a `url` option to override the default Hiro hosted Stacks node API.
138142
* Pass a `fetchFn` option to customize the default networking functions.
139143
* @example
@@ -157,7 +161,16 @@ export class StacksMainnet extends StacksNetwork {
157161
}
158162

159163
/**
160-
* Same as {@link StacksMainnet} but defaults to values for the Stacks testnet.
164+
* A {@link StacksNetwork} with the parameters for the Stacks testnet.
165+
* Pass a `url` option to override the default Hiro hosted Stacks node API.
166+
* Pass a `fetchFn` option to customize the default networking functions.
167+
* @example
168+
* ```
169+
* const network = new StacksTestnet();
170+
* const network = new StacksTestnet({ url: "https://stacks-node-api.testnet.stacks.co" });
171+
* const network = new StacksTestnet({ fetch: createFetchFn() });
172+
* ```
173+
* @related {@link createFetchFn}, {@link createApiKeyMiddleware}
161174
*/
162175
export class StacksTestnet extends StacksNetwork {
163176
version = TransactionVersion.Testnet;
@@ -171,6 +184,9 @@ export class StacksTestnet extends StacksNetwork {
171184
}
172185
}
173186

187+
/**
188+
* A {@link StacksNetwork} using the testnet parameters, but `localhost:3999` as the API URL.
189+
*/
174190
export class StacksMocknet extends StacksNetwork {
175191
version = TransactionVersion.Testnet;
176192
chainId = ChainID.Testnet;
@@ -182,3 +198,6 @@ export class StacksMocknet extends StacksNetwork {
182198
});
183199
}
184200
}
201+
202+
/** Alias for {@link StacksMocknet} */
203+
export const StacksDevnet = StacksMocknet;

packages/network/tests/network.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,33 @@ import {
44
HIRO_TESTNET_DEFAULT,
55
StacksMainnet,
66
StacksMocknet,
7+
StacksNetwork,
78
StacksTestnet,
89
} from '../src/network';
910

1011
describe('Setting coreApiUrl', () => {
11-
test('it sets mainnet default url', () => {
12+
it('sets mainnet default url', () => {
1213
const mainnet = new StacksMainnet();
1314
expect(mainnet.coreApiUrl).toEqual(HIRO_MAINNET_DEFAULT);
1415
});
15-
test('it sets testnet url', () => {
16+
it('sets testnet url', () => {
1617
const testnet = new StacksTestnet();
1718
expect(testnet.coreApiUrl).toEqual(HIRO_TESTNET_DEFAULT);
1819
});
19-
test('it sets mocknet url', () => {
20+
it('sets mocknet url', () => {
2021
const mocknet = new StacksMocknet();
2122
expect(mocknet.coreApiUrl).toEqual(HIRO_MOCKNET_DEFAULT);
2223
});
23-
test('it sets custom url', () => {
24+
it('sets custom url', () => {
2425
const customURL = 'https://customurl.com';
2526
const customNET = new StacksMainnet({ url: customURL });
2627
expect(customNET.coreApiUrl).toEqual(customURL);
2728
});
2829
});
30+
31+
it('uses the correct constructor for stacks network from name strings', () => {
32+
expect(StacksNetwork.fromName('mainnet').constructor.toString()).toContain('StacksMainnet');
33+
expect(StacksNetwork.fromName('testnet').constructor.toString()).toContain('StacksTestnet');
34+
expect(StacksNetwork.fromName('devnet').constructor.toString()).toContain('StacksMocknet'); // devnet is an alias for mocknet
35+
expect(StacksNetwork.fromName('mocknet').constructor.toString()).toContain('StacksMocknet');
36+
});

0 commit comments

Comments
 (0)