@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 transactionsimport { 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
Name | Type | Required | Description |
---|---|---|---|
name | 'mainnet' | 'testnet' | 'devnet' | 'mocknet' | Yes | Network name |
Examples
import { networkFromName } from '@stacks/network';const mainnet = networkFromName('mainnet'); // Same as STACKS_MAINNETconst testnet = networkFromName('testnet'); // Same as STACKS_TESTNETconst devnet = networkFromName('devnet'); // Same as STACKS_DEVNETconst 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
Name | Type | Required | Description |
---|---|---|---|
network | StacksNetwork | Yes | Network 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 fetchconst customClient = {...client,fetch: customFetchFunction};
Network configuration properties
All network constants share these properties:
Property | Type | Description |
---|---|---|
chainId | number | Unique identifier for the network |
transactionVersion | number | Transaction serialization version |
peerNetworkId | number | P2P network identifier |
magicBytes | string | Network magic bytes for serialization |
bootAddress | string | Boot 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