@stacks/stacking
Library for PoX Stacking.
Installation
npm install @stacks/stacking bn.js
Initialization
import { getNonce } from '@stacks/transactions';
import { StacksTestnet, StacksMainnet } from '@stacks/network';
import { StackingClient } from '@stacks/stacking';
import BN from 'bn.js';
const network = new StacksTestnet();
const client = new StackingClient(address, network);
const address = 'ST3XKKN4RPV69NN1PHFDNX3TYKXT7XPC4N8KC1ARH';
const poxAddress = 'mvuYDknzDtPgGqm2GnbAbmGMLwiyW3AwFP';
const cycles = 3;
const amountMicroStx = new BN(100000000000);
const privateKey = 'd48f215481c16cbe6426f8e557df9b78895661971d71735126545abddcd5377001';
const burnBlockHeight = 2000;
Check stacking eligibility
const stackingEligibility = await client.canStack({ poxAddress, cycles });
Stack STX
const stackingResults = await client.stack({
amountMicroStx,
poxAddress,
cycles,
privateKey,
burnBlockHeight,
});
Will Stacking be executed in the next cycle?
const stackingEnabledNextCycle = await client.isStackingEnabledNextCycle();
How long (in seconds) is a Stacking cycle?
const cycleDuration = await client.getCycleDuration();
How much time is left (in seconds) until the next cycle begins?
const secondsUntilNextCycle = await client.getSecondsUntilNextCycle();
Get PoX info
const poxInfo = await client.getPoxInfo();
Get Stacks node info
const coreInfo = await client.getCoreInfo();
Get account balance
const responseBalanceInfo = await client.getAccountBalance();
Does account have sufficient STX to meet minimum threshold?
const hasMinStxAmount = await client.hasMinimumStx();
Get account stacking status
const stackingStatus = await client.getStatus();
Delegation
There are four methods available for delegation, two for the delegators and two for the delegatee.
Delegatee
If you are the account owner ("stacker"), you can delegate or revoke delegation rights.
Delegate STX
const delegateTo = 'ST2MCYPWTFMD2MGR5YY695EJG0G1R4J2BTJPRGM7H';
const untilBurnBlockHeight = 5000;
const delegetateResponse = await client.delegateStx({
amountMicroStx,
delegateTo,
untilBurnBlockHeight,
privateKey,
});
Revoke delegation
const revokeResponse = await client.revokeDelegateStx(privateKey);
Delegator
If you are the delegator, you can stack ("lock up") tokens for your users and commit to stacking participation for upcoming reward cycles.
Stack delegated STX
const delegatorAddress = 'ST22X605P0QX2BJC3NXEENXDPFCNJPHE02DTX5V74';
const delegatorPrivateKey = 'd48f215481c16cbe6426f8e557df9b78895661971d71735126545abddcd5377001';
const delegatorBtcAddress = 'msiYwJCvXEzjgq6hDwD9ueBka6MTfN962Z';
let nonce = getNonce(delegatorAddress, network);
nonce = nonce.add(new BN(1));
const delegatorClient = new StackingClient(delegatorAddress, network);
const delegetateStackResponses = await delegatorClient.delegateStackStx({
stacker: address,
amountMicroStx,
poxAddress: delegatorBtcAddress,
burnBlockHeight,
cycles,
privateKey: delegatorPrivateKey,
nonce,
});
Commit to stacking
const rewardCycle = 12;
const delegetateCommitResponse = await delegatorClient.stackAggregationCommit({
poxAddress: delegatorBtcAddress,
rewardCycle,
privateKey: privateKeyDelegate,
});