caelum0x 94c26e9496 feat: QIE Forge — clean-room AMM + lending vault, SDK, and CLI for QIE
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>
2026-10-26 23:34:51 +03:00

565 lines
21 KiB
Solidity

pragma solidity ^0.8.31;
import {Arrays} from "../Arrays.sol";
import {Math} from "../math/Math.sol";
/**
* @dev Library for managing
* https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
* types.
*
* Sets have the following properties:
*
* - Elements are added, removed, and checked for existence in constant time
* (O(1)).
* - Elements are enumerated in O(n). No guarantees are made on the ordering.
* - Set can be cleared (all elements removed) in O(n).
*
* ```solidity
* contract Example {
* // Add the library methods
* using EnumerableSet for EnumerableSet.AddressSet;
*
* // Declare a set state variable
* EnumerableSet.AddressSet private mySet;
* }
* ```
*
* The following types are supported:
*
* - `bytes32` (`Bytes32Set`) since v3.3.0
* - `address` (`AddressSet`) since v3.3.0
* - `uint256` (`UintSet`) since v3.3.0
* - `string` (`StringSet`) since v5.4.0
* - `bytes` (`BytesSet`) since v5.4.0
* - `bytes4` (`Bytes4Set`) since v5.6.0
*
* [WARNING]
* ====
* Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
* unusable.
* See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
*
* In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an
* array of EnumerableSet.
* ====
*/
library EnumerableSet {
// To implement this library for multiple types with as little code
// repetition as possible, we write it in terms of a generic Set type with
// bytes32 values.
// The Set implementation uses private functions, and user-facing
// implementations (such as AddressSet) are just wrappers around the
// underlying Set.
// This means that we can only create new EnumerableSets for types that fit
// in bytes32.
struct Set {
// Storage of set values
bytes32[] _values;
// Position is the index of the value in the `values` array plus 1.
// Position 0 is used to mean a value is not in the set.
mapping(bytes32 value => uint256) _positions;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function _add(Set storage set, bytes32 value) private returns (bool) {
if (!_contains(set, value)) {
set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value
set._positions[value] = set._values.length;
return true;
} else {
return false;
}
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function _remove(Set storage set, bytes32 value) private returns (bool) {
uint256 position = set._positions[value];
if (position != 0) {
_removeValueAt(set, value, position - 1);
return true;
} else {
return false;
}
}
/**
* @dev Removes the value stored at position `index` from a set. O(1).
*
* Returns the removed value.
*
* This is cheaper than {remove} when the caller already knows the index, because it skips the position lookup
* that {remove} performs.
*
* Note that there are no guarantees on the ordering of values inside the array, and it may change when more
* values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function _removeAt(Set storage set, uint256 index) private returns (bytes32 value) {
value = set._values[index];
_removeValueAt(set, value, index);
}
/**
* @dev Removes the value stored at position `index` from a set. O(1).
*
* To delete an element from the `_values` array in O(1), we swap the element to delete with the last one in the
* array, and then remove the last element (sometimes called as 'swap and pop'). This modifies the order of the
* array, as noted in {at}.
*
* IMPORTANT: This does not verify that `value` is the value currently stored at `index`. Callers must ensure
* both arguments are consistent, otherwise the set is left in a corrupted state.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function _removeValueAt(Set storage set, bytes32 value, uint256 index) private {
uint256 lastIndex = set._values.length - 1;
if (index != lastIndex) {
bytes32 lastValue = Arrays.unsafeAccess(set._values, lastIndex).value;
// Move the lastValue to the index where the value to delete is
Arrays.unsafeAccess(set._values, index).value = lastValue;
// Update the tracked position of the lastValue (that was just moved)
set._positions[lastValue] = index + 1;
}
// Delete the slot where the moved value was stored
set._values.pop();
// Delete the tracked position for the deleted slot
delete set._positions[value];
}
/**
* @dev Removes all the values from a set. O(n).
*
* WARNING: This function has an unbounded cost that scales with set size. Developers should keep in mind that
* using it may render the function uncallable if the set grows to the point where clearing it consumes too much
* gas to fit in a block.
*/
function _clear(Set storage set) private {
uint256 len = _length(set);
for (uint256 i = 0; i < len; ++i) {
delete set._positions[Arrays.unsafeAccess(set._values, i).value];
}
Arrays.unsafeSetLength(set._values, 0);
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function _contains(Set storage set, bytes32 value) private view returns (bool) {
return set._positions[value] != 0;
}
/**
* @dev Returns the number of values on the set. O(1).
*/
function _length(Set storage set) private view returns (uint256) {
return set._values.length;
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function _pos(Set storage set, uint256 index) private view returns (bytes32) {
return set._values[index];
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function _values(Set storage set) private view returns (bytes32[] memory) {
return set._values;
}
/**
* @dev Return a slice of the set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function _values(Set storage set, uint256 start, uint256 end) private view returns (bytes32[] memory) {
unchecked {
end = Math.min(end, _length(set));
start = Math.min(start, end);
uint256 len = end - start;
bytes32[] memory result = new bytes32[](len);
for (uint256 i = 0; i < len; ++i) {
result[i] = Arrays.unsafeAccess(set._values, start + i).value;
}
return result;
}
}
<%- for (const { name, value } of it.SET_TYPES.filter(({ value }) => value.size)) { %>
// <%= name %>
struct <%= name %> {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(<%= name %> storage set, <%= value.type %> value) internal returns (bool) {
return _add(set._inner, <%= it.toBytes32(value.type, 'value') %>);
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(<%= name %> storage set, <%= value.type %> value) internal returns (bool) {
return _remove(set._inner, <%= it.toBytes32(value.type, 'value') %>);
}
/**
* @dev Removes the value stored at position `index` from a set. O(1).
*
* Returns the removed value.
*
* This is cheaper than {remove} when the caller already knows the index, because it skips the position lookup
* that {remove} performs.
*
* Note that there are no guarantees on the ordering of values inside the array, and it may change when more
* values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function removeAt(<%= name %> storage set, uint256 index) internal returns (<%= value.type %>) {
return <%= it.fromBytes32(value.type, '_removeAt(set._inner, index)') %>;
}
/**
* @dev Removes all the values from a set. O(n).
*
* WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
* function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
*/
function clear(<%= name %> storage set) internal {
_clear(set._inner);
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(<%= name %> storage set, <%= value.type %> value) internal view returns (bool) {
return _contains(set._inner, <%= it.toBytes32(value.type, 'value') %>);
}
/**
* @dev Returns the number of values in the set. O(1).
*/
function length(<%= name %> storage set) internal view returns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*
* IMPORTANT: Deprecated. This function's name clashes with a keyword scheduled for inclusion in Solidity. Developers
* should use {pos} instead.
*/
function at(<%= name %> storage set, uint256 index) internal view returns (<%= value.type %>) {
return pos(set, index);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*
* Replacement of the deprecated {at} function.
*/
function pos(<%= name %> storage set, uint256 index) internal view returns (<%= value.type %>) {
return <%= it.fromBytes32(value.type, '_pos(set._inner, index)') %>;
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(<%= name %> storage set) internal view returns (<%= value.type %>[] memory) {
bytes32[] memory store = _values(set._inner);
<%= value.type %>[] memory result;
assembly ("memory-safe") {
result := store
}
return result;
}
/**
* @dev Return a slice of the set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(<%= name %> storage set, uint256 start, uint256 end) internal view returns (<%= value.type %>[] memory) {
bytes32[] memory store = _values(set._inner, start, end);
<%= value.type %>[] memory result;
assembly ("memory-safe") {
result := store
}
return result;
}
<% } -%>
<%- for (const { name, value } of it.SET_TYPES.filter(({ value }) => !value.size)) { %>
struct <%= name %> {
// Storage of set values
<%= value.type %>[] _values;
// Position is the index of the value in the `values` array plus 1.
// Position 0 is used to mean a value is not in the set.
mapping(<%= value.type %> value => uint256) _positions;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(<%= name %> storage set, <%= value.type %> memory value) internal returns (bool) {
if (!contains(set, value)) {
set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value
set._positions[value] = set._values.length;
return true;
} else {
return false;
}
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(<%= name %> storage set, <%= value.type %> memory value) internal returns (bool) {
// We cache the value's position to prevent multiple reads from the same storage slot
uint256 position = set._positions[value];
if (position != 0) {
_removeValueAt(set, value, position - 1);
return true;
} else {
return false;
}
}
/**
* @dev Removes the value stored at position `index` from a set. O(1).
*
* Returns the removed value.
*
* This is cheaper than {remove} when the caller already knows the index, because it skips the position lookup
* that {remove} performs.
*
* Note that there are no guarantees on the ordering of values inside the array, and it may change when more
* values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function removeAt(<%= name %> storage set, uint256 index) internal returns (<%= value.type %> memory value) {
value = set._values[index];
_removeValueAt(set, value, index);
}
/**
* @dev Removes the value stored at position `index` from a set. O(1).
*
* To delete an element from the `_values` array in O(1), we swap the element to delete with the last one in the
* array, and then remove the last element (sometimes called as 'swap and pop'). This modifies the order of the
* array, as noted in {at}.
*
* IMPORTANT: This does not verify that `value` is the value currently stored at `index`. Callers must ensure
* both arguments are consistent, otherwise the set is left in a corrupted state.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function _removeValueAt(<%= name %> storage set, <%= value.type %> memory value, uint256 index) private {
uint256 lastIndex = set._values.length - 1;
if (index != lastIndex) {
<%= value.type %> memory lastValue = Arrays.unsafeAccess(set._values, lastIndex).value;
// Move the lastValue to the index where the value to delete is
Arrays.unsafeAccess(set._values, index).value = lastValue;
// Update the tracked position of the lastValue (that was just moved)
set._positions[lastValue] = index + 1;
}
// Delete the slot where the moved value was stored
set._values.pop();
// Delete the tracked position for the deleted slot
delete set._positions[value];
}
/**
* @dev Removes all the values from a set. O(n).
*
* WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
* function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
*/
function clear(<%= name %> storage set) internal {
uint256 len = length(set);
for (uint256 i = 0; i < len; ++i) {
delete set._positions[Arrays.unsafeAccess(set._values, i).value];
}
Arrays.unsafeSetLength(set._values, 0);
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(<%= name %> storage set, <%= value.type %> memory value) internal view returns (bool) {
return set._positions[value] != 0;
}
/**
* @dev Returns the number of values on the set. O(1).
*/
function length(<%= name %> storage set) internal view returns (uint256) {
return set._values.length;
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*
* IMPORTANT: Deprecated. This function's name clashes with a keyword scheduled for inclusion in Solidity. Developers
* should use {pos} instead.
*/
function at(<%= name %> storage set, uint256 index) internal view returns (<%= value.type %> memory) {
return pos(set, index);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*
* Replacement of the deprecated {at} function.
*/
function pos(<%= name %> storage set, uint256 index) internal view returns (<%= value.type %> memory) {
return set._values[index];
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(<%= name %> storage set) internal view returns (<%= value.type %>[] memory) {
return set._values;
}
/**
* @dev Return a slice of the set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(<%= name %> storage set, uint256 start, uint256 end) internal view returns (<%= value.type %>[] memory) {
unchecked {
end = Math.min(end, length(set));
start = Math.min(start, end);
uint256 len = end - start;
<%= value.type %>[] memory result = new <%= value.type %>[](len);
for (uint256 i = 0; i < len; ++i) {
result[i] = Arrays.unsafeAccess(set._values, start + i).value;
}
return result;
}
}
<% } -%>
}