@ -25,13 +25,13 @@ This tutorial highlights the following functionality:
- Add stacking action
- Display stacking status
-> Alternatively to integration using JS libraries, you can [use the CLI](https://gist.github.com/kantai/c261ca04114231f0f6a7ce34f0d2499b).
-> Alternatively to integration using JS libraries, you can use the [Rust CLI](https://gist.github.com/kantai/c261ca04114231f0f6a7ce34f0d2499b) or [JS CLI](https://github.com/blockstack/stacks.js/tree/master/packages/cli).
## Requirements
First, you'll need to understand the [Stacking mechanism](/understand-stacks/stacking).
You'll also need [NodeJS](https://nodejs.org/en/download/) `8.12.0` or higher to complete this tutorial. You can verify your installation by opening up your terminal and run the following command:
You'll also need [NodeJS](https://nodejs.org/en/download/) `12.10.0` or higher to complete this tutorial. You can verify your installation by opening up your terminal and run the following command:
```bash
node --version
@ -41,174 +41,116 @@ node --version
In this tutorial, we'll implement the Stacking flow laid out in the [Stacking guide](/understand-stacks/stacking#stacking-flow).
-> Check out the sample source code for this tutorial in this GitHub repository: [stacking-integration-sample](https://github.com/agraebe/stacking-integration-sample)
## Step 1: Integrate libraries
Install the stacks transactions library and an API client for the [Stacks 2.0 Blockchain API](/references/stacks-blockchain):
Install the stacking, network, transactions libraries and bn.js for large number handling:
-> The API client is generated from the [OpenAPI specification](https://github.com/blockstack/stacks-blockchain-api/blob/master/docs/openapi.yaml) ([openapi-generator](https://github.com/OpenAPITools/openapi-generator)). Many other languages and frameworks are supported by the generator.
-> See additional [Stacking library reference](https://github.com/blockstack/stacks.js/tree/master/packages/stacking)
## Step 2: Generating an account
## Step 2: Generating an account and initialization
To get started, let's create a new, random Stacks 2.0 account:
-> Check out the API references for the 3 endpoints used here: [GET /v2/info](https://blockstack.github.io/stacks-blockchain-api/#operation/get_core_api_info), [GET v2/pox](https://blockstack.github.io/stacks-blockchain-api/#operation/get_pox_info), and [GET /extended/v1/info/network_block_times](https://blockstack.github.io/stacks-blockchain-api/#operation/get_network_block_times)
The object, including PoX, core, and block time information, looks like this:
```js
{
poxInfo: {
contract_id: 'ST000000000000000000002AMW42H.pox',
first_burnchain_block_height: 0,
min_amount_ustx: 2000000000000,
registration_window_length: undefined,
rejection_fraction: 25,
reward_cycle_id: 4,
reward_cycle_length: 120
},
coreInfo: {
limit: undefined,
peer_version: 385875968,
burn_consensus: undefined,
burn_block_height: 605,
stable_burn_consensus: undefined,
stable_burn_block_height: 604,
server_version: 'blockstack-core 0.0.1 => 23.0.0.0 (master:5b816c2+, release build, linux [x86_64])',
-> Stacking execution will differ between mainnet and testnet in terms of cycle times and participation thresholds
In order to inform users about the upcoming reward cycle, we can use the following methods to obtain information for Stacking:
With the obtained PoX info, you can present whether Stacking has been executed in the next cycle, when the next cycle begins, the duration of a cycle, and the minimum microstacks required to participate:
At this point, your app shows Stacking details. If Stacking is executed and the user has enough funds, the user should be asked to provide input for the amount of microstacks to lockup and a bitcoin address to be used to pay out rewards.
-> The sample code used assumes usage of the bitcoin address associated with the Stacks account. You can replace this with an address provided by the users or read from your database. Read more about the [bitcoin address format](/understand-stacks/stacking#bitcoin-address).
At this point, your app shows Stacking details. If Stacking will be executed and the user has enough funds, the user should be asked to provide input for the amount of microstacks to lockup and a Bitcoin address to receive the pay out rewards.
With this input, and the data from previous steps, we can determine the eligibility for the next reward cycle:
```js
// microstacks tokens to lockup, must be >= poxInfo.min_amount_ustx and <=accountSTXBalance
let microstacksoLockup = poxInfo.min_amount_ustx;
// derive bitcoin address from Stacks account and convert into required format
If the user is eligible, the stacking action should be enabled on the UI. If not, the respective error message should be shown to the user.
-> Note that the eligibility check assumes the user will be stacking the maximum balance available in the account.
-> For more information on this read-only API call, please review the [API references](https://blockstack.github.io/stacks-blockchain-api/#operation/call_read_only_function)
-> The eligibility check is a read-only function call to the PoX smart contract which does not require broadcasting a transaction
## Step 5: Add stacking action
If the user is eligible, the stacking action should be enabled on the UI. If not, the respective error message should be shown to the user.
Next, the Stacking action should be implemented. Once the user confirms the action, a new transaction needs to be broadcasted to the network:
The transaction completion will take several minutes. Concurrent stacking actions should be disabled to ensure the user doesn't lock up more tokens as expected.
The transaction completion will take several minutes. Only one stacking transaction from each account/address is active at any time. Multiple/concurrent stacking actions from the same account will fail.
## Step 6: Confirm lock-up
@ -368,34 +283,24 @@ await sub.unsubscribe();
With the completed transactions, Stacks tokens are locked up for the lockup duration. During that time, your application can display the following details: unlocking time, amount of Stacks locked, and bitcoin address used for rewards.