Transfer STX
Send STX tokens between addresses with post-conditions for secure transfers
import { STACKS_TESTNET } from "@stacks/network";import {AnchorMode,broadcastTransaction,makeSTXTokenTransfer,Pc,PostConditionMode,} from "@stacks/transactions";// Define sender and recipientconst senderAddress = "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM";const recipientAddress = "ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5";// Create post-condition to ensure exact amount is sentconst postConditions = Pc.principal(senderAddress).willSendEq(1000000) // 1 STX in micro-STX.ustx();// Configure transaction optionsconst txOptions = {recipient: recipientAddress,amount: 1000000, // 1 STX in micro-STXsenderKey: "753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601",network: STACKS_TESTNET,memo: "Transfer memo", // Optional memo fieldpostConditions: [postConditions],postConditionMode: PostConditionMode.Deny,anchorMode: AnchorMode.Any,};// Create and broadcast the transactionconst transaction = await makeSTXTokenTransfer(txOptions);const broadcastResponse = await broadcastTransaction({ transaction });console.log("Transaction ID:", broadcastResponse.txid);
Use cases
- Transferring STX tokens securely between addresses
- Validating transaction amounts for secure transfers
Key concepts
- Post-conditions: Ensure the exact amount of STX is transferred