Skip to content

Commit

Permalink
fix: add StacksDevnet constructor, closes #1470
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Apr 19, 2023
1 parent 913483c commit 5789937
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
27 changes: 23 additions & 4 deletions packages/network/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export interface NetworkConfig {
fetchFn?: FetchFn;
}

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

/**
* @related {@link StacksMainnet}, {@link StacksTestnet}, {@link StacksMocknet}
* @related {@link StacksMainnet}, {@link StacksTestnet}, {@link StacksDevnet}, {@link StacksMocknet}
*/
export class StacksNetwork {
version = TransactionVersion.Mainnet;
Expand Down Expand Up @@ -42,6 +42,10 @@ export class StacksNetwork {
return new StacksMainnet();
case 'testnet':
return new StacksTestnet();
case 'devnet':
return new StacksDevnet();
case 'mocknet':
return new StacksMocknet();
default:
throw new Error(
`Invalid network name provided. Must be one of the following: ${StacksNetworks.join(
Expand Down Expand Up @@ -133,7 +137,7 @@ export class StacksNetwork {
}

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

/**
* Same as {@link StacksMainnet} but defaults to values for the Stacks testnet.
* A {@link StacksNetwork} with the parameters for the Stacks testnet.
* Pass a `url` option to override the default Hiro hosted Stacks node API.
* Pass a `fetchFn` option to customize the default networking functions.
* @example
* ```
* const network = new StacksTestnet();
* const network = new StacksTestnet({ url: "https://stacks-node-api.testnet.stacks.co" });
* const network = new StacksTestnet({ fetch: createFetchFn() });
* ```
* @related {@link createFetchFn}, {@link createApiKeyMiddleware}
*/
export class StacksTestnet extends StacksNetwork {
version = TransactionVersion.Testnet;
Expand All @@ -171,6 +184,9 @@ export class StacksTestnet extends StacksNetwork {
}
}

/**
* A {@link StacksNetwork} using the testnet parameters, but `localhost:3999` as the API URL.
*/
export class StacksMocknet extends StacksNetwork {
version = TransactionVersion.Testnet;
chainId = ChainID.Testnet;
Expand All @@ -182,3 +198,6 @@ export class StacksMocknet extends StacksNetwork {
});
}
}

/** Alias for {@link StacksMocknet} */
export const StacksDevnet = StacksMocknet;
16 changes: 12 additions & 4 deletions packages/network/tests/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@ import {
HIRO_TESTNET_DEFAULT,
StacksMainnet,
StacksMocknet,
StacksNetwork,
StacksTestnet,
} from '../src/network';

describe('Setting coreApiUrl', () => {
test('it sets mainnet default url', () => {
it('sets mainnet default url', () => {
const mainnet = new StacksMainnet();
expect(mainnet.coreApiUrl).toEqual(HIRO_MAINNET_DEFAULT);
});
test('it sets testnet url', () => {
it('sets testnet url', () => {
const testnet = new StacksTestnet();
expect(testnet.coreApiUrl).toEqual(HIRO_TESTNET_DEFAULT);
});
test('it sets mocknet url', () => {
it('sets mocknet url', () => {
const mocknet = new StacksMocknet();
expect(mocknet.coreApiUrl).toEqual(HIRO_MOCKNET_DEFAULT);
});
test('it sets custom url', () => {
it('sets custom url', () => {
const customURL = 'https://customurl.com';
const customNET = new StacksMainnet({ url: customURL });
expect(customNET.coreApiUrl).toEqual(customURL);
});
});

it('uses the correct constructor for stacks network from name strings', () => {
expect(StacksNetwork.fromName('mainnet').constructor.toString()).toContain('StacksMainnet');
expect(StacksNetwork.fromName('testnet').constructor.toString()).toContain('StacksTestnet');
expect(StacksNetwork.fromName('devnet').constructor.toString()).toContain('StacksMocknet'); // devnet is an alias for mocknet
expect(StacksNetwork.fromName('mocknet').constructor.toString()).toContain('StacksMocknet');
});

1 comment on commit 5789937

@vercel
Copy link

@vercel vercel bot commented on 5789937 Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.