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>
113 lines
3.7 KiB
TypeScript
113 lines
3.7 KiB
TypeScript
import { createRequire } from 'node:module';
|
|
import path from 'node:path';
|
|
import fs, { promises as fsPromise } from 'node:fs';
|
|
import { mapKeys } from './utils/map-keys';
|
|
import { DocItemContext } from './site';
|
|
|
|
const require = createRequire(import.meta.url);
|
|
|
|
import * as defaultProperties from './common/properties';
|
|
|
|
export type PropertyGetter = (ctx: DocItemContext, original?: unknown) => unknown;
|
|
export type Properties = Record<string, PropertyGetter>;
|
|
|
|
export interface Templates {
|
|
partials?: Record<string, () => string>;
|
|
helpers?: Record<string, (...args: unknown[]) => string>;
|
|
properties?: Record<string, PropertyGetter>;
|
|
}
|
|
|
|
/**
|
|
* Loads the templates that will be used for rendering a site based on a
|
|
* default theme and user templates.
|
|
*
|
|
* The result contains all partials, helpers, and property getters defined in
|
|
* the user templates and the default theme, where the user's take precedence
|
|
* if there is a clash. Additionally, all theme partials and helpers are
|
|
* included with the theme prefix, e.g. `markdown/contract` will be a partial.
|
|
*/
|
|
export async function loadTemplates(
|
|
defaultTheme: string,
|
|
root: string,
|
|
userTemplatesPath?: string,
|
|
): Promise<Templates> {
|
|
const themes = await readThemes();
|
|
|
|
// Initialize templates with the default theme.
|
|
const templates: Required<Templates> = {
|
|
partials: { ...themes[defaultTheme]?.partials },
|
|
helpers: { ...themes[defaultTheme]?.helpers },
|
|
properties: { ...defaultProperties },
|
|
};
|
|
|
|
// Overwrite default theme with user templates.
|
|
if (userTemplatesPath) {
|
|
const userTemplates = await readTemplates(path.resolve(root, userTemplatesPath));
|
|
Object.assign(templates.partials, userTemplates.partials);
|
|
Object.assign(templates.helpers, userTemplates.helpers);
|
|
Object.assign(templates.properties, userTemplates.properties);
|
|
}
|
|
|
|
// Add partials and helpers from all themes, prefixed with the theme name.
|
|
for (const [themeName, theme] of Object.entries(themes)) {
|
|
const addPrefix = (k: string) => `${themeName}/${k}`;
|
|
Object.assign(templates.partials, mapKeys(theme.partials, addPrefix));
|
|
Object.assign(templates.helpers, mapKeys(theme.helpers, addPrefix));
|
|
}
|
|
|
|
return templates;
|
|
}
|
|
|
|
/**
|
|
* Read templates and helpers from a directory.
|
|
*/
|
|
export async function readTemplates(partialsDir: string, helpersDir = partialsDir): Promise<Required<Templates>> {
|
|
return {
|
|
partials: await readPartials(partialsDir),
|
|
helpers: await readHelpers(helpersDir, 'helpers'),
|
|
properties: await readHelpers(helpersDir, 'properties'),
|
|
};
|
|
}
|
|
|
|
async function readPartials(dir: string) {
|
|
const partials: NonNullable<Templates['partials']> = {};
|
|
for (const p of await fsPromise.readdir(dir)) {
|
|
const { name, ext } = path.parse(p);
|
|
if (ext === '.hbs') {
|
|
partials[name] = () => fs.readFileSync(path.join(dir, p), 'utf8');
|
|
}
|
|
}
|
|
return partials;
|
|
}
|
|
|
|
async function readHelpers(dir: string, name: string) {
|
|
let helpersPath;
|
|
try {
|
|
helpersPath = require.resolve(path.join(dir, name + '.ts'));
|
|
} catch {
|
|
return {};
|
|
}
|
|
const h = await import(helpersPath);
|
|
const helpers: Record<string, (...args: any[]) => any> = {};
|
|
for (const name in h) {
|
|
if (typeof h[name] === 'function') {
|
|
helpers[name] = h[name];
|
|
}
|
|
}
|
|
return helpers;
|
|
}
|
|
|
|
// Reads all built-in themes (partials + helpers) into an object.
|
|
async function readThemes(): Promise<Record<string, Required<Templates>>> {
|
|
const themes: Record<string, Required<Templates>> = {};
|
|
const themesDir = path.resolve(import.meta.dirname, 'themes');
|
|
|
|
for (const theme of await fsPromise.readdir(themesDir, { withFileTypes: true })) {
|
|
if (theme.isDirectory()) {
|
|
themes[theme.name] = await readTemplates(path.join(themesDir, theme.name));
|
|
}
|
|
}
|
|
|
|
return themes;
|
|
}
|