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>
116 lines
4.3 KiB
Solidity
116 lines
4.3 KiB
Solidity
pragma solidity ^0.8.20;
|
|
|
|
import {Test} from "forge-std/Test.sol";
|
|
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
|
|
import {Checkpoints} from "@openzeppelin/contracts/utils/structs/Checkpoints.sol";
|
|
|
|
<%- for (const { size, keySize } of it.CHECKPOINTS_LENGTH) { %>
|
|
contract CheckpointsTrace<%= size %>Test is Test {
|
|
using Checkpoints for Checkpoints.Trace<%= size %>;
|
|
|
|
// Maximum gap between keys used during the fuzzing tests: the `_prepareKeys` function will make sure that
|
|
// key#n+1 is in the [key#n, key#n + _KEY_MAX_GAP] range.
|
|
uint8 internal constant _KEY_MAX_GAP = 64;
|
|
|
|
Checkpoints.Trace<%= size %> internal _ckpts;
|
|
|
|
// helpers
|
|
function _boundUint<%= keySize %>(uint<%= keySize %> x, uint<%= keySize %> min, uint<%= keySize %> max) internal pure returns (uint<%= keySize %>) {
|
|
return <%= size === 256 ? 'bound(x, min, max)' : `SafeCast.toUint${keySize}(bound(uint256(x), uint256(min), uint256(max)))` %>;
|
|
}
|
|
|
|
function _prepareKeys(uint<%= keySize %>[] memory keys, uint<%= keySize %> maxSpread) internal pure {
|
|
uint<%= keySize %> lastKey = 0;
|
|
for (uint256 i = 0; i < keys.length; ++i) {
|
|
uint<%= keySize %> key = _boundUint<%= keySize %>(keys[i], lastKey, lastKey + maxSpread);
|
|
keys[i] = key;
|
|
lastKey = key;
|
|
}
|
|
}
|
|
|
|
function _assertLatestCheckpoint(bool exist, uint<%= keySize %> key, uint<%= size %> value) internal view {
|
|
(bool _exist, uint<%= keySize %> _key, uint<%= size %> _value) = _ckpts.latestCheckpoint();
|
|
assertEq(_exist, exist);
|
|
assertEq(_key, key);
|
|
assertEq(_value, value);
|
|
}
|
|
|
|
// tests
|
|
function testPush(uint<%= keySize %>[] memory keys, uint<%= size %>[] memory values, uint<%= keySize %> pastKey) public {
|
|
vm.assume(values.length > 0 && values.length <= keys.length);
|
|
_prepareKeys(keys, _KEY_MAX_GAP);
|
|
|
|
// initial state
|
|
assertEq(_ckpts.length(), 0);
|
|
assertEq(_ckpts.latest(), 0);
|
|
_assertLatestCheckpoint(false, 0, 0);
|
|
|
|
uint256 duplicates = 0;
|
|
for (uint256 i = 0; i < keys.length; ++i) {
|
|
uint<%= keySize %> key = keys[i];
|
|
uint<%= size %> value = values[i % values.length];
|
|
if (i > 0 && key == keys[i - 1]) ++duplicates;
|
|
|
|
// push
|
|
_ckpts.push(key, value);
|
|
|
|
// check length & latest
|
|
assertEq(_ckpts.length(), i + 1 - duplicates);
|
|
assertEq(_ckpts.latest(), value);
|
|
_assertLatestCheckpoint(true, key, value);
|
|
}
|
|
|
|
if (keys.length > 0) {
|
|
uint<%= keySize %> lastKey = keys[keys.length - 1];
|
|
if (lastKey > 0) {
|
|
pastKey = _boundUint<%= keySize %>(pastKey, 0, lastKey - 1);
|
|
|
|
vm.expectRevert();
|
|
this.push(pastKey, values[keys.length % values.length]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// used to test reverts
|
|
function push(uint<%= keySize %> key, uint<%= size %> value) external {
|
|
_ckpts.push(key, value);
|
|
}
|
|
|
|
function testLookup(uint<%= keySize %>[] memory keys, uint<%= size %>[] memory values, uint<%= keySize %> lookup) public {
|
|
vm.assume(values.length > 0 && values.length <= keys.length);
|
|
_prepareKeys(keys, _KEY_MAX_GAP);
|
|
|
|
uint<%= keySize %> lastKey = keys.length == 0 ? 0 : keys[keys.length - 1];
|
|
lookup = _boundUint<%= keySize %>(lookup, 0, lastKey + _KEY_MAX_GAP);
|
|
|
|
uint<%= size %> upper = 0;
|
|
uint<%= size %> lower = 0;
|
|
uint<%= keySize %> lowerKey = type(uint<%= keySize %>).max;
|
|
for (uint256 i = 0; i < keys.length; ++i) {
|
|
uint<%= keySize %> key = keys[i];
|
|
uint<%= size %> value = values[i % values.length];
|
|
|
|
// push
|
|
_ckpts.push(key, value);
|
|
|
|
// track expected result of lookups
|
|
if (key <= lookup) {
|
|
upper = value;
|
|
}
|
|
// find the first key that is not smaller than the lookup key
|
|
if (key >= lookup && (i == 0 || keys[i - 1] < lookup)) {
|
|
lowerKey = key;
|
|
}
|
|
if (key == lowerKey) {
|
|
lower = value;
|
|
}
|
|
}
|
|
|
|
// check lookup
|
|
assertEq(_ckpts.lowerLookup(lookup), lower);
|
|
assertEq(_ckpts.upperLookup(lookup), upper);
|
|
assertEq(_ckpts.upperLookupRecent(lookup), upper);
|
|
}
|
|
}
|
|
<% } -%>
|