Verified contractExactMIT
FusingFeeConverter
Address0xff7a80738f6e9004bc6e16c855a9063701239d7d
- Balance
0 ETH
- Transactions
193
- Compiler
solc 0.8.26
- Same code
1 more contract
Details
- Compiler
- solc 0.8.26+commit.8a97fa7a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,975,549
- Creation transaction
- 0xc109ede743…3a703c0711
82 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/v2/FusingFeeConverter.solmain22.83 KB
- lib/openzeppelin-contracts/contracts/access/Ownable.sol3.1 KB
- lib/openzeppelin-contracts/contracts/access/Ownable2Step.sol2.48 KB
- lib/openzeppelin-contracts/contracts/interfaces/IERC1363.sol4.45 KB
- lib/openzeppelin-contracts/contracts/interfaces/IERC165.sol193 B
- lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol182 B
- lib/openzeppelin-contracts/contracts/interfaces/IERC20Metadata.sol216 B
- lib/openzeppelin-contracts/contracts/interfaces/draft-IERC6093.sol6.54 KB
- lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol10.8 KB
- lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol2.78 KB
- lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Burnable.sol1.18 KB
- lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol684 B
- lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol13.96 KB
- lib/openzeppelin-contracts/contracts/token/ERC721/IERC721.sol4.93 KB
- lib/openzeppelin-contracts/contracts/utils/Context.sol963 B
- lib/openzeppelin-contracts/contracts/utils/Panic.sol1.97 KB
- lib/openzeppelin-contracts/contracts/utils/ReentrancyGuard.sol4.77 KB
- lib/openzeppelin-contracts/contracts/utils/StorageSlot.sol4.18 KB
- lib/openzeppelin-contracts/contracts/utils/introspection/IERC165.sol871 B
- lib/openzeppelin-contracts/contracts/utils/math/Math.sol32.66 KB
- lib/openzeppelin-contracts/contracts/utils/math/SafeCast.sol34.94 KB
- lib/v4-core/src/interfaces/IExtsload.sol1.02 KB
- lib/v4-core/src/interfaces/IExttload.sol699 B
- lib/v4-core/src/interfaces/IHooks.sol8.23 KB
- lib/v4-core/src/interfaces/IPoolManager.sol12.84 KB
- lib/v4-core/src/interfaces/IProtocolFees.sol2.49 KB
- lib/v4-core/src/interfaces/callback/IUnlockCallback.sol488 B
- lib/v4-core/src/interfaces/external/IERC20Minimal.sol3.04 KB
- lib/v4-core/src/interfaces/external/IERC6909Claims.sol3.16 KB
- lib/v4-core/src/libraries/BitMath.sol2.61 KB
- lib/v4-core/src/libraries/CustomRevert.sol5.17 KB
- lib/v4-core/src/libraries/FixedPoint128.sol297 B
- lib/v4-core/src/libraries/FixedPoint96.sol366 B
- lib/v4-core/src/libraries/FullMath.sol5.46 KB
- lib/v4-core/src/libraries/Hooks.sol16.51 KB
- lib/v4-core/src/libraries/LPFeeLibrary.sol3.6 KB
- lib/v4-core/src/libraries/LiquidityMath.sol742 B
- lib/v4-core/src/libraries/ParseBytes.sol1.2 KB
- lib/v4-core/src/libraries/Position.sol4.81 KB
- lib/v4-core/src/libraries/SafeCast.sol2.27 KB
- lib/v4-core/src/libraries/SqrtPriceMath.sol14.08 KB
- lib/v4-core/src/libraries/StateLibrary.sol16.03 KB
- lib/v4-core/src/libraries/TickMath.sol12.38 KB
- lib/v4-core/src/libraries/UnsafeMath.sol1.17 KB
- lib/v4-core/src/types/BalanceDelta.sol2.34 KB
- lib/v4-core/src/types/BeforeSwapDelta.sol1.47 KB
- lib/v4-core/src/types/Currency.sol5.46 KB
- lib/v4-core/src/types/PoolId.sol539 B
- lib/v4-core/src/types/PoolKey.sol805 B
- lib/v4-core/src/types/PoolOperation.sol907 B
- lib/v4-hooks-public/src/base/BaseHook.sol7.13 KB
- lib/v4-periphery/lib/permit2/src/interfaces/IAllowanceTransfer.sol7.7 KB
- lib/v4-periphery/lib/permit2/src/interfaces/IEIP712.sol144 B
- lib/v4-periphery/src/base/ImmutableState.sol818 B
- lib/v4-periphery/src/interfaces/IEIP712_v4.sol328 B
- lib/v4-periphery/src/interfaces/IERC721Permit_v4.sol2.14 KB
- lib/v4-periphery/src/interfaces/IImmutableState.sol365 B
- lib/v4-periphery/src/interfaces/IMulticall_v4.sol774 B
- lib/v4-periphery/src/interfaces/INotifier.sol3.2 KB
- lib/v4-periphery/src/interfaces/IPermit2Forwarder.sol1.44 KB
- lib/v4-periphery/src/interfaces/IPoolInitializer_v4.sol782 B
- lib/v4-periphery/src/interfaces/IPositionManager.sol3.93 KB
- lib/v4-periphery/src/interfaces/ISubscriber.sol2.27 KB
- lib/v4-periphery/src/interfaces/IUnorderedNonce.sol799 B
- lib/v4-periphery/src/libraries/Actions.sol2.97 KB
- lib/v4-periphery/src/libraries/LiquidityAmounts.sol3.8 KB
- lib/v4-periphery/src/libraries/PositionInfoLibrary.sol3.92 KB
- src/v2/FusingFeeHook.sol16.2 KB
- src/v2/FusingHolderDistributor.sol9.37 KB
- src/v2/FusingLaunchDeployer.sol12.26 KB
- src/v2/FusingLaunchFactory.sol55.27 KB
- src/v2/FusingLaunchLocker.sol29.26 KB
- src/v2/FusingLauncherToken.sol20.9 KB
- src/v2/FusingPositionMinter.sol6.22 KB
- src/v2/FusingQuotePricer.sol52.49 KB
- src/v2/FusingRouter.sol30.73 KB
- src/v2/interfaces/ILaunchpadV2.sol4.63 KB
- src/v2/libraries/FusingPositionMath.sol9.44 KB
- src/v2/libraries/FusingQuoteAssetProbe.sol5.86 KB
- src/v2/libraries/FusingSwapGuard.sol4.02 KB
- src/v2/libraries/FusingTerms.sol5.24 KB
- src/v2/libraries/Hop.sol1.49 KB
src/v2/FusingFeeConverter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
/*
* fusi.ng | the Ethereum launchpad
* ----------------------------------
* Web: https://fusi.ng
* X: https://x.com/fusi_ng
* Telegram: https://t.me/fusi_ng
* Docs: https://fusi.ng/docs
*
* Fusing V2 contracts. Every launch pays 1.1% per trade, 60% to the creator
* and 40% to the protocol, paid out in ETH. V2 puts the terms in the
* creator's hands: token limits, fee schedules, anti-sniper windows, holder
* rewards, five-way fee splits and the payout mode.
*/
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {LPFeeLibrary} from "@uniswap/v4-core/src/libraries/LPFeeLibrary.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {FusingLaunchFactory} from "./FusingLaunchFactory.sol";
import {FusingRouter} from "./FusingRouter.sol";
import {FusingQuotePricer} from "./FusingQuotePricer.sol";
import {FusingTerms} from "./libraries/FusingTerms.sol";
import {FusingSwapGuard} from "./libraries/FusingSwapGuard.sol";
import {Hop} from "./libraries/Hop.sol";
import {IFusingFeeEscrow, IFusingLaunchFactory} from "./interfaces/ILaunchpadV2.sol";
/// @dev The single fee hook getter this contract reads.
interface IFusingFeeHookFee {
function currentFee(PoolId id) external view returns (uint24);
}
/**
* @title FusingFeeConverter
* @notice Converts each collected fee into the asset it is to be paid in.
*
* A Uniswap V4 pool charges its fee in whatever the trader sent, so buys pay
* fees in the quote asset and sells pay them in the launch token. In V1 the
* creator received both as they were and the protocol's token side was
* burned. In V2 the locker passes every collection to this contract, which
*
* 1. divides it 60/40 (or on whatever split the launch fixed) between the
* creator and the protocol,
* 2. sells the protocol's token side into the launch's own pool, converts
* the protocol's whole share to ETH without exception (along the
* pricer's route whenever the quote is not ETH), and pays it out,
* 3. pays the creator according to the launch's payout mode:
* ETH everything is sold and converted the same way,
* QUOTE the token side is sold, all of it paid in the quote,
* TOKEN_AND_QUOTE nothing is sold; tokens stay tokens, quote stays quote,
* TOKEN the quote side buys the token back from the pool,
* all of it paid in tokens,
* and credits that to the escrow, where the launch's payout plan splits
* it between the dev wallet, the liquidity vault and the burn vault.
*
* Each sale and each buyback is bounded by FusingSwapGuard: no more than a
* MAX_SQRT_MOVE_BPS move in the pool's square-root price per call, with a
* floor at the pre-swap price minus TOLERANCE_BPS. Anything a call cannot
* trade inside those bounds is kept here as `pending` and retried on the
* next collection, or when anyone calls `convert`. A trade whose floor
* cannot be met is skipped; it is never sent unprotected.
*
* This contract has no owner. Only the locker may call `settle`; `convert`
* is open to all; and every path pays the recipients recorded for the
* launch and no one else.
*/
contract FusingFeeConverter is ReentrancyGuard {
using SafeERC20 for IERC20;
using StateLibrary for IPoolManager;
using PoolIdLibrary for PoolKey;
error NotLocker();
error ZeroAddress();
error NativeMismatch(uint256 expected, uint256 got);
/// @dev The router returned less than the floor it was asked to honour. Only a router other than fusi.ng's could do that.
error ShortFill(uint256 got, uint256 minOut);
/**
* @notice Emitted once per settlement or conversion round. `quoteToCreator`
* is only ever nonzero for a pool not quoted in ETH running in QUOTE or
* TOKEN_AND_QUOTE mode; `tokensHanded` only in TOKEN_AND_QUOTE and TOKEN
* mode (and in TOKEN mode it includes `tokensBought`, the amount the quote
* side bought back).
*/
event Settled(
address indexed token,
uint256 ethToProtocol,
uint256 ethToCreator,
uint256 quoteToCreator,
uint256 tokensHanded,
uint256 tokensBought,
uint256 tokensSold,
uint256 quoteSold
);
event Pending(address indexed token, uint256 protocolToken, uint256 creatorToken, uint256 protocolQuote, uint256 creatorQuote);
/// @notice Cap on how far one trade may push the pool's sqrt price: 0.5%, roughly 1% in price terms.
uint256 public constant MAX_SQRT_MOVE_BPS = 50;
/// @notice How far below the pre-swap price a trade may fill: 3%.
uint256 public constant TOLERANCE_BPS = 300;
/// @notice Amounts under this many raw units are not traded; they pile up as dust until they are.
uint256 public constant MIN_SWAP = 1000;
uint256 private constant BPS = 10_000;
uint256 private constant WAD = 1e18;
address public immutable locker;
FusingLaunchFactory public immutable factory;
FusingRouter public immutable router;
FusingQuotePricer public immutable pricer;
IFusingFeeEscrow public immutable feeEscrow;
IPoolManager public immutable poolManager;
/**
* @notice Per-launch fees that have been collected but not converted yet.
* Launch tokens are queued to be sold for the quote; the protocol's quote
* is queued to be sold for ETH. `creatorQuote` has one of two meanings,
* decided per launch by its mode: under ETH mode it is quote waiting to
* be sold for ETH; under TOKEN mode it is quote (or ETH, on a native
* pool) waiting to buy the token back. In the remaining modes the
* creator's quote is paid straight through and is never queued.
*/
struct PendingFees {
uint256 protocolToken;
uint256 creatorToken;
uint256 protocolQuote;
uint256 creatorQuote;
}
mapping(address token => PendingFees) public pending;
/// @dev Running totals of what a round pays out, filled in step by step.
struct Payout {
uint256 ethProtocol;
uint256 ethCreator;
uint256 quoteCreator;
uint256 tokensHanded;
uint256 tokensBought;
uint256 tokensSold;
uint256 quoteSold;
}
constructor(address locker_, FusingLaunchFactory factory_, FusingRouter router_, FusingQuotePricer pricer_, IFusingFeeEscrow escrow_, IPoolManager poolManager_) {
if (
locker_ == address(0) || address(factory_) == address(0) || address(router_) == address(0)
|| address(pricer_) == address(0) || address(escrow_) == address(0) || address(poolManager_) == address(0)
) revert ZeroAddress();
locker = locker_;
factory = factory_;
router = router_;
pricer = pricer_;
feeEscrow = escrow_;
poolManager = poolManager_;
}
receive() external payable {}
/**
* @notice The router trades are sent through. It is the factory's launch
* forwarder, which is the fusi.ng router and can be rotated on the
* factory, so a fixed router reaches this contract with no redeploy. The
* router passed at construction serves as the fallback while no
* forwarder is set. Every trade compares what actually arrived with its
* floor, so a forwarder that is not our router cannot short-change us;
* the worst it can do is fail.
*/
function activeRouter() public view returns (FusingRouter) {
address forwarder = factory.launchForwarder();
return forwarder == address(0) ? router : FusingRouter(payable(forwarder));
}
/// @dev This contract's balance of `asset` (address zero means ETH).
function _held(address asset) private view returns (uint256) {
return asset == address(0) ? address(this).balance : IERC20(asset).balanceOf(address(this));
}
/**
* @notice Entry point for the locker to deliver one collection:
* `tokenAmount` of the launch token and `quoteAmount` of the quote, both
* already sent to this contract (a native quote comes as msg.value).
* Splits it, converts it and pays it out.
*/
function settle(address token, uint256 tokenAmount, uint256 quoteAmount) external payable nonReentrant {
if (msg.sender != locker) revert NotLocker();
IFusingLaunchFactory.LaunchedToken memory launch = factory.getLaunchedToken(token);
PoolKey memory key = factory.poolKeyFor(token);
bool nativeQuote = launch.pairToken == address(0);
if (nativeQuote ? msg.value != quoteAmount : msg.value != 0) revert NativeMismatch(nativeQuote ? quoteAmount : 0, msg.value);
uint256 protocolWad = _protocolShareWad(launch);
PendingFees storage p = pending[token];
FusingTerms.PayoutMode mode = factory.payoutModeOf(token);
Payout memory out;
// The token side. The protocol's portion is sold, always. The creator's
// is passed on as tokens if the mode pays in tokens, otherwise sold.
{
uint256 protocolTok = tokenAmount * protocolWad / WAD;
uint256 creatorTok = tokenAmount - protocolTok;
p.protocolToken += protocolTok;
if (mode == FusingTerms.PayoutMode.TOKEN_AND_QUOTE || mode == FusingTerms.PayoutMode.TOKEN) {
out.tokensHanded += creatorTok;
} else {
p.creatorToken += creatorTok;
}
}
// The quote side. The protocol's portion: kept as is when it is ETH,
// queued for conversion otherwise. The creator's: depends on the mode.
{
uint256 protocolQ = quoteAmount * protocolWad / WAD;
uint256 creatorQ = quoteAmount - protocolQ;
if (nativeQuote) out.ethProtocol += protocolQ;
else p.protocolQuote += protocolQ;
if (mode == FusingTerms.PayoutMode.TOKEN) {
p.creatorQuote += creatorQ; // spent on the buyback further down
} else if (nativeQuote) {
out.ethCreator += creatorQ;
} else if (mode == FusingTerms.PayoutMode.ETH) {
p.creatorQuote += creatorQ; // converted to ETH further down
} else {
out.quoteCreator += creatorQ; // QUOTE and TOKEN_AND_QUOTE: passed through unchanged
}
}
_convert(token, launch, key, p, mode, out);
_pay(token, launch, out);
_emit(token, p, out);
}
/**
* @notice Converts and pays out everything still queued for `token`.
* Anyone may call it, so a trade that was deferred need not wait for the
* next collection to happen.
*/
function convert(address token) external nonReentrant {
IFusingLaunchFactory.LaunchedToken memory launch = factory.getLaunchedToken(token);
PoolKey memory key = factory.poolKeyFor(token);
PendingFees storage p = pending[token];
Payout memory out;
_convert(token, launch, key, p, factory.payoutModeOf(token), out);
_pay(token, launch, out);
_emit(token, p, out);
}
function _emit(address token, PendingFees storage p, Payout memory out) private {
emit Settled(
token,
out.ethProtocol,
out.ethCreator,
out.quoteCreator,
out.tokensHanded,
out.tokensBought,
out.tokensSold,
out.quoteSold
);
emit Pending(token, p.protocolToken, p.creatorToken, p.protocolQuote, p.creatorQuote);
}
// ---------------------------------------------------------------------
// Converting
// ---------------------------------------------------------------------
/**
* @dev Three bounded steps, each one skipped if it has nothing to do:
* queued launch tokens are sold into the pool for the quote; queued quote
* is sold for ETH along the pricer's route; and, under TOKEN mode, the
* creator's queued quote buys the token back. When a sale covers both
* parties, the proceeds are divided between protocol and creator in the
* ratio of what each had queued, so a partial fill pays each their fair
* fraction and leaves the remainder queued in that same ratio.
*/
function _convert(
address token,
IFusingLaunchFactory.LaunchedToken memory launch,
PoolKey memory key,
PendingFees storage p,
FusingTerms.PayoutMode mode,
Payout memory out
) private {
bool nativeQuote = launch.pairToken == address(0);
bool creatorInQuote =
mode == FusingTerms.PayoutMode.QUOTE || mode == FusingTerms.PayoutMode.TOKEN_AND_QUOTE;
// 1. Launch token into quote (straight into ETH when the pool is quoted in ETH).
uint256 totalTok = p.protocolToken + p.creatorToken;
if (totalTok >= MIN_SWAP) {
(uint256 sold, uint256 got) = _sellToken(token, key, launch.pairToken, totalTok);
if (sold > 0) {
uint256 protocolPart = sold * p.protocolToken / totalTok;
uint256 creatorPart = sold - protocolPart;
p.protocolToken -= protocolPart;
p.creatorToken -= creatorPart;
out.tokensSold += sold;
uint256 gotProtocol = got * protocolPart / sold;
uint256 gotCreator = got - gotProtocol;
if (nativeQuote) out.ethProtocol += gotProtocol;
else p.protocolQuote += gotProtocol;
if (mode == FusingTerms.PayoutMode.TOKEN) p.creatorQuote += gotCreator;
else if (nativeQuote) out.ethCreator += gotCreator;
else if (creatorInQuote) out.quoteCreator += gotCreator;
else p.creatorQuote += gotCreator;
}
}
// 2. Quote into ETH along the pricer's route: always for the protocol,
// for the creator only under ETH mode.
if (!nativeQuote) {
uint256 creatorQ = mode == FusingTerms.PayoutMode.ETH ? p.creatorQuote : 0;
uint256 totalQ = p.protocolQuote + creatorQ;
if (totalQ >= MIN_SWAP) {
uint256 got = _sellQuote(launch.pairToken, totalQ);
if (got > 0) {
uint256 gotProtocol = got * p.protocolQuote / totalQ;
out.ethProtocol += gotProtocol;
out.ethCreator += got - gotProtocol;
out.quoteSold += totalQ;
p.protocolQuote = 0;
p.creatorQuote -= creatorQ;
}
}
}
// 3. Under TOKEN mode, the creator's quote is spent buying the token back.
if (mode == FusingTerms.PayoutMode.TOKEN && p.creatorQuote >= MIN_SWAP) {
(uint256 spent, uint256 got) = _buyToken(token, key, launch.pairToken, p.creatorQuote);
if (spent > 0) {
p.creatorQuote -= spent;
out.tokensBought += got;
out.tokensHanded += got;
}
}
}
/// @dev The pool's fee right now: the static fee, or what the V2 hook's schedule says. A value of PIPS means "do not trade".
function _fee(PoolKey memory key, PoolId id) private view returns (uint24 fee) {
fee = key.fee;
if (fee == LPFeeLibrary.DYNAMIC_FEE_FLAG) {
try IFusingFeeHookFee(address(key.hooks)).currentFee(id) returns (uint24 f) {
fee = f;
} catch {
fee = uint24(FusingSwapGuard.PIPS);
}
}
}
/// @dev A single bounded sale of the launch token into its own pool. Returns the amount sold and the amount received, both measured from this contract's balance rather than trusted from the router.
function _sellToken(address token, PoolKey memory key, address quote, uint256 available)
private
returns (uint256 sold, uint256 got)
{
PoolId id = key.toId();
(uint160 sqrtPriceX96,,,) = poolManager.getSlot0(id);
uint128 liquidity = poolManager.getLiquidity(id);
if (sqrtPriceX96 == 0 || liquidity == 0) return (0, 0);
uint24 fee = _fee(key, id);
if (fee >= FusingSwapGuard.PIPS) return (0, 0);
bool tokenIs0 = Currency.unwrap(key.currency0) == token;
uint256 amountIn = FusingSwapGuard.cappedInput(sqrtPriceX96, liquidity, fee, tokenIs0, available, MAX_SQRT_MOVE_BPS);
if (amountIn < MIN_SWAP) return (0, 0);
(uint256 minOut, bool ok) = FusingSwapGuard.floor(sqrtPriceX96, fee, tokenIs0, amountIn, TOLERANCE_BPS);
if (!ok) return (0, 0);
FusingRouter r = activeRouter();
uint256 before = _held(quote);
IERC20(token).forceApprove(address(r), amountIn);
try r.swapExactIn(key, tokenIs0, amountIn, minOut, address(this)) returns (uint256) {}
catch {
IERC20(token).forceApprove(address(r), 0);
return (0, 0);
}
IERC20(token).forceApprove(address(r), 0);
got = _held(quote) - before;
if (got < minOut) revert ShortFill(got, minOut);
sold = amountIn;
}
/// @dev A single bounded buyback of the launch token via its own pool, paid for in the quote (or ETH). Returns the amount spent and the amount received, measured from this contract's balance.
function _buyToken(address token, PoolKey memory key, address quote, uint256 available) private returns (uint256 spent, uint256 got) {
PoolId id = key.toId();
(uint160 sqrtPriceX96,,,) = poolManager.getSlot0(id);
uint128 liquidity = poolManager.getLiquidity(id);
if (sqrtPriceX96 == 0 || liquidity == 0) return (0, 0);
uint24 fee = _fee(key, id);
if (fee >= FusingSwapGuard.PIPS) return (0, 0);
bool zeroForOne = Currency.unwrap(key.currency0) != token; // the quote is the input side
uint256 amountIn = FusingSwapGuard.cappedInput(sqrtPriceX96, liquidity, fee, zeroForOne, available, MAX_SQRT_MOVE_BPS);
if (amountIn < MIN_SWAP) return (0, 0);
(uint256 minOut, bool ok) = FusingSwapGuard.floor(sqrtPriceX96, fee, zeroForOne, amountIn, TOLERANCE_BPS);
if (!ok) return (0, 0);
FusingRouter r = activeRouter();
uint256 before = _held(token);
if (quote == address(0)) {
try r.swapExactIn{value: amountIn}(key, zeroForOne, amountIn, minOut, address(this)) returns (uint256) {}
catch {
return (0, 0);
}
} else {
IERC20(quote).forceApprove(address(r), amountIn);
try r.swapExactIn(key, zeroForOne, amountIn, minOut, address(this)) returns (uint256) {}
catch {
IERC20(quote).forceApprove(address(r), 0);
return (0, 0);
}
IERC20(quote).forceApprove(address(r), 0);
}
got = _held(token) - before;
if (got < minOut) revert ShortFill(got, minOut);
spent = amountIn;
}
/// @dev Sells a quote asset for ETH along the pricer's route, with a floor at the pricer's own valuation minus the tolerance. Returns the ETH that arrived, measured from this contract's balance.
function _sellQuote(address quote, uint256 amount) private returns (uint256 got) {
(Hop[] memory hops, bool qualifies) = pricer.route(quote);
if (!qualifies || hops.length == 0) return 0;
uint256 quotePerEth;
try pricer.priceEthAmountInQuote(quote, 1 ether) returns (uint256 q) {
quotePerEth = q;
} catch {
return 0;
}
if (quotePerEth == 0) return 0;
uint256 expectedEth = amount * 1 ether / quotePerEth;
uint256 minOut = expectedEth * (BPS - TOLERANCE_BPS) / BPS;
if (minOut == 0) return 0;
FusingRouter r = activeRouter();
uint256 before = address(this).balance;
IERC20(quote).forceApprove(address(r), amount);
try r.quoteToEth(Currency.wrap(quote), amount, hops, minOut, address(this)) returns (uint256) {}
catch {
IERC20(quote).forceApprove(address(r), 0);
return 0;
}
IERC20(quote).forceApprove(address(r), 0);
got = address(this).balance - before;
if (got < minOut) revert ShortFill(got, minOut);
}
// ---------------------------------------------------------------------
// Paying out
// ---------------------------------------------------------------------
/**
* @dev The protocol gets paid directly under a capped gas stipend, with
* the escrow as fallback if the recipient rejects the transfer. The
* creator's ETH, quote and tokens, whichever the mode produces, are
* credited to the escrow for the launch's payout plan to divide.
*/
function _pay(address token, IFusingLaunchFactory.LaunchedToken memory launch, Payout memory out) private {
if (out.ethProtocol > 0) {
(bool ok,) = launch.protocolFeeRecipient.call{value: out.ethProtocol, gas: 50_000}("");
if (!ok) feeEscrow.credit{value: out.ethProtocol}(launch.protocolFeeRecipient);
}
if (out.ethCreator > 0) feeEscrow.creditFor{value: out.ethCreator}(token, launch.creatorFeeRecipient);
if (out.quoteCreator > 0) {
IERC20(launch.pairToken).forceApprove(address(feeEscrow), out.quoteCreator);
feeEscrow.creditTokenFor(token, launch.creatorFeeRecipient, launch.pairToken, out.quoteCreator);
}
if (out.tokensHanded > 0) {
IERC20(token).forceApprove(address(feeEscrow), out.tokensHanded);
feeEscrow.creditTokenFor(token, launch.creatorFeeRecipient, token, out.tokensHanded);
}
}
/// @dev The protocol's fraction of the total fee, scaled by 1e18: its share of the base fee, taken as a fraction of base plus creator tax. The same arithmetic the V1 locker used, carried at full precision.
function _protocolShareWad(IFusingLaunchFactory.LaunchedToken memory launch) private pure returns (uint256) {
uint256 total = uint256(launch.baseFeeBps) + launch.creatorTaxBps;
if (total == 0) return 0;
return uint256(launch.baseFeeBps) * launch.protocolFeeShareBps * WAD / (total * BPS);
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"@uniswap/v4-core/=lib/v4-core/",
"@uniswap/v4-periphery/=lib/v4-periphery/",
"@uniswap/v4-hooks-public/=lib/v4-hooks-public/",
"permit2/=lib/v4-periphery/lib/permit2/",
"forge-std/=C:/Users/User/Documents/fusing/contracts/lib/forge-std/src/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/",
"v4-core/=lib/v4-core/src/",
"v4-hooks-public/=lib/v4-hooks-public/src/",
"v4-periphery/=lib/v4-periphery/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.