Working with Post-Conditions

Ryan Waits,stacksstacks.jspost conditions

Note: This guide assumes you have a basic understanding of post conditions in Stacks transactions. If you're new to post conditions, we recommend reading the Intro to Post Conditions before proceeding.

Overview of Post Conditions

Post conditions in Stacks transactions come in various flavors, each serving a unique purpose and requiring different arguments. Here's a quick rundown:

  1. STX Post Conditions: These involve the transfer of STX tokens. They can be standard or contract-based.
  2. Fungible Token Post Conditions: These deal with fungible tokens (identical and interchangeable), like an ERC-20 token in Ethereum.
  3. NFT Post Conditions: These are about non-fungible tokens (unique), similar to ERC-721 tokens in Ethereum.

Remember, the devil is in the details. So, let's dive in!

STX Post Conditions

To create a STX post condition, use makeStandardSTXPostCondition.

The arguments are:

  • address: this is the address of the sending principal (the address that will be sending the STX)
  • conditionCode: this is the condition, represented by an integer, that must be met for the transaction to be valid.
  • amount: the amount of STX to be sent (represented in uSTX)

import {
FungibleConditionCode,
makeStandardSTXPostCondition,
} from "@stacks/transactions";
const address = "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM";
const conditionCode = FungibleConditionCode.Equal;
const amount = 42000000; // 42 STX
const postConditions = [
makeStandardSTXPostCondition(
address,
conditionCode,
amount
),
];

Once you have a Post-Condition setup, you can introduce it into your transaction by using makeSTXTokenTransfer or makeContractCall.

Use with makeSTXTokenTransfer

You can use post conditions with makeSTXTokenTransfer by passing the postConditions array as an argument.


import { makeSTXTokenTransfer, broadcastTransaction, AnchorMode } from '@stacks/transactions';
const txOptions = {
recipient: 'ST2ST2H80NP5C9SPR4ENJ1Z9CDM9PKAJVPYWPQZ50',
...
postConditions: postConditions,
};
const transaction = await makeSTXTokenTransfer(txOptions);
const broadcastResponse = await broadcastTransaction(transaction);
const txId = broadcastResponse.txid;

Use with makeContractCall

You can use post conditions with makeContractCall - when interacting with a smart contract - by passing the postConditions array as an argument.

For this example, let's assume we have a smart contract called vault that has a function called send-stx that takes a principal and uint as an argument.


import {
makeContractCall,
broadcastTransaction,
principalCV,
uintCV,
AnchorMode
} from '@stacks/transactions';
// Define the recipient's address in Clarity Value format
const to = principalCV("ST2ST2H80NP5C9SPR4ENJ1Z9CDM9PKAJVPYWPQZ50");
// Define the amount to be transferred in Clarity Value format
const amount = uintCV(42000000);
// Define the network to be used, in this case, it's the Stacks Devnet
const network = new StacksDevnet();
const txOptions = {
contractAddress: 'ST2ST2H80NP5C9SPR4ENJ1Z9CDM9PKAJVPYWPQZ50'
contractName: 'vault',
functionName: 'send-stx',
functionArgs: [to, amount],
senderKey,
network,
postConditions,
anchorMode: AnchorMode.Any,
};
const transaction = await makeContractCall(txOptions);
const broadcastResponse = await broadcastTransaction(transaction);
const txId = broadcastResponse.txid;

Resources

© By Ryan Waits 2024