• Stars
    star
    174
  • Rank 219,104 (Top 5 %)
  • Language
    TypeScript
  • License
    GNU General Publi...
  • Created almost 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

API for the Stacks blockchain

@hirosystems/stacks-blockchain-api

CI GitHub Releases Docker Pulls NPM client package

Quick start

A self-contained Docker image is provided which starts a Stacks 2.05 blockchain and API instance.

Ensure Docker is installed, then run the command:

docker run -p 3999:3999 hirosystems/stacks-blockchain-api-standalone

Similarly, a "mocknet" instance can be started. This runs a local node, isolated from the testnet/mainnet:

docker run -p 3999:3999 -e STACKS_NETWORK=mocknet hirosystems/stacks-blockchain-api-standalone

Once the blockchain has synced with network, the API will be available at: http://localhost:3999

Development quick start

First, ensure Docker is installed on your machine.

Clone repo and install dependencies with npm install.

Run npm run dev:integrated.

This command will concurrently start the API server app and the service dependencies.

Check to see if the server started successfully by visiting http://localhost:3999/extended/v1/status

Local Development

Setup Services

Then run npm run devenv:deploy which uses docker-compose to deploy the service dependencies (e.g. PostgreSQL, Stacks core node, etc).

Running the server

To run the server in 'watch' mode (restart for every code change), run npm run dev:watch. You'll have a server on port 3999.

Architecture

API architecture!

See overview.md for architecture details.

Deployment

For optimal performance, we recommend running the API database on PostgreSQL version 14 or newer.

Upgrading

If upgrading the API to a new major version (e.g. 3.0.0 to 4.0.0) then the Postgres database from the previous version will not be compatible and the process will fail to start.

Event Replay must be used when upgrading major versions. Follow the event replay instructions below. Failure to do so will require wiping both the Stacks Blockchain chainstate data and the API Postgres database, and re-syncing from scratch.

API Run Modes

The API supports a series of run modes, each accommodating different use cases for scaling and data access by toggling architecture components on or off depending on its objective.

Default mode (Read-write)

The default mode runs with all components enabled. It consumes events from a Stacks node, writes them to a postgres database, and serves API endpoints.

Write-only mode

During Write-only mode, the API only runs the Stacks node events server to populate the postgres database but it does not serve any API endpoints.

This mode is very useful when you need to consume blockchain data from the postgres database directly and you're not interested in taking on the overhead of running an API web server.

For write-only mode, set the environment variable STACKS_API_MODE=writeonly.

Read-only mode

During Read-only mode, the API runs without an internal event server that listens to events from a Stacks node. The API only reads data from the connected postgres database when building endpoint responses. In order to keep serving updated blockchain data, this mode requires the presence of another API instance that keeps writing stacks-node events to the same database.

This mode is very useful when building an environment that load-balances incoming HTTP requests between multiple API instances that can be scaled up and down very quickly. Read-only instances support websockets and socket.io clients.

For read-only mode, set the environment variable STACKS_API_MODE=readonly.

Offline mode

In Offline mode app runs without a stacks-node or postgres connection. In this mode, only the given rosetta endpoints are supported: https://www.rosetta-api.org/docs/node_deployment.html#offline-mode-endpoints.

For running offline mode set an environment variable STACKS_API_MODE=offline

Event Replay

The stacks-node is only able to emit events live as they happen. This poses a problem in the scenario where the stacks-blockchain-api needs to be upgraded and its database cannot be migrated to a new schema. One way to handle this upgrade is to wipe the stacks-blockchain-api's database and stacks-node working directory, and re-sync from scratch.

Alternatively, an event-replay feature is available where the API records the HTTP POST requests from the stacks-node event emitter, then streams these events back to itself. Essentially simulating a wipe & full re-sync, but much quicker.

The feature can be used via program args. For example, if there are breaking changes in the API's sql schema, like adding a new column which requires event's to be re-played, the following steps could be ran:

Event Replay Instructions

V1 BNS Data

Optional but recommended - If you want the V1 BNS data, there are going to be a few extra steps:

  1. Download BNS data:

    curl -L https://storage.googleapis.com/blockstack-v1-migration-data/export-data.tar.gz -o /stacks-node/bns/export-data.tar.gz
  2. Extract it:

    tar -xzvf ./bns/export-data.tar.gz -C /stacks-node/bns/
  3. Each file in ./bns will have a corresponding sha256 value. To Verify, run a script like the following to check the sha256sum:

    for file in `ls /stacks-node/bns/* | grep -v sha256 | grep -v .tar.gz`; do
        if [ $(sha256sum $file | awk {'print $1'}) == $(cat ${file}.sha256 ) ]; then
            echo "sha256 Matched $file"
        else
            echo "sha256 Mismatch $file"
        fi
    done
  4. Set the data's location as the value of BNS_IMPORT_DIR in your .env file.

Export and Import

  1. Ensure the API process is not running. When stopping the API, let the process exit gracefully so that any in-progress SQL writes can finish.

  2. Export event data to disk with the export-events command:

    node ./lib/index.js export-events --file /tmp/stacks-node-events.tsv
  3. Update to the new stacks-blockchain-api version.

  4. Perform the event playback using the import-events command:

    WARNING: This will drop all tables from the configured Postgres database, including any tables not automatically added by the API.

    node ./lib/index.js import-events --file /tmp/stacks-node-events.tsv --wipe-db --force

    This command has two modes of operation, specified by the --mode option:

    • archival (default): The process will import and ingest all blockchain events that have happened since the first block.
    • pruned: The import process will ignore some prunable events (mempool, microblocks) until the import block height has reached chain tip - 256 blocks. This saves a considerable amount of time during import, but sacrifices some historical data. You can use this mode if you're mostly interested in running an API that prioritizes real time information.

Bugs and feature requests

If you encounter a bug or have a feature request, we encourage you to follow the steps below:

  1. Search for existing issues: Before submitting a new issue, please search existing and closed issues to check if a similar problem or feature request has already been reported.
  2. Open a new issue: If it hasn't been addressed, please open a new issue. Choose the appropriate issue template and provide as much detail as possible, including steps to reproduce the bug or a clear description of the requested feature.
  3. Evaluation SLA: Our team reads and evaluates all the issues and pull requests. We are avaliable Monday to Friday and we make a best effort to respond within 7 business days.

Please do not use the issue tracker for personal support requests or to ask for the status of a transaction. You'll find help at the #support Discord channel.

Contribute

Development of this product happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving the product.

Code of Conduct

Please read our Code of conduct since we expect project participants to adhere to it.

Contributing Guide

Read our contributing guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes.

Community

Join our community and stay connected with the latest updates and discussions:

Client library

You can use the Stacks Blockchain API Client library if you require a way to call the API via JavaScript or receive real-time updates via Websockets or Socket.io. Learn more here.

More Repositories

1

stacks.js

JavaScript libraries for identity, auth, storage and transactions on the Stacks blockchain.
TypeScript
952
star
2

clarinet

Write, test and deploy high-quality smart contracts to the Stacks blockchain and Bitcoin.
Rust
301
star
3

wallet

The most popular and trusted wallet for apps built on Bitcoin
TypeScript
248
star
4

ordinals-api

Bitcoin Ordinals API
TypeScript
199
star
5

ordhook

Build indexers, standards and protocols on top of Ordinals and Inscriptions (BRC20, etc).
Rust
179
star
6

desktop-wallet

Stacks Walletโ€”Send and receive STX tokens on the Stacks Blockchain.
TypeScript
174
star
7

chainhook

Extract transactions from Stacks and Bitcoin and build event driven re-org resistant indexers and databases.
Rust
143
star
8

explorer

Explore transactions and accounts on the Stacks blockchain. Clone any contract and experiment in your browser with the Explorer sandbox.
TypeScript
136
star
9

connect

A library for building excellent user experiences with Stacks.
SCSS
76
star
10

todos

A simple todos app built with Stacks
JavaScript
70
star
11

stacks-subnets

Stacks Subnets: a layer-2 scaling solution for Stacks, intended for high-throughput, low-latency workloads
Rust
51
star
12

btc-us-website

Svelte
50
star
13

ui

The Blockstack design system implemented with react, styled-components, and styled-system.
TypeScript
34
star
14

docs

Hiro developer documentation website
MDX
34
star
15

ordinals-explorer

Explore Ordinals inscriptions, using the Hiro Ordinals API powered by Chainhooks.
TypeScript
32
star
16

heystack

TypeScript
17
star
17

stacks.js-starters

Quick-start frontend projects using Stacks.js
JavaScript
15
star
18

lockstacks

TypeScript
13
star
19

clarity-notebook

A set of Jupyter notebooks for getting started with Clarity.
Jupyter Notebook
12
star
20

stacks-pyth-bridge

Retrieve trading pairs (BTC-USD, STX-USD, etc) from Clarity smart contracts.
Clarity
12
star
21

clarity-examples

Shell
9
star
22

rx

Reactive Extensions client for the Stacks Blockchain
TypeScript
8
star
23

token-metadata-api

Stacks Token Metadata API
TypeScript
8
star
24

runehook

Bitcoin Runes indexer and Runes API
Rust
8
star
25

stacks-e2e-testing

TypeScript
7
star
26

stacks-encoding-native-js

Encoding & decoding functions for the Stacks blockchain exposed as a fast native Node.js addon
Rust
6
star
27

MIT-Bitcoin-Expo-2022-Hiro-Hackathon-Resources

Some resources and educational materials to get hackers started at the MIT Bitcoin Expo 2022
5
star
28

stacks-scout

TypeScript
5
star
29

orchestra-archive

Development Studio designed for the Stacks blockchain. Bleeding edge prototype.
Rust
5
star
30

clarity-lsp

Rust
5
star
31

nft-nyc-exclusive-app

Exclusive Bitcoin NFT minting app for NFT.NYC
TypeScript
5
star
32

community-meeting

Monthly developer calls between the Hiro product teams and the developers building on Stacks
4
star
33

clarity-starter

Clarity
4
star
34

bitcoin-docker

Dockerize bitcoind
Shell
4
star
35

stacks-regtest-env

Easily run a Stacks node in Kypton mode with a Bitcoind regtest instance
Shell
4
star
36

multisig-stx-btc

A webscript for generating, signing, decoding multisig STX operations in the STX-over-BTC wire format
JavaScript
4
star
37

gaia-docker

Shell
3
star
38

charts

Hiro Systems Helm Charts
Smarty
3
star
39

clarity-factory

TypeScript
3
star
40

debug_types

Rust
3
star
41

api-toolkit

API toolkit
TypeScript
3
star
42

stacks-devnet-api

The Stacks Devnet API runs a server that can be used to deploy, delete, manage, and make requests to Stacks Devnets run on Kubernetes.
Rust
3
star
43

lunar-hirover

Rust
3
star
44

clarity-playground

Run Clarity code in the browser Clarinet
JavaScript
3
star
45

rfc-clarity-lang-converter

2
star
46

stacks-repl

JavaScript
2
star
47

clarity.tmbundle

A repository with Clarity syntax-highlighting grammar files
2
star
48

eslint-config

A set of re-usable ESLint configuration for Blockstack PBC projects
JavaScript
2
star
49

stacks-event-replay

Python
2
star
50

stacks-adoption-report

This project is used to generate a list of Stacks-related projects
JavaScript
2
star
51

stacks-billboard

TypeScript
2
star
52

vitest-environment-clarinet

JavaScript
2
star
53

gh-actions-example

Dockerfile
1
star
54

prettier-config

@stacks Prettier config
JavaScript
1
star
55

hiro-kit.clar

Hiro Kit: collection of utility functions for Clarity contracts
Clarity
1
star
56

clear-waters

A Clarity Learning Game
Clarity
1
star
57

clar2wasm

Compile Clarity to WebAssembly
1
star
58

rfc-web3-oauth

1
star
59

examples

A collection of example apps showcasing /-/iro products
TypeScript
1
star
60

hiro-hacks-template

Starter template for Hiro Hacks
TypeScript
1
star
61

stacks-rpc-proxy

TypeScript
1
star
62

app.co-api

API Server for app.co
JavaScript
1
star
63

subnet-demo-app

The project demonstrates some of the capabilities of subnets through a simple NFT marketplace where minting, listing and offers happen on L2.
TypeScript
1
star