5.2 Sell Flow (Same Chain)
5.2 Sell Flow (Same Chain)
Scenario: A user sells tokens back to the bonding curve on the same chain it was created.
-
User Action – Approve & Sell: Before calling sell, the user must approve the TokenFactory to spend their tokens (standard ERC-20 approve). Then the user calls the
sell(tokenAddress, amount)function on the TokenFactory, specifying how many tokens they want to redeem for the base currency.
-
Receive Tokens: The TokenFactory contract transfers the specified amount of tokens from the user’s balance. This is done via the ERC-20
transferFrom(thanks to the approval). The contract now holds those tokens (it will burn them next).
-
Payout Calculation: Using the bonding curve math, the TokenFactory calculates the payout in the base currency for those tokens. It uses the formula (integral) in reverse: essentially payout=ab(eb⋅S−eb⋅(S−ΔS))\text{payout} = \frac{a}{b}(e^{b \cdot S} - e^{b \cdot (S - \Delta S)})payout=ba(eb⋅S−eb⋅(S−ΔS)). It determines the amount of ETH/BNB/etc to return to the user. It also updates the supply state: it will decrement the recorded sold supply S:=S−ΔSS := S - \Delta SS:=S−ΔS.
-
Burn Tokens: The contract now burns the tokens it took. It calls the meme token contract’s burn function to reduce the ERC-20 total supply by that amount. (After this, those tokens are effectively removed from circulation.)
-
Transfer Funds: The TokenFactory then transfers the owed base currency to the user. It holds a pool of funds from previous buyers (since all buyer payments went into the contract). For example, it will send the calculated ETH back to the user’s address.
-
Emit Event: A
SoldMemeToken(seller, token, amount, payout)event is emitted to record the sale.
-
Completion: The user receives the ETH/BNB payout. The token supply and TokenFactory state are updated. If the sale was ongoing, price will be slightly lower now (since supply $S$ decreased). Users can continue to buy or sell as long as the bonding curve phase is active.
Cross Chain Buy Logic