This step builds on Step 4: Odin canister calls. Make sure you have successfully deposited BTC before proceeding.
What Youโll Accomplish
By the end of this step, youโll have:- โ Fetched available tokens from the Odin API
- โ Calculated price impact for trades
- โ Calculated minimum amounts received for trades
- โ Implemented risk assessment logic
- โ Executed your first token purchase
- โ Implemented slippage protection for safer trading
- โ Learned best practices for trade configuration
Understanding Token Trading
Price Impact and Risk Assessment
Before executing any trade, itโs crucial to understand the price impact your transaction will have on the tokenโs price. This guide provides utilities to calculate this impact and assess the risk level.Learn more about price impact calculations here
Trade Types and Settings
Odin supports different trade types and allows you to configure various settings including slippage tolerance to protect against unfavorable price movements.Update Type Definitions
First, letโs update our type definitions to support trading functionality:types/odin.ts
Token Discovery and Trading Logic
Now letโs implement the complete trading workflow:index.ts
Key Trading Concepts
1. Token Discovery
2. Price Impact Calculation
3. Minimum Amount Calculation
4. Risk Assessment
5. Trade Execution
6. Slippage Protection
token_trade documentation available here.
Testing Your Trading Implementation
Run the complete trading workflow:Understanding Slippage Protection
What is Slippage?
Slippage occurs when the actual execution price of a trade differs from the expected price. This typically happens in volatile markets or when trading large amounts that impact the tokenโs price.How Slippage Protection Works
Slippage Calculation Breakdown
- Price Impact: The immediate effect your trade has on the token price
- User Slippage: Additional tolerance youโre willing to accept (e.g., 5%)
- Total Allowable Slippage: Price impact + user-defined slippage
- Precision: Converted to basis points (100000 = 100%)
Trade Types with Slippage
Error Handling
Always implement proper error handling for trading operations:Congratulations! Youโve successfully implemented token trading functionality with price impact analysis and risk assessment.
The complete code for this guide is available at https://github.com/Toniq-Labs/odin-docs/tree/main/demo/getting-started.