ETH Price: $2,439.37 (-3.15%)

Transaction Decoder

Block:
21058977 at Oct-27-2024 07:12:11 PM +UTC
Transaction Fee:
0.001173875 ETH $2.86
Gas Used:
234,775 Gas / 5 Gwei

Emitted Events:

766 SDT.Transfer( from=[Sender] 0xd4f9fe0039da59e6ddb21bbb6e84e0c9e83d73ed, to=[Receiver] Vyper_contract, value=500000000000000000000 )
767 SDT.Approval( owner=[Sender] 0xd4f9fe0039da59e6ddb21bbb6e84e0c9e83d73ed, spender=[Receiver] Vyper_contract, value=3902938249010121491273 )
768 Vyper_contract.TokenExchange( buyer=[Sender] 0xd4f9fe0039da59e6ddb21bbb6e84e0c9e83d73ed, sold_id=1, tokens_sold=500000000000000000000, bought_id=0, tokens_bought=49714617152138405 )

Account State Difference:

  Address   Before After State Difference Code
(Titan Builder)
5.917410023287115333 Eth5.917423438912857333 Eth0.000013415625742
0x73968b9a...27d6CDB2F
0xD4f9FE00...9e83D73eD
0.816078955627545082 Eth
Nonce: 1675
0.864619697779683487 Eth
Nonce: 1676
0.048540742152138405
0xfB8814D0...fE7a27902 15.694739343497347615 Eth15.64502472634520921 Eth0.049714617152138405

Execution Trace

Vyper_contract.exchange_underlying( i=1, j=0, dx=500000000000000000000, min_dy=49664902534986267 ) => ( 49714617152138405 )
  • Vyper_contract.exchange_underlying( i=1, j=0, dx=500000000000000000000, min_dy=49664902534986267 ) => ( 49714617152138405 )
    • SDT.transferFrom( sender=0xD4f9FE0039Da59e6DDb21bbb6E84e0C9e83D73eD, recipient=0xfB8814D005C5f32874391e888da6eB2fE7a27902, amount=500000000000000000000 ) => ( True )
    • Null: 0x000...004.00000000( )
    • ETH 0.049714617152138405 0xd4f9fe0039da59e6ddb21bbb6e84e0c9e83d73ed.CALL( )
    • Vyper_contract.STATICCALL( )
      • Vyper_contract.DELEGATECALL( )
        File 1 of 5: Vyper_contract
        # @version 0.3.1
        # (c) Curve.Fi, 2021
        # Pool for two crypto assets
        
        # Universal implementation which can use both ETH and ERC20s
        from vyper.interfaces import ERC20
        
        
        interface Factory:
            def admin() -> address: view
            def fee_receiver() -> address: view
        
        interface CurveToken:
            def totalSupply() -> uint256: view
            def mint(_to: address, _value: uint256) -> bool: nonpayable
            def mint_relative(_to: address, frac: uint256) -> uint256: nonpayable
            def burnFrom(_to: address, _value: uint256) -> bool: nonpayable
        
        interface WETH:
            def deposit(): payable
            def withdraw(_amount: uint256): nonpayable
        
        
        # Events
        event TokenExchange:
            buyer: indexed(address)
            sold_id: uint256
            tokens_sold: uint256
            bought_id: uint256
            tokens_bought: uint256
        
        event AddLiquidity:
            provider: indexed(address)
            token_amounts: uint256[N_COINS]
            fee: uint256
            token_supply: uint256
        
        event RemoveLiquidity:
            provider: indexed(address)
            token_amounts: uint256[N_COINS]
            token_supply: uint256
        
        event RemoveLiquidityOne:
            provider: indexed(address)
            token_amount: uint256
            coin_index: uint256
            coin_amount: uint256
        
        event CommitNewParameters:
            deadline: indexed(uint256)
            admin_fee: uint256
            mid_fee: uint256
            out_fee: uint256
            fee_gamma: uint256
            allowed_extra_profit: uint256
            adjustment_step: uint256
            ma_half_time: uint256
        
        event NewParameters:
            admin_fee: uint256
            mid_fee: uint256
            out_fee: uint256
            fee_gamma: uint256
            allowed_extra_profit: uint256
            adjustment_step: uint256
            ma_half_time: uint256
        
        event RampAgamma:
            initial_A: uint256
            future_A: uint256
            initial_gamma: uint256
            future_gamma: uint256
            initial_time: uint256
            future_time: uint256
        
        event StopRampA:
            current_A: uint256
            current_gamma: uint256
            time: uint256
        
        event ClaimAdminFee:
            admin: indexed(address)
            tokens: uint256
        
        
        ADMIN_ACTIONS_DELAY: constant(uint256) = 3 * 86400
        MIN_RAMP_TIME: constant(uint256) = 86400
        
        MAX_ADMIN_FEE: constant(uint256) = 10 * 10 ** 9
        MIN_FEE: constant(uint256) = 5 * 10 ** 5  # 0.5 bps
        MAX_FEE: constant(uint256) = 10 * 10 ** 9
        MAX_A_CHANGE: constant(uint256) = 10
        NOISE_FEE: constant(uint256) = 10**5  # 0.1 bps
        
        MIN_GAMMA: constant(uint256) = 10**10
        MAX_GAMMA: constant(uint256) = 2 * 10**16
        
        MIN_A: constant(uint256) = N_COINS**N_COINS * A_MULTIPLIER / 10
        MAX_A: constant(uint256) = N_COINS**N_COINS * A_MULTIPLIER * 100000
        
        EXP_PRECISION: constant(uint256) = 10**10
        
        N_COINS: constant(int128) = 2
        PRECISION: constant(uint256) = 10 ** 18  # The precision to convert to
        A_MULTIPLIER: constant(uint256) = 10000
        
        
        # Implementation can be changed by changing this constant
        WETH20: immutable(address)
        
        
        token: public(address)
        coins: public(address[N_COINS])
        
        price_scale: public(uint256)   # Internal price scale
        _price_oracle: uint256  # Price target given by MA
        
        last_prices: public(uint256)
        last_prices_timestamp: public(uint256)
        
        initial_A_gamma: public(uint256)
        future_A_gamma: public(uint256)
        initial_A_gamma_time: public(uint256)
        future_A_gamma_time: public(uint256)
        
        allowed_extra_profit: public(uint256)  # 2 * 10**12 - recommended value
        future_allowed_extra_profit: public(uint256)
        
        fee_gamma: public(uint256)
        future_fee_gamma: public(uint256)
        
        adjustment_step: public(uint256)
        future_adjustment_step: public(uint256)
        
        ma_half_time: public(uint256)
        future_ma_half_time: public(uint256)
        
        mid_fee: public(uint256)
        out_fee: public(uint256)
        admin_fee: public(uint256)
        future_mid_fee: public(uint256)
        future_out_fee: public(uint256)
        future_admin_fee: public(uint256)
        
        balances: public(uint256[N_COINS])
        D: public(uint256)
        
        factory: public(address)
        
        xcp_profit: public(uint256)
        xcp_profit_a: public(uint256)  # Full profit at last claim of admin fees
        virtual_price: public(uint256)  # Cached (fast to read) virtual price also used internally
        not_adjusted: bool
        
        admin_actions_deadline: public(uint256)
        
        # This must be changed for different N_COINS
        # For example:
        # N_COINS = 3 -> 1  (10**18 -> 10**18)
        # N_COINS = 4 -> 10**8  (10**18 -> 10**10)
        # PRICE_PRECISION_MUL: constant(uint256) = 1
        PRECISIONS: uint256  # packed
        
        
        @external
        def __init__(_weth: address):
            WETH20 = _weth
            self.mid_fee = 22022022
        
        
        @payable
        @external
        def __default__():
            pass
        
        
        # Internal Functions
        
        @internal
        @view
        def _get_precisions() -> uint256[2]:
            p0: uint256 = self.PRECISIONS
            p1: uint256 = 10 ** shift(p0, -8)
            p0 = 10 ** bitwise_and(p0, 255)
            return [p0, p1]
        
        
        @internal
        @view
        def xp() -> uint256[N_COINS]:
            precisions: uint256[2] = self._get_precisions()
            return [self.balances[0] * precisions[0],
                    self.balances[1] * precisions[1] * self.price_scale / PRECISION]
        
        
        @view
        @internal
        def _A_gamma() -> uint256[2]:
            t1: uint256 = self.future_A_gamma_time
        
            A_gamma_1: uint256 = self.future_A_gamma
            gamma1: uint256 = bitwise_and(A_gamma_1, 2**128-1)
            A1: uint256 = shift(A_gamma_1, -128)
        
            if block.timestamp < t1:
                # handle ramping up and down of A
                A_gamma_0: uint256 = self.initial_A_gamma
                t0: uint256 = self.initial_A_gamma_time
        
                # Less readable but more compact way of writing and converting to uint256
                # gamma0: uint256 = bitwise_and(A_gamma_0, 2**128-1)
                # A0: uint256 = shift(A_gamma_0, -128)
                # A1 = A0 + (A1 - A0) * (block.timestamp - t0) / (t1 - t0)
                # gamma1 = gamma0 + (gamma1 - gamma0) * (block.timestamp - t0) / (t1 - t0)
        
                t1 -= t0
                t0 = block.timestamp - t0
                t2: uint256 = t1 - t0
        
                A1 = (shift(A_gamma_0, -128) * t2 + A1 * t0) / t1
                gamma1 = (bitwise_and(A_gamma_0, 2**128-1) * t2 + gamma1 * t0) / t1
        
            return [A1, gamma1]
        
        
        @internal
        @view
        def _fee(xp: uint256[N_COINS]) -> uint256:
            """
            f = fee_gamma / (fee_gamma + (1 - K))
            where
            K = prod(x) / (sum(x) / N)**N
            (all normalized to 1e18)
            """
            fee_gamma: uint256 = self.fee_gamma
            f: uint256 = xp[0] + xp[1]  # sum
            f = fee_gamma * 10**18 / (
                fee_gamma + 10**18 - (10**18 * N_COINS**N_COINS) * xp[0] / f * xp[1] / f
            )
            return (self.mid_fee * f + self.out_fee * (10**18 - f)) / 10**18
        
        
        ### Math functions
        @internal
        @pure
        def geometric_mean(unsorted_x: uint256[N_COINS], sort: bool) -> uint256:
            """
            (x[0] * x[1] * ...) ** (1/N)
            """
            x: uint256[N_COINS] = unsorted_x
            if sort and x[0] < x[1]:
                x = [unsorted_x[1], unsorted_x[0]]
            D: uint256 = x[0]
            diff: uint256 = 0
            for i in range(255):
                D_prev: uint256 = D
                # tmp: uint256 = 10**18
                # for _x in x:
                #     tmp = tmp * _x / D
                # D = D * ((N_COINS - 1) * 10**18 + tmp) / (N_COINS * 10**18)
                # line below makes it for 2 coins
                D = (D + x[0] * x[1] / D) / N_COINS
                if D > D_prev:
                    diff = D - D_prev
                else:
                    diff = D_prev - D
                if diff <= 1 or diff * 10**18 < D:
                    return D
            raise "Did not converge"
        
        
        @internal
        @view
        def newton_D(ANN: uint256, gamma: uint256, x_unsorted: uint256[N_COINS]) -> uint256:
            """
            Finding the invariant using Newton method.
            ANN is higher by the factor A_MULTIPLIER
            ANN is already A * N**N
        
            Currently uses 60k gas
            """
            # Safety checks
            assert ANN > MIN_A - 1 and ANN < MAX_A + 1  # dev: unsafe values A
            assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1  # dev: unsafe values gamma
        
            # Initial value of invariant D is that for constant-product invariant
            x: uint256[N_COINS] = x_unsorted
            if x[0] < x[1]:
                x = [x_unsorted[1], x_unsorted[0]]
        
            assert x[0] > 10**9 - 1 and x[0] < 10**15 * 10**18 + 1  # dev: unsafe values x[0]
            assert x[1] * 10**18 / x[0] > 10**14-1  # dev: unsafe values x[i] (input)
        
            D: uint256 = N_COINS * self.geometric_mean(x, False)
            S: uint256 = x[0] + x[1]
        
            for i in range(255):
                D_prev: uint256 = D
        
                # K0: uint256 = 10**18
                # for _x in x:
                #     K0 = K0 * _x * N_COINS / D
                # collapsed for 2 coins
                K0: uint256 = (10**18 * N_COINS**2) * x[0] / D * x[1] / D
        
                _g1k0: uint256 = gamma + 10**18
                if _g1k0 > K0:
                    _g1k0 = _g1k0 - K0 + 1
                else:
                    _g1k0 = K0 - _g1k0 + 1
        
                # D / (A * N**N) * _g1k0**2 / gamma**2
                mul1: uint256 = 10**18 * D / gamma * _g1k0 / gamma * _g1k0 * A_MULTIPLIER / ANN
        
                # 2*N*K0 / _g1k0
                mul2: uint256 = (2 * 10**18) * N_COINS * K0 / _g1k0
        
                neg_fprime: uint256 = (S + S * mul2 / 10**18) + mul1 * N_COINS / K0 - mul2 * D / 10**18
        
                # D -= f / fprime
                D_plus: uint256 = D * (neg_fprime + S) / neg_fprime
                D_minus: uint256 = D*D / neg_fprime
                if 10**18 > K0:
                    D_minus += D * (mul1 / neg_fprime) / 10**18 * (10**18 - K0) / K0
                else:
                    D_minus -= D * (mul1 / neg_fprime) / 10**18 * (K0 - 10**18) / K0
        
                if D_plus > D_minus:
                    D = D_plus - D_minus
                else:
                    D = (D_minus - D_plus) / 2
        
                diff: uint256 = 0
                if D > D_prev:
                    diff = D - D_prev
                else:
                    diff = D_prev - D
                if diff * 10**14 < max(10**16, D):  # Could reduce precision for gas efficiency here
                    # Test that we are safe with the next newton_y
                    for _x in x:
                        frac: uint256 = _x * 10**18 / D
                        assert (frac > 10**16 - 1) and (frac < 10**20 + 1)  # dev: unsafe values x[i]
                    return D
        
            raise "Did not converge"
        
        
        @internal
        @pure
        def newton_y(ANN: uint256, gamma: uint256, x: uint256[N_COINS], D: uint256, i: uint256) -> uint256:
            """
            Calculating x[i] given other balances x[0..N_COINS-1] and invariant D
            ANN = A * N**N
            """
            # Safety checks
            assert ANN > MIN_A - 1 and ANN < MAX_A + 1  # dev: unsafe values A
            assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1  # dev: unsafe values gamma
            assert D > 10**17 - 1 and D < 10**15 * 10**18 + 1 # dev: unsafe values D
        
            x_j: uint256 = x[1 - i]
            y: uint256 = D**2 / (x_j * N_COINS**2)
            K0_i: uint256 = (10**18 * N_COINS) * x_j / D
            # S_i = x_j
        
            # frac = x_j * 1e18 / D => frac = K0_i / N_COINS
            assert (K0_i > 10**16*N_COINS - 1) and (K0_i < 10**20*N_COINS + 1)  # dev: unsafe values x[i]
        
            # x_sorted: uint256[N_COINS] = x
            # x_sorted[i] = 0
            # x_sorted = self.sort(x_sorted)  # From high to low
            # x[not i] instead of x_sorted since x_soted has only 1 element
        
            convergence_limit: uint256 = max(max(x_j / 10**14, D / 10**14), 100)
        
            for j in range(255):
                y_prev: uint256 = y
        
                K0: uint256 = K0_i * y * N_COINS / D
                S: uint256 = x_j + y
        
                _g1k0: uint256 = gamma + 10**18
                if _g1k0 > K0:
                    _g1k0 = _g1k0 - K0 + 1
                else:
                    _g1k0 = K0 - _g1k0 + 1
        
                # D / (A * N**N) * _g1k0**2 / gamma**2
                mul1: uint256 = 10**18 * D / gamma * _g1k0 / gamma * _g1k0 * A_MULTIPLIER / ANN
        
                # 2*K0 / _g1k0
                mul2: uint256 = 10**18 + (2 * 10**18) * K0 / _g1k0
        
                yfprime: uint256 = 10**18 * y + S * mul2 + mul1
                _dyfprime: uint256 = D * mul2
                if yfprime < _dyfprime:
                    y = y_prev / 2
                    continue
                else:
                    yfprime -= _dyfprime
                fprime: uint256 = yfprime / y
        
                # y -= f / f_prime;  y = (y * fprime - f) / fprime
                # y = (yfprime + 10**18 * D - 10**18 * S) // fprime + mul1 // fprime * (10**18 - K0) // K0
                y_minus: uint256 = mul1 / fprime
                y_plus: uint256 = (yfprime + 10**18 * D) / fprime + y_minus * 10**18 / K0
                y_minus += 10**18 * S / fprime
        
                if y_plus < y_minus:
                    y = y_prev / 2
                else:
                    y = y_plus - y_minus
        
                diff: uint256 = 0
                if y > y_prev:
                    diff = y - y_prev
                else:
                    diff = y_prev - y
                if diff < max(convergence_limit, y / 10**14):
                    frac: uint256 = y * 10**18 / D
                    assert (frac > 10**16 - 1) and (frac < 10**20 + 1)  # dev: unsafe value for y
                    return y
        
            raise "Did not converge"
        
        
        @internal
        @pure
        def halfpow(power: uint256) -> uint256:
            """
            1e18 * 0.5 ** (power/1e18)
        
            Inspired by: https://github.com/balancer-labs/balancer-core/blob/master/contracts/BNum.sol#L128
            """
            intpow: uint256 = power / 10**18
            otherpow: uint256 = power - intpow * 10**18
            if intpow > 59:
                return 0
            result: uint256 = 10**18 / (2**intpow)
            if otherpow == 0:
                return result
        
            term: uint256 = 10**18
            x: uint256 = 5 * 10**17
            S: uint256 = 10**18
            neg: bool = False
        
            for i in range(1, 256):
                K: uint256 = i * 10**18
                c: uint256 = K - 10**18
                if otherpow > c:
                    c = otherpow - c
                    neg = not neg
                else:
                    c -= otherpow
                term = term * (c * x / 10**18) / K
                if neg:
                    S -= term
                else:
                    S += term
                if term < EXP_PRECISION:
                    return result * S / 10**18
        
            raise "Did not converge"
        ### end of Math functions
        
        
        @internal
        @view
        def get_xcp(D: uint256) -> uint256:
            x: uint256[N_COINS] = [D / N_COINS, D * PRECISION / (self.price_scale * N_COINS)]
            return self.geometric_mean(x, True)
        
        
        @internal
        def _claim_admin_fees():
            A_gamma: uint256[2] = self._A_gamma()
        
            xcp_profit: uint256 = self.xcp_profit
            xcp_profit_a: uint256 = self.xcp_profit_a
        
            # Gulp here
            for i in range(N_COINS):
                coin: address = self.coins[i]
                if coin == WETH20:
                    self.balances[i] = self.balance
                else:
                    self.balances[i] = ERC20(coin).balanceOf(self)
        
            vprice: uint256 = self.virtual_price
        
            if xcp_profit > xcp_profit_a:
                fees: uint256 = (xcp_profit - xcp_profit_a) * self.admin_fee / (2 * 10**10)
                if fees > 0:
                    receiver: address = Factory(self.factory).fee_receiver()
                    if receiver != ZERO_ADDRESS:
                        frac: uint256 = vprice * 10**18 / (vprice - fees) - 10**18
                        claimed: uint256 = CurveToken(self.token).mint_relative(receiver, frac)
                        xcp_profit -= fees*2
                        self.xcp_profit = xcp_profit
                        log ClaimAdminFee(receiver, claimed)
        
            total_supply: uint256 = CurveToken(self.token).totalSupply()
        
            # Recalculate D b/c we gulped
            D: uint256 = self.newton_D(A_gamma[0], A_gamma[1], self.xp())
            self.D = D
        
            self.virtual_price = 10**18 * self.get_xcp(D) / total_supply
        
            if xcp_profit > xcp_profit_a:
                self.xcp_profit_a = xcp_profit
        
        
        @internal
        @view
        def internal_price_oracle() -> uint256:
            price_oracle: uint256 = self._price_oracle
            last_prices_timestamp: uint256 = self.last_prices_timestamp
        
            if last_prices_timestamp < block.timestamp:
                ma_half_time: uint256 = self.ma_half_time
                last_prices: uint256 = self.last_prices
                alpha: uint256 = self.halfpow((block.timestamp - last_prices_timestamp) * 10**18 / ma_half_time)
                return (last_prices * (10**18 - alpha) + price_oracle * alpha) / 10**18
        
            else:
                return price_oracle
        
        
        @internal
        def tweak_price(A_gamma: uint256[2],_xp: uint256[N_COINS], p_i: uint256, new_D: uint256):
            price_oracle: uint256 = self._price_oracle
            last_prices: uint256 = self.last_prices
            price_scale: uint256 = self.price_scale
            last_prices_timestamp: uint256 = self.last_prices_timestamp
            p_new: uint256 = 0
        
            if last_prices_timestamp < block.timestamp:
                # MA update required
                ma_half_time: uint256 = self.ma_half_time
                alpha: uint256 = self.halfpow((block.timestamp - last_prices_timestamp) * 10**18 / ma_half_time)
                price_oracle = (last_prices * (10**18 - alpha) + price_oracle * alpha) / 10**18
                self._price_oracle = price_oracle
                self.last_prices_timestamp = block.timestamp
        
            D_unadjusted: uint256 = new_D  # Withdrawal methods know new D already
            if new_D == 0:
                # We will need this a few times (35k gas)
                D_unadjusted = self.newton_D(A_gamma[0], A_gamma[1], _xp)
        
            if p_i > 0:
                last_prices = p_i
        
            else:
                # calculate real prices
                __xp: uint256[N_COINS] = _xp
                dx_price: uint256 = __xp[0] / 10**6
                __xp[0] += dx_price
                last_prices = price_scale * dx_price / (_xp[1] - self.newton_y(A_gamma[0], A_gamma[1], __xp, D_unadjusted, 1))
        
            self.last_prices = last_prices
        
            total_supply: uint256 = CurveToken(self.token).totalSupply()
            old_xcp_profit: uint256 = self.xcp_profit
            old_virtual_price: uint256 = self.virtual_price
        
            # Update profit numbers without price adjustment first
            xp: uint256[N_COINS] = [D_unadjusted / N_COINS, D_unadjusted * PRECISION / (N_COINS * price_scale)]
            xcp_profit: uint256 = 10**18
            virtual_price: uint256 = 10**18
        
            if old_virtual_price > 0:
                xcp: uint256 = self.geometric_mean(xp, True)
                virtual_price = 10**18 * xcp / total_supply
                xcp_profit = old_xcp_profit * virtual_price / old_virtual_price
        
                t: uint256 = self.future_A_gamma_time
                if virtual_price < old_virtual_price and t == 0:
                    raise "Loss"
                if t == 1:
                    self.future_A_gamma_time = 0
        
            self.xcp_profit = xcp_profit
        
            norm: uint256 = price_oracle * 10**18 / price_scale
            if norm > 10**18:
                norm -= 10**18
            else:
                norm = 10**18 - norm
            adjustment_step: uint256 = max(self.adjustment_step, norm / 5)
        
            needs_adjustment: bool = self.not_adjusted
            # if not needs_adjustment and (virtual_price-10**18 > (xcp_profit-10**18)/2 + self.allowed_extra_profit):
            # (re-arrange for gas efficiency)
            if not needs_adjustment and (virtual_price * 2 - 10**18 > xcp_profit + 2*self.allowed_extra_profit) and (norm > adjustment_step) and (old_virtual_price > 0):
                needs_adjustment = True
                self.not_adjusted = True
        
            if needs_adjustment:
                if norm > adjustment_step and old_virtual_price > 0:
                    p_new = (price_scale * (norm - adjustment_step) + adjustment_step * price_oracle) / norm
        
                    # Calculate balances*prices
                    xp = [_xp[0], _xp[1] * p_new / price_scale]
        
                    # Calculate "extended constant product" invariant xCP and virtual price
                    D: uint256 = self.newton_D(A_gamma[0], A_gamma[1], xp)
                    xp = [D / N_COINS, D * PRECISION / (N_COINS * p_new)]
                    # We reuse old_virtual_price here but it's not old anymore
                    old_virtual_price = 10**18 * self.geometric_mean(xp, True) / total_supply
        
                    # Proceed if we've got enough profit
                    # if (old_virtual_price > 10**18) and (2 * (old_virtual_price - 10**18) > xcp_profit - 10**18):
                    if (old_virtual_price > 10**18) and (2 * old_virtual_price - 10**18 > xcp_profit):
                        self.price_scale = p_new
                        self.D = D
                        self.virtual_price = old_virtual_price
        
                        return
        
                    else:
                        self.not_adjusted = False
        
                        # Can instead do another flag variable if we want to save bytespace
                        self.D = D_unadjusted
                        self.virtual_price = virtual_price
                        self._claim_admin_fees()
        
                        return
        
            # If we are here, the price_scale adjustment did not happen
            # Still need to update the profit counter and D
            self.D = D_unadjusted
            self.virtual_price = virtual_price
        
            # norm appeared < adjustment_step after
            if needs_adjustment:
                self.not_adjusted = False
                self._claim_admin_fees()
        
        
        @internal
        def _exchange(sender: address, mvalue: uint256, i: uint256, j: uint256, dx: uint256, min_dy: uint256,
                      use_eth: bool, receiver: address, callbacker: address, callback_sig: bytes32) -> uint256:
            assert i != j  # dev: coin index out of range
            assert i < N_COINS  # dev: coin index out of range
            assert j < N_COINS  # dev: coin index out of range
            assert dx > 0  # dev: do not exchange 0 coins
        
            A_gamma: uint256[2] = self._A_gamma()
            xp: uint256[N_COINS] = self.balances
            p: uint256 = 0
            dy: uint256 = 0
        
            in_coin: address = self.coins[i]
            out_coin: address = self.coins[j]
        
            y: uint256 = xp[j]
            x0: uint256 = xp[i]
            xp[i] = x0 + dx
            self.balances[i] = xp[i]
        
            price_scale: uint256 = self.price_scale
            precisions: uint256[2] = self._get_precisions()
        
            xp = [xp[0] * precisions[0], xp[1] * price_scale * precisions[1] / PRECISION]
        
            prec_i: uint256 = precisions[0]
            prec_j: uint256 = precisions[1]
            if i == 1:
                prec_i = precisions[1]
                prec_j = precisions[0]
        
            # In case ramp is happening
            t: uint256 = self.future_A_gamma_time
            if t > 0:
                x0 *= prec_i
                if i > 0:
                    x0 = x0 * price_scale / PRECISION
                x1: uint256 = xp[i]  # Back up old value in xp
                xp[i] = x0
                self.D = self.newton_D(A_gamma[0], A_gamma[1], xp)
                xp[i] = x1  # And restore
                if block.timestamp >= t:
                    self.future_A_gamma_time = 1
        
            dy = xp[j] - self.newton_y(A_gamma[0], A_gamma[1], xp, self.D, j)
            # Not defining new "y" here to have less variables / make subsequent calls cheaper
            xp[j] -= dy
            dy -= 1
        
            if j > 0:
                dy = dy * PRECISION / price_scale
            dy /= prec_j
        
            dy -= self._fee(xp) * dy / 10**10
            assert dy >= min_dy, "Slippage"
            y -= dy
        
            self.balances[j] = y
        
            # Do transfers in and out together
            # XXX coin vs ETH
            if use_eth and in_coin == WETH20:
                assert mvalue == dx  # dev: incorrect eth amount
            else:
                assert mvalue == 0  # dev: nonzero eth amount
                if callback_sig == EMPTY_BYTES32:
                    response: Bytes[32] = raw_call(
                        in_coin,
                        _abi_encode(
                            sender, self, dx, method_id=method_id("transferFrom(address,address,uint256)")
                        ),
                        max_outsize=32,
                    )
                    if len(response) != 0:
                        assert convert(response, bool)  # dev: failed transfer
                else:
                    b: uint256 = ERC20(in_coin).balanceOf(self)
                    raw_call(
                        callbacker,
                        concat(slice(callback_sig, 0, 4), _abi_encode(sender, receiver, in_coin, dx, dy))
                    )
                    assert ERC20(in_coin).balanceOf(self) - b == dx  # dev: callback didn't give us coins
                if in_coin == WETH20:
                    WETH(WETH20).withdraw(dx)
        
            if use_eth and out_coin == WETH20:
                raw_call(receiver, b"", value=dy)
            else:
                if out_coin == WETH20:
                    WETH(WETH20).deposit(value=dy)
                response: Bytes[32] = raw_call(
                    out_coin,
                    _abi_encode(receiver, dy, method_id=method_id("transfer(address,uint256)")),
                    max_outsize=32,
                )
                if len(response) != 0:
                    assert convert(response, bool)
        
            y *= prec_j
            if j > 0:
                y = y * price_scale / PRECISION
            xp[j] = y
        
            # Calculate price
            if dx > 10**5 and dy > 10**5:
                _dx: uint256 = dx * prec_i
                _dy: uint256 = dy * prec_j
                if i == 0:
                    p = _dx * 10**18 / _dy
                else:  # j == 0
                    p = _dy * 10**18 / _dx
        
            self.tweak_price(A_gamma, xp, p, 0)
        
            log TokenExchange(sender, i, dx, j, dy)
        
            return dy
        
        
        @view
        @internal
        def _calc_token_fee(amounts: uint256[N_COINS], xp: uint256[N_COINS]) -> uint256:
            # fee = sum(amounts_i - avg(amounts)) * fee' / sum(amounts)
            fee: uint256 = self._fee(xp) * N_COINS / (4 * (N_COINS-1))
            S: uint256 = 0
            for _x in amounts:
                S += _x
            avg: uint256 = S / N_COINS
            Sdiff: uint256 = 0
            for _x in amounts:
                if _x > avg:
                    Sdiff += _x - avg
                else:
                    Sdiff += avg - _x
            return fee * Sdiff / S + NOISE_FEE
        
        
        @internal
        @view
        def _calc_withdraw_one_coin(A_gamma: uint256[2], token_amount: uint256, i: uint256, update_D: bool,
                                    calc_price: bool) -> (uint256, uint256, uint256, uint256[N_COINS]):
            token_supply: uint256 = CurveToken(self.token).totalSupply()
            assert token_amount <= token_supply  # dev: token amount more than supply
            assert i < N_COINS  # dev: coin out of range
        
            xx: uint256[N_COINS] = self.balances
            D0: uint256 = 0
            precisions: uint256[2] = self._get_precisions()
        
            price_scale_i: uint256 = self.price_scale * precisions[1]
            xp: uint256[N_COINS] = [xx[0] * precisions[0], xx[1] * price_scale_i / PRECISION]
            if i == 0:
                price_scale_i = PRECISION * precisions[0]
        
            if update_D:
                D0 = self.newton_D(A_gamma[0], A_gamma[1], xp)
            else:
                D0 = self.D
        
            D: uint256 = D0
        
            # Charge the fee on D, not on y, e.g. reducing invariant LESS than charging the user
            fee: uint256 = self._fee(xp)
            dD: uint256 = token_amount * D / token_supply
            D -= (dD - (fee * dD / (2 * 10**10) + 1))
            y: uint256 = self.newton_y(A_gamma[0], A_gamma[1], xp, D, i)
            dy: uint256 = (xp[i] - y) * PRECISION / price_scale_i
            xp[i] = y
        
            # Price calc
            p: uint256 = 0
            if calc_price and dy > 10**5 and token_amount > 10**5:
                # p_i = dD / D0 * sum'(p_k * x_k) / (dy - dD / D0 * y0)
                S: uint256 = 0
                precision: uint256 = precisions[0]
                if i == 1:
                    S = xx[0] * precisions[0]
                    precision = precisions[1]
                else:
                    S = xx[1] * precisions[1]
                S = S * dD / D0
                p = S * PRECISION / (dy * precision - dD * xx[i] * precision / D0)
                if i == 0:
                    p = (10**18)**2 / p
        
            return dy, p, D, xp
        
        
        @internal
        @pure
        def sqrt_int(x: uint256) -> uint256:
            """
            Originating from: https://github.com/vyperlang/vyper/issues/1266
            """
        
            if x == 0:
                return 0
        
            z: uint256 = (x + 10**18) / 2
            y: uint256 = x
        
            for i in range(256):
                if z == y:
                    return y
                y = z
                z = (x * 10**18 / z + z) / 2
        
            raise "Did not converge"
        
        
        # External Functions
        
        
        @payable
        @external
        @nonreentrant('lock')
        def exchange(i: uint256, j: uint256, dx: uint256, min_dy: uint256,
                     use_eth: bool = False, receiver: address = msg.sender) -> uint256:
            """
            Exchange using WETH by default
            """
            return self._exchange(msg.sender, msg.value, i, j, dx, min_dy, use_eth, receiver, ZERO_ADDRESS, EMPTY_BYTES32)
        
        
        @payable
        @external
        @nonreentrant('lock')
        def exchange_underlying(i: uint256, j: uint256, dx: uint256, min_dy: uint256,
                                receiver: address = msg.sender) -> uint256:
            """
            Exchange using ETH
            """
            return self._exchange(msg.sender, msg.value, i, j, dx, min_dy, True, receiver, ZERO_ADDRESS, EMPTY_BYTES32)
        
        
        @payable
        @external
        @nonreentrant('lock')
        def exchange_extended(i: uint256, j: uint256, dx: uint256, min_dy: uint256,
                              use_eth: bool, sender: address, receiver: address, cb: bytes32) -> uint256:
            assert cb != EMPTY_BYTES32  # dev: No callback specified
            return self._exchange(sender, msg.value, i, j, dx, min_dy, use_eth, receiver, msg.sender, cb)
        
        
        @payable
        @external
        @nonreentrant('lock')
        def add_liquidity(amounts: uint256[N_COINS], min_mint_amount: uint256,
                          use_eth: bool = False, receiver: address = msg.sender) -> uint256:
            assert amounts[0] > 0 or amounts[1] > 0  # dev: no coins to add
        
            A_gamma: uint256[2] = self._A_gamma()
        
            xp: uint256[N_COINS] = self.balances
            amountsp: uint256[N_COINS] = empty(uint256[N_COINS])
            xx: uint256[N_COINS] = empty(uint256[N_COINS])
            d_token: uint256 = 0
            d_token_fee: uint256 = 0
            old_D: uint256 = 0
        
            xp_old: uint256[N_COINS] = xp
        
            for i in range(N_COINS):
                bal: uint256 = xp[i] + amounts[i]
                xp[i] = bal
                self.balances[i] = bal
            xx = xp
        
            precisions: uint256[2] = self._get_precisions()
        
            price_scale: uint256 = self.price_scale * precisions[1]
            xp = [xp[0] * precisions[0], xp[1] * price_scale / PRECISION]
            xp_old = [xp_old[0] * precisions[0], xp_old[1] * price_scale / PRECISION]
        
            if not use_eth:
                assert msg.value == 0  # dev: nonzero eth amount
        
            for i in range(N_COINS):
                coin: address = self.coins[i]
                if use_eth and coin == WETH20:
                    assert msg.value == amounts[i]  # dev: incorrect eth amount
                if amounts[i] > 0:
                    if (not use_eth) or (coin != WETH20):
                        response: Bytes[32] = raw_call(
                            coin,
                            _abi_encode(
                                msg.sender,
                                self,
                                amounts[i],
                                method_id=method_id("transferFrom(address,address,uint256)"),
                            ),
                            max_outsize=32,
                        )
                        if len(response) != 0:
                            assert convert(response, bool)  # dev: failed transfer
                        if coin == WETH20:
                            WETH(WETH20).withdraw(amounts[i])
                    amountsp[i] = xp[i] - xp_old[i]
        
            t: uint256 = self.future_A_gamma_time
            if t > 0:
                old_D = self.newton_D(A_gamma[0], A_gamma[1], xp_old)
                if block.timestamp >= t:
                    self.future_A_gamma_time = 1
            else:
                old_D = self.D
        
            D: uint256 = self.newton_D(A_gamma[0], A_gamma[1], xp)
        
            lp_token: address = self.token
            token_supply: uint256 = CurveToken(lp_token).totalSupply()
            if old_D > 0:
                d_token = token_supply * D / old_D - token_supply
            else:
                d_token = self.get_xcp(D)  # making initial virtual price equal to 1
            assert d_token > 0  # dev: nothing minted
        
            if old_D > 0:
                d_token_fee = self._calc_token_fee(amountsp, xp) * d_token / 10**10 + 1
                d_token -= d_token_fee
                token_supply += d_token
                CurveToken(lp_token).mint(receiver, d_token)
        
                # Calculate price
                # p_i * (dx_i - dtoken / token_supply * xx_i) = sum{k!=i}(p_k * (dtoken / token_supply * xx_k - dx_k))
                # Simplified for 2 coins
                p: uint256 = 0
                if d_token > 10**5:
                    if amounts[0] == 0 or amounts[1] == 0:
                        S: uint256 = 0
                        precision: uint256 = 0
                        ix: uint256 = 0
                        if amounts[0] == 0:
                            S = xx[0] * precisions[0]
                            precision = precisions[1]
                            ix = 1
                        else:
                            S = xx[1] * precisions[1]
                            precision = precisions[0]
                        S = S * d_token / token_supply
                        p = S * PRECISION / (amounts[ix] * precision - d_token * xx[ix] * precision / token_supply)
                        if ix == 0:
                            p = (10**18)**2 / p
        
                self.tweak_price(A_gamma, xp, p, D)
        
            else:
                self.D = D
                self.virtual_price = 10**18
                self.xcp_profit = 10**18
                CurveToken(lp_token).mint(receiver, d_token)
        
            assert d_token >= min_mint_amount, "Slippage"
        
            log AddLiquidity(receiver, amounts, d_token_fee, token_supply)
        
            return d_token
        
        
        @external
        @nonreentrant('lock')
        def remove_liquidity(_amount: uint256, min_amounts: uint256[N_COINS],
                             use_eth: bool = False, receiver: address = msg.sender):
            """
            This withdrawal method is very safe, does no complex math
            """
            lp_token: address = self.token
            total_supply: uint256 = CurveToken(lp_token).totalSupply()
            CurveToken(lp_token).burnFrom(msg.sender, _amount)
            balances: uint256[N_COINS] = self.balances
            amount: uint256 = _amount - 1  # Make rounding errors favoring other LPs a tiny bit
        
            for i in range(N_COINS):
                d_balance: uint256 = balances[i] * amount / total_supply
                assert d_balance >= min_amounts[i]
                self.balances[i] = balances[i] - d_balance
                balances[i] = d_balance  # now it's the amounts going out
                coin: address = self.coins[i]
                if use_eth and coin == WETH20:
                    raw_call(receiver, b"", value=d_balance)
                else:
                    if coin == WETH20:
                        WETH(WETH20).deposit(value=d_balance)
                    response: Bytes[32] = raw_call(
                        coin,
                        _abi_encode(receiver, d_balance, method_id=method_id("transfer(address,uint256)")),
                        max_outsize=32,
                    )
                    if len(response) != 0:
                        assert convert(response, bool)
        
            D: uint256 = self.D
            self.D = D - D * amount / total_supply
        
            log RemoveLiquidity(msg.sender, balances, total_supply - _amount)
        
        
        @external
        @nonreentrant('lock')
        def remove_liquidity_one_coin(token_amount: uint256, i: uint256, min_amount: uint256,
                                      use_eth: bool = False, receiver: address = msg.sender) -> uint256:
            A_gamma: uint256[2] = self._A_gamma()
        
            dy: uint256 = 0
            D: uint256 = 0
            p: uint256 = 0
            xp: uint256[N_COINS] = empty(uint256[N_COINS])
            future_A_gamma_time: uint256 = self.future_A_gamma_time
            dy, p, D, xp = self._calc_withdraw_one_coin(A_gamma, token_amount, i, (future_A_gamma_time > 0), True)
            assert dy >= min_amount, "Slippage"
        
            if block.timestamp >= future_A_gamma_time:
                self.future_A_gamma_time = 1
        
            self.balances[i] -= dy
            CurveToken(self.token).burnFrom(msg.sender, token_amount)
        
            coin: address = self.coins[i]
            if use_eth and coin == WETH20:
                raw_call(receiver, b"", value=dy)
            else:
                if coin == WETH20:
                    WETH(WETH20).deposit(value=dy)
                response: Bytes[32] = raw_call(
                    coin,
                    _abi_encode(receiver, dy, method_id=method_id("transfer(address,uint256)")),
                    max_outsize=32,
                )
                if len(response) != 0:
                    assert convert(response, bool)
        
            self.tweak_price(A_gamma, xp, p, D)
        
            log RemoveLiquidityOne(msg.sender, token_amount, i, dy)
        
            return dy
        
        
        @external
        @nonreentrant('lock')
        def claim_admin_fees():
            self._claim_admin_fees()
        
        
        # Admin parameters
        @external
        def ramp_A_gamma(future_A: uint256, future_gamma: uint256, future_time: uint256):
            assert msg.sender == Factory(self.factory).admin()  # dev: only owner
            assert block.timestamp > self.initial_A_gamma_time + (MIN_RAMP_TIME-1)
            assert future_time > block.timestamp + (MIN_RAMP_TIME-1)  # dev: insufficient time
        
            A_gamma: uint256[2] = self._A_gamma()
            initial_A_gamma: uint256 = shift(A_gamma[0], 128)
            initial_A_gamma = bitwise_or(initial_A_gamma, A_gamma[1])
        
            assert future_A > MIN_A-1
            assert future_A < MAX_A+1
            assert future_gamma > MIN_GAMMA-1
            assert future_gamma < MAX_GAMMA+1
        
            ratio: uint256 = 10**18 * future_A / A_gamma[0]
            assert ratio < 10**18 * MAX_A_CHANGE + 1
            assert ratio > 10**18 / MAX_A_CHANGE - 1
        
            ratio = 10**18 * future_gamma / A_gamma[1]
            assert ratio < 10**18 * MAX_A_CHANGE + 1
            assert ratio > 10**18 / MAX_A_CHANGE - 1
        
            self.initial_A_gamma = initial_A_gamma
            self.initial_A_gamma_time = block.timestamp
        
            future_A_gamma: uint256 = shift(future_A, 128)
            future_A_gamma = bitwise_or(future_A_gamma, future_gamma)
            self.future_A_gamma_time = future_time
            self.future_A_gamma = future_A_gamma
        
            log RampAgamma(A_gamma[0], future_A, A_gamma[1], future_gamma, block.timestamp, future_time)
        
        
        @external
        def stop_ramp_A_gamma():
            assert msg.sender == Factory(self.factory).admin()  # dev: only owner
        
            A_gamma: uint256[2] = self._A_gamma()
            current_A_gamma: uint256 = shift(A_gamma[0], 128)
            current_A_gamma = bitwise_or(current_A_gamma, A_gamma[1])
            self.initial_A_gamma = current_A_gamma
            self.future_A_gamma = current_A_gamma
            self.initial_A_gamma_time = block.timestamp
            self.future_A_gamma_time = block.timestamp
            # now (block.timestamp < t1) is always False, so we return saved A
        
            log StopRampA(A_gamma[0], A_gamma[1], block.timestamp)
        
        
        @external
        def commit_new_parameters(
            _new_mid_fee: uint256,
            _new_out_fee: uint256,
            _new_admin_fee: uint256,
            _new_fee_gamma: uint256,
            _new_allowed_extra_profit: uint256,
            _new_adjustment_step: uint256,
            _new_ma_half_time: uint256,
            ):
            assert msg.sender == Factory(self.factory).admin()  # dev: only owner
            assert self.admin_actions_deadline == 0  # dev: active action
        
            new_mid_fee: uint256 = _new_mid_fee
            new_out_fee: uint256 = _new_out_fee
            new_admin_fee: uint256 = _new_admin_fee
            new_fee_gamma: uint256 = _new_fee_gamma
            new_allowed_extra_profit: uint256 = _new_allowed_extra_profit
            new_adjustment_step: uint256 = _new_adjustment_step
            new_ma_half_time: uint256 = _new_ma_half_time
        
            # Fees
            if new_out_fee < MAX_FEE+1:
                assert new_out_fee > MIN_FEE-1  # dev: fee is out of range
            else:
                new_out_fee = self.out_fee
            if new_mid_fee > MAX_FEE:
                new_mid_fee = self.mid_fee
            assert new_mid_fee <= new_out_fee  # dev: mid-fee is too high
            if new_admin_fee > MAX_ADMIN_FEE:
                new_admin_fee = self.admin_fee
        
            # AMM parameters
            if new_fee_gamma < 10**18:
                assert new_fee_gamma > 0  # dev: fee_gamma out of range [1 .. 10**18]
            else:
                new_fee_gamma = self.fee_gamma
            if new_allowed_extra_profit > 10**18:
                new_allowed_extra_profit = self.allowed_extra_profit
            if new_adjustment_step > 10**18:
                new_adjustment_step = self.adjustment_step
        
            # MA
            if new_ma_half_time < 7*86400:
                assert new_ma_half_time > 0  # dev: MA time should be longer than 1 second
            else:
                new_ma_half_time = self.ma_half_time
        
            _deadline: uint256 = block.timestamp + ADMIN_ACTIONS_DELAY
            self.admin_actions_deadline = _deadline
        
            self.future_admin_fee = new_admin_fee
            self.future_mid_fee = new_mid_fee
            self.future_out_fee = new_out_fee
            self.future_fee_gamma = new_fee_gamma
            self.future_allowed_extra_profit = new_allowed_extra_profit
            self.future_adjustment_step = new_adjustment_step
            self.future_ma_half_time = new_ma_half_time
        
            log CommitNewParameters(_deadline, new_admin_fee, new_mid_fee, new_out_fee,
                                    new_fee_gamma,
                                    new_allowed_extra_profit, new_adjustment_step,
                                    new_ma_half_time)
        
        
        @external
        @nonreentrant('lock')
        def apply_new_parameters():
            assert msg.sender == Factory(self.factory).admin()  # dev: only owner
            assert block.timestamp >= self.admin_actions_deadline  # dev: insufficient time
            assert self.admin_actions_deadline != 0  # dev: no active action
        
            self.admin_actions_deadline = 0
        
            admin_fee: uint256 = self.future_admin_fee
            if self.admin_fee != admin_fee:
                self._claim_admin_fees()
                self.admin_fee = admin_fee
        
            mid_fee: uint256 = self.future_mid_fee
            self.mid_fee = mid_fee
            out_fee: uint256 = self.future_out_fee
            self.out_fee = out_fee
            fee_gamma: uint256 = self.future_fee_gamma
            self.fee_gamma = fee_gamma
            allowed_extra_profit: uint256 = self.future_allowed_extra_profit
            self.allowed_extra_profit = allowed_extra_profit
            adjustment_step: uint256 = self.future_adjustment_step
            self.adjustment_step = adjustment_step
            ma_half_time: uint256 = self.future_ma_half_time
            self.ma_half_time = ma_half_time
        
            log NewParameters(admin_fee, mid_fee, out_fee,
                              fee_gamma,
                              allowed_extra_profit, adjustment_step,
                              ma_half_time)
        
        
        @external
        def revert_new_parameters():
            assert msg.sender == Factory(self.factory).admin()  # dev: only owner
        
            self.admin_actions_deadline = 0
        
        
        # View Methods
        
        
        @external
        @view
        def get_dy(i: uint256, j: uint256, dx: uint256) -> uint256:
            assert i != j  # dev: same input and output coin
            assert i < N_COINS  # dev: coin index out of range
            assert j < N_COINS  # dev: coin index out of range
        
            precisions: uint256[2] = self._get_precisions()
        
            price_scale: uint256 = self.price_scale * precisions[1]
            xp: uint256[N_COINS] = self.balances
        
            A_gamma: uint256[2] = self._A_gamma()
            D: uint256 = self.D
            if self.future_A_gamma_time > 0:
                D = self.newton_D(A_gamma[0], A_gamma[1], self.xp())
        
            xp[i] += dx
            xp = [xp[0] * precisions[0], xp[1] * price_scale / PRECISION]
        
            y: uint256 = self.newton_y(A_gamma[0], A_gamma[1], xp, D, j)
            dy: uint256 = xp[j] - y - 1
            xp[j] = y
            if j > 0:
                dy = dy * PRECISION / price_scale
            else:
                dy /= precisions[0]
            dy -= self._fee(xp) * dy / 10**10
        
            return dy
        
        
        @view
        @external
        def calc_token_amount(amounts: uint256[N_COINS]) -> uint256:
            token_supply: uint256 = CurveToken(self.token).totalSupply()
            precisions: uint256[2] = self._get_precisions()
            price_scale: uint256 = self.price_scale * precisions[1]
            A_gamma: uint256[2] = self._A_gamma()
            xp: uint256[N_COINS] = self.xp()
            amountsp: uint256[N_COINS] = [
                amounts[0] * precisions[0],
                amounts[1] * price_scale / PRECISION]
            D0: uint256 = self.D
            if self.future_A_gamma_time > 0:
                D0 = self.newton_D(A_gamma[0], A_gamma[1], xp)
            xp[0] += amountsp[0]
            xp[1] += amountsp[1]
            D: uint256 = self.newton_D(A_gamma[0], A_gamma[1], xp)
            d_token: uint256 = token_supply * D / D0 - token_supply
            d_token -= self._calc_token_fee(amountsp, xp) * d_token / 10**10 + 1
            return d_token
        
        
        @view
        @external
        def calc_withdraw_one_coin(token_amount: uint256, i: uint256) -> uint256:
            return self._calc_withdraw_one_coin(self._A_gamma(), token_amount, i, True, False)[0]
        
        
        @external
        @view
        def lp_price() -> uint256:
            """
            Approximate LP token price
            """
            return 2 * self.virtual_price * self.sqrt_int(self.internal_price_oracle()) / 10**18
        
        
        @view
        @external
        def A() -> uint256:
            return self._A_gamma()[0]
        
        
        @view
        @external
        def gamma() -> uint256:
            return self._A_gamma()[1]
        
        
        @external
        @view
        def fee() -> uint256:
            return self._fee(self.xp())
        
        
        @external
        @view
        def get_virtual_price() -> uint256:
            return 10**18 * self.get_xcp(self.D) / CurveToken(self.token).totalSupply()
        
        
        @external
        @view
        def price_oracle() -> uint256:
            return self.internal_price_oracle()
        
        
        # Initializer
        
        
        @external
        def initialize(
            A: uint256,
            gamma: uint256,
            mid_fee: uint256,
            out_fee: uint256,
            allowed_extra_profit: uint256,
            fee_gamma: uint256,
            adjustment_step: uint256,
            admin_fee: uint256,
            ma_half_time: uint256,
            initial_price: uint256,
            _token: address,
            _coins: address[N_COINS],
            _precisions: uint256,
        ):
            assert self.mid_fee == 0  # dev: check that we call it from factory
        
            self.factory = msg.sender
        
            # Pack A and gamma:
            # shifted A + gamma
            A_gamma: uint256 = shift(A, 128)
            A_gamma = bitwise_or(A_gamma, gamma)
            self.initial_A_gamma = A_gamma
            self.future_A_gamma = A_gamma
        
            self.mid_fee = mid_fee
            self.out_fee = out_fee
            self.allowed_extra_profit = allowed_extra_profit
            self.fee_gamma = fee_gamma
            self.adjustment_step = adjustment_step
            self.admin_fee = admin_fee
        
            self.price_scale = initial_price
            self._price_oracle = initial_price
            self.last_prices = initial_price
            self.last_prices_timestamp = block.timestamp
            self.ma_half_time = ma_half_time
        
            self.xcp_profit_a = 10**18
        
            self.token = _token
            self.coins = _coins
            self.PRECISIONS = _precisions

        File 2 of 5: SDT
        // SPDX-License-Identifier: NONE
        
        pragma solidity 0.6.7;
        
        
        
        // Part: Address
        
        /**
         * @dev Collection of functions related to the address type
         */
        library Address {
            /**
             * @dev Returns true if `account` is a contract.
             *
             * [IMPORTANT]
             * ====
             * It is unsafe to assume that an address for which this function returns
             * false is an externally-owned account (EOA) and not a contract.
             *
             * Among others, `isContract` will return false for the following
             * types of addresses:
             *
             *  - an externally-owned account
             *  - a contract in construction
             *  - an address where a contract will be created
             *  - an address where a contract lived, but was destroyed
             * ====
             */
            function isContract(address account) internal view returns (bool) {
                // This method relies on extcodesize, which returns 0 for contracts in
                // construction, since the code is only stored at the end of the
                // constructor execution.
        
                uint256 size;
                // solhint-disable-next-line no-inline-assembly
                assembly {
                    size := extcodesize(account)
                }
                return size > 0;
            }
        
            /**
             * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
             * `recipient`, forwarding all available gas and reverting on errors.
             *
             * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
             * of certain opcodes, possibly making contracts go over the 2300 gas limit
             * imposed by `transfer`, making them unable to receive funds via
             * `transfer`. {sendValue} removes this limitation.
             *
             * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
             *
             * IMPORTANT: because control is transferred to `recipient`, care must be
             * taken to not create reentrancy vulnerabilities. Consider using
             * {ReentrancyGuard} or the
             * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
             */
            function sendValue(address payable recipient, uint256 amount) internal {
                require(
                    address(this).balance >= amount,
                    "Address: insufficient balance"
                );
        
                // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
                (bool success, ) = recipient.call{value: amount}("");
                require(
                    success,
                    "Address: unable to send value, recipient may have reverted"
                );
            }
        
            /**
             * @dev Performs a Solidity function call using a low level `call`. A
             * plain`call` is an unsafe replacement for a function call: use this
             * function instead.
             *
             * If `target` reverts with a revert reason, it is bubbled up by this
             * function (like regular Solidity function calls).
             *
             * Returns the raw returned data. To convert to the expected return value,
             * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
             *
             * Requirements:
             *
             * - `target` must be a contract.
             * - calling `target` with `data` must not revert.
             *
             * _Available since v3.1._
             */
            function functionCall(address target, bytes memory data)
                internal
                returns (bytes memory)
            {
                return functionCall(target, data, "Address: low-level call failed");
            }
        
            /**
             * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
             * `errorMessage` as a fallback revert reason when `target` reverts.
             *
             * _Available since v3.1._
             */
            function functionCall(
                address target,
                bytes memory data,
                string memory errorMessage
            ) internal returns (bytes memory) {
                return functionCallWithValue(target, data, 0, errorMessage);
            }
        
            /**
             * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
             * but also transferring `value` wei to `target`.
             *
             * Requirements:
             *
             * - the calling contract must have an ETH balance of at least `value`.
             * - the called Solidity function must be `payable`.
             *
             * _Available since v3.1._
             */
            function functionCallWithValue(
                address target,
                bytes memory data,
                uint256 value
            ) internal returns (bytes memory) {
                return
                    functionCallWithValue(
                        target,
                        data,
                        value,
                        "Address: low-level call with value failed"
                    );
            }
        
            /**
             * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
             * with `errorMessage` as a fallback revert reason when `target` reverts.
             *
             * _Available since v3.1._
             */
            function functionCallWithValue(
                address target,
                bytes memory data,
                uint256 value,
                string memory errorMessage
            ) internal returns (bytes memory) {
                require(
                    address(this).balance >= value,
                    "Address: insufficient balance for call"
                );
                require(isContract(target), "Address: call to non-contract");
        
                // solhint-disable-next-line avoid-low-level-calls
                (bool success, bytes memory returndata) = target.call{value: value}(
                    data
                );
                return _verifyCallResult(success, returndata, errorMessage);
            }
        
            /**
             * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
             * but performing a static call.
             *
             * _Available since v3.3._
             */
            function functionStaticCall(address target, bytes memory data)
                internal
                view
                returns (bytes memory)
            {
                return
                    functionStaticCall(
                        target,
                        data,
                        "Address: low-level static call failed"
                    );
            }
        
            /**
             * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
             * but performing a static call.
             *
             * _Available since v3.3._
             */
            function functionStaticCall(
                address target,
                bytes memory data,
                string memory errorMessage
            ) internal view returns (bytes memory) {
                require(isContract(target), "Address: static call to non-contract");
        
                // solhint-disable-next-line avoid-low-level-calls
                (bool success, bytes memory returndata) = target.staticcall(data);
                return _verifyCallResult(success, returndata, errorMessage);
            }
        
            /**
             * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
             * but performing a delegate call.
             *
             * _Available since v3.3._
             */
            function functionDelegateCall(address target, bytes memory data)
                internal
                returns (bytes memory)
            {
                return
                    functionDelegateCall(
                        target,
                        data,
                        "Address: low-level delegate call failed"
                    );
            }
        
            /**
             * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
             * but performing a delegate call.
             *
             * _Available since v3.3._
             */
            function functionDelegateCall(
                address target,
                bytes memory data,
                string memory errorMessage
            ) internal returns (bytes memory) {
                require(isContract(target), "Address: delegate call to non-contract");
        
                // solhint-disable-next-line avoid-low-level-calls
                (bool success, bytes memory returndata) = target.delegatecall(data);
                return _verifyCallResult(success, returndata, errorMessage);
            }
        
            function _verifyCallResult(
                bool success,
                bytes memory returndata,
                string memory errorMessage
            ) private pure returns (bytes memory) {
                if (success) {
                    return returndata;
                } else {
                    // Look for revert reason and bubble it up if present
                    if (returndata.length > 0) {
                        // The easiest way to bubble the revert reason is using memory via assembly
        
                        // solhint-disable-next-line no-inline-assembly
                        assembly {
                            let returndata_size := mload(returndata)
                            revert(add(32, returndata), returndata_size)
                        }
                    } else {
                        revert(errorMessage);
                    }
                }
            }
        }
        
        // Part: Context
        
        /*
         * @dev Provides information about the current execution context, including the
         * sender of the transaction and its data. While these are generally available
         * via msg.sender and msg.data, they should not be accessed in such a direct
         * manner, since when dealing with GSN meta-transactions the account sending and
         * paying for execution may not be the actual sender (as far as an application
         * is concerned).
         *
         * This contract is only required for intermediate, library-like contracts.
         */
        abstract contract Context {
            function _msgSender() internal virtual view returns (address payable) {
                return msg.sender;
            }
        
            function _msgData() internal virtual view returns (bytes memory) {
                this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
                return msg.data;
            }
        }
        
        // Part: IERC20
        
        /**
         * @dev Interface of the ERC20 standard as defined in the EIP.
         */
        interface IERC20 {
            /**
             * @dev Returns the amount of tokens in existence.
             */
            function totalSupply() external view returns (uint256);
        
            /**
             * @dev Returns the amount of tokens owned by `account`.
             */
            function balanceOf(address account) external view returns (uint256);
        
            /**
             * @dev Moves `amount` tokens from the caller's account to `recipient`.
             *
             * Returns a boolean value indicating whether the operation succeeded.
             *
             * Emits a {Transfer} event.
             */
            function transfer(address recipient, uint256 amount)
                external
                returns (bool);
        
            /**
             * @dev Returns the remaining number of tokens that `spender` will be
             * allowed to spend on behalf of `owner` through {transferFrom}. This is
             * zero by default.
             *
             * This value changes when {approve} or {transferFrom} are called.
             */
            function allowance(address owner, address spender)
                external
                view
                returns (uint256);
        
            /**
             * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
             *
             * Returns a boolean value indicating whether the operation succeeded.
             *
             * IMPORTANT: Beware that changing an allowance with this method brings the risk
             * that someone may use both the old and the new allowance by unfortunate
             * transaction ordering. One possible solution to mitigate this race
             * condition is to first reduce the spender's allowance to 0 and set the
             * desired value afterwards:
             * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
             *
             * Emits an {Approval} event.
             */
            function approve(address spender, uint256 amount) external returns (bool);
        
            /**
             * @dev Moves `amount` tokens from `sender` to `recipient` using the
             * allowance mechanism. `amount` is then deducted from the caller's
             * allowance.
             *
             * Returns a boolean value indicating whether the operation succeeded.
             *
             * Emits a {Transfer} event.
             */
            function transferFrom(
                address sender,
                address recipient,
                uint256 amount
            ) external returns (bool);
        
            /**
             * @dev Emitted when `value` tokens are moved from one account (`from`) to
             * another (`to`).
             *
             * Note that `value` may be zero.
             */
            event Transfer(address indexed from, address indexed to, uint256 value);
        
            /**
             * @dev Emitted when the allowance of a `spender` for an `owner` is set by
             * a call to {approve}. `value` is the new allowance.
             */
            event Approval(
                address indexed owner,
                address indexed spender,
                uint256 value
            );
        }
        
        // Part: SafeMath
        
        /**
         * @dev Wrappers over Solidity's arithmetic operations with added overflow
         * checks.
         *
         * Arithmetic operations in Solidity wrap on overflow. This can easily result
         * in bugs, because programmers usually assume that an overflow raises an
         * error, which is the standard behavior in high level programming languages.
         * `SafeMath` restores this intuition by reverting the transaction when an
         * operation overflows.
         *
         * Using this library instead of the unchecked operations eliminates an entire
         * class of bugs, so it's recommended to use it always.
         */
        library SafeMath {
            /**
             * @dev Returns the addition of two unsigned integers, reverting on overflow.
             *
             * Counterpart to Solidity's `+` operator.
             *
             * Requirements:
             * - Addition cannot overflow.
             */
            function add(uint256 a, uint256 b) internal pure returns (uint256) {
                uint256 c = a + b;
                require(c >= a, "add: +");
        
                return c;
            }
        
            /**
             * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow.
             *
             * Counterpart to Solidity's `+` operator.
             *
             * Requirements:
             * - Addition cannot overflow.
             */
            function add(
                uint256 a,
                uint256 b,
                string memory errorMessage
            ) internal pure returns (uint256) {
                uint256 c = a + b;
                require(c >= a, errorMessage);
        
                return c;
            }
        
            /**
             * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative).
             *
             * Counterpart to Solidity's `-` operator.
             *
             * Requirements:
             * - Subtraction cannot underflow.
             */
            function sub(uint256 a, uint256 b) internal pure returns (uint256) {
                return sub(a, b, "sub: -");
            }
        
            /**
             * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative).
             *
             * Counterpart to Solidity's `-` operator.
             *
             * Requirements:
             * - Subtraction cannot underflow.
             */
            function sub(
                uint256 a,
                uint256 b,
                string memory errorMessage
            ) internal pure returns (uint256) {
                require(b <= a, errorMessage);
                uint256 c = a - b;
        
                return c;
            }
        
            /**
             * @dev Returns the multiplication of two unsigned integers, reverting on overflow.
             *
             * Counterpart to Solidity's `*` operator.
             *
             * Requirements:
             * - Multiplication cannot overflow.
             */
            function mul(uint256 a, uint256 b) internal pure returns (uint256) {
                // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
                // benefit is lost if 'b' is also tested.
                // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
                if (a == 0) {
                    return 0;
                }
        
                uint256 c = a * b;
                require(c / a == b, "mul: *");
        
                return c;
            }
        
            /**
             * @dev Returns the multiplication of two unsigned integers, reverting on overflow.
             *
             * Counterpart to Solidity's `*` operator.
             *
             * Requirements:
             * - Multiplication cannot overflow.
             */
            function mul(
                uint256 a,
                uint256 b,
                string memory errorMessage
            ) internal pure returns (uint256) {
                // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
                // benefit is lost if 'b' is also tested.
                // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
                if (a == 0) {
                    return 0;
                }
        
                uint256 c = a * b;
                require(c / a == b, errorMessage);
        
                return c;
            }
        
            /**
             * @dev Returns the integer division of two unsigned integers.
             * Reverts on division by zero. The result is rounded towards zero.
             *
             * Counterpart to Solidity's `/` operator. Note: this function uses a
             * `revert` opcode (which leaves remaining gas untouched) while Solidity
             * uses an invalid opcode to revert (consuming all remaining gas).
             *
             * Requirements:
             * - The divisor cannot be zero.
             */
            function div(uint256 a, uint256 b) internal pure returns (uint256) {
                return div(a, b, "div: /");
            }
        
            /**
             * @dev Returns the integer division of two unsigned integers.
             * Reverts with custom message on division by zero. The result is rounded towards zero.
             *
             * Counterpart to Solidity's `/` operator. Note: this function uses a
             * `revert` opcode (which leaves remaining gas untouched) while Solidity
             * uses an invalid opcode to revert (consuming all remaining gas).
             *
             * Requirements:
             * - The divisor cannot be zero.
             */
            function div(
                uint256 a,
                uint256 b,
                string memory errorMessage
            ) internal pure returns (uint256) {
                // Solidity only automatically asserts when dividing by 0
                require(b > 0, errorMessage);
                uint256 c = a / b;
                // assert(a == b * c + a % b); // There is no case in which this doesn't hold
        
                return c;
            }
        
            /**
             * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
             * Reverts when dividing by zero.
             *
             * Counterpart to Solidity's `%` operator. This function uses a `revert`
             * opcode (which leaves remaining gas untouched) while Solidity uses an
             * invalid opcode to revert (consuming all remaining gas).
             *
             * Requirements:
             * - The divisor cannot be zero.
             */
            function mod(uint256 a, uint256 b) internal pure returns (uint256) {
                return mod(a, b, "mod: %");
            }
        
            /**
             * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
             * Reverts with custom message when dividing by zero.
             *
             * Counterpart to Solidity's `%` operator. This function uses a `revert`
             * opcode (which leaves remaining gas untouched) while Solidity uses an
             * invalid opcode to revert (consuming all remaining gas).
             *
             * Requirements:
             * - The divisor cannot be zero.
             */
            function mod(
                uint256 a,
                uint256 b,
                string memory errorMessage
            ) internal pure returns (uint256) {
                require(b != 0, errorMessage);
                return a % b;
            }
        }
        
        // Part: ERC20
        
        // File: contracts/token/ERC20/ERC20.sol
        
        /**
         * @dev Implementation of the {IERC20} interface.
         *
         * This implementation is agnostic to the way tokens are created. This means
         * that a supply mechanism has to be added in a derived contract using {_mint}.
         * For a generic mechanism see {ERC20PresetMinterPauser}.
         *
         * TIP: For a detailed writeup see our guide
         * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
         * to implement supply mechanisms].
         *
         * We have followed general OpenZeppelin guidelines: functions revert instead
         * of returning `false` on failure. This behavior is nonetheless conventional
         * and does not conflict with the expectations of ERC20 applications.
         *
         * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
         * This allows applications to reconstruct the allowance for all accounts just
         * by listening to said events. Other implementations of the EIP may not emit
         * these events, as it isn't required by the specification.
         *
         * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
         * functions have been added to mitigate the well-known issues around setting
         * allowances. See {IERC20-approve}.
         */
        contract ERC20 is Context, IERC20 {
            using SafeMath for uint256;
            using Address for address;
        
            mapping(address => uint256) private _balances;
        
            mapping(address => mapping(address => uint256)) private _allowances;
        
            uint256 private _totalSupply;
        
            string private _name;
            string private _symbol;
            uint8 private _decimals;
        
            /**
             * @dev Sets the values for {name} and {symbol}, initializes {decimals} with
             * a default value of 18.
             *
             * To select a different value for {decimals}, use {_setupDecimals}.
             *
             * All three of these values are immutable: they can only be set once during
             * construction.
             */
            constructor(string memory name, string memory symbol) public {
                _name = name;
                _symbol = symbol;
                _decimals = 18;
            }
        
            /**
             * @dev Returns the name of the token.
             */
            function name() public view returns (string memory) {
                return _name;
            }
        
            /**
             * @dev Returns the symbol of the token, usually a shorter version of the
             * name.
             */
            function symbol() public view returns (string memory) {
                return _symbol;
            }
        
            /**
             * @dev Returns the number of decimals used to get its user representation.
             * For example, if `decimals` equals `2`, a balance of `505` tokens should
             * be displayed to a user as `5,05` (`505 / 10 ** 2`).
             *
             * Tokens usually opt for a value of 18, imitating the relationship between
             * Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
             * called.
             *
             * NOTE: This information is only used for _display_ purposes: it in
             * no way affects any of the arithmetic of the contract, including
             * {IERC20-balanceOf} and {IERC20-transfer}.
             */
            function decimals() public view returns (uint8) {
                return _decimals;
            }
        
            /**
             * @dev See {IERC20-totalSupply}.
             */
            function totalSupply() public override view returns (uint256) {
                return _totalSupply;
            }
        
            /**
             * @dev See {IERC20-balanceOf}.
             */
            function balanceOf(address account) public override view returns (uint256) {
                return _balances[account];
            }
        
            /**
             * @dev See {IERC20-transfer}.
             *
             * Requirements:
             *
             * - `recipient` cannot be the zero address.
             * - the caller must have a balance of at least `amount`.
             */
            function transfer(address recipient, uint256 amount)
                public
                virtual
                override
                returns (bool)
            {
                _transfer(_msgSender(), recipient, amount);
                return true;
            }
        
            /**
             * @dev See {IERC20-allowance}.
             */
            function allowance(address owner, address spender)
                public
                virtual
                override
                view
                returns (uint256)
            {
                return _allowances[owner][spender];
            }
        
            /**
             * @dev See {IERC20-approve}.
             *
             * Requirements:
             *
             * - `spender` cannot be the zero address.
             */
            function approve(address spender, uint256 amount)
                public
                virtual
                override
                returns (bool)
            {
                _approve(_msgSender(), spender, amount);
                return true;
            }
        
            /**
             * @dev See {IERC20-transferFrom}.
             *
             * Emits an {Approval} event indicating the updated allowance. This is not
             * required by the EIP. See the note at the beginning of {ERC20};
             *
             * Requirements:
             * - `sender` and `recipient` cannot be the zero address.
             * - `sender` must have a balance of at least `amount`.
             * - the caller must have allowance for ``sender``'s tokens of at least
             * `amount`.
             */
            function transferFrom(
                address sender,
                address recipient,
                uint256 amount
            ) public virtual override returns (bool) {
                _transfer(sender, recipient, amount);
                _approve(
                    sender,
                    _msgSender(),
                    _allowances[sender][_msgSender()].sub(
                        amount,
                        "ERC20: transfer amount exceeds allowance"
                    )
                );
                return true;
            }
        
            /**
             * @dev Atomically increases the allowance granted to `spender` by the caller.
             *
             * This is an alternative to {approve} that can be used as a mitigation for
             * problems described in {IERC20-approve}.
             *
             * Emits an {Approval} event indicating the updated allowance.
             *
             * Requirements:
             *
             * - `spender` cannot be the zero address.
             */
            function increaseAllowance(address spender, uint256 addedValue)
                public
                virtual
                returns (bool)
            {
                _approve(
                    _msgSender(),
                    spender,
                    _allowances[_msgSender()][spender].add(addedValue)
                );
                return true;
            }
        
            /**
             * @dev Atomically decreases the allowance granted to `spender` by the caller.
             *
             * This is an alternative to {approve} that can be used as a mitigation for
             * problems described in {IERC20-approve}.
             *
             * Emits an {Approval} event indicating the updated allowance.
             *
             * Requirements:
             *
             * - `spender` cannot be the zero address.
             * - `spender` must have allowance for the caller of at least
             * `subtractedValue`.
             */
            function decreaseAllowance(address spender, uint256 subtractedValue)
                public
                virtual
                returns (bool)
            {
                _approve(
                    _msgSender(),
                    spender,
                    _allowances[_msgSender()][spender].sub(
                        subtractedValue,
                        "ERC20: decreased allowance below zero"
                    )
                );
                return true;
            }
        
            /**
             * @dev Moves tokens `amount` from `sender` to `recipient`.
             *
             * This is internal function is equivalent to {transfer}, and can be used to
             * e.g. implement automatic token fees, slashing mechanisms, etc.
             *
             * Emits a {Transfer} event.
             *
             * Requirements:
             *
             * - `sender` cannot be the zero address.
             * - `recipient` cannot be the zero address.
             * - `sender` must have a balance of at least `amount`.
             */
            function _transfer(
                address sender,
                address recipient,
                uint256 amount
            ) internal virtual {
                require(sender != address(0), "ERC20: transfer from the zero address");
                require(recipient != address(0), "ERC20: transfer to the zero address");
        
                _beforeTokenTransfer(sender, recipient, amount);
        
                _balances[sender] = _balances[sender].sub(
                    amount,
                    "ERC20: transfer amount exceeds balance"
                );
                _balances[recipient] = _balances[recipient].add(amount);
                emit Transfer(sender, recipient, amount);
            }
        
            /** @dev Creates `amount` tokens and assigns them to `account`, increasing
             * the total supply.
             *
             * Emits a {Transfer} event with `from` set to the zero address.
             *
             * Requirements
             *
             * - `to` cannot be the zero address.
             */
            function _mint(address account, uint256 amount) internal virtual {
                require(account != address(0), "ERC20: mint to the zero address");
        
                _beforeTokenTransfer(address(0), account, amount);
        
                _totalSupply = _totalSupply.add(amount);
                _balances[account] = _balances[account].add(amount);
                emit Transfer(address(0), account, amount);
            }
        
            /**
             * @dev Destroys `amount` tokens from `account`, reducing the
             * total supply.
             *
             * Emits a {Transfer} event with `to` set to the zero address.
             *
             * Requirements
             *
             * - `account` cannot be the zero address.
             * - `account` must have at least `amount` tokens.
             */
            function _burn(address account, uint256 amount) internal virtual {
                require(account != address(0), "ERC20: burn from the zero address");
        
                _beforeTokenTransfer(account, address(0), amount);
        
                _balances[account] = _balances[account].sub(
                    amount,
                    "ERC20: burn amount exceeds balance"
                );
                _totalSupply = _totalSupply.sub(amount);
                emit Transfer(account, address(0), amount);
            }
        
            /**
             * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
             *
             * This internal function is equivalent to `approve`, and can be used to
             * e.g. set automatic allowances for certain subsystems, etc.
             *
             * Emits an {Approval} event.
             *
             * Requirements:
             *
             * - `owner` cannot be the zero address.
             * - `spender` cannot be the zero address.
             */
            function _approve(
                address owner,
                address spender,
                uint256 amount
            ) internal virtual {
                require(owner != address(0), "ERC20: approve from the zero address");
                require(spender != address(0), "ERC20: approve to the zero address");
        
                _allowances[owner][spender] = amount;
                emit Approval(owner, spender, amount);
            }
        
            /**
             * @dev Sets {decimals} to a value other than the default one of 18.
             *
             * WARNING: This function should only be called from the constructor. Most
             * applications that interact with token contracts will not expect
             * {decimals} to ever change, and may work incorrectly if it does.
             */
            function _setupDecimals(uint8 decimals_) internal {
                _decimals = decimals_;
            }
        
            /**
             * @dev Hook that is called before any transfer of tokens. This includes
             * minting and burning.
             *
             * Calling conditions:
             *
             * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
             * will be to transferred to `to`.
             * - when `from` is zero, `amount` tokens will be minted for `to`.
             * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
             * - `from` and `to` are never both zero.
             *
             * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
             */
            function _beforeTokenTransfer(
                address from,
                address to,
                uint256 amount
            ) internal virtual {}
        }
        
        // Part: Ownable
        
        /**
         * @dev Contract module which provides a basic access control mechanism, where
         * there is an account (an owner) that can be granted exclusive access to
         * specific functions.
         *
         * By default, the owner account will be the one that deploys the contract. This
         * can later be changed with {transferOwnership}.
         *
         * This module is used through inheritance. It will make available the modifier
         * `onlyOwner`, which can be applied to your functions to restrict their use to
         * the owner.
         */
        contract Ownable is Context {
            address private _owner;
        
            event OwnershipTransferred(
                address indexed previousOwner,
                address indexed newOwner
            );
        
            /**
             * @dev Initializes the contract setting the deployer as the initial owner.
             */
            constructor() internal {
                address msgSender = _msgSender();
                _owner = msgSender;
                emit OwnershipTransferred(address(0), msgSender);
            }
        
            /**
             * @dev Returns the address of the current owner.
             */
            function owner() public view returns (address) {
                return _owner;
            }
        
            /**
             * @dev Throws if called by any account other than the owner.
             */
            modifier onlyOwner() {
                require(_owner == _msgSender(), "Ownable: caller is not the owner");
                _;
            }
        
            /**
             * @dev Leaves the contract without owner. It will not be possible to call
             * `onlyOwner` functions anymore. Can only be called by the current owner.
             *
             * NOTE: Renouncing ownership will leave the contract without an owner,
             * thereby removing any functionality that is only available to the owner.
             */
            function renounceOwnership() public virtual onlyOwner {
                emit OwnershipTransferred(_owner, address(0));
                _owner = address(0);
            }
        
            /**
             * @dev Transfers ownership of the contract to a new account (`newOwner`).
             * Can only be called by the current owner.
             */
            function transferOwnership(address newOwner) public virtual onlyOwner {
                require(
                    newOwner != address(0),
                    "Ownable: new owner is the zero address"
                );
                emit OwnershipTransferred(_owner, newOwner);
                _owner = newOwner;
            }
        }
        
        // File: SDT.sol
        
        // StakeDaoToken with Governance.
        contract SDT is ERC20("Stake DAO Token", "SDT"), Ownable {
            /// @notice Creates `_amount` token to `_to`. Must only be called by the owner (MasterChef).
            function mint(address _to, uint256 _amount) public onlyOwner {
                _mint(_to, _amount);
            }
        }
        

        File 3 of 5: Vyper_contract
        # @version 0.3.1
        # (c) Curve.Fi, 2021
        # Pool for two crypto assets
        
        # Universal implementation which can use both ETH and ERC20s
        from vyper.interfaces import ERC20
        
        
        interface Factory:
            def admin() -> address: view
            def fee_receiver() -> address: view
        
        interface CurveToken:
            def totalSupply() -> uint256: view
            def mint(_to: address, _value: uint256) -> bool: nonpayable
            def mint_relative(_to: address, frac: uint256) -> uint256: nonpayable
            def burnFrom(_to: address, _value: uint256) -> bool: nonpayable
        
        interface WETH:
            def deposit(): payable
            def withdraw(_amount: uint256): nonpayable
        
        
        # Events
        event TokenExchange:
            buyer: indexed(address)
            sold_id: uint256
            tokens_sold: uint256
            bought_id: uint256
            tokens_bought: uint256
        
        event AddLiquidity:
            provider: indexed(address)
            token_amounts: uint256[N_COINS]
            fee: uint256
            token_supply: uint256
        
        event RemoveLiquidity:
            provider: indexed(address)
            token_amounts: uint256[N_COINS]
            token_supply: uint256
        
        event RemoveLiquidityOne:
            provider: indexed(address)
            token_amount: uint256
            coin_index: uint256
            coin_amount: uint256
        
        event CommitNewParameters:
            deadline: indexed(uint256)
            admin_fee: uint256
            mid_fee: uint256
            out_fee: uint256
            fee_gamma: uint256
            allowed_extra_profit: uint256
            adjustment_step: uint256
            ma_half_time: uint256
        
        event NewParameters:
            admin_fee: uint256
            mid_fee: uint256
            out_fee: uint256
            fee_gamma: uint256
            allowed_extra_profit: uint256
            adjustment_step: uint256
            ma_half_time: uint256
        
        event RampAgamma:
            initial_A: uint256
            future_A: uint256
            initial_gamma: uint256
            future_gamma: uint256
            initial_time: uint256
            future_time: uint256
        
        event StopRampA:
            current_A: uint256
            current_gamma: uint256
            time: uint256
        
        event ClaimAdminFee:
            admin: indexed(address)
            tokens: uint256
        
        
        ADMIN_ACTIONS_DELAY: constant(uint256) = 3 * 86400
        MIN_RAMP_TIME: constant(uint256) = 86400
        
        MAX_ADMIN_FEE: constant(uint256) = 10 * 10 ** 9
        MIN_FEE: constant(uint256) = 5 * 10 ** 5  # 0.5 bps
        MAX_FEE: constant(uint256) = 10 * 10 ** 9
        MAX_A_CHANGE: constant(uint256) = 10
        NOISE_FEE: constant(uint256) = 10**5  # 0.1 bps
        
        MIN_GAMMA: constant(uint256) = 10**10
        MAX_GAMMA: constant(uint256) = 2 * 10**16
        
        MIN_A: constant(uint256) = N_COINS**N_COINS * A_MULTIPLIER / 10
        MAX_A: constant(uint256) = N_COINS**N_COINS * A_MULTIPLIER * 100000
        
        EXP_PRECISION: constant(uint256) = 10**10
        
        N_COINS: constant(int128) = 2
        PRECISION: constant(uint256) = 10 ** 18  # The precision to convert to
        A_MULTIPLIER: constant(uint256) = 10000
        
        
        # Implementation can be changed by changing this constant
        WETH20: immutable(address)
        
        
        token: public(address)
        coins: public(address[N_COINS])
        
        price_scale: public(uint256)   # Internal price scale
        _price_oracle: uint256  # Price target given by MA
        
        last_prices: public(uint256)
        last_prices_timestamp: public(uint256)
        
        initial_A_gamma: public(uint256)
        future_A_gamma: public(uint256)
        initial_A_gamma_time: public(uint256)
        future_A_gamma_time: public(uint256)
        
        allowed_extra_profit: public(uint256)  # 2 * 10**12 - recommended value
        future_allowed_extra_profit: public(uint256)
        
        fee_gamma: public(uint256)
        future_fee_gamma: public(uint256)
        
        adjustment_step: public(uint256)
        future_adjustment_step: public(uint256)
        
        ma_half_time: public(uint256)
        future_ma_half_time: public(uint256)
        
        mid_fee: public(uint256)
        out_fee: public(uint256)
        admin_fee: public(uint256)
        future_mid_fee: public(uint256)
        future_out_fee: public(uint256)
        future_admin_fee: public(uint256)
        
        balances: public(uint256[N_COINS])
        D: public(uint256)
        
        factory: public(address)
        
        xcp_profit: public(uint256)
        xcp_profit_a: public(uint256)  # Full profit at last claim of admin fees
        virtual_price: public(uint256)  # Cached (fast to read) virtual price also used internally
        not_adjusted: bool
        
        admin_actions_deadline: public(uint256)
        
        # This must be changed for different N_COINS
        # For example:
        # N_COINS = 3 -> 1  (10**18 -> 10**18)
        # N_COINS = 4 -> 10**8  (10**18 -> 10**10)
        # PRICE_PRECISION_MUL: constant(uint256) = 1
        PRECISIONS: uint256  # packed
        
        
        @external
        def __init__(_weth: address):
            WETH20 = _weth
            self.mid_fee = 22022022
        
        
        @payable
        @external
        def __default__():
            pass
        
        
        # Internal Functions
        
        @internal
        @view
        def _get_precisions() -> uint256[2]:
            p0: uint256 = self.PRECISIONS
            p1: uint256 = 10 ** shift(p0, -8)
            p0 = 10 ** bitwise_and(p0, 255)
            return [p0, p1]
        
        
        @internal
        @view
        def xp() -> uint256[N_COINS]:
            precisions: uint256[2] = self._get_precisions()
            return [self.balances[0] * precisions[0],
                    self.balances[1] * precisions[1] * self.price_scale / PRECISION]
        
        
        @view
        @internal
        def _A_gamma() -> uint256[2]:
            t1: uint256 = self.future_A_gamma_time
        
            A_gamma_1: uint256 = self.future_A_gamma
            gamma1: uint256 = bitwise_and(A_gamma_1, 2**128-1)
            A1: uint256 = shift(A_gamma_1, -128)
        
            if block.timestamp < t1:
                # handle ramping up and down of A
                A_gamma_0: uint256 = self.initial_A_gamma
                t0: uint256 = self.initial_A_gamma_time
        
                # Less readable but more compact way of writing and converting to uint256
                # gamma0: uint256 = bitwise_and(A_gamma_0, 2**128-1)
                # A0: uint256 = shift(A_gamma_0, -128)
                # A1 = A0 + (A1 - A0) * (block.timestamp - t0) / (t1 - t0)
                # gamma1 = gamma0 + (gamma1 - gamma0) * (block.timestamp - t0) / (t1 - t0)
        
                t1 -= t0
                t0 = block.timestamp - t0
                t2: uint256 = t1 - t0
        
                A1 = (shift(A_gamma_0, -128) * t2 + A1 * t0) / t1
                gamma1 = (bitwise_and(A_gamma_0, 2**128-1) * t2 + gamma1 * t0) / t1
        
            return [A1, gamma1]
        
        
        @internal
        @view
        def _fee(xp: uint256[N_COINS]) -> uint256:
            """
            f = fee_gamma / (fee_gamma + (1 - K))
            where
            K = prod(x) / (sum(x) / N)**N
            (all normalized to 1e18)
            """
            fee_gamma: uint256 = self.fee_gamma
            f: uint256 = xp[0] + xp[1]  # sum
            f = fee_gamma * 10**18 / (
                fee_gamma + 10**18 - (10**18 * N_COINS**N_COINS) * xp[0] / f * xp[1] / f
            )
            return (self.mid_fee * f + self.out_fee * (10**18 - f)) / 10**18
        
        
        ### Math functions
        @internal
        @pure
        def geometric_mean(unsorted_x: uint256[N_COINS], sort: bool) -> uint256:
            """
            (x[0] * x[1] * ...) ** (1/N)
            """
            x: uint256[N_COINS] = unsorted_x
            if sort and x[0] < x[1]:
                x = [unsorted_x[1], unsorted_x[0]]
            D: uint256 = x[0]
            diff: uint256 = 0
            for i in range(255):
                D_prev: uint256 = D
                # tmp: uint256 = 10**18
                # for _x in x:
                #     tmp = tmp * _x / D
                # D = D * ((N_COINS - 1) * 10**18 + tmp) / (N_COINS * 10**18)
                # line below makes it for 2 coins
                D = (D + x[0] * x[1] / D) / N_COINS
                if D > D_prev:
                    diff = D - D_prev
                else:
                    diff = D_prev - D
                if diff <= 1 or diff * 10**18 < D:
                    return D
            raise "Did not converge"
        
        
        @internal
        @view
        def newton_D(ANN: uint256, gamma: uint256, x_unsorted: uint256[N_COINS]) -> uint256:
            """
            Finding the invariant using Newton method.
            ANN is higher by the factor A_MULTIPLIER
            ANN is already A * N**N
        
            Currently uses 60k gas
            """
            # Safety checks
            assert ANN > MIN_A - 1 and ANN < MAX_A + 1  # dev: unsafe values A
            assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1  # dev: unsafe values gamma
        
            # Initial value of invariant D is that for constant-product invariant
            x: uint256[N_COINS] = x_unsorted
            if x[0] < x[1]:
                x = [x_unsorted[1], x_unsorted[0]]
        
            assert x[0] > 10**9 - 1 and x[0] < 10**15 * 10**18 + 1  # dev: unsafe values x[0]
            assert x[1] * 10**18 / x[0] > 10**14-1  # dev: unsafe values x[i] (input)
        
            D: uint256 = N_COINS * self.geometric_mean(x, False)
            S: uint256 = x[0] + x[1]
        
            for i in range(255):
                D_prev: uint256 = D
        
                # K0: uint256 = 10**18
                # for _x in x:
                #     K0 = K0 * _x * N_COINS / D
                # collapsed for 2 coins
                K0: uint256 = (10**18 * N_COINS**2) * x[0] / D * x[1] / D
        
                _g1k0: uint256 = gamma + 10**18
                if _g1k0 > K0:
                    _g1k0 = _g1k0 - K0 + 1
                else:
                    _g1k0 = K0 - _g1k0 + 1
        
                # D / (A * N**N) * _g1k0**2 / gamma**2
                mul1: uint256 = 10**18 * D / gamma * _g1k0 / gamma * _g1k0 * A_MULTIPLIER / ANN
        
                # 2*N*K0 / _g1k0
                mul2: uint256 = (2 * 10**18) * N_COINS * K0 / _g1k0
        
                neg_fprime: uint256 = (S + S * mul2 / 10**18) + mul1 * N_COINS / K0 - mul2 * D / 10**18
        
                # D -= f / fprime
                D_plus: uint256 = D * (neg_fprime + S) / neg_fprime
                D_minus: uint256 = D*D / neg_fprime
                if 10**18 > K0:
                    D_minus += D * (mul1 / neg_fprime) / 10**18 * (10**18 - K0) / K0
                else:
                    D_minus -= D * (mul1 / neg_fprime) / 10**18 * (K0 - 10**18) / K0
        
                if D_plus > D_minus:
                    D = D_plus - D_minus
                else:
                    D = (D_minus - D_plus) / 2
        
                diff: uint256 = 0
                if D > D_prev:
                    diff = D - D_prev
                else:
                    diff = D_prev - D
                if diff * 10**14 < max(10**16, D):  # Could reduce precision for gas efficiency here
                    # Test that we are safe with the next newton_y
                    for _x in x:
                        frac: uint256 = _x * 10**18 / D
                        assert (frac > 10**16 - 1) and (frac < 10**20 + 1)  # dev: unsafe values x[i]
                    return D
        
            raise "Did not converge"
        
        
        @internal
        @pure
        def newton_y(ANN: uint256, gamma: uint256, x: uint256[N_COINS], D: uint256, i: uint256) -> uint256:
            """
            Calculating x[i] given other balances x[0..N_COINS-1] and invariant D
            ANN = A * N**N
            """
            # Safety checks
            assert ANN > MIN_A - 1 and ANN < MAX_A + 1  # dev: unsafe values A
            assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1  # dev: unsafe values gamma
            assert D > 10**17 - 1 and D < 10**15 * 10**18 + 1 # dev: unsafe values D
        
            x_j: uint256 = x[1 - i]
            y: uint256 = D**2 / (x_j * N_COINS**2)
            K0_i: uint256 = (10**18 * N_COINS) * x_j / D
            # S_i = x_j
        
            # frac = x_j * 1e18 / D => frac = K0_i / N_COINS
            assert (K0_i > 10**16*N_COINS - 1) and (K0_i < 10**20*N_COINS + 1)  # dev: unsafe values x[i]
        
            # x_sorted: uint256[N_COINS] = x
            # x_sorted[i] = 0
            # x_sorted = self.sort(x_sorted)  # From high to low
            # x[not i] instead of x_sorted since x_soted has only 1 element
        
            convergence_limit: uint256 = max(max(x_j / 10**14, D / 10**14), 100)
        
            for j in range(255):
                y_prev: uint256 = y
        
                K0: uint256 = K0_i * y * N_COINS / D
                S: uint256 = x_j + y
        
                _g1k0: uint256 = gamma + 10**18
                if _g1k0 > K0:
                    _g1k0 = _g1k0 - K0 + 1
                else:
                    _g1k0 = K0 - _g1k0 + 1
        
                # D / (A * N**N) * _g1k0**2 / gamma**2
                mul1: uint256 = 10**18 * D / gamma * _g1k0 / gamma * _g1k0 * A_MULTIPLIER / ANN
        
                # 2*K0 / _g1k0
                mul2: uint256 = 10**18 + (2 * 10**18) * K0 / _g1k0
        
                yfprime: uint256 = 10**18 * y + S * mul2 + mul1
                _dyfprime: uint256 = D * mul2
                if yfprime < _dyfprime:
                    y = y_prev / 2
                    continue
                else:
                    yfprime -= _dyfprime
                fprime: uint256 = yfprime / y
        
                # y -= f / f_prime;  y = (y * fprime - f) / fprime
                # y = (yfprime + 10**18 * D - 10**18 * S) // fprime + mul1 // fprime * (10**18 - K0) // K0
                y_minus: uint256 = mul1 / fprime
                y_plus: uint256 = (yfprime + 10**18 * D) / fprime + y_minus * 10**18 / K0
                y_minus += 10**18 * S / fprime
        
                if y_plus < y_minus:
                    y = y_prev / 2
                else:
                    y = y_plus - y_minus
        
                diff: uint256 = 0
                if y > y_prev:
                    diff = y - y_prev
                else:
                    diff = y_prev - y
                if diff < max(convergence_limit, y / 10**14):
                    frac: uint256 = y * 10**18 / D
                    assert (frac > 10**16 - 1) and (frac < 10**20 + 1)  # dev: unsafe value for y
                    return y
        
            raise "Did not converge"
        
        
        @internal
        @pure
        def halfpow(power: uint256) -> uint256:
            """
            1e18 * 0.5 ** (power/1e18)
        
            Inspired by: https://github.com/balancer-labs/balancer-core/blob/master/contracts/BNum.sol#L128
            """
            intpow: uint256 = power / 10**18
            otherpow: uint256 = power - intpow * 10**18
            if intpow > 59:
                return 0
            result: uint256 = 10**18 / (2**intpow)
            if otherpow == 0:
                return result
        
            term: uint256 = 10**18
            x: uint256 = 5 * 10**17
            S: uint256 = 10**18
            neg: bool = False
        
            for i in range(1, 256):
                K: uint256 = i * 10**18
                c: uint256 = K - 10**18
                if otherpow > c:
                    c = otherpow - c
                    neg = not neg
                else:
                    c -= otherpow
                term = term * (c * x / 10**18) / K
                if neg:
                    S -= term
                else:
                    S += term
                if term < EXP_PRECISION:
                    return result * S / 10**18
        
            raise "Did not converge"
        ### end of Math functions
        
        
        @internal
        @view
        def get_xcp(D: uint256) -> uint256:
            x: uint256[N_COINS] = [D / N_COINS, D * PRECISION / (self.price_scale * N_COINS)]
            return self.geometric_mean(x, True)
        
        
        @internal
        def _claim_admin_fees():
            A_gamma: uint256[2] = self._A_gamma()
        
            xcp_profit: uint256 = self.xcp_profit
            xcp_profit_a: uint256 = self.xcp_profit_a
        
            # Gulp here
            for i in range(N_COINS):
                coin: address = self.coins[i]
                if coin == WETH20:
                    self.balances[i] = self.balance
                else:
                    self.balances[i] = ERC20(coin).balanceOf(self)
        
            vprice: uint256 = self.virtual_price
        
            if xcp_profit > xcp_profit_a:
                fees: uint256 = (xcp_profit - xcp_profit_a) * self.admin_fee / (2 * 10**10)
                if fees > 0:
                    receiver: address = Factory(self.factory).fee_receiver()
                    if receiver != ZERO_ADDRESS:
                        frac: uint256 = vprice * 10**18 / (vprice - fees) - 10**18
                        claimed: uint256 = CurveToken(self.token).mint_relative(receiver, frac)
                        xcp_profit -= fees*2
                        self.xcp_profit = xcp_profit
                        log ClaimAdminFee(receiver, claimed)
        
            total_supply: uint256 = CurveToken(self.token).totalSupply()
        
            # Recalculate D b/c we gulped
            D: uint256 = self.newton_D(A_gamma[0], A_gamma[1], self.xp())
            self.D = D
        
            self.virtual_price = 10**18 * self.get_xcp(D) / total_supply
        
            if xcp_profit > xcp_profit_a:
                self.xcp_profit_a = xcp_profit
        
        
        @internal
        @view
        def internal_price_oracle() -> uint256:
            price_oracle: uint256 = self._price_oracle
            last_prices_timestamp: uint256 = self.last_prices_timestamp
        
            if last_prices_timestamp < block.timestamp:
                ma_half_time: uint256 = self.ma_half_time
                last_prices: uint256 = self.last_prices
                alpha: uint256 = self.halfpow((block.timestamp - last_prices_timestamp) * 10**18 / ma_half_time)
                return (last_prices * (10**18 - alpha) + price_oracle * alpha) / 10**18
        
            else:
                return price_oracle
        
        
        @internal
        def tweak_price(A_gamma: uint256[2],_xp: uint256[N_COINS], p_i: uint256, new_D: uint256):
            price_oracle: uint256 = self._price_oracle
            last_prices: uint256 = self.last_prices
            price_scale: uint256 = self.price_scale
            last_prices_timestamp: uint256 = self.last_prices_timestamp
            p_new: uint256 = 0
        
            if last_prices_timestamp < block.timestamp:
                # MA update required
                ma_half_time: uint256 = self.ma_half_time
                alpha: uint256 = self.halfpow((block.timestamp - last_prices_timestamp) * 10**18 / ma_half_time)
                price_oracle = (last_prices * (10**18 - alpha) + price_oracle * alpha) / 10**18
                self._price_oracle = price_oracle
                self.last_prices_timestamp = block.timestamp
        
            D_unadjusted: uint256 = new_D  # Withdrawal methods know new D already
            if new_D == 0:
                # We will need this a few times (35k gas)
                D_unadjusted = self.newton_D(A_gamma[0], A_gamma[1], _xp)
        
            if p_i > 0:
                last_prices = p_i
        
            else:
                # calculate real prices
                __xp: uint256[N_COINS] = _xp
                dx_price: uint256 = __xp[0] / 10**6
                __xp[0] += dx_price
                last_prices = price_scale * dx_price / (_xp[1] - self.newton_y(A_gamma[0], A_gamma[1], __xp, D_unadjusted, 1))
        
            self.last_prices = last_prices
        
            total_supply: uint256 = CurveToken(self.token).totalSupply()
            old_xcp_profit: uint256 = self.xcp_profit
            old_virtual_price: uint256 = self.virtual_price
        
            # Update profit numbers without price adjustment first
            xp: uint256[N_COINS] = [D_unadjusted / N_COINS, D_unadjusted * PRECISION / (N_COINS * price_scale)]
            xcp_profit: uint256 = 10**18
            virtual_price: uint256 = 10**18
        
            if old_virtual_price > 0:
                xcp: uint256 = self.geometric_mean(xp, True)
                virtual_price = 10**18 * xcp / total_supply
                xcp_profit = old_xcp_profit * virtual_price / old_virtual_price
        
                t: uint256 = self.future_A_gamma_time
                if virtual_price < old_virtual_price and t == 0:
                    raise "Loss"
                if t == 1:
                    self.future_A_gamma_time = 0
        
            self.xcp_profit = xcp_profit
        
            norm: uint256 = price_oracle * 10**18 / price_scale
            if norm > 10**18:
                norm -= 10**18
            else:
                norm = 10**18 - norm
            adjustment_step: uint256 = max(self.adjustment_step, norm / 5)
        
            needs_adjustment: bool = self.not_adjusted
            # if not needs_adjustment and (virtual_price-10**18 > (xcp_profit-10**18)/2 + self.allowed_extra_profit):
            # (re-arrange for gas efficiency)
            if not needs_adjustment and (virtual_price * 2 - 10**18 > xcp_profit + 2*self.allowed_extra_profit) and (norm > adjustment_step) and (old_virtual_price > 0):
                needs_adjustment = True
                self.not_adjusted = True
        
            if needs_adjustment:
                if norm > adjustment_step and old_virtual_price > 0:
                    p_new = (price_scale * (norm - adjustment_step) + adjustment_step * price_oracle) / norm
        
                    # Calculate balances*prices
                    xp = [_xp[0], _xp[1] * p_new / price_scale]
        
                    # Calculate "extended constant product" invariant xCP and virtual price
                    D: uint256 = self.newton_D(A_gamma[0], A_gamma[1], xp)
                    xp = [D / N_COINS, D * PRECISION / (N_COINS * p_new)]
                    # We reuse old_virtual_price here but it's not old anymore
                    old_virtual_price = 10**18 * self.geometric_mean(xp, True) / total_supply
        
                    # Proceed if we've got enough profit
                    # if (old_virtual_price > 10**18) and (2 * (old_virtual_price - 10**18) > xcp_profit - 10**18):
                    if (old_virtual_price > 10**18) and (2 * old_virtual_price - 10**18 > xcp_profit):
                        self.price_scale = p_new
                        self.D = D
                        self.virtual_price = old_virtual_price
        
                        return
        
                    else:
                        self.not_adjusted = False
        
                        # Can instead do another flag variable if we want to save bytespace
                        self.D = D_unadjusted
                        self.virtual_price = virtual_price
                        self._claim_admin_fees()
        
                        return
        
            # If we are here, the price_scale adjustment did not happen
            # Still need to update the profit counter and D
            self.D = D_unadjusted
            self.virtual_price = virtual_price
        
            # norm appeared < adjustment_step after
            if needs_adjustment:
                self.not_adjusted = False
                self._claim_admin_fees()
        
        
        @internal
        def _exchange(sender: address, mvalue: uint256, i: uint256, j: uint256, dx: uint256, min_dy: uint256,
                      use_eth: bool, receiver: address, callbacker: address, callback_sig: bytes32) -> uint256:
            assert i != j  # dev: coin index out of range
            assert i < N_COINS  # dev: coin index out of range
            assert j < N_COINS  # dev: coin index out of range
            assert dx > 0  # dev: do not exchange 0 coins
        
            A_gamma: uint256[2] = self._A_gamma()
            xp: uint256[N_COINS] = self.balances
            p: uint256 = 0
            dy: uint256 = 0
        
            in_coin: address = self.coins[i]
            out_coin: address = self.coins[j]
        
            y: uint256 = xp[j]
            x0: uint256 = xp[i]
            xp[i] = x0 + dx
            self.balances[i] = xp[i]
        
            price_scale: uint256 = self.price_scale
            precisions: uint256[2] = self._get_precisions()
        
            xp = [xp[0] * precisions[0], xp[1] * price_scale * precisions[1] / PRECISION]
        
            prec_i: uint256 = precisions[0]
            prec_j: uint256 = precisions[1]
            if i == 1:
                prec_i = precisions[1]
                prec_j = precisions[0]
        
            # In case ramp is happening
            t: uint256 = self.future_A_gamma_time
            if t > 0:
                x0 *= prec_i
                if i > 0:
                    x0 = x0 * price_scale / PRECISION
                x1: uint256 = xp[i]  # Back up old value in xp
                xp[i] = x0
                self.D = self.newton_D(A_gamma[0], A_gamma[1], xp)
                xp[i] = x1  # And restore
                if block.timestamp >= t:
                    self.future_A_gamma_time = 1
        
            dy = xp[j] - self.newton_y(A_gamma[0], A_gamma[1], xp, self.D, j)
            # Not defining new "y" here to have less variables / make subsequent calls cheaper
            xp[j] -= dy
            dy -= 1
        
            if j > 0:
                dy = dy * PRECISION / price_scale
            dy /= prec_j
        
            dy -= self._fee(xp) * dy / 10**10
            assert dy >= min_dy, "Slippage"
            y -= dy
        
            self.balances[j] = y
        
            # Do transfers in and out together
            # XXX coin vs ETH
            if use_eth and in_coin == WETH20:
                assert mvalue == dx  # dev: incorrect eth amount
            else:
                assert mvalue == 0  # dev: nonzero eth amount
                if callback_sig == EMPTY_BYTES32:
                    response: Bytes[32] = raw_call(
                        in_coin,
                        _abi_encode(
                            sender, self, dx, method_id=method_id("transferFrom(address,address,uint256)")
                        ),
                        max_outsize=32,
                    )
                    if len(response) != 0:
                        assert convert(response, bool)  # dev: failed transfer
                else:
                    b: uint256 = ERC20(in_coin).balanceOf(self)
                    raw_call(
                        callbacker,
                        concat(slice(callback_sig, 0, 4), _abi_encode(sender, receiver, in_coin, dx, dy))
                    )
                    assert ERC20(in_coin).balanceOf(self) - b == dx  # dev: callback didn't give us coins
                if in_coin == WETH20:
                    WETH(WETH20).withdraw(dx)
        
            if use_eth and out_coin == WETH20:
                raw_call(receiver, b"", value=dy)
            else:
                if out_coin == WETH20:
                    WETH(WETH20).deposit(value=dy)
                response: Bytes[32] = raw_call(
                    out_coin,
                    _abi_encode(receiver, dy, method_id=method_id("transfer(address,uint256)")),
                    max_outsize=32,
                )
                if len(response) != 0:
                    assert convert(response, bool)
        
            y *= prec_j
            if j > 0:
                y = y * price_scale / PRECISION
            xp[j] = y
        
            # Calculate price
            if dx > 10**5 and dy > 10**5:
                _dx: uint256 = dx * prec_i
                _dy: uint256 = dy * prec_j
                if i == 0:
                    p = _dx * 10**18 / _dy
                else:  # j == 0
                    p = _dy * 10**18 / _dx
        
            self.tweak_price(A_gamma, xp, p, 0)
        
            log TokenExchange(sender, i, dx, j, dy)
        
            return dy
        
        
        @view
        @internal
        def _calc_token_fee(amounts: uint256[N_COINS], xp: uint256[N_COINS]) -> uint256:
            # fee = sum(amounts_i - avg(amounts)) * fee' / sum(amounts)
            fee: uint256 = self._fee(xp) * N_COINS / (4 * (N_COINS-1))
            S: uint256 = 0
            for _x in amounts:
                S += _x
            avg: uint256 = S / N_COINS
            Sdiff: uint256 = 0
            for _x in amounts:
                if _x > avg:
                    Sdiff += _x - avg
                else:
                    Sdiff += avg - _x
            return fee * Sdiff / S + NOISE_FEE
        
        
        @internal
        @view
        def _calc_withdraw_one_coin(A_gamma: uint256[2], token_amount: uint256, i: uint256, update_D: bool,
                                    calc_price: bool) -> (uint256, uint256, uint256, uint256[N_COINS]):
            token_supply: uint256 = CurveToken(self.token).totalSupply()
            assert token_amount <= token_supply  # dev: token amount more than supply
            assert i < N_COINS  # dev: coin out of range
        
            xx: uint256[N_COINS] = self.balances
            D0: uint256 = 0
            precisions: uint256[2] = self._get_precisions()
        
            price_scale_i: uint256 = self.price_scale * precisions[1]
            xp: uint256[N_COINS] = [xx[0] * precisions[0], xx[1] * price_scale_i / PRECISION]
            if i == 0:
                price_scale_i = PRECISION * precisions[0]
        
            if update_D:
                D0 = self.newton_D(A_gamma[0], A_gamma[1], xp)
            else:
                D0 = self.D
        
            D: uint256 = D0
        
            # Charge the fee on D, not on y, e.g. reducing invariant LESS than charging the user
            fee: uint256 = self._fee(xp)
            dD: uint256 = token_amount * D / token_supply
            D -= (dD - (fee * dD / (2 * 10**10) + 1))
            y: uint256 = self.newton_y(A_gamma[0], A_gamma[1], xp, D, i)
            dy: uint256 = (xp[i] - y) * PRECISION / price_scale_i
            xp[i] = y
        
            # Price calc
            p: uint256 = 0
            if calc_price and dy > 10**5 and token_amount > 10**5:
                # p_i = dD / D0 * sum'(p_k * x_k) / (dy - dD / D0 * y0)
                S: uint256 = 0
                precision: uint256 = precisions[0]
                if i == 1:
                    S = xx[0] * precisions[0]
                    precision = precisions[1]
                else:
                    S = xx[1] * precisions[1]
                S = S * dD / D0
                p = S * PRECISION / (dy * precision - dD * xx[i] * precision / D0)
                if i == 0:
                    p = (10**18)**2 / p
        
            return dy, p, D, xp
        
        
        @internal
        @pure
        def sqrt_int(x: uint256) -> uint256:
            """
            Originating from: https://github.com/vyperlang/vyper/issues/1266
            """
        
            if x == 0:
                return 0
        
            z: uint256 = (x + 10**18) / 2
            y: uint256 = x
        
            for i in range(256):
                if z == y:
                    return y
                y = z
                z = (x * 10**18 / z + z) / 2
        
            raise "Did not converge"
        
        
        # External Functions
        
        
        @payable
        @external
        @nonreentrant('lock')
        def exchange(i: uint256, j: uint256, dx: uint256, min_dy: uint256,
                     use_eth: bool = False, receiver: address = msg.sender) -> uint256:
            """
            Exchange using WETH by default
            """
            return self._exchange(msg.sender, msg.value, i, j, dx, min_dy, use_eth, receiver, ZERO_ADDRESS, EMPTY_BYTES32)
        
        
        @payable
        @external
        @nonreentrant('lock')
        def exchange_underlying(i: uint256, j: uint256, dx: uint256, min_dy: uint256,
                                receiver: address = msg.sender) -> uint256:
            """
            Exchange using ETH
            """
            return self._exchange(msg.sender, msg.value, i, j, dx, min_dy, True, receiver, ZERO_ADDRESS, EMPTY_BYTES32)
        
        
        @payable
        @external
        @nonreentrant('lock')
        def exchange_extended(i: uint256, j: uint256, dx: uint256, min_dy: uint256,
                              use_eth: bool, sender: address, receiver: address, cb: bytes32) -> uint256:
            assert cb != EMPTY_BYTES32  # dev: No callback specified
            return self._exchange(sender, msg.value, i, j, dx, min_dy, use_eth, receiver, msg.sender, cb)
        
        
        @payable
        @external
        @nonreentrant('lock')
        def add_liquidity(amounts: uint256[N_COINS], min_mint_amount: uint256,
                          use_eth: bool = False, receiver: address = msg.sender) -> uint256:
            assert amounts[0] > 0 or amounts[1] > 0  # dev: no coins to add
        
            A_gamma: uint256[2] = self._A_gamma()
        
            xp: uint256[N_COINS] = self.balances
            amountsp: uint256[N_COINS] = empty(uint256[N_COINS])
            xx: uint256[N_COINS] = empty(uint256[N_COINS])
            d_token: uint256 = 0
            d_token_fee: uint256 = 0
            old_D: uint256 = 0
        
            xp_old: uint256[N_COINS] = xp
        
            for i in range(N_COINS):
                bal: uint256 = xp[i] + amounts[i]
                xp[i] = bal
                self.balances[i] = bal
            xx = xp
        
            precisions: uint256[2] = self._get_precisions()
        
            price_scale: uint256 = self.price_scale * precisions[1]
            xp = [xp[0] * precisions[0], xp[1] * price_scale / PRECISION]
            xp_old = [xp_old[0] * precisions[0], xp_old[1] * price_scale / PRECISION]
        
            if not use_eth:
                assert msg.value == 0  # dev: nonzero eth amount
        
            for i in range(N_COINS):
                coin: address = self.coins[i]
                if use_eth and coin == WETH20:
                    assert msg.value == amounts[i]  # dev: incorrect eth amount
                if amounts[i] > 0:
                    if (not use_eth) or (coin != WETH20):
                        response: Bytes[32] = raw_call(
                            coin,
                            _abi_encode(
                                msg.sender,
                                self,
                                amounts[i],
                                method_id=method_id("transferFrom(address,address,uint256)"),
                            ),
                            max_outsize=32,
                        )
                        if len(response) != 0:
                            assert convert(response, bool)  # dev: failed transfer
                        if coin == WETH20:
                            WETH(WETH20).withdraw(amounts[i])
                    amountsp[i] = xp[i] - xp_old[i]
        
            t: uint256 = self.future_A_gamma_time
            if t > 0:
                old_D = self.newton_D(A_gamma[0], A_gamma[1], xp_old)
                if block.timestamp >= t:
                    self.future_A_gamma_time = 1
            else:
                old_D = self.D
        
            D: uint256 = self.newton_D(A_gamma[0], A_gamma[1], xp)
        
            lp_token: address = self.token
            token_supply: uint256 = CurveToken(lp_token).totalSupply()
            if old_D > 0:
                d_token = token_supply * D / old_D - token_supply
            else:
                d_token = self.get_xcp(D)  # making initial virtual price equal to 1
            assert d_token > 0  # dev: nothing minted
        
            if old_D > 0:
                d_token_fee = self._calc_token_fee(amountsp, xp) * d_token / 10**10 + 1
                d_token -= d_token_fee
                token_supply += d_token
                CurveToken(lp_token).mint(receiver, d_token)
        
                # Calculate price
                # p_i * (dx_i - dtoken / token_supply * xx_i) = sum{k!=i}(p_k * (dtoken / token_supply * xx_k - dx_k))
                # Simplified for 2 coins
                p: uint256 = 0
                if d_token > 10**5:
                    if amounts[0] == 0 or amounts[1] == 0:
                        S: uint256 = 0
                        precision: uint256 = 0
                        ix: uint256 = 0
                        if amounts[0] == 0:
                            S = xx[0] * precisions[0]
                            precision = precisions[1]
                            ix = 1
                        else:
                            S = xx[1] * precisions[1]
                            precision = precisions[0]
                        S = S * d_token / token_supply
                        p = S * PRECISION / (amounts[ix] * precision - d_token * xx[ix] * precision / token_supply)
                        if ix == 0:
                            p = (10**18)**2 / p
        
                self.tweak_price(A_gamma, xp, p, D)
        
            else:
                self.D = D
                self.virtual_price = 10**18
                self.xcp_profit = 10**18
                CurveToken(lp_token).mint(receiver, d_token)
        
            assert d_token >= min_mint_amount, "Slippage"
        
            log AddLiquidity(receiver, amounts, d_token_fee, token_supply)
        
            return d_token
        
        
        @external
        @nonreentrant('lock')
        def remove_liquidity(_amount: uint256, min_amounts: uint256[N_COINS],
                             use_eth: bool = False, receiver: address = msg.sender):
            """
            This withdrawal method is very safe, does no complex math
            """
            lp_token: address = self.token
            total_supply: uint256 = CurveToken(lp_token).totalSupply()
            CurveToken(lp_token).burnFrom(msg.sender, _amount)
            balances: uint256[N_COINS] = self.balances
            amount: uint256 = _amount - 1  # Make rounding errors favoring other LPs a tiny bit
        
            for i in range(N_COINS):
                d_balance: uint256 = balances[i] * amount / total_supply
                assert d_balance >= min_amounts[i]
                self.balances[i] = balances[i] - d_balance
                balances[i] = d_balance  # now it's the amounts going out
                coin: address = self.coins[i]
                if use_eth and coin == WETH20:
                    raw_call(receiver, b"", value=d_balance)
                else:
                    if coin == WETH20:
                        WETH(WETH20).deposit(value=d_balance)
                    response: Bytes[32] = raw_call(
                        coin,
                        _abi_encode(receiver, d_balance, method_id=method_id("transfer(address,uint256)")),
                        max_outsize=32,
                    )
                    if len(response) != 0:
                        assert convert(response, bool)
        
            D: uint256 = self.D
            self.D = D - D * amount / total_supply
        
            log RemoveLiquidity(msg.sender, balances, total_supply - _amount)
        
        
        @external
        @nonreentrant('lock')
        def remove_liquidity_one_coin(token_amount: uint256, i: uint256, min_amount: uint256,
                                      use_eth: bool = False, receiver: address = msg.sender) -> uint256:
            A_gamma: uint256[2] = self._A_gamma()
        
            dy: uint256 = 0
            D: uint256 = 0
            p: uint256 = 0
            xp: uint256[N_COINS] = empty(uint256[N_COINS])
            future_A_gamma_time: uint256 = self.future_A_gamma_time
            dy, p, D, xp = self._calc_withdraw_one_coin(A_gamma, token_amount, i, (future_A_gamma_time > 0), True)
            assert dy >= min_amount, "Slippage"
        
            if block.timestamp >= future_A_gamma_time:
                self.future_A_gamma_time = 1
        
            self.balances[i] -= dy
            CurveToken(self.token).burnFrom(msg.sender, token_amount)
        
            coin: address = self.coins[i]
            if use_eth and coin == WETH20:
                raw_call(receiver, b"", value=dy)
            else:
                if coin == WETH20:
                    WETH(WETH20).deposit(value=dy)
                response: Bytes[32] = raw_call(
                    coin,
                    _abi_encode(receiver, dy, method_id=method_id("transfer(address,uint256)")),
                    max_outsize=32,
                )
                if len(response) != 0:
                    assert convert(response, bool)
        
            self.tweak_price(A_gamma, xp, p, D)
        
            log RemoveLiquidityOne(msg.sender, token_amount, i, dy)
        
            return dy
        
        
        @external
        @nonreentrant('lock')
        def claim_admin_fees():
            self._claim_admin_fees()
        
        
        # Admin parameters
        @external
        def ramp_A_gamma(future_A: uint256, future_gamma: uint256, future_time: uint256):
            assert msg.sender == Factory(self.factory).admin()  # dev: only owner
            assert block.timestamp > self.initial_A_gamma_time + (MIN_RAMP_TIME-1)
            assert future_time > block.timestamp + (MIN_RAMP_TIME-1)  # dev: insufficient time
        
            A_gamma: uint256[2] = self._A_gamma()
            initial_A_gamma: uint256 = shift(A_gamma[0], 128)
            initial_A_gamma = bitwise_or(initial_A_gamma, A_gamma[1])
        
            assert future_A > MIN_A-1
            assert future_A < MAX_A+1
            assert future_gamma > MIN_GAMMA-1
            assert future_gamma < MAX_GAMMA+1
        
            ratio: uint256 = 10**18 * future_A / A_gamma[0]
            assert ratio < 10**18 * MAX_A_CHANGE + 1
            assert ratio > 10**18 / MAX_A_CHANGE - 1
        
            ratio = 10**18 * future_gamma / A_gamma[1]
            assert ratio < 10**18 * MAX_A_CHANGE + 1
            assert ratio > 10**18 / MAX_A_CHANGE - 1
        
            self.initial_A_gamma = initial_A_gamma
            self.initial_A_gamma_time = block.timestamp
        
            future_A_gamma: uint256 = shift(future_A, 128)
            future_A_gamma = bitwise_or(future_A_gamma, future_gamma)
            self.future_A_gamma_time = future_time
            self.future_A_gamma = future_A_gamma
        
            log RampAgamma(A_gamma[0], future_A, A_gamma[1], future_gamma, block.timestamp, future_time)
        
        
        @external
        def stop_ramp_A_gamma():
            assert msg.sender == Factory(self.factory).admin()  # dev: only owner
        
            A_gamma: uint256[2] = self._A_gamma()
            current_A_gamma: uint256 = shift(A_gamma[0], 128)
            current_A_gamma = bitwise_or(current_A_gamma, A_gamma[1])
            self.initial_A_gamma = current_A_gamma
            self.future_A_gamma = current_A_gamma
            self.initial_A_gamma_time = block.timestamp
            self.future_A_gamma_time = block.timestamp
            # now (block.timestamp < t1) is always False, so we return saved A
        
            log StopRampA(A_gamma[0], A_gamma[1], block.timestamp)
        
        
        @external
        def commit_new_parameters(
            _new_mid_fee: uint256,
            _new_out_fee: uint256,
            _new_admin_fee: uint256,
            _new_fee_gamma: uint256,
            _new_allowed_extra_profit: uint256,
            _new_adjustment_step: uint256,
            _new_ma_half_time: uint256,
            ):
            assert msg.sender == Factory(self.factory).admin()  # dev: only owner
            assert self.admin_actions_deadline == 0  # dev: active action
        
            new_mid_fee: uint256 = _new_mid_fee
            new_out_fee: uint256 = _new_out_fee
            new_admin_fee: uint256 = _new_admin_fee
            new_fee_gamma: uint256 = _new_fee_gamma
            new_allowed_extra_profit: uint256 = _new_allowed_extra_profit
            new_adjustment_step: uint256 = _new_adjustment_step
            new_ma_half_time: uint256 = _new_ma_half_time
        
            # Fees
            if new_out_fee < MAX_FEE+1:
                assert new_out_fee > MIN_FEE-1  # dev: fee is out of range
            else:
                new_out_fee = self.out_fee
            if new_mid_fee > MAX_FEE:
                new_mid_fee = self.mid_fee
            assert new_mid_fee <= new_out_fee  # dev: mid-fee is too high
            if new_admin_fee > MAX_ADMIN_FEE:
                new_admin_fee = self.admin_fee
        
            # AMM parameters
            if new_fee_gamma < 10**18:
                assert new_fee_gamma > 0  # dev: fee_gamma out of range [1 .. 10**18]
            else:
                new_fee_gamma = self.fee_gamma
            if new_allowed_extra_profit > 10**18:
                new_allowed_extra_profit = self.allowed_extra_profit
            if new_adjustment_step > 10**18:
                new_adjustment_step = self.adjustment_step
        
            # MA
            if new_ma_half_time < 7*86400:
                assert new_ma_half_time > 0  # dev: MA time should be longer than 1 second
            else:
                new_ma_half_time = self.ma_half_time
        
            _deadline: uint256 = block.timestamp + ADMIN_ACTIONS_DELAY
            self.admin_actions_deadline = _deadline
        
            self.future_admin_fee = new_admin_fee
            self.future_mid_fee = new_mid_fee
            self.future_out_fee = new_out_fee
            self.future_fee_gamma = new_fee_gamma
            self.future_allowed_extra_profit = new_allowed_extra_profit
            self.future_adjustment_step = new_adjustment_step
            self.future_ma_half_time = new_ma_half_time
        
            log CommitNewParameters(_deadline, new_admin_fee, new_mid_fee, new_out_fee,
                                    new_fee_gamma,
                                    new_allowed_extra_profit, new_adjustment_step,
                                    new_ma_half_time)
        
        
        @external
        @nonreentrant('lock')
        def apply_new_parameters():
            assert msg.sender == Factory(self.factory).admin()  # dev: only owner
            assert block.timestamp >= self.admin_actions_deadline  # dev: insufficient time
            assert self.admin_actions_deadline != 0  # dev: no active action
        
            self.admin_actions_deadline = 0
        
            admin_fee: uint256 = self.future_admin_fee
            if self.admin_fee != admin_fee:
                self._claim_admin_fees()
                self.admin_fee = admin_fee
        
            mid_fee: uint256 = self.future_mid_fee
            self.mid_fee = mid_fee
            out_fee: uint256 = self.future_out_fee
            self.out_fee = out_fee
            fee_gamma: uint256 = self.future_fee_gamma
            self.fee_gamma = fee_gamma
            allowed_extra_profit: uint256 = self.future_allowed_extra_profit
            self.allowed_extra_profit = allowed_extra_profit
            adjustment_step: uint256 = self.future_adjustment_step
            self.adjustment_step = adjustment_step
            ma_half_time: uint256 = self.future_ma_half_time
            self.ma_half_time = ma_half_time
        
            log NewParameters(admin_fee, mid_fee, out_fee,
                              fee_gamma,
                              allowed_extra_profit, adjustment_step,
                              ma_half_time)
        
        
        @external
        def revert_new_parameters():
            assert msg.sender == Factory(self.factory).admin()  # dev: only owner
        
            self.admin_actions_deadline = 0
        
        
        # View Methods
        
        
        @external
        @view
        def get_dy(i: uint256, j: uint256, dx: uint256) -> uint256:
            assert i != j  # dev: same input and output coin
            assert i < N_COINS  # dev: coin index out of range
            assert j < N_COINS  # dev: coin index out of range
        
            precisions: uint256[2] = self._get_precisions()
        
            price_scale: uint256 = self.price_scale * precisions[1]
            xp: uint256[N_COINS] = self.balances
        
            A_gamma: uint256[2] = self._A_gamma()
            D: uint256 = self.D
            if self.future_A_gamma_time > 0:
                D = self.newton_D(A_gamma[0], A_gamma[1], self.xp())
        
            xp[i] += dx
            xp = [xp[0] * precisions[0], xp[1] * price_scale / PRECISION]
        
            y: uint256 = self.newton_y(A_gamma[0], A_gamma[1], xp, D, j)
            dy: uint256 = xp[j] - y - 1
            xp[j] = y
            if j > 0:
                dy = dy * PRECISION / price_scale
            else:
                dy /= precisions[0]
            dy -= self._fee(xp) * dy / 10**10
        
            return dy
        
        
        @view
        @external
        def calc_token_amount(amounts: uint256[N_COINS]) -> uint256:
            token_supply: uint256 = CurveToken(self.token).totalSupply()
            precisions: uint256[2] = self._get_precisions()
            price_scale: uint256 = self.price_scale * precisions[1]
            A_gamma: uint256[2] = self._A_gamma()
            xp: uint256[N_COINS] = self.xp()
            amountsp: uint256[N_COINS] = [
                amounts[0] * precisions[0],
                amounts[1] * price_scale / PRECISION]
            D0: uint256 = self.D
            if self.future_A_gamma_time > 0:
                D0 = self.newton_D(A_gamma[0], A_gamma[1], xp)
            xp[0] += amountsp[0]
            xp[1] += amountsp[1]
            D: uint256 = self.newton_D(A_gamma[0], A_gamma[1], xp)
            d_token: uint256 = token_supply * D / D0 - token_supply
            d_token -= self._calc_token_fee(amountsp, xp) * d_token / 10**10 + 1
            return d_token
        
        
        @view
        @external
        def calc_withdraw_one_coin(token_amount: uint256, i: uint256) -> uint256:
            return self._calc_withdraw_one_coin(self._A_gamma(), token_amount, i, True, False)[0]
        
        
        @external
        @view
        def lp_price() -> uint256:
            """
            Approximate LP token price
            """
            return 2 * self.virtual_price * self.sqrt_int(self.internal_price_oracle()) / 10**18
        
        
        @view
        @external
        def A() -> uint256:
            return self._A_gamma()[0]
        
        
        @view
        @external
        def gamma() -> uint256:
            return self._A_gamma()[1]
        
        
        @external
        @view
        def fee() -> uint256:
            return self._fee(self.xp())
        
        
        @external
        @view
        def get_virtual_price() -> uint256:
            return 10**18 * self.get_xcp(self.D) / CurveToken(self.token).totalSupply()
        
        
        @external
        @view
        def price_oracle() -> uint256:
            return self.internal_price_oracle()
        
        
        # Initializer
        
        
        @external
        def initialize(
            A: uint256,
            gamma: uint256,
            mid_fee: uint256,
            out_fee: uint256,
            allowed_extra_profit: uint256,
            fee_gamma: uint256,
            adjustment_step: uint256,
            admin_fee: uint256,
            ma_half_time: uint256,
            initial_price: uint256,
            _token: address,
            _coins: address[N_COINS],
            _precisions: uint256,
        ):
            assert self.mid_fee == 0  # dev: check that we call it from factory
        
            self.factory = msg.sender
        
            # Pack A and gamma:
            # shifted A + gamma
            A_gamma: uint256 = shift(A, 128)
            A_gamma = bitwise_or(A_gamma, gamma)
            self.initial_A_gamma = A_gamma
            self.future_A_gamma = A_gamma
        
            self.mid_fee = mid_fee
            self.out_fee = out_fee
            self.allowed_extra_profit = allowed_extra_profit
            self.fee_gamma = fee_gamma
            self.adjustment_step = adjustment_step
            self.admin_fee = admin_fee
        
            self.price_scale = initial_price
            self._price_oracle = initial_price
            self.last_prices = initial_price
            self.last_prices_timestamp = block.timestamp
            self.ma_half_time = ma_half_time
        
            self.xcp_profit_a = 10**18
        
            self.token = _token
            self.coins = _coins
            self.PRECISIONS = _precisions

        File 4 of 5: Vyper_contract
        # @version 0.3.1
        """
        @title Curve LP Token V5
        @author Curve.Fi
        @notice Base implementation for an LP token provided for supplying liquidity
        @dev Follows the ERC-20 token standard as defined at https://eips.ethereum.org/EIPS/eip-20
        """
        from vyper.interfaces import ERC20
        
        implements: ERC20
        
        
        interface ERC1271:
            def isValidSignature(_hash: bytes32, _signature: Bytes[65]) -> bytes32: view
        
        
        event Approval:
            _owner: indexed(address)
            _spender: indexed(address)
            _value: uint256
        
        event Transfer:
            _from: indexed(address)
            _to: indexed(address)
            _value: uint256
        
        
        EIP712_TYPEHASH: constant(bytes32) = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
        PERMIT_TYPEHASH: constant(bytes32) = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
        
        # keccak256("isValidSignature(bytes32,bytes)")[:4] << 224
        ERC1271_MAGIC_VAL: constant(bytes32) = 0x1626ba7e00000000000000000000000000000000000000000000000000000000
        VERSION: constant(String[8]) = "v5.0.0"
        
        
        name: public(String[64])
        symbol: public(String[32])
        DOMAIN_SEPARATOR: public(bytes32)
        
        balanceOf: public(HashMap[address, uint256])
        allowance: public(HashMap[address, HashMap[address, uint256]])
        totalSupply: public(uint256)
        
        minter: public(address)
        nonces: public(HashMap[address, uint256])
        
        
        @external
        def __init__():
            self.minter = 0x0000000000000000000000000000000000000001
        
        
        @external
        def transfer(_to: address, _value: uint256) -> bool:
            """
            @dev Transfer token for a specified address
            @param _to The address to transfer to.
            @param _value The amount to be transferred.
            """
            # NOTE: vyper does not allow underflows
            #       so the following subtraction would revert on insufficient balance
            self.balanceOf[msg.sender] -= _value
            self.balanceOf[_to] += _value
        
            log Transfer(msg.sender, _to, _value)
            return True
        
        
        @external
        def transferFrom(_from: address, _to: address, _value: uint256) -> bool:
            """
             @dev Transfer tokens from one address to another.
             @param _from address The address which you want to send tokens from
             @param _to address The address which you want to transfer to
             @param _value uint256 the amount of tokens to be transferred
            """
            self.balanceOf[_from] -= _value
            self.balanceOf[_to] += _value
        
            _allowance: uint256 = self.allowance[_from][msg.sender]
            if _allowance != MAX_UINT256:
                self.allowance[_from][msg.sender] = _allowance - _value
        
            log Transfer(_from, _to, _value)
            return True
        
        
        @external
        def approve(_spender: address, _value: uint256) -> bool:
            """
            @notice Approve the passed address to transfer the specified amount of
                    tokens on behalf of msg.sender
            @dev Beware that changing an allowance via this method brings the risk
                 that someone may use both the old and new allowance by unfortunate
                 transaction ordering. This may be mitigated with the use of
                 {increaseAllowance} and {decreaseAllowance}.
                 https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
            @param _spender The address which will transfer the funds
            @param _value The amount of tokens that may be transferred
            @return bool success
            """
            self.allowance[msg.sender][_spender] = _value
        
            log Approval(msg.sender, _spender, _value)
            return True
        
        
        @external
        def permit(
            _owner: address,
            _spender: address,
            _value: uint256,
            _deadline: uint256,
            _v: uint8,
            _r: bytes32,
            _s: bytes32
        ) -> bool:
            """
            @notice Approves spender by owner's signature to expend owner's tokens.
                See https://eips.ethereum.org/EIPS/eip-2612.
            @dev Inspired by https://github.com/yearn/yearn-vaults/blob/main/contracts/Vault.vy#L753-L793
            @dev Supports smart contract wallets which implement ERC1271
                https://eips.ethereum.org/EIPS/eip-1271
            @param _owner The address which is a source of funds and has signed the Permit.
            @param _spender The address which is allowed to spend the funds.
            @param _value The amount of tokens to be spent.
            @param _deadline The timestamp after which the Permit is no longer valid.
            @param _v The bytes[64] of the valid secp256k1 signature of permit by owner
            @param _r The bytes[0:32] of the valid secp256k1 signature of permit by owner
            @param _s The bytes[32:64] of the valid secp256k1 signature of permit by owner
            @return True, if transaction completes successfully
            """
            assert _owner != ZERO_ADDRESS
            assert block.timestamp <= _deadline
        
            nonce: uint256 = self.nonces[_owner]
            digest: bytes32 = keccak256(
                concat(
                    b"\x19\x01",
                    self.DOMAIN_SEPARATOR,
                    keccak256(_abi_encode(PERMIT_TYPEHASH, _owner, _spender, _value, nonce, _deadline))
                )
            )
        
            if _owner.is_contract:
                sig: Bytes[65] = concat(_abi_encode(_r, _s), slice(convert(_v, bytes32), 31, 1))
                # reentrancy not a concern since this is a staticcall
                assert ERC1271(_owner).isValidSignature(digest, sig) == ERC1271_MAGIC_VAL
            else:
                assert ecrecover(digest, convert(_v, uint256), convert(_r, uint256), convert(_s, uint256)) == _owner
        
            self.allowance[_owner][_spender] = _value
            self.nonces[_owner] = nonce + 1
        
            log Approval(_owner, _spender, _value)
            return True
        
        
        @external
        def increaseAllowance(_spender: address, _added_value: uint256) -> bool:
            """
            @notice Increase the allowance granted to `_spender` by the caller
            @dev This is alternative to {approve} that can be used as a mitigation for
                 the potential race condition
            @param _spender The address which will transfer the funds
            @param _added_value The amount of to increase the allowance
            @return bool success
            """
            allowance: uint256 = self.allowance[msg.sender][_spender] + _added_value
            self.allowance[msg.sender][_spender] = allowance
        
            log Approval(msg.sender, _spender, allowance)
            return True
        
        
        @external
        def decreaseAllowance(_spender: address, _subtracted_value: uint256) -> bool:
            """
            @notice Decrease the allowance granted to `_spender` by the caller
            @dev This is alternative to {approve} that can be used as a mitigation for
                 the potential race condition
            @param _spender The address which will transfer the funds
            @param _subtracted_value The amount of to decrease the allowance
            @return bool success
            """
            allowance: uint256 = self.allowance[msg.sender][_spender] - _subtracted_value
            self.allowance[msg.sender][_spender] = allowance
        
            log Approval(msg.sender, _spender, allowance)
            return True
        
        
        @external
        def mint(_to: address, _value: uint256) -> bool:
            """
            @dev Mint an amount of the token and assigns it to an account.
                 This encapsulates the modification of balances such that the
                 proper events are emitted.
            @param _to The account that will receive the created tokens.
            @param _value The amount that will be created.
            """
            assert msg.sender == self.minter
        
            self.totalSupply += _value
            self.balanceOf[_to] += _value
        
            log Transfer(ZERO_ADDRESS, _to, _value)
            return True
        
        
        @external
        def mint_relative(_to: address, frac: uint256) -> uint256:
            """
            @dev Increases supply by factor of (1 + frac/1e18) and mints it for _to
            """
            assert msg.sender == self.minter
        
            supply: uint256 = self.totalSupply
            d_supply: uint256 = supply * frac / 10**18
            if d_supply > 0:
                self.totalSupply = supply + d_supply
                self.balanceOf[_to] += d_supply
                log Transfer(ZERO_ADDRESS, _to, d_supply)
        
            return d_supply
        
        
        @external
        def burnFrom(_to: address, _value: uint256) -> bool:
            """
            @dev Burn an amount of the token from a given account.
            @param _to The account whose tokens will be burned.
            @param _value The amount that will be burned.
            """
            assert msg.sender == self.minter
        
            self.totalSupply -= _value
            self.balanceOf[_to] -= _value
        
            log Transfer(_to, ZERO_ADDRESS, _value)
            return True
        
        
        @view
        @external
        def decimals() -> uint8:
            """
            @notice Get the number of decimals for this token
            @dev Implemented as a view method to reduce gas costs
            @return uint8 decimal places
            """
            return 18
        
        
        @view
        @external
        def version() -> String[8]:
            """
            @notice Get the version of this token contract
            """
            return VERSION
        
        
        @external
        def initialize(_name: String[64], _symbol: String[32], _pool: address):
            assert self.minter == ZERO_ADDRESS  # dev: check that we call it from factory
        
            self.name = _name
            self.symbol = _symbol
            self.minter = _pool
        
            self.DOMAIN_SEPARATOR = keccak256(
                _abi_encode(EIP712_TYPEHASH, keccak256(_name), keccak256(VERSION), chain.id, self)
            )
        
            # fire a transfer event so block explorers identify the contract as an ERC20
            log Transfer(ZERO_ADDRESS, msg.sender, 0)

        File 5 of 5: Vyper_contract
        # @version 0.3.1
        """
        @title Curve LP Token V5
        @author Curve.Fi
        @notice Base implementation for an LP token provided for supplying liquidity
        @dev Follows the ERC-20 token standard as defined at https://eips.ethereum.org/EIPS/eip-20
        """
        from vyper.interfaces import ERC20
        
        implements: ERC20
        
        
        interface ERC1271:
            def isValidSignature(_hash: bytes32, _signature: Bytes[65]) -> bytes32: view
        
        
        event Approval:
            _owner: indexed(address)
            _spender: indexed(address)
            _value: uint256
        
        event Transfer:
            _from: indexed(address)
            _to: indexed(address)
            _value: uint256
        
        
        EIP712_TYPEHASH: constant(bytes32) = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
        PERMIT_TYPEHASH: constant(bytes32) = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)")
        
        # keccak256("isValidSignature(bytes32,bytes)")[:4] << 224
        ERC1271_MAGIC_VAL: constant(bytes32) = 0x1626ba7e00000000000000000000000000000000000000000000000000000000
        VERSION: constant(String[8]) = "v5.0.0"
        
        
        name: public(String[64])
        symbol: public(String[32])
        DOMAIN_SEPARATOR: public(bytes32)
        
        balanceOf: public(HashMap[address, uint256])
        allowance: public(HashMap[address, HashMap[address, uint256]])
        totalSupply: public(uint256)
        
        minter: public(address)
        nonces: public(HashMap[address, uint256])
        
        
        @external
        def __init__():
            self.minter = 0x0000000000000000000000000000000000000001
        
        
        @external
        def transfer(_to: address, _value: uint256) -> bool:
            """
            @dev Transfer token for a specified address
            @param _to The address to transfer to.
            @param _value The amount to be transferred.
            """
            # NOTE: vyper does not allow underflows
            #       so the following subtraction would revert on insufficient balance
            self.balanceOf[msg.sender] -= _value
            self.balanceOf[_to] += _value
        
            log Transfer(msg.sender, _to, _value)
            return True
        
        
        @external
        def transferFrom(_from: address, _to: address, _value: uint256) -> bool:
            """
             @dev Transfer tokens from one address to another.
             @param _from address The address which you want to send tokens from
             @param _to address The address which you want to transfer to
             @param _value uint256 the amount of tokens to be transferred
            """
            self.balanceOf[_from] -= _value
            self.balanceOf[_to] += _value
        
            _allowance: uint256 = self.allowance[_from][msg.sender]
            if _allowance != MAX_UINT256:
                self.allowance[_from][msg.sender] = _allowance - _value
        
            log Transfer(_from, _to, _value)
            return True
        
        
        @external
        def approve(_spender: address, _value: uint256) -> bool:
            """
            @notice Approve the passed address to transfer the specified amount of
                    tokens on behalf of msg.sender
            @dev Beware that changing an allowance via this method brings the risk
                 that someone may use both the old and new allowance by unfortunate
                 transaction ordering. This may be mitigated with the use of
                 {increaseAllowance} and {decreaseAllowance}.
                 https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
            @param _spender The address which will transfer the funds
            @param _value The amount of tokens that may be transferred
            @return bool success
            """
            self.allowance[msg.sender][_spender] = _value
        
            log Approval(msg.sender, _spender, _value)
            return True
        
        
        @external
        def permit(
            _owner: address,
            _spender: address,
            _value: uint256,
            _deadline: uint256,
            _v: uint8,
            _r: bytes32,
            _s: bytes32
        ) -> bool:
            """
            @notice Approves spender by owner's signature to expend owner's tokens.
                See https://eips.ethereum.org/EIPS/eip-2612.
            @dev Inspired by https://github.com/yearn/yearn-vaults/blob/main/contracts/Vault.vy#L753-L793
            @dev Supports smart contract wallets which implement ERC1271
                https://eips.ethereum.org/EIPS/eip-1271
            @param _owner The address which is a source of funds and has signed the Permit.
            @param _spender The address which is allowed to spend the funds.
            @param _value The amount of tokens to be spent.
            @param _deadline The timestamp after which the Permit is no longer valid.
            @param _v The bytes[64] of the valid secp256k1 signature of permit by owner
            @param _r The bytes[0:32] of the valid secp256k1 signature of permit by owner
            @param _s The bytes[32:64] of the valid secp256k1 signature of permit by owner
            @return True, if transaction completes successfully
            """
            assert _owner != ZERO_ADDRESS
            assert block.timestamp <= _deadline
        
            nonce: uint256 = self.nonces[_owner]
            digest: bytes32 = keccak256(
                concat(
                    b"\x19\x01",
                    self.DOMAIN_SEPARATOR,
                    keccak256(_abi_encode(PERMIT_TYPEHASH, _owner, _spender, _value, nonce, _deadline))
                )
            )
        
            if _owner.is_contract:
                sig: Bytes[65] = concat(_abi_encode(_r, _s), slice(convert(_v, bytes32), 31, 1))
                # reentrancy not a concern since this is a staticcall
                assert ERC1271(_owner).isValidSignature(digest, sig) == ERC1271_MAGIC_VAL
            else:
                assert ecrecover(digest, convert(_v, uint256), convert(_r, uint256), convert(_s, uint256)) == _owner
        
            self.allowance[_owner][_spender] = _value
            self.nonces[_owner] = nonce + 1
        
            log Approval(_owner, _spender, _value)
            return True
        
        
        @external
        def increaseAllowance(_spender: address, _added_value: uint256) -> bool:
            """
            @notice Increase the allowance granted to `_spender` by the caller
            @dev This is alternative to {approve} that can be used as a mitigation for
                 the potential race condition
            @param _spender The address which will transfer the funds
            @param _added_value The amount of to increase the allowance
            @return bool success
            """
            allowance: uint256 = self.allowance[msg.sender][_spender] + _added_value
            self.allowance[msg.sender][_spender] = allowance
        
            log Approval(msg.sender, _spender, allowance)
            return True
        
        
        @external
        def decreaseAllowance(_spender: address, _subtracted_value: uint256) -> bool:
            """
            @notice Decrease the allowance granted to `_spender` by the caller
            @dev This is alternative to {approve} that can be used as a mitigation for
                 the potential race condition
            @param _spender The address which will transfer the funds
            @param _subtracted_value The amount of to decrease the allowance
            @return bool success
            """
            allowance: uint256 = self.allowance[msg.sender][_spender] - _subtracted_value
            self.allowance[msg.sender][_spender] = allowance
        
            log Approval(msg.sender, _spender, allowance)
            return True
        
        
        @external
        def mint(_to: address, _value: uint256) -> bool:
            """
            @dev Mint an amount of the token and assigns it to an account.
                 This encapsulates the modification of balances such that the
                 proper events are emitted.
            @param _to The account that will receive the created tokens.
            @param _value The amount that will be created.
            """
            assert msg.sender == self.minter
        
            self.totalSupply += _value
            self.balanceOf[_to] += _value
        
            log Transfer(ZERO_ADDRESS, _to, _value)
            return True
        
        
        @external
        def mint_relative(_to: address, frac: uint256) -> uint256:
            """
            @dev Increases supply by factor of (1 + frac/1e18) and mints it for _to
            """
            assert msg.sender == self.minter
        
            supply: uint256 = self.totalSupply
            d_supply: uint256 = supply * frac / 10**18
            if d_supply > 0:
                self.totalSupply = supply + d_supply
                self.balanceOf[_to] += d_supply
                log Transfer(ZERO_ADDRESS, _to, d_supply)
        
            return d_supply
        
        
        @external
        def burnFrom(_to: address, _value: uint256) -> bool:
            """
            @dev Burn an amount of the token from a given account.
            @param _to The account whose tokens will be burned.
            @param _value The amount that will be burned.
            """
            assert msg.sender == self.minter
        
            self.totalSupply -= _value
            self.balanceOf[_to] -= _value
        
            log Transfer(_to, ZERO_ADDRESS, _value)
            return True
        
        
        @view
        @external
        def decimals() -> uint8:
            """
            @notice Get the number of decimals for this token
            @dev Implemented as a view method to reduce gas costs
            @return uint8 decimal places
            """
            return 18
        
        
        @view
        @external
        def version() -> String[8]:
            """
            @notice Get the version of this token contract
            """
            return VERSION
        
        
        @external
        def initialize(_name: String[64], _symbol: String[32], _pool: address):
            assert self.minter == ZERO_ADDRESS  # dev: check that we call it from factory
        
            self.name = _name
            self.symbol = _symbol
            self.minter = _pool
        
            self.DOMAIN_SEPARATOR = keccak256(
                _abi_encode(EIP712_TYPEHASH, keccak256(_name), keccak256(VERSION), chain.id, self)
            )
        
            # fire a transfer event so block explorers identify the contract as an ERC20
            log Transfer(ZERO_ADDRESS, msg.sender, 0)