Skip to content

Commit b7f0ed3

Browse files
feat: add SIP-044 staking, pox post-conditions, clarity 6 (#1869)
* feat: add SIP-044 staking and pox post-conditions * feat: add ClarityVersion.Clarity6 * style: oxfmt wrap in postcondition.ts * refactor: rename Pc .stake() to .ustxToLock() * docs: clarify staking/pox postcondition field docs
1 parent 494487c commit b7f0ed3

12 files changed

Lines changed: 481 additions & 3 deletions

File tree

‎.changeset/legal-wasps-shout.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@stacks/transactions': minor
3+
---
4+
5+
Add SIP-044 staking (`0x03`) and PoX (`0x04`) post-conditions, including `Pc` builder support (`.stake()`, `.willPerformPox()`, `.willNotPerformPox()`, `.mayPerformPox()`).

‎.changeset/tired-dolls-like.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@stacks/transactions': minor
3+
---
4+
5+
Add `ClarityVersion.Clarity6` (enabled with Epoch 4.0).

‎packages/transactions/README.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,38 @@ const contractNonFungiblePostCondition = makeContractNonFungiblePostCondition(
501501
);
502502
```
503503

504+
### Staking post condition (SIP-044)
505+
506+
Guards staking STX (or modifying staked STX) for a principal — e.g. pox-5
507+
`stake`, `register-for-bond`, `stake-update`. Uses the same comparators as the
508+
STX post condition; amounts are denoted in uSTX.
509+
510+
```typescript
511+
import { Pc } from '@stacks/transactions';
512+
513+
const stakingPostCondition = Pc.principal(
514+
'SP2ZD731ANQZT6J4K3F5N8A40ZXWXC1XFXHVVQFKE'
515+
)
516+
.willSendGte(1000000)
517+
.ustxToLock();
518+
```
519+
520+
### PoX post condition (SIP-044)
521+
522+
Guards PoX state changes that do not alter locking status — e.g. pox-5
523+
`unstake`, `unstake-sbtc`, `update-bond-registration`, `announce-l1-early-exit`.
524+
Carries only a principal and one of three condition codes.
525+
526+
```typescript
527+
import { Pc } from '@stacks/transactions';
528+
529+
const principal = 'SP2ZD731ANQZT6J4K3F5N8A40ZXWXC1XFXHVVQFKE';
530+
531+
Pc.principal(principal).willPerformPox(); // principal will perform a gated PoX action
532+
Pc.principal(principal).willNotPerformPox(); // principal will not perform any gated PoX action
533+
Pc.principal(principal).mayPerformPox(); // principal may or may not (always passes)
534+
```
535+
504536
## Helper functions
505537

506538
### Conversion of Clarity Values to JSON

‎packages/transactions/src/constants.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ export enum ClarityVersion {
5353
Clarity3 = 3,
5454
Clarity4 = 4,
5555
Clarity5 = 5,
56+
/**
57+
* `Clarity6` enabled starting with Epoch 4.0
58+
*
59+
* @see [SIP-044](https://github.com/stacksgov/sips)
60+
*/
61+
Clarity6 = 6,
5662
}
5763

5864
/**
@@ -127,6 +133,16 @@ export enum PostConditionType {
127133
STX = 0x00,
128134
Fungible = 0x01,
129135
NonFungible = 0x02,
136+
/**
137+
* `Staking` (SIP-044) — Guards staking STX (or modifying staked STX) for a principal.
138+
* Mirrors the {@link PostConditionType.STX} shape (principal + {@link FungibleConditionCode} + amount).
139+
*/
140+
Staking = 0x03,
141+
/**
142+
* `PoX` (SIP-044) — Guards PoX state changes that do not alter locking status.
143+
* Carries a principal and a {@link PoxConditionCode}.
144+
*/
145+
PoX = 0x04,
130146
}
131147

132148
/**
@@ -208,6 +224,19 @@ export enum NonFungibleConditionCode {
208224
MaybeSent = 0x12,
209225
}
210226

227+
/**
228+
* The type of PoX post-condition comparison (SIP-044).
229+
* Used for serializing PoX post-conditions.
230+
*/
231+
export enum PoxConditionCode {
232+
/** `WillNotPerform` — The principal must not perform a gated PoX action. */
233+
WillNotPerform = 0x30,
234+
/** `MayPerform` — The principal may or may not perform a gated PoX action (always passes). */
235+
MayPerform = 0x31,
236+
/** `WillPerform` — The principal must perform a gated PoX action. */
237+
WillPerform = 0x32,
238+
}
239+
211240
/**
212241
* The type of sender for a post-condition.
213242
*/

‎packages/transactions/src/pc.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
NonFungibleComparator,
77
NonFungiblePostCondition,
88
PostCondition,
9+
PoxPostCondition,
10+
StakingPostCondition,
911
StxPostCondition,
1012
} from './postcondition-types';
1113
import { AddressString, AssetString, ContractIdString } from './types';
@@ -179,6 +181,48 @@ class PartialPcWithPrincipal {
179181
willMaybeSendAsset() {
180182
return new PartialPcNftWithCode(this.address, 'maybe-sent');
181183
}
184+
185+
/**
186+
* ### PoX Post Condition (SIP-044)
187+
* A post-condition asserting the principal will perform a gated PoX action
188+
* (`PoxConditionCode.WillPerform`).
189+
* @example
190+
* ```
191+
* import { Pc } from '@stacks/transactions';
192+
* Pc.principal('STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6').willPerformPox();
193+
* ```
194+
*/
195+
willPerformPox(): PoxPostCondition {
196+
return { type: 'pox-postcondition', address: this.address, condition: 'will-perform' };
197+
}
198+
199+
/**
200+
* ### PoX Post Condition (SIP-044)
201+
* A post-condition asserting the principal will not perform any gated PoX
202+
* action (`PoxConditionCode.WillNotPerform`).
203+
* @example
204+
* ```
205+
* import { Pc } from '@stacks/transactions';
206+
* Pc.principal('STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6').willNotPerformPox();
207+
* ```
208+
*/
209+
willNotPerformPox(): PoxPostCondition {
210+
return { type: 'pox-postcondition', address: this.address, condition: 'will-not-perform' };
211+
}
212+
213+
/**
214+
* ### PoX Post Condition (SIP-044)
215+
* A post-condition where the principal may or may not perform a gated PoX
216+
* action (`PoxConditionCode.MayPerform`; always passes).
217+
* @example
218+
* ```
219+
* import { Pc } from '@stacks/transactions';
220+
* Pc.principal('STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6').mayPerformPox();
221+
* ```
222+
*/
223+
mayPerformPox(): PoxPostCondition {
224+
return { type: 'pox-postcondition', address: this.address, condition: 'may-perform' };
225+
}
182226
}
183227

184228
/**
@@ -225,6 +269,20 @@ class PartialPcFtWithCode {
225269
asset: `${contractId}::${tokenName}`,
226270
};
227271
}
272+
273+
/**
274+
* ### Staking Post Condition (SIP-044)
275+
* Guards staking STX (or modifying staked STX) for the principal.
276+
* ⚠ Amount of STX is denoted in uSTX (micro-STX)
277+
*/
278+
ustxToLock(): StakingPostCondition {
279+
return {
280+
type: 'staking-postcondition',
281+
address: this.address,
282+
condition: this.code,
283+
amount: parsePostConditionAmount(this.amount).toString(),
284+
};
285+
}
228286
}
229287

230288
/**

‎packages/transactions/src/postcondition-types.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,53 @@ export type NonFungiblePostCondition = {
5151
assetId: ClarityValue;
5252
};
5353

54-
export type PostCondition = StxPostCondition | FungiblePostCondition | NonFungiblePostCondition;
54+
/**
55+
* Staking post-condition (SIP-044).
56+
*
57+
* Guards staking STX (or modifying staked STX) for a principal — e.g. pox-5
58+
* `stake`, `register-for-bond`, `stake-update`. Uses the same fungible
59+
* comparators as {@link StxPostCondition}.
60+
*/
61+
export interface StakingPostCondition {
62+
type: 'staking-postcondition';
63+
/** Address staking the STX (principal `address`, `contract-id`, or `'origin'`) */
64+
address: string;
65+
/** Comparator to check the amount to be staked (`eq`, `gt`, `gte`, `lt`, `lte`) */
66+
condition: `${FungibleComparator}`;
67+
/** `BigInt` compatible amount to be checked in post-condition */
68+
amount: string | bigint | number;
69+
}
70+
71+
/**
72+
* The type of PoX post-condition comparison (SIP-044).
73+
*
74+
* - `will-not-perform`: The principal must not perform a gated PoX action.
75+
* - `may-perform`: The principal may or may not perform a gated PoX action (always passes).
76+
* - `will-perform`: The principal must perform a gated PoX action.
77+
*/
78+
export type PoxComparator = 'will-not-perform' | 'may-perform' | 'will-perform';
79+
80+
/**
81+
* PoX post-condition (SIP-044).
82+
*
83+
* Guards PoX state changes that do not alter locking status — e.g. pox-5
84+
* `unstake`, `unstake-sbtc`, `update-bond-registration`,
85+
* `announce-l1-early-exit`.
86+
*/
87+
export interface PoxPostCondition {
88+
type: 'pox-postcondition';
89+
/** Address whose PoX actions are constrained (principal `address`, `contract-id`, or `'origin'`) */
90+
address: string;
91+
/** Comparator constraining PoX actions (`will-not-perform`, `may-perform`, `will-perform`) */
92+
condition: `${PoxComparator}`;
93+
}
94+
95+
export type PostCondition =
96+
| StxPostCondition
97+
| FungiblePostCondition
98+
| NonFungiblePostCondition
99+
| StakingPostCondition
100+
| PoxPostCondition;
55101

56102
/**
57103
* Describes how unspecified asset transfers are handled in a transaction:

‎packages/transactions/src/postcondition.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import {
55
PostConditionMode,
66
PostConditionPrincipalId,
77
PostConditionType,
8+
PoxConditionCode,
89
} from './constants';
910
import {
1011
FungibleComparator,
1112
NonFungibleComparator,
1213
PostCondition,
1314
PostConditionModeName,
15+
PoxComparator,
1416
} from './postcondition-types';
1517
import { AssetString } from './types';
1618
import {
@@ -50,6 +52,13 @@ enum PostConditionCodeWireType {
5052
'maybe-sent' = NonFungibleConditionCode.MaybeSent,
5153
}
5254

55+
/** @internal */
56+
enum PoxConditionCodeWireType {
57+
'will-not-perform' = PoxConditionCode.WillNotPerform,
58+
'may-perform' = PoxConditionCode.MayPerform,
59+
'will-perform' = PoxConditionCode.WillPerform,
60+
}
61+
5362
export function postConditionToWire(postcondition: PostCondition): PostConditionWire {
5463
switch (postcondition.type) {
5564
case 'stx-postcondition':
@@ -87,6 +96,27 @@ export function postConditionToWire(postcondition: PostCondition): PostCondition
8796
asset: parseAssetString(postcondition.asset),
8897
assetName: postcondition.assetId,
8998
};
99+
case 'staking-postcondition':
100+
return {
101+
type: StacksWireType.PostCondition,
102+
conditionType: PostConditionType.Staking,
103+
principal:
104+
postcondition.address === 'origin'
105+
? { type: StacksWireType.Principal, prefix: PostConditionPrincipalId.Origin }
106+
: parsePrincipalString(postcondition.address),
107+
conditionCode: conditionTypeToByte(postcondition.condition) as FungibleConditionCode,
108+
amount: parsePostConditionAmount(postcondition.amount),
109+
};
110+
case 'pox-postcondition':
111+
return {
112+
type: StacksWireType.PostCondition,
113+
conditionType: PostConditionType.PoX,
114+
principal:
115+
postcondition.address === 'origin'
116+
? { type: StacksWireType.Principal, prefix: PostConditionPrincipalId.Origin }
117+
: parsePrincipalString(postcondition.address),
118+
conditionCode: poxConditionTypeToByte(postcondition.condition),
119+
};
90120
default:
91121
throw new Error('Invalid post condition type');
92122
}
@@ -117,6 +147,19 @@ export function wireToPostCondition(wire: PostConditionWire): PostCondition {
117147
asset: assetWireToString(wire.asset),
118148
assetId: wire.assetName,
119149
};
150+
case PostConditionType.Staking:
151+
return {
152+
type: 'staking-postcondition',
153+
address: principalWireToString(wire.principal),
154+
condition: conditionByteToType(wire.conditionCode),
155+
amount: wire.amount.toString(),
156+
};
157+
case PostConditionType.PoX:
158+
return {
159+
type: 'pox-postcondition',
160+
address: principalWireToString(wire.principal),
161+
condition: poxConditionByteToType(wire.conditionCode),
162+
};
120163
default: {
121164
const _exhaustiveCheck: never = wire;
122165
throw new Error(`Invalid post condition type: ${_exhaustiveCheck}`);
@@ -149,6 +192,19 @@ export function conditionByteToType<T extends FungibleConditionCode | NonFungibl
149192
)[wireType];
150193
}
151194

195+
/** @internal */
196+
export function poxConditionTypeToByte(condition: PoxComparator): PoxConditionCode {
197+
return (PoxConditionCodeWireType as unknown as Record<PoxComparator, PoxConditionCode>)[
198+
condition
199+
];
200+
}
201+
202+
/** @internal */
203+
export function poxConditionByteToType(wireType: PoxConditionCode): PoxComparator {
204+
// numerical enums are bidirectional in TypeScript
205+
return (PoxConditionCodeWireType as unknown as Record<PoxConditionCode, PoxComparator>)[wireType];
206+
}
207+
152208
/**
153209
* Convert a post condition to a hex string
154210
* @param postcondition - The post condition object to convert

‎packages/transactions/src/wire/serialization.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
PayloadType,
3535
PostConditionPrincipalId,
3636
PostConditionType,
37+
PoxConditionCode,
3738
PubKeyEncoding,
3839
RECOVERABLE_ECDSA_SIG_LENGTH_BYTES,
3940
TenureChangeCause,
@@ -369,7 +370,8 @@ export function serializePostConditionWireBytes(postCondition: PostConditionWire
369370

370371
if (
371372
postCondition.conditionType === PostConditionType.STX ||
372-
postCondition.conditionType === PostConditionType.Fungible
373+
postCondition.conditionType === PostConditionType.Fungible ||
374+
postCondition.conditionType === PostConditionType.Staking
373375
) {
374376
// SIP-005: Maximal length of amount is 8 bytes
375377
if (postCondition.amount > BigInt('0xffffffffffffffff'))
@@ -436,6 +438,29 @@ export function deserializePostConditionWire(
436438
asset,
437439
assetName,
438440
};
441+
case PostConditionType.Staking:
442+
conditionCode = bytesReader.readUInt8Enum(FungibleConditionCode, n => {
443+
throw new DeserializationError(`Could not read ${n} as FungibleConditionCode`);
444+
});
445+
amount = BigInt(`0x${bytesToHex(bytesReader.readBytes(8))}`);
446+
return {
447+
type: StacksWireType.PostCondition,
448+
conditionType: PostConditionType.Staking,
449+
principal,
450+
conditionCode,
451+
amount,
452+
};
453+
case PostConditionType.PoX: {
454+
const poxConditionCode = bytesReader.readUInt8Enum(PoxConditionCode, n => {
455+
throw new DeserializationError(`Could not read ${n} as PoxConditionCode`);
456+
});
457+
return {
458+
type: StacksWireType.PostCondition,
459+
conditionType: PostConditionType.PoX,
460+
principal,
461+
conditionCode: poxConditionCode,
462+
};
463+
}
439464
}
440465
}
441466

0 commit comments

Comments
 (0)