Trade Tokens
Get A Quote
If a token is already migrated, you can still get a quote using the following method. However, we will always use the pool that the token has migrated to, this may not be the best quote. You can also do an off-chain quote to save the RPC calls. Check the end of this document to learn more about the off-chain 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:
inputTokenis zero address, representing the gas tokenoutputTokenis the token you wanna buy
sell a token:
inputTokenis the token you wanna selloutputTokenis 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:
inputTokenis USD1 addressoutputTokenis the token you wanna buy
buy with native gas token (only when the token’s
nativeToQuoteSwapis enabled , check below to inspect if a token’s quote token supportsnativeToQuoteSwap):inputTokenis zero address, representing the gas tokenoutputTokenis the token you wanna buyunder the hood, we will help you swap BNB for for USD1 on PancakeSwap as an intermediate step in the contract.
sell a token:
inputTokenis the token you wanna selloutputTokenis 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
TokenBoughtEmitted 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
TokenSoldEmitted 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
This event is available since v4.12.1
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 tokenTokenCurveSetandTokenCurveSetV2: 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
ras 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