buy
Purchase tokens from the bonding curve during funding phase.
Function Signature
function buy(
address tokenAddress,
string memory referral
) external payable
Parameters
Address of the token to purchase
Referral code for tracking and rewards
Amount of ETH to spend (sent as msg.value)
Events Emitted
event TokenPriceUpdate(
address indexed token,
address indexed pair,
TransactionType txType,
TokenPriceData priceData,
TransactionData txData,
string referral
)
Example
import { ethers } from 'ethers'
const factory = new ethers.Contract(
TOKEN_FACTORY_ADDRESS,
FACTORY_ABI,
signer
)
async function buyTokens(tokenAddress: string, ethAmount: string) {
const quote = await factory.getQuoteBuy(
tokenAddress,
ethers.utils.parseEther(ethAmount)
)
console.log(`Expected tokens: ${ethers.utils.formatEther(quote)}`)
const tx = await factory.buy(
tokenAddress,
"REFERRAL_CODE",
{ value: ethers.utils.parseEther(ethAmount) }
)
await tx.wait()
}
sell
Sell tokens back to the bonding curve during funding phase.
Function Signature
function sell(
address tokenAddress,
uint256 amount,
string memory referral
) external payable
Parameters
Address of the token to sell
Referral code for tracking
Example
async function sellTokens(
tokenAddress: string,
tokenAmount: string
) {
const token = new ethers.Contract(
tokenAddress,
ERC20_ABI,
signer
)
const amount = ethers.utils.parseEther(tokenAmount)
const approveTx = await token.approve(
TOKEN_FACTORY_ADDRESS,
amount
)
await approveTx.wait()
const quote = await factory.getQuoteSell(tokenAddress, amount)
console.log(`Expected ETH: ${ethers.utils.formatEther(quote)}`)
const tx = await factory.sell(
tokenAddress,
amount,
"REFERRAL_CODE"
)
await tx.wait()
}
Error Handling
TokenNotFunding
: Token already graduated to Uniswap
InvalidAmount
: Zero amount provided for sell
InsufficientETH
: Not enough ETH sent for purchase
ETHTransferFailed
: ETH transfer to user failed
Cannot sell locked tokens
: Attempting to sell within 24h lock period
- Tokens from createAndBuyToken are locked for 24 hours during funding phase
- Lock is removed when token graduates to Uniswap (3 ETH raised)
- Only affects initial purchase amount from createAndBuyToken