Asset Expressions
This guide covers how to work with blockchain assets and values in Tx3.
Overview
Section titled “Overview”Tx3 provides built-in support for working with blockchain assets:
- Native currency (ADA)
- Custom tokens
- NFTs
- Multi-asset bundles
Asset Definitions
Section titled “Asset Definitions”Native Asset (ADA)
Section titled “Native Asset (ADA)”// ADA is built-inAda(1000000) // 1 ADAAda(500000) // 0.5 ADA
Custom Assets
Section titled “Custom Assets”// Define custom assetasset MyToken = "policy_id" "asset_name";
// Use custom assetMyToken(100)
AnyAsset Catch-All Constructor
Section titled “AnyAsset Catch-All Constructor”// Define custom asset and amountAnyAsset("policy_id", "asset_name", <amount_int>)
// Define NFTAnyAsset("policy_id", "asset_name", 1)
Asset Bundles
Section titled “Asset Bundles”// Combine multiple assetsAda(1000000) + MyToken(50)
// Complex bundlesAda(1000000) + MyToken(50) + AnyAsset("policy_id", "asset_name", 1)
Asset Expressions
Section titled “Asset Expressions”Basic Operations
Section titled “Basic Operations”// Additionasset1 + asset2
// Subtractionasset1 - asset2
// Property accessasset.amount
Examples
Section titled “Examples”// Simple transfertx transfer(amount: Int) { input source { from: Sender, min_amount: Ada(amount), }
output { to: Receiver, amount: Ada(amount), }}
// Multi-asset transfertx transfer_multi( ada_amount: Int, token_amount: Int) { input source { from: Sender, min_amount: Ada(ada_amount) + MyToken(token_amount), }
output { to: Receiver, amount: Ada(ada_amount) + MyToken(token_amount), }}
Asset Validation
Section titled “Asset Validation”Minimum Amounts
Section titled “Minimum Amounts”input source { from: Sender, min_amount: Ada(1000000), // At least 1 ADA}
Exact Amounts
Section titled “Exact Amounts”output { to: Receiver, amount: Ada(1000000), // Exactly 1 ADA}
Asset Combinations
Section titled “Asset Combinations”input source { from: Sender, min_amount: Ada(1000000) + MyToken(50), // Multiple assets}
Common Patterns
Section titled “Common Patterns”Token Swap
Section titled “Token Swap”tx swap( input_amount: Int, output_amount: Int) { input source { from: User, min_amount: TokenA(input_amount), }
output { to: User, amount: TokenB(output_amount), }}
NFT Transfer
Section titled “NFT Transfer”tx transfer_nft( policy_id: Bytes, asset_name: Bytes) { input source { from: Owner, min_amount: AnyAsset(policy_id, asset_name, 1), }
output { to: Recipient, amount: AnyAsset(policy_id, asset_name, 1), }}
### FT Transfer```tx3tx transfer_nft( policy_id: Bytes, asset_name: Bytes, amount: Int) { input source { from: Owner, min_amount: AnyAsset(policy_id, asset_name, amount), }
output { to: Recipient, amount: AnyAsset(policy_id, asset_name, amount), }}
Liquidity Pool
Section titled “Liquidity Pool”tx add_liquidity( ada_amount: Int, token_amount: Int) { input ada { from: User, min_amount: Ada(ada_amount), }
input token { from: User, min_amount: MyToken(token_amount), }
output { to: Pool, amount: Ada(ada_amount) + MyToken(token_amount), }}
Common Use Cases
Section titled “Common Use Cases”Token Minting
Section titled “Token Minting”tx mint_tokens( amount: Int) { input source { from: MintingAuthority, min_amount: Ada(fees), }
mint { amount: MyToken(amount), redeemer: MintData { quantity: amount }, }}
Token Burning
Section titled “Token Burning”tx burn_tokens( amount: Int) { input source { from: User, min_amount: MyToken(amount), }
burn { amount: MyToken(amount), redeemer: BurnData { quantity: amount }, }}
Asset Locking
Section titled “Asset Locking”tx lock_assets( amount: Int) { input source { from: User, min_amount: Ada(amount) + MyToken(amount), }
output locked { to: LockContract, amount: Ada(amount) + MyToken(amount), datum: LockState { amount: amount, owner: User, } }}