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.