Stacking
Stacking is implemented as a smart contract using Clarity. You can always find the Stacking contract identifier using the Stacks Blockchain API v2/pox
endpoint.
Currently stacking uses the pox-4 contract. The deployed pox-4 contract and included comments can be viewed in the explorer.
In this walkthrough, we'll cover the entire stacking contract from start to finish, including descriptions of the various functions and errors, and when you might use/encounter them.
If you are running into a specific stacking error, jump to the errors section.
Rather than walking through the contract line by line, which you can do by simply reading the contract code and the comments, we'll instead explore it from the perspective of conducting stacking operations, including solo stacking, delegating, and running a pool.
At the bottom you will find a list of some errors you may run into and their explanations.
There are a few utilities that make interacting with this contract easier including Lockstacks as a UI and the @stacks/stacking package for a JS library.
Hiro has a detailed guide available for stacking using this library as well as a Nakamoto guide specifically for the additions made to work with pox-4
.
Prerequisites
If you are not familiar with stacking as a concept, it will be useful to familiarize yourself with that first before diving into the contract.
Solo Stacking
Solo stacking is the simplest option, and begins by calling the stack-stx
function.
stack-stx
This function locks up the given amount of STX for the given lock period (number of reward cycles) for the tx-sender
.
Here's the full code for that function, then we'll dive into how it works below that.
First let's cover the needed parameters.
amount-ustx
is the amount of STX you would like to lock, denoted in micro-STX, or uSTX (1 STX = 1,000,000 uSTX)pox-addr
is a tuple that encodes the Bitcoin address to be used for the PoX rewards, details belowstart-burn-ht
is the Bitcoin block height you would like to begin stacking. You will receive rewards in the reward cycle followingstart-burn-ht
. Importantly,start-burn-ht
may not be further into the future than the next reward cycle, and in most cases should be set to the current burn block height.lock-period
sets the number of reward cycles you would like you lock your STX for, this can be 1-12signer-sig
is a unique generatedf signature that proves ownership of this signer. Further details for its role and how to generate it can be found in the How to Stack docsigner-key
is the public key of your signer, more details in the How to Run a Signer docmax-amount
sets the maximum amount allowed to be stacked during the provided stacking periodauth-id
is a unique string to prevent re-use of this stacking transaction
It's important to make sure that these fields match what you pass in to the signer signature generation. If they don't, you will likely get error 35 (ERR_INVALID_SIGNATURE_PUBKEY
)
when trying to submit this transaction as the signer signature will not be valid.
Supported Reward Address Types
For the pox-addr
field, the version
buffer must represent what kind of bitcoin address is being submitted. These are all the possible values you can pass here depending on your address type:
The hashbytes
are the 20 hash bytes of the bitcoin address. You can obtain that from a bitcoin library, for instance using bitcoinjs-lib
:
The first thing the stack-stx
function will do perform several checks including:
The
start-burn-ht
results in the next reward cycleThe function is being called by the
tx-sender
or an allowed contract callerThe
tx-sender
is not currently stacking or delegatingThe
tx-sender
has enough fundsThe given
signer-key
is valid, proving ownershipStacking can be performed. This is determined through additional checks including if the amount meets the minimum stacking threshold and if the lock period and provided Bitcoin address are valid
You can explore the private functions that handle these checks to see exactly how they do so.
Next we register the provided PoX address for the next reward cycle, assign its specific reward slot, and add it to the stacking-state
map, which keeps track of all current stackers per reward cycle.
Finally we return the lock up information so the node can carry out the lock by reading this information. This step is what actually locks the STX and prevents the stacker from using them on-chain.
From here, the locked STX tokens will be unlocked automatically at the end of the lock period.
The other option is that the stacker can call the stack-increase
or stack-extend
functions to either increase the amount of STX they have locked or increase the amount of time to lock them, respectively.
Delegated Stacking
Delegated stacking has a few additional steps to it. It is essentially a three-step process:
Delegator delegates their STX to a pool operator
Pool operator stacks their delegated STX
Pool operator commits an aggregate of all STX delegated to them
There are also a few alternative steps here depending on the action you want to take.
Revoke delegation
Each of these steps has a corresponding function. Let's dig into them.
delegate-stx
This function is called by the individual stacker delegating their STX to a pool operator. An individual stacker choosing to delegate does not need to run their own signer.
This function does not actually lock the STX, but just allows the pool operator to issue the lock.
This function takes a few parameters:
amount-ustx
is the amount the stacker is delegating denoted in uSTXdelegate-to
is the Stacks address of the pool operator (or delegate) being delegated tountil-burn-ht
is an optional parameter that describes when this delegation expirespox-addr
is an optional Bitcoin address. If this is provided, the pool operator must send rewards to this address. If it is not provided, it is up to the discretion of the pool operator where to send the rewards.
It first runs through a few checks to ensure that the function caller is allowed, the provided pox-addr
is valid, and that the stacker is not already delegating.
Finally it updates the delegation-state
of the contract with the details.
At this point, no STX have been locked. The pool operator next needs to call the delegate-stack-stx
function in order to partially lock the STX.
delegate-stack-stx
At this point, the stacker has given their permission to the pool operator to delegate their STX on their behalf.
The delegation is now in the hands of the pool operator to stack these delegated STX using the delegate-stack-stx
function.
This function can only be called after a stacker has called their respective delegate-stx
function and must be called by the pool operator for each instance of a stacker calling delegate-stx
.
Like the other functions, this function takes in several parameters and runs through several checks before updating the contract state.
stacker
is the principal of the stacker who has delegated their STXamount-ustx
is the amount they have delegated in uSTXpox-addr
is the Bitcoin address the rewards will be sent to. If the stacker passed this field in to theirdelegate-stx
function, this must be the same valuestart-burn-ht
corresponds to the field passed in by the stackerlock-period
corresponds to the same field that the stacker passed in
Now we assign a few variables using let
before running several checks.
first-reward-cycle
is set to the next reward cycle automaticallyspecified-reward-cycle
is the reward cycle that the passed-instart-burn-ht
parameter falls withinunlock-burn-height
is the Bitcoin block height at which the stackers STX will be unlocked
Now we proceed to run through some checks including:
The first reward cycle is the same as the specified reward cycle
The function is being called by the
tx-sender
or an approved contractThat the stacker actually delegated to the contract caller
Then we get the information from the delegation and make sure the information we are passing here matches it
That the stacker is not currently stacking
They have sufficient unlocked funds
Run the
minimal-can-stack-stx
to run a few additional checks to make sure stacking can occur
Next we call a function called add-pox-partial-stacked
which will add this stacker to a partial-stacked-by-cycle
map.
After those checks and partial stacking, we update the stacking-state
map and return the information for the node to process.
At this point this stacker's STX are considered partially stacked. We still need to perform one more step as the pool operator in order to officially lock them.
stack-aggregation-commit
The stack-aggregation-commit
function is just a wrapper for the private inner-stack-aggregation-commit
function, so that is the source code included here.
This function contains many of the same parameters as the stack-stx
function, with a few changes:
pox-addr
is a tuple that encodes the Bitcoin address to be used for the PoX rewards, details belowreward-cycle
is the reward cycle that these delegated STX will be stacked insigner-sig
is a unique generated signature that proves ownership of this signer. Further details for its role and how to generate it can be found in the How to Stack docsigner-key
is the public key of your signer, more details in the How to Run a Signer docmax-amount
sets the maximum amount allowed to be stacked during the provided stacking periodauth-id
is a unique string to prevent re-use of this stacking transaction
It's important to make sure that these fields match what you pass in to the signer signature generation. If they don't, you will likely get error 35 (ERR_INVALID_SIGNATURE_PUBKEY
) when trying to submit this transaction as the signer signature will not be valid.
The first thing we do is pull the partially stacked STX that have been added to the contract state via our delegate-stack-stx
calls.
Next we run through many of the same checks as in previous functions:
Making sure the contract caller is allowed
Making sure the signer signature is valid
Making sure we can actually perform this stacking operation
Now we take all of the delegated STX and actually add it to the reward cycle. At this point the pool operator has a reward slot in the chosen cycle.
We don't need to update the stacking state because we already did that in the delegate-stack-stx
function.
All we need to do is delete and log the partially stacked STX state.
How Stacking Reward Distribution Works
All of the above stacking functions take in a pox-reward
field that corresponds to a Bitcoin address where BTC rewards will be sent. It's important to understand how these addresses are used and how reward distribution is handled in general.
How Bitcoin rewards are distributed is primarily up to the discretion of the pool operator. Since PoX reward distributions are handled using Bitcoin transactions, there is currently not an effective way to automate their distribution to individual delegated stackers.
Let's go over the role of pox-addr
in each function and how it should be used.
stack-stx
This is the simplest option and simply corresponds to the Bitcoin address that the stacker would like to receive their rewards.
delegate-stx
In this function, which is the one that the delegator will be calling to give permission to the pool operator to stack on their behalf, the pox-addr
argument is optional.
If no pox-addr
argument is not passed in, the pool operator determines where this delegator's rewards are sent.
If a pox-addr
is passed in, then rewards must be distributed to that address. However, if this is passed in, the delegator must have enough STX to meet the minimum stacking amount.
The reason is because there are a finite amount of reward slots (4,000) and each pox-addr
takes up one of these reward slots.
Stackers need to be able to stack the minimum in order to be eligible for one of these reward slots. A delegator may choose to delegate to a pool (even if they meet the minimum stacking requirement) if they do not want to handle the infrastructure of running a signer or the actual stacking operations, which is why this option exists.
delegate-stack-stx and stack-aggregation-commit
In both of these functions, pox-addr
corresponds to the address where the pool operator would like the rewards to be sent.
At this point, it is up to the pool operator to determine how to allocate rewards. In most cases, a pool operator will use a wrapper contract in order to transparently track this information on-chain, and manually send rewards out to participants according to the proportion that they delegated.
Errors
You may encounter several errors when trying to perform stacking operations. We won't cover them all in detail here, as you can see the error in the failed transaction and trace the source code to find it.
But we will cover some of the more common errors you may encounter, what they mean, and how to resolve them.
Error 35 - ERR_INVALID_SIGNATURE_PUBKEY
ERR_INVALID_SIGNATURE_PUBKEY
This is likely the most common error you will encounter, and you'll usually see it in a failed stack-stx
or stack-aggregation-commit
transaction.
This error actually occurs in the consume-signer-key-authorization
which is called any time we pass in a signer signature.
This means one of two things:
The public key you used to generate the signer signature is not the same as the one you are passing in to the
signer-key
fieldOne of the fields you passed in to generate your signer signature does not match the field you passed in to either the
stack-stx
orstack-aggregation-commit
function
To fix this, check all of the data you passed in to see where the mismatch is.
Error 4 - ERR_STACKING_NO_SUCH_PRINCIPAL
The stacking contract looks up partially stacked stx (after you have called delegate-stack-stx
) with the lookup key (pox-addr, stx-address, reward-cycle
. This error means that either when you generated your signature or called the stack-aggregation-commit
function, you passed in the wrong parameter for one of these. More information in the stacking guide.
Error 24 -
24
: the start-burn-height
param was invalid
Last updated