Initial build for QIE Mainnet Hackathon 3.0 (DeFi & Financial Infrastructure track). Includes: - packages/contracts: constant-product AMM (factory/pair/router), over-collateralized lending vault, and an oracle adapter wrapping QIE's real deployed price-feed contracts. 25/25 Foundry tests pass locally. Clean-room: no existing DEX/lending protocol source was read or copied. - packages/core: TypeScript SDK (viem-based) for QIE's chain, the real (corrected) oracle ABI, a fail-closed QUSDC helper, a Blockscout explorer client, and QIE Wallet connection constants. Live-verified against real testnet (chain 1983) and mainnet (chain 1990) RPCs and the real oracle contracts. - packages/cli: a small CLI built on @qie-forge/core. Real findings from building this, documented in README.md and docs/STATUS.md: the previously-documented testnet RPC URL is dead (replacement found and verified live); QIE's own oracle docs describe a getPrice(string) function that reverts on the real deployed contracts, which actually implement a Chainlink AggregatorV3-style interface (verified live via raw eth_call); the oracle is deployed on Mainnet only, not Testnet. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
235 lines
8.3 KiB
Solidity
235 lines
8.3 KiB
Solidity
pragma solidity ^0.8.31;
|
|
|
|
import {Math} from "../math/Math.sol";
|
|
|
|
/**
|
|
* @dev This library defines the `Trace*` struct, for checkpointing values as they change at different points in
|
|
* time, and later looking up past values by block number. See {Votes} as an example.
|
|
*
|
|
* To create a history of checkpoints define a variable type `Checkpoints.Trace*` in your contract, and store a new
|
|
* checkpoint for the current transaction block using the {push} function.
|
|
*/
|
|
library Checkpoints {
|
|
/**
|
|
* @dev A value was attempted to be inserted on a past checkpoint.
|
|
*/
|
|
error CheckpointUnorderedInsertion();
|
|
|
|
<%- for (const { size, keySize } of it.CHECKPOINTS_LENGTH) { %>
|
|
struct Trace<%= size %> {
|
|
Checkpoint<%= size %>[] _checkpoints;
|
|
}
|
|
|
|
struct Checkpoint<%= size %> {
|
|
uint<%= keySize %> _key;
|
|
uint<%= size %> _value;
|
|
}
|
|
|
|
/**
|
|
* @dev Pushes a (`key`, `value`) pair into a Trace<%= size %> so that it is stored as the checkpoint.
|
|
*
|
|
* Returns previous value and new value.
|
|
*
|
|
* IMPORTANT: Never accept `key` as a user input, since an arbitrary `type(uint<%= keySize %>).max` key set will disable the
|
|
* library.
|
|
*/
|
|
function push(
|
|
Trace<%= size %> storage self,
|
|
uint<%= keySize %> key,
|
|
uint<%= size %> value
|
|
) internal returns (uint<%= size %> oldValue, uint<%= size %> newValue) {
|
|
return _insert(self._checkpoints, key, value);
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if
|
|
* there is none.
|
|
*/
|
|
function lowerLookup(Trace<%= size %> storage self, uint<%= keySize %> key) internal view returns (uint<%= size %>) {
|
|
uint256 len = self._checkpoints.length;
|
|
uint256 index = _lowerBinaryLookup(self._checkpoints, key, 0, len);
|
|
return index == len ? 0 : _unsafeAccess(self._checkpoints, index)._value;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
|
|
* if there is none.
|
|
*/
|
|
function upperLookup(Trace<%= size %> storage self, uint<%= keySize %> key) internal view returns (uint<%= size %>) {
|
|
uint256 len = self._checkpoints.length;
|
|
uint256 index = _upperBinaryLookup(self._checkpoints, key, 0, len);
|
|
return index == 0 ? 0 : _unsafeAccess(self._checkpoints, index - 1)._value;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero
|
|
* if there is none.
|
|
*
|
|
* NOTE: This is a variant of {upperLookup} that is optimized to find "recent" checkpoint (checkpoints with high
|
|
* keys).
|
|
*/
|
|
function upperLookupRecent(Trace<%= size %> storage self, uint<%= keySize %> key) internal view returns (uint<%= size %>) {
|
|
uint256 len = self._checkpoints.length;
|
|
|
|
uint256 low = 0;
|
|
uint256 high = len;
|
|
|
|
if (len > 5) {
|
|
uint256 mid = len - Math.sqrt(len);
|
|
if (key < _unsafeAccess(self._checkpoints, mid)._key) {
|
|
high = mid;
|
|
} else {
|
|
low = mid + 1;
|
|
}
|
|
}
|
|
|
|
uint256 index = _upperBinaryLookup(self._checkpoints, key, low, high);
|
|
|
|
return index == 0 ? 0 : _unsafeAccess(self._checkpoints, index - 1)._value;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
|
|
*/
|
|
function latest(Trace<%= size %> storage self) internal view returns (uint<%= size %>) {
|
|
uint256 len = self._checkpoints.length;
|
|
return len == 0 ? 0 : _unsafeAccess(self._checkpoints, len - 1)._value;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value
|
|
* in the most recent checkpoint.
|
|
*/
|
|
function latestCheckpoint(Trace<%= size %> storage self) internal view returns (bool exists, uint<%= keySize %> _key, uint<%= size %> _value) {
|
|
uint256 len = self._checkpoints.length;
|
|
if (len == 0) {
|
|
return (false, 0, 0);
|
|
} else {
|
|
Checkpoint<%= size %> storage ckpt = _unsafeAccess(self._checkpoints, len - 1);
|
|
return (true, ckpt._key, ckpt._value);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the number of checkpoints.
|
|
*/
|
|
function length(Trace<%= size %> storage self) internal view returns (uint256) {
|
|
return self._checkpoints.length;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns checkpoint at given position.
|
|
*
|
|
* IMPORTANT: Deprecated. This function's name clashes with a keyword scheduled for inclusion in Solidity. Developers
|
|
* should use {pos} instead.
|
|
*/
|
|
function at(Trace<%= size %> storage self, uint32 index) internal view returns (Checkpoint<%= size %> memory) {
|
|
return pos(self, index);
|
|
}
|
|
|
|
/**
|
|
* @dev Returns checkpoint at given position.
|
|
*
|
|
* Replacement of the deprecated {at} function.
|
|
*/
|
|
function pos(Trace<%= size %> storage self, uint32 index) internal view returns (Checkpoint<%= size %> memory) {
|
|
return self._checkpoints[index];
|
|
}
|
|
|
|
/**
|
|
* @dev Pushes a (`key`, `value`) pair into an ordered list of checkpoints, either by inserting a new checkpoint,
|
|
* or by updating the last one.
|
|
*/
|
|
function _insert(
|
|
Checkpoint<%= size %>[] storage self,
|
|
uint<%= keySize %> key,
|
|
uint<%= size %> value
|
|
) private returns (uint<%= size %> oldValue, uint<%= size %> newValue) {
|
|
uint256 len = self.length;
|
|
|
|
if (len > 0) {
|
|
Checkpoint<%= size %> storage last = _unsafeAccess(self, len - 1);
|
|
uint<%= keySize %> lastKey = last._key;
|
|
uint<%= size %> lastValue = last._value;
|
|
|
|
// Checkpoint keys must be non-decreasing.
|
|
if (lastKey > key) {
|
|
revert CheckpointUnorderedInsertion();
|
|
}
|
|
|
|
// Update or push new checkpoint
|
|
if (lastKey == key) {
|
|
last._value = value;
|
|
} else {
|
|
self.push(Checkpoint<%= size %>({_key: key, _value: value}));
|
|
}
|
|
return (lastValue, value);
|
|
} else {
|
|
self.push(Checkpoint<%= size %>({_key: key, _value: value}));
|
|
return (0, value);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dev Return the index of the first (oldest) checkpoint with key strictly bigger than the search key, or `high`
|
|
* if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
|
|
* `high`.
|
|
*
|
|
* WARNING: `high` should not be greater than the array's length.
|
|
*/
|
|
function _upperBinaryLookup(
|
|
Checkpoint<%= size %>[] storage self,
|
|
uint<%= keySize %> key,
|
|
uint256 low,
|
|
uint256 high
|
|
) private view returns (uint256) {
|
|
while (low < high) {
|
|
uint256 mid = Math.average(low, high);
|
|
if (_unsafeAccess(self, mid)._key > key) {
|
|
high = mid;
|
|
} else {
|
|
low = mid + 1;
|
|
}
|
|
}
|
|
return high;
|
|
}
|
|
|
|
/**
|
|
* @dev Return the index of the first (oldest) checkpoint with key greater or equal than the search key, or `high`
|
|
* if there is none. `low` and `high` define a section where to do the search, with inclusive `low` and exclusive
|
|
* `high`.
|
|
*
|
|
* WARNING: `high` should not be greater than the array's length.
|
|
*/
|
|
function _lowerBinaryLookup(
|
|
Checkpoint<%= size %>[] storage self,
|
|
uint<%= keySize %> key,
|
|
uint256 low,
|
|
uint256 high
|
|
) private view returns (uint256) {
|
|
while (low < high) {
|
|
uint256 mid = Math.average(low, high);
|
|
if (_unsafeAccess(self, mid)._key < key) {
|
|
low = mid + 1;
|
|
} else {
|
|
high = mid;
|
|
}
|
|
}
|
|
return high;
|
|
}
|
|
|
|
/**
|
|
* @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
|
|
*/
|
|
function _unsafeAccess(
|
|
Checkpoint<%= size %>[] storage self,
|
|
uint256 index
|
|
) private pure returns (Checkpoint<%= size %> storage result) {
|
|
assembly {
|
|
mstore(0x00, self.slot)
|
|
result.slot := add(keccak256(0x00, 0x20), <%= size < 256 ? 'index' : `mul(index, 2)` %>)
|
|
}
|
|
}
|
|
<% } -%>
|
|
}
|