@stacks/network

Network configuration constants and utilities for Stacks.

The @stacks/network package exports network configurations and helper functions for working with different Stacks networks.

Installation

Terminal
$
npm install @stacks/network

Network constants

STACKS_MAINNET

STACKS_MAINNET provides the mainnet network configuration.

import { STACKS_MAINNET } from '@stacks/network';
console.log(STACKS_MAINNET);
// {
// chainId: 1,
// transactionVersion: 0,
// peerNetworkId: 385875968,
// magicBytes: 'X2',
// bootAddress: 'SP000000000000000000002Q6VF78'
// }

STACKS_TESTNET

STACKS_TESTNET provides the testnet network configuration.

import { STACKS_TESTNET } from '@stacks/network';
console.log(STACKS_TESTNET.chainId); // 2147483648

STACKS_DEVNET / STACKS_MOCKNET

STACKS_DEVNET provides the devnet network configuration.

import { STACKS_DEVNET, STACKS_MOCKNET } from '@stacks/network';
// Use in transactions
import { makeSTXTokenTransfer } from '@stacks/transactions';
const tx = await makeSTXTokenTransfer({
network: STACKS_DEVNET, // or STACKS_MOCKNET
// ... other options
});

networkFromName

networkFromName returns a network configuration for a given name string.

Signature

function networkFromName(name: 'mainnet' | 'testnet' | 'devnet' | 'mocknet'): StacksNetwork

Parameters

NameTypeRequiredDescription
name'mainnet' | 'testnet' | 'devnet' | 'mocknet'YesNetwork name

Examples

import { networkFromName } from '@stacks/network';
const mainnet = networkFromName('mainnet'); // Same as STACKS_MAINNET
const testnet = networkFromName('testnet'); // Same as STACKS_TESTNET
const devnet = networkFromName('devnet'); // Same as STACKS_DEVNET
const mocknet = networkFromName('mocknet'); // Same as STACKS_MOCKNET

Using with transactions

import { networkFromName } from '@stacks/network';
import { makeContractCall } from '@stacks/transactions';
const network = networkFromName('testnet');
const tx = await makeContractCall({
network,
contractAddress: 'ST2ZRX0K27GW0SP3GJCEMHD95TQGJMKB7G9Y0X1MH',
contractName: 'hello-world',
functionName: 'say-hi',
functionArgs: [],
senderKey: privateKey
});

clientFromNetwork

clientFromNetwork extracts the API client configuration from a network.

Signature

function clientFromNetwork(network: StacksNetwork): Required<ClientOpts>

Parameters

NameTypeRequiredDescription
networkStacksNetworkYesNetwork configuration object

Example

import { clientFromNetwork, STACKS_MAINNET } from '@stacks/network';
const client = clientFromNetwork(STACKS_MAINNET);
console.log(client.baseUrl); // 'https://api.mainnet.hiro.so'
// Use with custom fetch
const customClient = {
...client,
fetch: customFetchFunction
};

Network configuration properties

All network constants share these properties:

PropertyTypeDescription
chainIdnumberUnique identifier for the network
transactionVersionnumberTransaction serialization version
peerNetworkIdnumberP2P network identifier
magicBytesstringNetwork magic bytes for serialization
bootAddressstringBoot contract address

Default values

The package also exports default configuration values:

import { DEFAULT_CHAIN_ID, DEFAULT_TRANSACTION_VERSION } from '@stacks/network';
console.log(DEFAULT_CHAIN_ID); // 1 (mainnet)
console.log(DEFAULT_TRANSACTION_VERSION); // 0

Further reading