Build an FT post-condition
Create post-conditions for fungible token transfers to ensure exact amounts are transferred as expected
import { Pc } from '@stacks/transactions';// Create a post-condition for fungible token transfersconst postCondition = Pc.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM").willSendGte(500) // Amount in token's smallest unit.ft("ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-ft", "my-token");// Use in transaction optionsconst txOptions = {// ... other transaction optionspostConditions: [postCondition],postConditionMode: PostConditionMode.Deny,};
Use cases
- Securing fungible token transfers with amount validation
- Protecting users from unexpected token transfers in DeFi protocols
- Ensuring token swaps happen with expected amounts
Key concepts
The Pc
builder for fungible tokens accepts:
.ft()
- Takes two parameters:- Contract address with asset name (e.g.,
"contract.asset-name"
) - Token name as defined in the contract
- Contract address with asset name (e.g.,
Post-condition comparison types
// Greater than or equalconst gtePC = Pc.principal(address).willSendGte(500).ft(contract, token);// Less than or equalconst ltePC = Pc.principal(address).willSendLte(1000).ft(contract, token);// Exact amountconst exactPC = Pc.principal(address).willSendEq(750).ft(contract, token);// Greater thanconst gtPC = Pc.principal(address).willSendGt(100).ft(contract, token);// Less thanconst ltPC = Pc.principal(address).willSendLt(2000).ft(contract, token);
Complete example
import {AnchorMode,FungibleConditionCode,makeContractFungiblePostCondition,makeStandardFungiblePostCondition,Pc,PostConditionMode,} from '@stacks/transactions';// Using the Pc builderconst ftPostCondition = Pc.principal(senderAddress).willSendEq(1000).ft(contractAddress + '.sip010-token', 'wrapped-btc');// Alternative: Using the standard functionconst altPostCondition = makeStandardFungiblePostCondition(senderAddress,FungibleConditionCode.Equal,1000n,createAssetInfo(contractAddress, 'sip010-token', 'wrapped-btc'));
Package installation
Terminal
$npm install @stacks/transactions