Initialize a StackingClient to interact with the Stacking contract.
Note: The StackingClient sets its transactions AnchorMode to Any by default.
import { StacksTestnet, StacksMainnet } from '@stacks/network';import { StackingClient } from '@stacks/stacking';// for mainnet: const network = new StacksMainnet();const network = new StacksTestnet();// the stacks STX addressconst address = 'ST3XKKN4RPV69NN1PHFDNX3TYKXT7XPC4N8KC1ARH';const client = new StackingClient(address, network);
// STX address of the pool/poolconst delegateTo = 'ST2MCYPWTFMD2MGR5YY695EJG0G1R4J2BTJPRGM7H';// burn height at which the delegation relationship should be revoked (optional)const untilBurnBlockHeight = 5000;// how much to stack, in microSTXconst amountMicroStx = 100000000000n;// private key for transaction signingconst privateKey = 'd48f215481c16cbe6426f8e557df9b78895661971d71735126545abddcd5377001';const delegetateResponse = await client.delegateStx({ amountMicroStx, delegateTo, untilBurnBlockHeight, // optional privateKey,});// {// txid: '0xf6e9dbf6a26c1b73a14738606cb2232375d1b440246e6bbc14a45b3a66618481',// }
If you are a pool operator (or wish to stack with someone else's funds), you can stack ("lock up") tokens for your users and commit to stacking participation for upcoming reward cycles.
These users need to first "delegate" some or all of their funds to you (the "delegator").
The following examples refer to the "delegator" as pool, but in practice a delegator can also stack for only single or few individuals.
Even a group of friends could stack together and share a multi-sig BTC wallet for payouts.
Stack STX, which have been previously delegated to the pool.
This step only locks the funds (partial stacking).
The pool operator will also need to "commit" to a reward cycle.
import { getNonce } from '@stacks/transactions';import { StacksTestnet, StacksMainnet } from '@stacks/network';import { StackingClient } from '@stacks/stacking';// for mainnet: const network = new StacksMainnet();const network = new StacksTestnet();// the stacks STX addressconst address = 'ST3XKKN4RPV69NN1PHFDNX3TYKXT7XPC4N8KC1ARH';// pools would initiate a different clientconst poolAddress = 'ST22X605P0QX2BJC3NXEENXDPFCNJPHE02DTX5V74';// pool private key for transaction signingconst poolPrivateKey = 'd48f215481c16cbe6426f8e557df9b78895661971d71735126545abddcd5377001';// the BTC address for reward payoutsconst poolBtcAddress = 'msiYwJCvXEzjgq6hDwD9ueBka6MTfN962Z';// how much to stack, in microSTXconst amountMicroStx = 100000000000n;// block height at which to stackconst burnBlockHeight = 2000;// number cycles to stackconst cycles = 3;// if you call this method multiple times in the same block, you need to increase the nonce manuallylet nonce = await getNonce(poolAddress, network);nonce = nonce + 1n;const poolClient = new StackingClient(poolAddress, network);const delegetateStackResponses = await poolClient.delegateStackStx({ stacker: address, amountMicroStx, poxAddress: poolBtcAddress, burnBlockHeight, cycles, privateKey: poolPrivateKey, nonce, // optional});// {// txid: '0xf6e9dbf6a26c1b73a14738606cb2232375d1b440246e6bbc14a45b3a66618481',// }
Extend stacking of STX previously delegated to the pool.
import { getNonce } from '@stacks/transactions';import { StacksTestnet, StacksMainnet } from '@stacks/network';import { StackingClient } from '@stacks/stacking';// for mainnet: const network = new StacksMainnet();const network = new StacksTestnet();// the stacks STX addressconst address = 'ST3XKKN4RPV69NN1PHFDNX3TYKXT7XPC4N8KC1ARH';// pools would initiate a different clientconst poolAddress = 'ST22X605P0QX2BJC3NXEENXDPFCNJPHE02DTX5V74';// pool private key for transaction signingconst poolPrivateKey = 'd48f215481c16cbe6426f8e557df9b78895661971d71735126545abddcd5377001';// the BTC address for reward payoutsconst poolBtcAddress = 'msiYwJCvXEzjgq6hDwD9ueBka6MTfN962Z';// number of cycles to extend byconst extendCount = 3;// if you call this method multiple times in the same block, you need to increase the nonce manuallylet nonce = await getNonce(poolAddress, network);nonce = nonce + 1n;const poolClient = new StackingClient(poolAddress, network);const delegetateExtendResponses = await poolClient.delegateStackExtend({ extendCount, stacker: address, poxAddress: poolBtcAddress, privateKey: poolPrivateKey, nonce, // optional});// {// txid: '0xf6e9dbf6a26c1b73a14738606cb2232375d1b440246e6bbc14a45b3a66618481',// }
Increase the locked amount of delegated STX stacked.
import { getNonce } from '@stacks/transactions';import { StacksTestnet, StacksMainnet } from '@stacks/network';import { StackingClient } from '@stacks/stacking';// for mainnet: const network = new StacksMainnet();const network = new StacksTestnet();// the stacks STX addressconst address = 'ST3XKKN4RPV69NN1PHFDNX3TYKXT7XPC4N8KC1ARH';// pools would initiate a different clientconst poolAddress = 'ST22X605P0QX2BJC3NXEENXDPFCNJPHE02DTX5V74';// pool private key for transaction signingconst poolPrivateKey = 'd48f215481c16cbe6426f8e557df9b78895661971d71735126545abddcd5377001';// the BTC address for reward payoutsconst poolBtcAddress = 'msiYwJCvXEzjgq6hDwD9ueBka6MTfN962Z';// amount to increase by, in microSTXconst increaseBy = 3;// if you call this method multiple times in the same block, you need to increase the nonce manuallylet nonce = await getNonce(poolAddress, network);nonce = nonce + 1n;const poolClient = new StackingClient(poolAddress, network);const delegetateIncreaseResponses = await poolClient.delegateStackIncrease({ increaseBy, stacker: address, poxAddress: poolBtcAddress, privateKey: poolPrivateKey, nonce, // optional});// {// txid: '0xf6e9dbf6a26c1b73a14738606cb2232375d1b440246e6bbc14a45b3a66618481',// }