library Samples {
    using WordCodec for int256;
    using WordCodec for uint256;
    using WordCodec for bytes32;

    uint256 internal constant _TIMESTAMP_OFFSET = 0;
    uint256 internal constant _ACC_LOG_INVARIANT_OFFSET = 31;
    uint256 internal constant _INST_LOG_INVARIANT_OFFSET = 84;
    uint256 internal constant _ACC_LOG_BPT_PRICE_OFFSET = 106;
    uint256 internal constant _INST_LOG_BPT_PRICE_OFFSET = 159;
    uint256 internal constant _ACC_LOG_PAIR_PRICE_OFFSET = 181;
    uint256 internal constant _INST_LOG_PAIR_PRICE_OFFSET = 234;

    /**
     * @dev Updates a sample, accumulating the new data based on the elapsed time since the previous update. Returns the
     * updated sample.
     *
     * IMPORTANT: This function does not perform any arithmetic checks. In particular, it assumes the caller will never
     * pass values that cannot be represented as 22 bit signed integers. Additionally, it also assumes
     * `currentTimestamp` is greater than `sample`'s timestamp.
     */
    function update(
        bytes32 sample,
        int256 instLogPairPrice,
        int256 instLogBptPrice,
        int256 instLogInvariant,
        uint256 currentTimestamp
    ) internal pure returns (bytes32) {
        // Because elapsed can be represented as a 31 bit unsigned integer, and the received values can be represented
        // as 22 bit signed integers, we don't need to perform checked arithmetic.

        int256 elapsed = int256(currentTimestamp - timestamp(sample));
        int256 accLogPairPrice = _accLogPairPrice(sample) + instLogPairPrice * elapsed;
        int256 accLogBptPrice = _accLogBptPrice(sample) + instLogBptPrice * elapsed;
        int256 accLogInvariant = _accLogInvariant(sample) + instLogInvariant * elapsed;

        return
            pack(
                instLogPairPrice,
                accLogPairPrice,
                instLogBptPrice,
                accLogBptPrice,
                instLogInvariant,
                accLogInvariant,
                currentTimestamp
            );
    }

    /**
     * @dev Returns the instant value stored in `sample` for `variable`.
     */
    function instant(bytes32 sample, IPriceOracle.Variable variable) internal pure returns (int256) {
        if (variable == IPriceOracle.Variable.PAIR_PRICE) {
            return _instLogPairPrice(sample);
        } else if (variable == IPriceOracle.Variable.BPT_PRICE) {
            return _instLogBptPrice(sample);
        } else {
            // variable == IPriceOracle.Variable.INVARIANT
            return _instLogInvariant(sample);
        }
    }

    /**
     * @dev Returns the accumulator value stored in `sample` for `variable`.
     */
    function accumulator(bytes32 sample, IPriceOracle.Variable variable) internal pure returns (int256) {
        if (variable == IPriceOracle.Variable.PAIR_PRICE) {
            return _accLogPairPrice(sample);
        } else if (variable == IPriceOracle.Variable.BPT_PRICE) {
            return _accLogBptPrice(sample);
        } else {
            // variable == IPriceOracle.Variable.INVARIANT
            return _accLogInvariant(sample);
        }
    }

    /**
     * @dev Returns `sample`'s timestamp.
     */
    function timestamp(bytes32 sample) internal pure returns (uint256) {
        return sample.decodeUint31(_TIMESTAMP_OFFSET);
    }

    /**
     * @dev Returns `sample`'s instant value for the logarithm of the pair price.
     */
    function _instLogPairPrice(bytes32 sample) private pure returns (int256) {
        return sample.decodeInt22(_INST_LOG_PAIR_PRICE_OFFSET);
    }

    /**
     * @dev Returns `sample`'s accumulator of the logarithm of the pair price.
     */
    function _accLogPairPrice(bytes32 sample) private pure returns (int256) {
        return sample.decodeInt53(_ACC_LOG_PAIR_PRICE_OFFSET);
    }

    /**
     * @dev Returns `sample`'s instant value for the logarithm of the BPT price.
     */
    function _instLogBptPrice(bytes32 sample) private pure returns (int256) {
        return sample.decodeInt22(_INST_LOG_BPT_PRICE_OFFSET);
    }

    /**
     * @dev Returns `sample`'s accumulator of the logarithm of the BPT price.
     */
    function _accLogBptPrice(bytes32 sample) private pure returns (int256) {
        return sample.decodeInt53(_ACC_LOG_BPT_PRICE_OFFSET);
    }

    /**
     * @dev Returns `sample`'s instant value for the logarithm of the invariant.
     */
    function _instLogInvariant(bytes32 sample) private pure returns (int256) {
        return sample.decodeInt22(_INST_LOG_INVARIANT_OFFSET);
    }

    /**
     * @dev Returns `sample`'s accumulator of the logarithm of the invariant.
     */
    function _accLogInvariant(bytes32 sample) private pure returns (int256) {
        return sample.decodeInt53(_ACC_LOG_INVARIANT_OFFSET);
    }

    /**
     * @dev Returns a sample created by packing together its components.
     */
    function pack(
        int256 instLogPairPrice,
        int256 accLogPairPrice,
        int256 instLogBptPrice,
        int256 accLogBptPrice,
        int256 instLogInvariant,
        int256 accLogInvariant,
        uint256 _timestamp
    ) internal pure returns (bytes32) {
        return
            instLogPairPrice.encodeInt22(_INST_LOG_PAIR_PRICE_OFFSET) |
            accLogPairPrice.encodeInt53(_ACC_LOG_PAIR_PRICE_OFFSET) |
            instLogBptPrice.encodeInt22(_INST_LOG_BPT_PRICE_OFFSET) |
            accLogBptPrice.encodeInt53(_ACC_LOG_BPT_PRICE_OFFSET) |
            instLogInvariant.encodeInt22(_INST_LOG_INVARIANT_OFFSET) |
            accLogInvariant.encodeInt53(_ACC_LOG_INVARIANT_OFFSET) |
            _timestamp.encodeUint31(_TIMESTAMP_OFFSET);
    }

    /**
     * @dev Unpacks a sample into its components.
     */
    function unpack(bytes32 sample)
        internal
        pure
        returns (
            int256 logPairPrice,
            int256 accLogPairPrice,
            int256 logBptPrice,
            int256 accLogBptPrice,
            int256 logInvariant,
            int256 accLogInvariant,
            uint256 _timestamp
        )
    {
        logPairPrice = _instLogPairPrice(sample);
        accLogPairPrice = _accLogPairPrice(sample);
        logBptPrice = _instLogBptPrice(sample);
        accLogBptPrice = _accLogBptPrice(sample);
        logInvariant = _instLogInvariant(sample);
        accLogInvariant = _accLogInvariant(sample);
        _timestamp = timestamp(sample);
    }
}
/**
 * @dev This library provides functions to help manipulating samples for Pool Price Oracles. It handles updates,
 * encoding, and decoding of samples.
 *
 * Each sample holds the timestamp of its last update, plus information about three pieces of data: the price pair, the
 * price of BPT (the associated Pool token), and the invariant.
 *
 * Prices and invariant are not stored directly: instead, we store their logarithm. These are known as the 'instant'
 * values: the exact value at that timestamp.
 *
 * Additionally, for each value we keep an accumulator with the sum of all past values, each weighted by the time
 * elapsed since the previous update. This lets us later subtract accumulators at different points in time and divide by
 * the time elapsed between them, arriving at the geometric mean of the values (also known as log-average).
 *
 * All samples are stored in a single 256 bit word with the following structure:
 *
 * [    log pair price     |        bpt price      |       invariant       ]
 * [ instant | accumulator | instant | accumulator | instant | accumulator |  timestamp ]
 * [  int22  |    int53    |  int22  |    int53    |  int22  |    int53    |    uint31  ]
 * MSB                                                                                LSB
 *
 * Assuming the timestamp doesn't overflow (which holds until the year 2038), the largest elapsed time is 2^31, which
 * means the largest possible accumulator value is 2^21 * 2^31, which can be represented using a signed 53 bit integer.
 */
function update(
    bytes32 sample,
    int256 instLogPairPrice,
    int256 instLogBptPrice,
    int256 instLogInvariant,
    uint256 currentTimestamp
) internal pure returns (bytes32) {
    // Because elapsed can be represented as a 31 bit unsigned integer, and the received values can be represented
    // as 22 bit signed integers, we don't need to perform checked arithmetic.

    int256 elapsed = int256(currentTimestamp - timestamp(sample));
    int256 accLogPairPrice = _accLogPairPrice(sample) + instLogPairPrice * elapsed;
    int256 accLogBptPrice = _accLogBptPrice(sample) + instLogBptPrice * elapsed;
    int256 accLogInvariant = _accLogInvariant(sample) + instLogInvariant * elapsed;

    return
        pack(
            instLogPairPrice,
            accLogPairPrice,
            instLogBptPrice,
            accLogBptPrice,
            instLogInvariant,
            accLogInvariant,
            currentTimestamp
        );
}

/**
 * @dev Updates a sample, accumulating the new data based on the elapsed time since the previous update. Returns the
 * updated sample.
 *
 * IMPORTANT: This function does not perform any arithmetic checks. In particular, it assumes the caller will never
 * pass values that cannot be represented as 22 bit signed integers. Additionally, it also assumes
 * `currentTimestamp` is greater than `sample`'s timestamp.
 */