bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 10쪽
l parameter/state value, not an external market oracle |
An application may use these values to monitor the pool or reproduce its logic. A lending, liquidation, minting, or solvency system should not adopt them without an independent oracle-security analysis covering manipulation cost, update timing, liquidity, stale states, external reference markets, and failure behavior.
<a id="what-to-monitor"></a>
## What to monitor
Protocols holding or wrapping FXSwap LP tokens should monitor:
- supported `version()`, pool code, factory, `VIEW()`, and `MATH()`;
- coin addresses, decimals, redemption behavior, and token incidents;
- account LP balance, `user_supply()`, `totalSupply()`, and `donation_shares()`;
- `balances`, withdrawal quotes, virtual-price signals, and external coin values;
- `last_prices`, `price_oracle`, and `price_scale`;
- dynamic-fee and recentering parameters;
- refuel unlock, protection, and depletion state;
- admin parameter and periphery events.
Re-evaluate risk assumptions after a parameter change, prolonged imbalance, depleted refuel budget, external-market disruption, or token-behavior change.
<a id="safe-composition-checklist"></a>
## Safe composition checklist
- Allowlist exact pool implementations and reject unknown versions.
- Use `user_supply()` where the calculation means user-owned LP supply.
- Never assign ownership or withdrawal rights to refuel shares.
- Quote and simulate deposits, swaps, and each supported withdrawal path.
- Value positions with explicit external prices and executable exit assumptions.
- Use stored pool balances for pool accounting; reconcile ERC-20 balances separately.
- Treat pool oracle getters as AMM state unless a separate oracle review proves suitability.
- Test every deployment's tokens, parameters, liquidity, and monitoring thresholds independently.
See [Mechanism & Parameters](https://docs.curve.finance/developer/amm/fxswap/pools/mechanism.md) for recentering behavior, [Oracles](https://docs.curve.finance/developer/amm/fxswap/pools/oracles.md) for price-state boundaries, and the [FXSwap Pool](https://docs.curve.finance/developer/amm/fxswap/pools/fxswap.md) for exact signatures, units, and events.
---
# Integrating FXSwap
Source: [https://docs.curve.finance/developer/amm/fxswap/guides/integration](https://docs.curve.finance/developer/amm/fxswap/guides/integration)
FXSwap keeps the common two-coin Curve routing surface: `coins`, `get_dy`, `get_dx`, `exchange`, `exchange_received`, and `TokenExchange`. The important integration work is identifying the implementation correctly, preserving raw token units, and choosing the correct token-transfer flow.
Use this integration model:
1. **Discover** registered pool addresses.
2. **Identify** the deployed implementation and supported version.
3. **Read** coin order, token decimals, and live pool state.
4. **Quote** the intended direction and amount.
5. **Simulate** the complete transaction against recent state.
6. **Execute** with explicit slippage and atomic token movement.
7. **Verify** the return value and emitted events.
<a id="1-discover-and-identify-pools"></a>
## 1\. Discover and identify pools
Use the [Curve API](https://docs.curve.finance/developer/integration/api/curve-api.md) or [MetaRegistry](https://docs.curve.finance/developer/integration/meta-registry.md) to discover registered pools. Do not use the API's generic `implementation` label as the only FXSwap classifier: an FXSwap pool may be described as a Twocrypto implementation.
Identify the implementation explicitly
Registry membership and interface probing establish compatibility, not provenance. Do not route through an unknown pool only because it exposes familiar Twocrypto methods.
For a newly encountered address:
1. Confirm it is a registered Curve pool.
2. Read `coins(0)` and `coins(1)`.
3. Probe `version()` and the FXSwap-specific getters such as `donation_shares()` and `user_supply()`.
4. Maintain a versioned allowlist for production routing. Interface probing establishes compatibility, not provenance.
The Twocrypto factory does not expose a reliable pool-to-implementation getter. Its current implementation slots also do not describe historical pools. This is why the deployment index does not offer an interactive FXSwap implementation scanner.
```ts
import { getAddress, parseAbi } from 'viem'
const fxswapReadAbi = parseAbi([
'function version() view returns (string)',
'function coins(uint256) view returns (address)',
'function donation_shares() view returns (uint256)',
'function user_supply() view returns (uint256)',
])
const pool = getAddress('0xD9FF8396554A0d18B2CFbeC53e1979b7ecCe8373')
const [version, coin0, coin1, refuelShares] = await Promise.all([
publicClient.readContract({ address: pool, abi: fxswapReadAbi, functionName: 'version' }),
publicClient.readContract({ address: pool, abi: fxswapReadAbi, functionName: 'coins', args: [0n] }),
publicClient.readContract({ address: pool, abi: fxswapReadAbi, functionName: 'coins', args: [1n] }),
publicClient.readContract({ address: pool, abi: fxswapReadAbi, functionName: 'donation_shares' }),
])
if (version !== 'v2.1.0d') throw new Error(`Unsupported FXSwap version: ${version}`)
```
`coins` and swap indices use `uint256`; valid indices are `0` and `1`.
<a id="2-preserve-coin-ordering-and-units"></a>
## 2\. Preserve coin ordering and units
All amounts passed to or returned by swap methods are in the token's **raw ERC-20 units**. `get_dy(0, 1, 1e18)` on a crvUSD/WBTC pool returns WBTC satoshi units, not a 1e18-normalized amount.
Read `decimals()` from each coin and parse user amounts against the input token. Never assume both coins use 18 decimals. `price_scale()` and `price_oracle()` are normalized to 1e18 and express the price of `coins(1)` in `coins(0)` units.
<a id="3-quote-a-route"></a>
## 3\. Quote a route
<a id="exact-input"></a>
### Exact input
Call:
```solidity
get_dy(uint256 i, uint256 j, uint256 dx) external view returns (uint256 dy);
```
The return value already reflects the pool's dynamic swap fee. Compute a non-zero `min_dy` from the quote and the user's slippage tolerance immediately before submission.
```ts
const swapAbi = parseAbi([
'function get_dy(uint256 i, uint256 j, uint256 dx) view returns (uint256)',
'function exchange(uint256 i, uint256 j, uint256 dx, uint256 minDy, address receiver) returns (uint256)',
])
const amountIn = 1_000n * 10n ** 18n
const quotedOut = await publicClient.readContract({
address: pool,
abi: swapAbi,
functionName: 'get_dy',
args: [0n, 1n, amountIn],
})
const minOut = quotedOut * 9_950n / 10_000n // example: 0.50% tolerance
```
<a id="exact-output"></a>
### Exact output
Call `get_dx(i, j, dy)` or `get_dx(i, j, dy, n_iter)`. It is an **iterative approximation**, not a promise of exact execution; the three-argument overload uses five iterations. Add an input buffer, simulate the complete transaction, and enforce your router's maximum input.
The pool has no exact-output swap method. A router implements exact output by estimating with `get_dx`, then executing an exact-input `exchange` with appropriate bounds and refund logic at the router layer.
<a id="4-execute-safely"></a>
## 4\. Execute safely
| Method | Input movement | Output receiver | Primary use |
| --- | --- | --- | --- |
| `exchange` | Pool pulls `dx` from `msg.sender` | Caller or explicit `receiver` | Direct integrations and approved router calls |
| `exchange_received` | Caller transfers `dx` before the call | Caller or explicit `receiver` | Routers that already custody input and can make both actions atomic |
Both methods return the raw output amount and emit `TokenExchange`. The event's indexed `buyer` is `msg.sender`; it does not identify an explicit output receiver.
<a id="approval-flow-exchange"></a>
### Approval flow: `exchange`
`exchange` pulls `dx` from `msg.sender`.
1. Approve the **pool address** to spend the input token.
2. Call `exchange(i, j, dx, min_dy, receiver)`.
3. Use the receiver overload when output should go somewhere other than the caller.
```ts
await walletClient.writeContract({
address: inputToken,
abi: parseAbi(['function approve(address,uint256) returns (bool)']),
functionName: 'approve',
args: [pool, amountIn],
})
await walletClient.writeContract({
address: pool,
abi: swapAbi,
functionName: 'exchange',
args: [0n, 1n, amountIn, minOut, account],
})
```
<a id="pre-transfer-flow-exchange_received"></a>
### Pre-transfer flow: `exchange_received`
`exchange_received` measures tokens already transferred to the pool. Transfer exactly `dx` to the pool, then call it **atomically in the same router transaction**.
```solidity
IERC20(input).transfer(pool, dx);
dy = IFXSwap(pool).exchange_received(i, j, dx, minDy, receiver);
```
Keep the pre-transfer atomic
Do not send tokens in one transaction and settle them in a later transaction. Another caller can change balances first, and unsolicited pool balances are not reserved for the sender. Fee-on-transfer and rebasing tokens require explicit compatibility testing; the interface assumes the amount received matches the stated `dx`.
FXSwap pool methods are nonpayable and operate on ERC-20 tokens. Wrap native currency before routing unless another router step handles wrapping.
<a id="fees-slippage-and-state-changes"></a>
## Fees, slippage, and state changes
- `fee()` returns the current dynamic fee at **1e10 precision**.
- `mid_fee()` and `out_fee()` are the configured bounds, also at 1e10 precision.
- `get_dy` calculates against the pool state of the call. A quote can become stale when balances, oracle state, or `price_scale` changes before execution.
- A swap can trigger oracle updates, recentering, and the burn of available refuel shares. This is expected and does not change the swap ABI.
- Never submit `min_dy = 0` for user trades.
For multi-pool routes, simulate the full route against the intended block state. Apply protection to the final user outcome as well as any per-hop limits required by the router.
<a id="defensive-failure-handling"></a>
### Defensive failure handling
Treat a revert as a failed route, not as evidence that the pool is unusable. Common causes include:
- unsupported, equal, or out-of-range coin indices;
- insufficient input-token allowance or balance;
- a token transfer that returns an unexpected value or amount;
- input not already present for `exchange_received`;
- output below `min_dy`;
- arithmetic or invariant guards reached after pool state changed.
Requote and re-simulate rather than automatically widening slippage. Set a transaction deadline in the router or calling contract because the pool methods do not accept one.
<a id="searcher-and-arbitrage-considerations"></a>
## Searcher and arbitrage considerations
FXSwap depends on arbitrage to connect the pool with external price discovery. Searchers should evaluate the executable trade against live state rather than interpreting a single getter as a guaranteed opportunity.
| Signal | Interpretation |
| --- | --- |
| `last_prices()` | Most recently observed normalized pool price; explicitly unsafe as a manipulation-resistant oracle |
| `price_oracle()` | Exponential moving-average target derived from prior observed prices |
| `price_scale()` | Current center of concentrated pool liquidity |
| External reference price | Offchain or onchain market input chosen by the searcher; not supplied by FXSwap |
| `fee()` | Current dynamic fee at 1e10 precision |
A state-changing swap can update `last_prices`, advance the exponential moving average once per block, and evaluate whether `price_scale` should move. A profitable opportunity can therefore alter the state used by another transaction in the same block.
For each candidate:
1. Read balances, the three price getters, fee parameters, and the intended quote from the same recent block.
2. Compare against an external executable price that includes market depth and settlement costs.
3. Simulate the exact calldata, receiver, token movements, and ordering.
4. Include gas, priority fees, token wrapping, and downstream settlement in profitability.
5. Re-evaluate after any competing pool transaction or parameter change.
Do not infer available depth or direction from `price_oracle() - price_scale()` alone. Dynamic fees, invariant shape, balances, refuel availability, and the proposed trade size all affect execution and whether recentering is accepted.
<a id="events-and-indexing"></a>
## Events and indexing
Successful swaps emit:
```solidity
event TokenExchange(
address indexed buyer,
uint256 sold_id,
uint256 tokens_sold,
uint256 bought_id,
uint256 tokens_bought,
uint256 fee,
uint256 price_scale
);
```
`buyer` is the caller, not necessarily the output receiver. Amounts are raw token units. Index `Donation`, `AddLiquidity`, and the removal events if your application also displays LP or refuel state.
<a id="integration-checklist"></a>
## Integration checklist
- Verify registry membership, code/version support, coin addresses, and token decimals.
- Use `uint256` indices and raw token units.
- Quote with `get_dy`; treat `get_dx` as an iterative estimate.
- Set a non-zero output floor and deadline at the router or transaction layer.
- Requote or invalidate cached quotes after relevant balance, oracle, `price_scale`, or parameter changes.
- Keep pre-transfer and `exchange_received` in one atomic call.
- Simulate unusual token behavior and the entire multi-hop route.
- Decode `TokenExchange` by pool address and versioned ABI.
- Monitor implementation and periphery changes instead of assuming all FXSwap pools are identical forever.
See the [FXSwap Pool](https://docs.curve.finance/developer/amm/fxswap/pools/fxswap.md) for all overloads, getters, guards, and events. Quote helpers are documented locally under [Views](https://docs.curve.finance/developer/amm/fxswap/utility-contracts/views.md).
---
# FXSwap
Source: [https://docs.curve.finance/developer/amm/fxswap/overview](https://docs.curve.finance/developer/amm/fxswap/overview)
FXSwap is Curve's two-asset automated market maker (AMM) for markets whose primary price discovery happens elsewhere. It is designed to keep passive liquidity concentrated around a moving external market price, from fiat FX pairs to markets such as BTC/USD.
It combines:
- **StableSwap-style concentration** around a variable `price_scale`;
- an **exponential moving-average oracle** that guides gradual recentering;
- **refuels**, finite external liquidity buffers that help pay for recentering and are depleted as the pool uses them;
- passive, full-range LP positions and the familiar Curve swap interface.
The distinguishing question is not volatility alone. FXSwap fits a market when the paired assets have reliable external price discovery, arbitrage can connect the pool to that market, and the pool can sustain the cost of moving concentrated liquidity as prices change.
Contract Source & Deployment
This section documents callable Ethereum pools reporting `version() == "v2.1.0d"`. The verified pool source was compiled with Vyper `0.4.3` and is closest to [`curvefi/twocrypto-ng@387fbe5`](https://github.com/curvefi/twocrypto-ng/commit/387fbe5f12473ef0e1f0c6a76bc38f1ca0da669d).
Review the [FXSwap Pool](https://docs.curve.finance/developer/amm/fxswap/pools/fxswap.md#deployed-version-and-source) for the exact source boundary and [Contract Deployments](https://docs.curve.finance/developer/deployments.md) for addresses.
Names used in this documentation
**FXSwap** is the product and pool family. A **refuel** is liquidity added to the pool's finite rebalancing buffer. Refuel shares unlock over time and are burned as the pool uses them to recenter liquidity. The deployed Vyper interface predates this product name and uses `donation_*`, the `Donation` event, and a `donation` flag. Use **refuel** in product-facing language and the deployed names only when referring to code.
<a id="where-fxswap-fits"></a>
## Where FXSwap fits
| Market | Prefer | Why |
| --- | --- | --- |
| Assets expected to stay near a fixed ratio | [Stableswap-NG](https://docs.curve.finance/developer/amm/stableswap-ng/overview.md) | Concentration around a fixed or rate-provider-adjusted peg |
| Two volatile assets where the Curve pool supports primary price discovery | [Twocrypto-NG](https://docs.curve.finance/developer/amm/twocrypto-ng/overview.md) | CryptoSwap invariant and self-funded recentering |
| Two externally priced assets that need passive concentrated liquidity | **FXSwap** | Variable-price StableSwap concentration and a configurable recentering budget |
FXSwap is not a general replacement for CryptoSwap. A pair is a poor fit when it lacks a reliable external reference market, arbitrage cannot keep the pool connected to that market, or no sustainable budget exists for the desired concentration and recentering frequency.
<a id="how-the-mechanism-fits-together"></a>
## How the mechanism fits together
Swaps use a StableSwap invariant centered on `price_scale`. The pool records recent prices in an exponential moving-average oracle. When the oracle moves far enough from `price_scale`, a state-changing pool operation can recenter liquidity toward it.
Recentering has a cost. FXSwap first burns refuel shares that have unlocked and passed the protection rules. If those shares are insufficient, the normal profit buffer provides the remainder. Regular LP balances are not burned.
`gamma()` remains in the deployed ABI for interface compatibility, but the FXSwap invariant does not use it. Read [Mechanism and Parameter Design](https://docs.curve.finance/developer/amm/fxswap/pools/mechanism.md) for the interactions between concentration, fees, oracle smoothing, refuel budgets, and external market depth.
<a id="find-what-you-need"></a>
## Find what you need
FXSwap documentation covers the work of market operators and capital managers, trade-execution integrators, protocol builders, and researchers or infrastructure providers. The pages are organized by task so teams with overlapping responsibilities share the same source of truth.
| If you need to… | Start with |
| --- | --- |
| Quote or execute swaps | [Integrating Swaps](https://docs.curve.finance/developer/amm/fxswap/guides/integration.md) |
| Search for arbitrage opportunities | [Searcher and arbitrage considerations](https://docs.curve.finance/developer/amm/fxswap/guides/integration.md#searcher-and-arbitrage-considerations) |
| Build a vault, strategy, or protocol | [Building on FXSwap](https://docs.curve.finance/developer/amm/fxswap/guides/building.md) |
| Understand recentering and parameters | [Mechanism & Parameters](https://docs.curve.finance/developer/amm/fxswap/pools/mechanism.md) |
| Interpret pool prices and oracle state | [Oracles](https://docs.curve.finance/developer/amm/fxswap/pools/oracles.md) |
| Fund the recentering buffer | [Refuels](https://docs.curve.finance/developer/amm/fxswap/pools/refuels.md) |
| Schedule recurring refuels | [Automation](https://docs.curve.finance/developer/amm/fxswap/automation/overview.md) |
| Inspect the deployed interface | [FXSwap Pool](https://docs.curve.finance/developer/amm/fxswap/pools/fxswap.md) |
<a id="fxswap-infrastructure"></a>
## FXSwap infrastructure
`FXSwap Pool`
* * *
The two-coin AMM, LP token, oracle, fee logic, recentering state, and refuel accounting are exposed through one pool contract.
[→ FXSwap Pool](https://docs.curve.finance/developer/amm/fxswap/pools/fxswap.md)
`Factory & Discovery`
* * *
FXSwap pools were deployed through shared two-coin factory infrastructure. Integrators must identify and allowlist the deployed pool version explicitly.
[→ Identify FXSwap Pools](https://docs.curve.finance/developer/amm/fxswap/overview.md#factory-and-pool-discovery)
`Price Oracles`
* * *
The pool's observed price, exponential moving average, and moving liquidity center.
[→ FXSwap Oracles](https://docs.curve.finance/dev프로젝트로 돌아가기