For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Contact SupportDiscordGo to dashboard
HomeDocsAPI ReferenceLiquidity IntegrationChangelog
HomeDocsAPI ReferenceLiquidity IntegrationChangelog
  • Introduction
    • Welcome
    • Supported Chains and Providers
    • Demo Apps
    • FAQ
    • API Issues & Error Codes
    • Need Help?
  • Swap API
    • Introduction
      • Set Token Allowances
      • Handle Native Tokens
      • Display Final Swap Amounts
      • Sell Entire Balance
      • Buy/Sell Tax Support
      • Multi-Fee Support
      • Swap and Send
      • About the RFQ System
    • FAQ
  • Gasless API
    • Introduction
    • Gasless FAQ
  • Solana Swap API
    • Introduction
    • Gasless FAQ
  • Cross-Chain API
    • Introduction
    • Learn More
  • Trade Analytics API
    • Introduction
    • Transaction Data
    • Trade Analytics FAQ
  • Core Concepts
    • Introduction to 0x
    • 0x Cheat Sheet
    • Contracts
    • Order Types
    • Glossary
    • White Paper
    • Transaction Data
  • Developer Resources
    • Bounties
    • Rate Limits
    • System Status
  • Upgrading
    • Overview
    • Upgrading to Swap v2
    • Upgrading to Gasless v2
  • Need Help?
    • FAQ
    • API Issues & Error Codes
    • Contact Support
    • Contact Sales
LogoLogo
Contact SupportDiscordGo to dashboard
On this page
  • Overview
  • The Problem
  • How 0x Solves the Problem
  • Implementation Best Practices
  • Display Buy/Sell Tax Information
  • Route All Tokens with Buy/Sell Taxes Through 0x
  • Educating Your Users
  • Deciding on Terminology
  • Learn More
Swap APIAdditional Topics

Buy/Sell Tax Support

||View as Markdown|
Was this page helpful?
Edit this page
Previous

Sell Entire Balance

Next

Multi-Fee Support

Built with

Overview

Fee-on-Transfer (FoT) tokens, which impose automatic Transfer Fees or Buy/Sell Taxes, can complicate transactions, leading to inaccurate quotes and failed swaps. This guide explains how 0x v2 addresses these challenges with real-time detection and tax calculation, ensuring accurate quotes and reliable on-chain execution for a smoother user experience.

FoT (Fee-on-Transfer) Tokens are a category of tokens with Custom Properties either:

  • a Transfer Fee (user pays a fee any time the token is transferred to another wallet) or,
  • a Buy/Sell Tax (user pays a fee as a % of a swap when the token is bought or sold).

The Problem

Tokens with buy/sell tax properties disrupt standard token transfer assumptions, leading to:

  • Revert rates as high as 30%
  • High error rates (the #1 cause of custom token related errors)
  • Inaccurate quotes
  • A poor user experience

When handling buy/sell tax tokens, integrators face two main challenges:

  • Exact Matching: The sent amount must match the received amount to avoid transaction reverts, but many tools used for detecting buy/sell tax and other custom token properties provide incomplete or outdated data which complicates this process.

  • User Experience: Users must know if a token has a buy/sell tax upfront. Without reliable detection, integrators may adjust slippage to account for fees, but setting slippage too low risks reverts, while setting it too high exposes users to MEV attacks, undermining trust in your app.

Generic Swap Error

How 0x Solves the Problem

0x v2 API provides a robust solution, available on both Swap API and Gasless API:

  • Automatic Detection & Optimized Routing: 0x detects buy/sell tax tokens and optimizes trade routes, minimizing errors and transaction reverts. The API automatically includes the token’s buy/sell tax in its response.

  • Real-Time Tax Calculation: 0x simulates each token transfer to provide precise, real-time tax information, ensuring accurate quotes and enhancing the user experience. The API returns valid quotes and calldata for tokens with buy/sell taxes in both /price and /quote endpoints.

  • User-Friendly Responses: The API returns tax values in a format that’s easy to display to your end users, making it simple to communicate costs transparently.

Implementation Best Practices

āš”ļø Explore the Swap API v2 demo code repo to see how to display the buy/sell tax for a selected token.

āš”ļø See how Matcha displays buy/sell tax in their UI.

Display Buy/Sell Tax Information

To build trust with users, it’s crucial to display buy/sell tax information transparently in the UI. Here’s how to effectively integrate the buy/sell tax information provided by the 0x API into your application:

Step 1: Decide on UI to Show Buy/Sell Taxes Clearly

Decide how to best display the buy/sell taxes for tokens directly in the UI to improve transparency and user trust. If both the buy and sell tokens have taxes, be sure to indicate this. Below are some UI examples from the Matcha app:

Example 1: Sell token (FLOKI) has a sell tax. See live trade here

Matcha Buy Tax UI

Example 2: Both buy (SMI) and sell tokens (FLOKI) have buy and sell taxes, respectively. See live trade here

Matcha Buy Sell Tax UI

Step 2: Utilize API Response Parameters

Use the tokenMetadata object returned in the /price and /quote endpoints for both Swap and Gasless API calls. This object contains the buy/sell tax information for the tokens involved in the swap.

Example response:

1"tokenMetadata": {
2 "buyToken": {
3 "buyTaxBps": "200",
4 "sellTaxBps": "147"
5 },
6 "sellToken": {
7 "buyTaxBps": "0",
8 "sellTaxBps": "0"
9 }
10}

Key Fields:

  • tokenMetadata: Contains metadata for the buy and sell tokens in the swap.
  • buyToken/sellToken: Each contains buyTaxBps and sellTaxBps, representing the buy/sell tax in basis points for buying or selling the token.
  • buyTaxBps/sellTaxBps: The buy/sell tax in basis points. If undetermined, this will be set to null.
Step 3: Format and Display Buy/Sell Tax Information

Convert the buy/sell tax basis points to a percentage and display them in the UI.

Example code to format and display tax information:

1// Helper function to format tax basis points to percentage
2const formatTax = (taxBps: string) => (parseFloat(taxBps) / 100).toFixed(2);
3
4// Display Tax Information in UI
5<div className="text-slate-400">
6 {quote.tokenMetadata.buyToken.buyTaxBps &&
7 quote.tokenMetadata.buyToken.buyTaxBps !== "0" && (
8 <p>
9 {buyTokenInfo(chainId).symbol +
10 ` Buy Tax: ${formatTax(quote.tokenMetadata.buyToken.buyTaxBps)}%`}
11 </p>
12 )}
13 {quote.tokenMetadata.sellToken.sellTaxBps &&
14 quote.tokenMetadata.sellToken.sellTaxBps !== "0" && (
15 <p>
16 {sellTokenInfo(chainId).symbol +
17 ` Sell Tax: ${formatTax(quote.tokenMetadata.sellToken.sellTaxBps)}%`}
18 </p>
19 )}
20</div>

Route All Tokens with Buy/Sell Taxes Through 0x

Many of our meta-aggregator integrators automatically route all tokens with buy/sell taxes through 0x by default. The 0x APIs are specifically designed to handle the complexities of tokens with buy/sell taxes by leveraging real-time tax detection and trade route optimization. This ensures accurate fee calculations for each transaction and keeps your application up-to-date with any changes to token tax structures, eliminating the need for manual updates or custom implementations.

Educating Your Users

Consider including either links or pop-up modals next to buy/sell tax information to direct users to resources explaining these fees. For example, Matcha uses such links to guide users to blog posts or documentation, enhancing transparency and understanding.

This approach helps users grasp the implications of taxes, reduces confusion, and builds trust in your platform.

šŸ‘‰ Try it on Matcha out here

Matcha Transfer Fee Info

Deciding on Terminology

There’s no standard for how to label buy/sell token taxes. You may see it referred to as - ā€œfeeā€, ā€œbuy/sell taxā€, ā€œtransfer feeā€ on different platforms. Your choice should fit your project and user base.

  • Be Consistent: Stick with the same term throughout your app.
  • Know Your Users: Use terms your audience will easily understand.
  • Educate: Provide links to explain the impact of buy/sell taxes. Clear, consistent terminology builds trust and improves user experience.

Learn More

  • (Blog) Introducing State-of-the-art Buy/Sell Tax support