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

108 lines
3.8 KiB
JavaScript

import { capitalize, range, product } from '../helpers.js';
// ─── Shared helpers ───
export * as sanitize from './helpers/sanitize.js';
export * from './helpers/conversion.js';
export * from '../helpers.js';
export const args = (...parts) => parts.filter(Boolean).join(', ');
const zeroBytes = size => '0x' + '00'.repeat(size);
// ─── All solidity types ───
export const TYPES = Object.fromEntries(
[
{ type: 'address', size: 160, zero: zeroBytes(20) },
{ type: 'bool', name: 'Boolean', size: 1, zero: false },
...range(1, 33).map(size => ({
type: `bytes${size}`,
size: 8 * size,
zero: zeroBytes(size),
upcastTo: size < 32 ? 'bytes32' : undefined,
})),
...range(8, 257, 8).map(size => ({
type: `uint${size}`,
size,
zero: 0n,
upcastTo: size < 256 ? 'uint256' : undefined,
})),
...range(8, 257, 8).map(size => ({
type: `int${size}`,
size,
zero: 0n,
upcastTo: size < 256 ? 'int256' : undefined,
signed: true,
})),
{ type: 'string', zero: '' },
{ type: 'bytes', zero: '0x' },
]
.map(entry => ({
...entry,
name: entry.name ?? capitalize(entry.type),
location: entry.size ? '' : 'memory',
}))
.map((entry, _, all) => ({
...entry,
upcastTo: all.find(type => type.type === entry.upcastTo),
upcastOf: all.filter(type => type.upcastTo === entry.type),
}))
.map(entry => [entry.type, entry]),
);
// ─── Arrays ───
export const ARRAYS_TYPES = [TYPES.address, TYPES.bytes32, TYPES.uint256, TYPES.bytes, TYPES.string];
export const ARRAYS_VALUE_TYPES = ARRAYS_TYPES.filter(type => type.size);
export const ARRAYS_CAST_TYPES = ARRAYS_VALUE_TYPES.filter(type => type.type !== 'uint256');
export const ARRAYS_SORT_TYPES = ARRAYS_VALUE_TYPES.toSorted((a, b) => (b.type == 'uint256') - (a.type == 'uint256'));
export const ARRAYS_COMPARATOR_TYPES = ARRAYS_VALUE_TYPES.filter(type => type.size < 256);
// ─── Checkpoints (Checkpoints + Checkpoints.t) ───
export const CHECKPOINTS_LENGTH = [256, 224, 208, 160].map(size => ({ size, keySize: size < 256 ? 256 - size : 256 }));
// ─── Enumerable (EnumerableSet, EnumerableMap) ───
const enumerableName = ({ type, name }) => (type === 'uint256' ? 'Uint' : name);
export const SET_TYPES = [TYPES.bytes32, TYPES.bytes4, TYPES.address, TYPES.uint256, TYPES.string, TYPES.bytes].map(
value => ({
name: `${enumerableName(value)}Set`,
value,
}),
);
export const MAP_TYPES = []
.concat(
// value type maps
[TYPES.uint256, TYPES.address, TYPES.bytes32]
.flatMap((key, _, array) => array.map(value => ({ key, value })))
.slice(0, -1), // remove bytes32 → bytes32 (last one) that is already defined
// other value type maps
{ key: TYPES.bytes4, value: TYPES.address },
// non-value type maps
{ key: TYPES.bytes, value: TYPES.bytes },
)
.map(({ key, value }) => ({
name: `${enumerableName(key)}To${enumerableName(value)}Map`,
key,
value,
}));
// ─── MerkleProof ───
export const MERKLEPROOF_DEFAULT_HASH = 'Hashes.commutativeKeccak256';
export const MERKLEPROOF_OPTS = product(
[
{ suffix: '', location: 'memory' },
{ suffix: 'Calldata', location: 'calldata' },
],
[{ visibility: 'pure' }, { visibility: 'view', hash: 'hasher' }],
).map(objs => Object.assign({}, ...objs));
// ─── Packing (Packing + Packing.t) ───
export const PACKING_SIZES = [1, 2, 4, 6, 8, 10, 12, 16, 20, 22, 24, 28, 32];
// ─── SafeCast ───
// downcast target bit-lengths: 248, 240, ..., 8
export const SAFECAST_LENGTHS = Array.from({ length: 31 }, (_, i) => 248 - i * 8);
// ─── Slot family (StorageSlot, TransientSlot, SlotDerivation, and their mocks/tests) ───
export const SLOT_TYPES = Object.values(TYPES).filter(type => !type.upcastTo);