Trade Tokens

Get A Quote

To get a quote , we call the quoteExactInput function:

/// @notice Parameters for quoting the output amount for a given input
struct QuoteExactInputParams {
    /// @notice The address of the input token (use address(0) for native asset)
    address inputToken;
    /// @notice The address of the output token (use address(0) for native asset)
    address outputToken;
    /// @notice The amount of input token to swap (in input token decimals)
    uint256 inputAmount;
}

/// @notice Quote the output amount for a given input
/// @param params The quote parameters
/// @return outputAmount The quoted output amount
/// @dev refer to the swapExactInput method for the scenarios
function quoteExactInput(QuoteExactInputParams calldata params) external returns (uint256 outputAmount);

Note: the quoteExactInput method is not a view function, but we don’t need to send a transaction to get the quote (an eth_call or the simulation in viem will do the work) .

Here are the possible scenarios:

  • When the quote token is the native gas token (BNB or ETH):

    • buy a token:

      • inputToken is zero address, representing the gas token

      • outputToken is the token you wanna buy

    • sell a token:

      • inputToken is the token you wanna sell

      • outputToken is zero address, representing the gas token

  • When the quote token is not the native gas token (taking USD1 as an example) :

    • buy a token with USD1:

      • inputToken is USD1 address

      • outputToken is the token you wanna buy

    • buy with native gas token (only when the token’s nativeToQuoteSwap is enabled , check below to inspect if a token’s quote token supports nativeToQuoteSwap ):

      • inputToken is zero address, representing the gas token

      • outputToken is the token you wanna buy

      • under the hood, we will help you swap BNB for for USD1 on PancakeSwap as an intermediate step in the contract.

    • sell a token:

      • inputToken is the token you wanna sell

      • outputToken is USD1

    • Sell directly from token to BNB is also supported, but we will not support this in our UI.

Swap

To swap we call the swapExactInput method:

This is quite straightforward after getting a quote.

Events

TokenBought

Emitted when a user buys tokens through the Portal.

Parameters:

  • ts: Timestamp of the trade.

  • token: Address of the token bought.

  • buyer: Address of the buyer.

  • amount: Amount of tokens bought.

  • eth: Amount of ETH (or quote token) spent.

  • fee: Amount of ETH (or quote token) spent as a fee.

  • postPrice: Price of the token after this trade.

When emitted: Whenever a user successfully buys tokens via the Portal.

TokenSold

Emitted when a user sells tokens through the Portal.

Parameters:

  • ts: Timestamp of the trade.

  • token: Address of the token sold.

  • seller: Address of the seller.

  • amount: Amount of tokens sold.

  • eth: Amount of ETH (or quote token) received.

  • fee: Amount of ETH (or quote token) deducted as a fee.

  • postPrice: Price of the token after this trade.

When emitted: Whenever a user successfully sells tokens via the Portal.

FlapTokenProgressChanged

Whenever a token's progress changes, the FlapTokenProgressChanged event will be emitted. Note that the newProgress is in wad (i.e, with 18 decimals, 1 ether = 100%).

How To Do An Off-Chain Quote?

[Off-Chain Quote] 1. The Preliminary

The preliminary steps to quote off-chain is to get the info of a token first. You can either use the methods provided by our smart contract (i.e: getTokenV6 or getTokenV7 ), or you can build your indexer by indexing the following events:

  • TokenCreated: This event gives us the basic info of the token

  • TokenCurveSet and TokenCurveSetV2: We use these events to determine our bonding curve parameters.

  • TokenDexSupplyThreshSet: this event gives us the circulating supply threshold for the token be migrated.

  • FlapTokenCirculatingSupplyChanged: This event gives us the current circulating supply of the token.

  • LaunchedToDEX : This event indicates that the token has been launched to a decentralized exchange (DEX), and the bonding curve equation ceases to be valid.

  • FlapTokenTaxSet : (optional), if a token does not emit this event, the tax is 0 or it is not a tax token.

  • FlapTokenProgressChanged : emitted when the bonding curve progress of a token changes

[Off-Chain Quote] 2. The Curve Library

With the bonding curve parameters (either a single r from TokenCurveSet or the full set (r, h, k) from TokenCurveSetV2), you can construct a curve instance. Here are the reference typescript and solidity code. You can always feed the code to your AI (Claude or others), and ask them to help you to convert to your preferred programming language:

  • The legacy curve is a special case of the new curve where h = 0. For constructing a legacy curve, we can pass only r as the parameter.

  • For latest curve, we should pass all the parameters (r, h, k).

The solidity version of the curve lib:

Note that the curve has the following methods:

  • estimateSupply(reserve: string): Estimates the token circulating supply given the reserve amount.

  • estimateReserve(amount: string): Estimates the reserve amount given the token’s circulating supply.

[Off-Chain Quote] 3. Quote With The Curve Instance

We will use the typescript pseudo code to demonstrate how to quote with the curve instance.

Case1: Buy 1 BNB Value of Token

Case 2: Sell 1M token for bnb

Last updated