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

141 lines
5.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
import fs from 'fs';
import graphlib from 'graphlib';
import semver from 'semver';
import pLimit from 'p-limit';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';
import { getContractsMetadata } from './get-contracts-metadata.js';
import { versions as allSolcVersions, compile } from './solc-versions.js';
const {
argv: { pattern, skipPatterns, minVersionForContracts, minVersionForInterfaces, concurrency, _: artifacts },
} = yargs(hideBin(process.argv))
.env('')
.options({
pattern: { alias: 'p', type: 'string', default: 'contracts/**/*.sol' },
skipPatterns: { alias: 's', type: 'string', default: 'contracts/mocks/**/*.sol' },
minVersionForContracts: { type: 'string', default: '0.8.20' },
minVersionForInterfaces: { type: 'string', default: '0.0.0' },
concurrency: { alias: 'c', type: 'number', default: 8 },
});
// limit concurrency
const limit = pLimit(concurrency);
/********************************************************************************************************************
* HELPERS *
********************************************************************************************************************/
/**
* Updates the pragma in the given file to the newPragma version.
* @param {*} file Absolute path to the file to update.
* @param {*} pragma New pragma version to set. (ex: '>=0.8.4')
*/
const updatePragma = (file, pragma) =>
fs.writeFileSync(
file,
fs
.readFileSync(file, 'utf8')
.replace(/pragma solidity [><=^]*[0-9]+\.[0-9]+\.[0-9]+;/, `pragma solidity ${pragma};`),
'utf8',
);
/**
* Get the applicable pragmas for a given file by compiling it with all solc versions.
* @param {*} file Absolute path to the file to compile.
* @param {*} candidates List of solc version to test. (ex: ['0.8.4','0.8.5'])
* @returns {Promise<string[]>} List of applicable pragmas.
*/
const getApplicablePragmas = (file, candidates = allSolcVersions) =>
Promise.all(
candidates.map(version =>
limit(() =>
compile(file, version).then(
() => version,
() => null,
),
),
),
).then(versions => versions.filter(Boolean));
/**
* Get the minimum applicable pragmas for a given file.
* @param {*} file Absolute path to the file to compile.
* @param {*} candidates List of solc version to test. (ex: ['0.8.4','0.8.5'])
* @returns {Promise<string>} Smallest applicable pragma out of the list.
*/
const getMinimalApplicablePragma = (file, candidates = allSolcVersions) =>
getApplicablePragmas(file, candidates).then(valid => {
if (valid.length == 0) {
throw new Error(`No valid pragma found for ${file}`);
} else {
return valid.sort(semver.compare).at(0);
}
});
/**
* Get the minimum applicable pragmas for a given file, and update the file to use it.
* @param {*} file Absolute path to the file to compile.
* @param {*} candidates List of solc version to test. (ex: ['0.8.4','0.8.5'])
* @param {*} prefix Prefix to use when building the pragma (ex: '^')
* @returns {Promise<string>} Version that was used and set in the file
*/
const setMinimalApplicablePragma = (file, candidates = allSolcVersions, prefix = '>=') =>
getMinimalApplicablePragma(file, candidates)
.then(version => `${prefix}${version}`)
.then(pragma => {
updatePragma(file, pragma);
return pragma;
});
/********************************************************************************************************************
* MAIN *
********************************************************************************************************************/
// Build metadata from artifact files (hardhat compilation)
const metadata = getContractsMetadata(pattern, skipPatterns, artifacts);
// Build dependency graph
const graph = new graphlib.Graph({ directed: true });
Object.keys(metadata).forEach(file => {
graph.setNode(file);
metadata[file].sources.forEach(dep => graph.setEdge(dep, file));
});
// Weaken all pragma to allow exploration
Object.keys(metadata).forEach(file => updatePragma(file, '>=0.0.0'));
// Do a topological traversal of the dependency graph, minimizing pragma for each file we encounter
(async () => {
const queue = graph.sources();
const pragmas = {};
while (queue.length) {
const file = queue.shift();
if (!Object.hasOwn(pragmas, file)) {
if (Object.hasOwn(metadata, file)) {
const minVersion = metadata[file].interface ? minVersionForInterfaces : minVersionForContracts;
const parentsPragmas = graph
.predecessors(file)
.map(file => pragmas[file])
.filter(Boolean);
const candidates = allSolcVersions.filter(
v => semver.gte(v, minVersion) && parentsPragmas.every(p => semver.satisfies(v, p)),
);
const pragmaPrefix = metadata[file].interface ? '>=' : '^';
process.stdout.write(
`[${Object.keys(pragmas).length + 1}/${Object.keys(metadata).length}] Searching minimal version for ${file} ... `,
);
const pragma = await setMinimalApplicablePragma(file, candidates, pragmaPrefix);
console.log(pragma);
pragmas[file] = pragma;
}
queue.push(...graph.successors(file));
}
}
})();