bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 8쪽
56 = ERC20(_coins[i]).decimals()
assert d < 19, "Max 18 decimals for coins"
decimals[i] = d
precisions[i] = 10 **(18 - d)
# pack precision
packed_precisions: uint256 = self._pack_2(precisions[0], precisions[1])
# pack fees
packed_fee_params: uint256 = self._pack_3(
[mid_fee, out_fee, fee_gamma]
)
# pack liquidity rebalancing params
packed_rebalancing_params: uint256 = self._pack_3(
[allowed_extra_profit, adjustment_step, ma_exp_time]
)
# pack gamma and A
packed_gamma_A: uint256 = self._pack_2(gamma, A)
# pool is an ERC20 implementation
_salt: bytes32 = block.prevhash
pool: address = create_from_blueprint(
pool_implementation, # blueprint: address
_name, # String[64]
_symbol, # String[32]
_coins, # address[N_COINS]
_math_implementation, # address
_salt, # bytes32
packed_precisions, # uint256
packed_gamma_A, # uint256
packed_fee_params, # uint256
packed_rebalancing_params, # uint256
initial_price, # uint256
code_offset=3,
)
# populate pool data
self.pool_list.append(pool)
self.pool_data[pool].decimals = decimals
self.pool_data[pool].coins = _coins
self.pool_data[pool].implementation = pool_implementation
# add coins to market:
self._add_coins_to_market(_coins[0], _coins[1], pool)
log TwocryptoPoolDeployed(
pool,
_name,
_symbol,
_coins,
_math_implementation,
_salt,
precisions,
packed_gamma_A,
packed_fee_params,
packed_rebalancing_params,
initial_price,
msg.sender,
)
return pool
```
**▶Example▼**
```shell
>>> Factory.deploy_pool(
_name: CRV/ETH,
_symbol: crv-eth,
_coins: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', '0xD533a949740bb3306d119CC777fa900bA034cd52',
implementation_id: 0,
A: 2700000,
gamma: 1300000000000,
mid_fee: 2999999,
out_fee: 80000000,
fee_gamma: 350000000000000,
allowed_extra_profit: 100000000000,
adjustment_step: 100000000000,
ma_exp_time: 600,
initial_prices: 0.00023684735380012821,
)
'returns address of the deployed pool'
```
* * *
<a id="deploying-gauges"></a>
## Deploying Gauges
<a id="deploy_gauge"></a>
### `deploy_gauge`
`Factory.deploy_gauge(_pool: address) -> address:`
warning
Deploying a liquidity gauge through the Factory is only possible on Ethereum Mainnet. Gauge deployments on sidechains must be done via the [`RootChainGaugeFactory`](https://docs.curve.finance/developer/gauges/xchain-gauges/root-gauge-factory.md).
Function to deploy a liquidity gauge on Ethereum mainnet. This function can only be used on pools deployed from this Factory contract.
| Input | Type | Description |
| --- | --- | --- |
| `_pool` | `address` | Pool to deploy a gauge for |
Returns: deployed gauge (`address`).
Emits: `LiquidityGaugeDeployed`
**<>Source code▼**
```vyper
event LiquidityGaugeDeployed:
pool: address
gauge: address
@external
def deploy_gauge(_pool: address) -> address:
"""
@notice Deploy a liquidity gauge for a factory pool
@param _pool Factory pool address to deploy a gauge for
@return Address of the deployed gauge
"""
assert self.pool_data[_pool].coins[0] != empty(address), "Unknown pool"
assert self.pool_data[_pool].liquidity_gauge == empty(address), "Gauge already deployed"
assert self.gauge_implementation != empty(address), "Gauge implementation not set"
gauge: address = create_from_blueprint(self.gauge_implementation, _pool, code_offset=3)
self.pool_data[_pool].liquidity_gauge = gauge
log LiquidityGaugeDeployed(_pool, gauge)
return gauge
```
**▶Example▼**
```shell
>>> Factory.deploy_gauge('pool address')
'returns address of the deployed gauge'
```
---
# DonationStreamer
Source: [https://docs.curve.finance/developer/amm/fxswap/automation/donation-streamer](https://docs.curve.finance/developer/amm/fxswap/automation/donation-streamer)
The `DonationStreamer` contract enables permissionless, scheduled refuels for FXSwap pools. Refuel providers deposit tokens and ETH rewards upfront to create streams that add refuels over multiple periods. Anyone can execute due streams and earn ETH bounties for doing so.
This contract is fully permissionless — there is no admin or owner. Once a stream is created, it can only be cancelled by its creator, stored in the `donor` field.
A frontend for creating, viewing, and executing streams is available at [curvefi.github.io/refuel-automation](https://curvefi.github.io/refuel-automation/).
Start with the [automation overview](https://docs.curve.finance/developer/amm/fxswap/automation/overview.md) for custody, permissions, execution risk, and indexing. For pool-level accounting, see [FXSwap Refuels](https://docs.curve.finance/developer/amm/fxswap/pools/refuels.md) and the [FXSwap Pool](https://docs.curve.finance/developer/amm/fxswap/pools/fxswap.md).
Contract naming
`DonationStreamer`, `DonationStream`, and `donor` are immutable deployed names. This page uses **refuel** for the mechanism and switches to the legacy names only for exact signatures, fields, events, and source excerpts.
`DonationStreamer.vy`
The source code for the `DonationStreamer.vy` contract can be found on [GitHub](https://github.com/curvefi/refuel-automation). The contract is written in [Vyper](https://vyperlang.org/) version `0.4.3`.
The contract is deployed at the same address on all chains (via CREATE3):
- Ethereum: [`0x2b786BB995978CC2242C567Ae62fd617b0eBC828`](https://etherscan.io/address/0x2b786BB995978CC2242C567Ae62fd617b0eBC828)
- Gnosis: [`0x2b786BB995978CC2242C567Ae62fd617b0eBC828`](https://gnosisscan.io/address/0x2b786BB995978CC2242C567Ae62fd617b0eBC828)
- Base: [`0x2b786BB995978CC2242C567Ae62fd617b0eBC828`](https://basescan.org/address/0x2b786BB995978CC2242C567Ae62fd617b0eBC828)
- Polygon: [`0x2b786BB995978CC2242C567Ae62fd617b0eBC828`](https://polygonscan.com/address/0x2b786BB995978CC2242C567Ae62fd617b0eBC828)
**{ }Contract ABI▼**
```json
[{"anonymous":false,"inputs":[{"indexed":false,"name":"stream_id","type":"uint256"},{"indexed":true,"name":"donor","type":"address"},{"indexed":true,"name":"pool","type":"address"},{"indexed":false,"name":"amounts","type":"uint256[2]"},{"indexed":false,"name":"period_length","type":"uint256"},{"indexed":false,"name":"n_periods","type":"uint256"},{"indexed":false,"name":"reward_per_period","type":"uint256"}],"name":"StreamCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"stream_id","type":"uint256"},{"indexed":true,"name":"caller","type":"address"},{"indexed":true,"name":"pool","type":"address"},{"indexed":false,"name":"periods","type":"uint256"},{"indexed":false,"name":"amounts","type":"uint256[2]"},{"indexed":false,"name":"reward_paid","type":"uint256"}],"name":"StreamExecuted","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"stream_id","type":"uint256"},{"indexed":true,"name":"donor","type":"address"},{"indexed":true,"name":"pool","type":"address"},{"indexed":false,"name":"amounts","type":"uint256[2]"},{"indexed":false,"name":"reward_refund","type":"uint256"}],"name":"StreamCancelled","type":"event"},{"inputs":[{"name":"pool","type":"address"},{"name":"coins","type":"address[2]"},{"name":"amounts","type":"uint256[2]"},{"name":"period_length","type":"uint256"},{"name":"n_periods","type":"uint256"},{"name":"reward_per_period","type":"uint256"}],"name":"create_stream","outputs":[{"name":"","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"name":"stream_id","type":"uint256"}],"name":"cancel_stream","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"stream_id","type":"uint256"}],"name":"execute","outputs":[{"name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"stream_ids","type":"uint256[]"}],"name":"execute_many","outputs":[{"name":"","type":"bool[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"stream_id","type":"uint256"}],"name":"is_due","outputs":[{"name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"streams_and_rewards_due","outputs":[{"name":"","type":"uint256[]"},{"name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"stream_count","outputs":[{"name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"name":"arg0","type":"uint256"}],"name":"streams","outputs":[{"components":[{"name":"donor","type":"address"},{"name":"pool","type":"address"},{"name":"coins","type":"address[2]"},{"name":"amounts_per_period","type":"uint256[2]"},{"name":"period_length","type":"uint256"},{"name":"reward_per_period","type":"uint256"},{"name":"next_ts","type":"uint256"},{"name":"reward_remaining","type":"uint256"},{"name":"amounts_remaining","type":"uint256[2]"},{"name":"periods_remaining","type":"uint256"}],"name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"outputs":[],"stateMutability":"nonpayable","type":"constructor"}]
```
<a id="stream-management"></a>
## Stream Management
<a id="create_stream"></a>
### `create_stream`
`DonationStreamer.create_stream(pool: address, coins: address[2], amounts: uint256[2], period_length: uint256, n_periods: uint256, reward_per_period: uint256) -> uint256`
Payable function to create a new refuel stream for a pool. The caller deposits the full token amounts and ETH rewards upfront. The contract adds the tokens to the pool's refuel buffer in portions over `n_periods`. The `coins` parameter must match the pool's coin configuration. Any excess ETH beyond the required `reward_per_period * n_periods` is refunded.
| Input | Type | Description |
| --- | --- | --- |
| `pool` | `address` | Address of the target FXSwap pool |
| `coins` | `address[2]` | Token addresses matching the pool's `coins(0)` and `coins(1)` |
| `amounts` | `uint256[2]` | Total token amounts to refuel across all periods |
| `period_length` | `uint256` | Duration of each period in seconds |
| `n_periods` | `uint256` | Number of refuel periods |
| `reward_per_period` | `uint256` | ETH reward paid to the executor for each period |
Returns: the stream ID (`uint256`).
Emits: `StreamCreated` event.
**<>Source code▼**
```vyper
@external
@payable
@nonreentrant
def create_stream(
pool: address,
coins: address[N_COINS],
amounts: uint256[N_COINS],
period_length: uint256,
n_periods: uint256,
reward_per_period: uint256,
) -> uint256:
"""
@notice Create a donation stream for a pool.
"""
assert pool != empty(address), "pool required"
assert n_periods > 0, "bad n_periods"
assert period_length > 0, "bad period_length"
assert amounts[0] > 0 or amounts[1] > 0, "zero amounts"
# Ensure caller-provided coins match the pool configuration.
assert (
coins[0] == staticcall DonationPoolTarget(pool).coins(0)
and coins[1] == staticcall DonationPoolTarget(pool).coins(1)
), "coin mismatch"
# Rewards are pre-funded for all periods; excess is refunded.
reward_total: uint256 = reward_per_period * n_periods
assert msg.value >= reward_total, "reward mismatch"
# Per-period amounts are truncated; remainders donate on the final period.
amounts_per_period: uint256[N_COINS] = empty(uint256[N_COINS])
for i: uint256 in range(N_COINS):
amount: uint256 = amounts[i]
if amount > 0:
balance_before: uint256 = staticcall IERC20(coins[i]).balanceOf(self)
assert extcall IERC20(coins[i]).transferFrom(
msg.sender, self, amount, default_return_value=True
), "transfer failed"
balance_after: uint256 = staticcall IERC20(coins[i]).balanceOf(self)
assert balance_after - balance_before == amount, "bad token transfer"
amounts_per_period[i] = amount // n_periods
stream_id: uint256 = self.stream_count
self.stream_count = stream_id + 1
self.streams[stream_id] = DonationStream(
donor=msg.sender,
pool=pool,
coins=coins,
amounts_per_period=amounts_per_period,
period_length=period_length,
reward_per_period=reward_per_period,
next_ts=block.timestamp,
reward_remaining=reward_total,
amounts_remaining=amounts,
periods_remaining=n_periods,
)
# Refund excess message value.
if msg.value > reward_total:
send(msg.sender, msg.value - reward_total)
log StreamCreated(
stream_id=stream_id,
donor=msg.sender,
pool=pool,
amounts=amounts,
period_length=period_length,
n_periods=n_periods,
reward_per_period=reward_per_period,
)
return stream_id
```
**▶Example▼**
```shell
>>> DonationStreamer.create_stream(
... pool, # FXSwap pool address
... [coin0, coin1], # pool coin addresses
... [1000e18, 500e18], # total refuel amounts
... 86400, # period length: 1 day
... 7, # number of periods
... 0.001e18, # 0.001 ETH reward per period
... value=0.007e18 # total ETH rewards
... )
0 # stream_id
```
<a id="cancel_stream"></a>
### `cancel_stream`
`DonationStreamer.cancel_stream(stream_id: uint256)`
Guarded Method
This function can only be called by the stream creator stored in `donor`.
Cancels a stream and refunds all remaining tokens and ETH rewards to its creator. The stream storage is cleared after cancellation.
| Input | Type | Description |
| --- | --- | --- |
| `stream_id` | `uint256` | ID of the stream to cancel |
Emits: `StreamCancelled` event.
**<>Source code▼**
```vyper
@external
@nonreentrant
def cancel_stream(stream_id: uint256):
"""
@notice Cancel a stream and refund remaining balances.
"""
stream: DonationStream = self.streams[stream_id]
assert stream.donor == msg.sender, "donor only"
pool: address = stream.pool
coins: address[N_COINS] = stream.coins
amounts_refund: uint256[N_COINS] = stream.amounts_remaining
reward_refund: uint256 = stream.reward_remaining
self.streams[stream_id] = empty(DonationStream)
for i: uint256 in range(N_COINS):
if amounts_refund[i] > 0:
assert extcall IERC20(coins[i]).transfer(
msg.sender, amounts_refund[i], default_return_value=True
), "refund failed"
if reward_refund > 0:
send(msg.sender, reward_refund)
log StreamCancelled(
stream_id=stream_id,
donor=msg.sender,
pool=pool,
amounts=amounts_refund,
reward_refund=reward_refund,
)
```
**▶Example▼**
```shell
>>> DonationStreamer.cancel_stream(0)
```
* * *
<a id="stream-execution"></a>
## Stream Execution
<a id="execute"></a>
### `execute`
`DonationStreamer.execute(stream_id: uint256) -> bool`
Executes a single stream if it has due periods. The function calculates how many periods are due, adds the proportional refuel amounts through `add_liquidity(donation=True)`, and pays the caller the corresponding ETH reward. If all periods are completed, the stream storage is cleared. Returns `False` if no periods are due.
| Input | Type | Description |
| --- | --- | --- |
| `stream_id` | `uint256` | ID of the stream to execute |
Returns: whether the stream was executed (`bool`).
Emits: `StreamExecuted` event.
**<>Source code▼**
```vyper
@external
@nonreentrant
def execute(stream_id: uint256) -> bool:
"""
@notice Execute a single stream id.
"""
return self._execute_stream(stream_id)
@internal
def _execute_stream(stream_id: uint256) -> bool:
"""
@dev Execute a single stream if due and pay its reward.
"""
stream: DonationStream = self.streams[stream_id]
periods_due: uint256 = self._due_periods(stream)
if periods_due == 0:
return False
is_final: bool = periods_due == stream.periods_remaining
pool: address = stream.pool
coins: address[N_COINS] = stream.coins
# Compute the slice for this execution, using the remainder on the final call.
amounts_to_donate: uint256[N_COINS] = empty(uint256[N_COINS])
for j: uint256 in range(N_COINS):
remaining: uint256 = stream.amounts_remaining[j]
if remaining == 0:
continue
amount: uint256 = stream.amounts_per_period[j] * periods_due
if is_final:
amount = remaining
amounts_to_donate[j] = amount
stream.amounts_remaining[j] = remaining - amount
stream.periods_remaining -= periods_due
stream.next_ts += stream.period_length * periods_due
# Rewards are prorated per period, with final execution paying the remainder.
reward_paid: uint256 = stream.reward_per_period * periods_due
if is_final:
reward_paid = stream.reward_remaining
stream.reward_remaining -= reward_paid
# Clear storage once the stream is finished.
if is_final:
self.streams[stream_id] = empty(DonationStream)
else:
self.streams[stream_id] = stream
# Only approve and add liquidity when there is a non-zero donation.
if amounts_to_donate[0] > 0 or amounts_to_donate[1] > 0:
balances_before: uint256[N_COINS] = empty(uint256[N_COINS])
for j: uint256 in range(N_COINS):
if amounts_to_donate[j] > 0:
balances_before[j] = staticcall IERC20(coins[j]).balanceOf(self)
for j: uint256 in range(N_COINS):
if amounts_to_donate[j] > 0:
self._safe_approve(coins[j], pool, amounts_to_donate[j])
extcall DonationPoolTarget(pool).add_liquidity(
amounts_to_donate,
0,
empty(address),
True,
)
for j: uint256 in range(N_COINS):
if amounts_to_donate[j] > 0:
balance_after: uint256 = staticcall IERC20(coins[j]).balanceOf(self)
assert balances_before[j] >= balance_after, "bad pool pull"
assert balances_before[j] - balance_after == amounts_to_donate[j], "bad pool pull"
self._safe_approve(coins[j], pool, 0)
if reward_paid > 0:
send(msg.sender, reward_paid)
log StreamExecuted(
stream_id=stream_id,
caller=msg.sender,
pool=pool,
periods=periods_due,
amounts=amounts_to_donate,
reward_paid=reward_paid,
)
return True
@internal
def _safe_approve(token: address, spender: address, amount: uint256):
"""
@dev Safely set allowance, resetting to zero when needed.
"""
allowance: uint256 = staticcall IERC20(token).allowance(self, spender)
if allowance == amount:
return
if allowance != 0 and amount != 0:
assert extcall IERC20(token).approve(
spender, 0, default_return_value=True
), "approve failed"
assert extcall IERC20(token).approve(
spender, amount, default_return_value=True
), "approve failed"
@internal
@view
def _due_periods(stream: DonationStream) -> uint256:
"""
@dev Return the number of due periods for a stream.
"""
if (
stream.donor == empty(address)
or stream.periods_remaining == 0
or stream.period_length == 0
or block.timestamp < stream.next_ts
):
return 0
return min(
(block.timestamp - stream.next_ts) // stream.period_length + 1,
stream.periods_remaining,
)
```
**▶Example▼**
```shell
>>> DonationStreamer.execute(0)
True
```
<a id="execute_many"></a>
### `execute_many`
`DonationStreamer.execute_many(stream_ids: DynArray[uint256, 32]) -> DynArray[bool, 32]`
Executes a batch of streams in a single transaction. Each stream is processed independently — if a stream is not due, its result is `False`. The caller receives the combined ETH rewards for all successfully executed streams. Maximum batch size is 32.
| Input | Type | Description |
| --- | --- | --- |
| `stream_ids` | `DynArray[uint256, 32]` | Array of stream IDs to execute |
Returns: per-stream execution results in input order (`DynArray[bool, 32]`).
**<>Source code▼**
```vyper
@external
@nonreentrant
def execute_many(stream_ids: DynArray[uint256, N_MAX_EXECUTE]) -> DynArray[bool, N_MAX_EXECUTE]:
"""
@notice Execute a batch of stream ids.
@return Per-stream execution results in input order.
"""
results: DynArray[bool, N_MAX_EXECUTE] = empty(DynArray[bool, N_MAX_EXECUTE])
for i: uint256 in range(len(stream_ids), bound=N_MAX_EXECUTE):
results.append(self._execute_stream(stream_ids[i]))
return results
```
**▶Example▼**
```shell
>>> DonationStreamer.execute_many([0, 1, 2])
[True, True, False]
```
* * *
<a id="view-methods"></a>
## View Methods
<a id="is_due"></a>
### `is_due`
`DonationStreamer.is_due(stream_id: uint256) -> bool: view`
Returns `True` if the stream has one or more periods that can be executed now.
| Input | Type | Description |
| --- | --- | --- |
| `stream_id` | `uint256` | ID of the stream to check |
Returns: whether the stream is due for execution (`bool`).
**<>Source code▼**
```vyper
@view
@external
def is_due(stream_id: uint256) -> bool:
"""
@notice Return true if the stream can be executed now.
"""
return self._due_periods(self.streams[stream_id]) > 0
@internal
@view
def _due_peri프로젝트로 돌아가기