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

194 lines
6.8 KiB
JavaScript

import { network } from 'hardhat';
import { expect } from 'chai';
import { PANIC_CODES } from '@nomicfoundation/hardhat-ethers-chai-matchers/panic';
import { RevertType } from '../helpers/enums';
const {
ethers,
networkHelpers: { loadFixture },
} = await network.create();
async function fixture() {
const [deployer, other] = await ethers.getSigners();
const factory = await ethers.deployContract('$Create2');
// Bytecode for deploying a contract that includes a constructor.
// We use a vesting wallet, with 3 constructor arguments.
const constructorByteCode = await ethers
.getContractFactory('VestingWallet')
.then(factory => ethers.concat([factory.bytecode, factory.interface.encodeDeploy([other.address, 0n, 0n])]));
// Bytecode for deploying a contract that has no constructor log.
// Here we use the Create2 helper factory.
const constructorLessBytecode = await ethers
.getContractFactory('$Create2')
.then(factory => ethers.concat([factory.bytecode, factory.interface.encodeDeploy([])]));
const mockFactory = await ethers.getContractFactory('ConstructorMock');
return { deployer, other, factory, constructorByteCode, constructorLessBytecode, mockFactory };
}
describe('Create2', function () {
const salt = 'salt message';
const saltHex = ethers.id(salt);
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('computeAddress', function () {
it('computes the correct contract address', async function () {
const onChainComputed = await this.factory.$computeAddress(saltHex, ethers.keccak256(this.constructorByteCode));
const offChainComputed = ethers.getCreate2Address(
this.factory.target,
saltHex,
ethers.keccak256(this.constructorByteCode),
);
expect(onChainComputed).to.equal(offChainComputed);
});
it('computes the correct contract address with deployer', async function () {
const onChainComputed = await this.factory.$computeAddress(
saltHex,
ethers.keccak256(this.constructorByteCode),
ethers.Typed.address(this.deployer),
);
const offChainComputed = ethers.getCreate2Address(
this.deployer.address,
saltHex,
ethers.keccak256(this.constructorByteCode),
);
expect(onChainComputed).to.equal(offChainComputed);
});
});
describe('deploy', function () {
it('deploys a contract without constructor', async function () {
const offChainComputed = ethers.getCreate2Address(
this.factory.target,
saltHex,
ethers.keccak256(this.constructorLessBytecode),
);
await expect(this.factory.$deploy(0n, saltHex, this.constructorLessBytecode))
.to.emit(this.factory, 'return$deploy')
.withArgs(offChainComputed);
expect(this.constructorLessBytecode).to.include((await ethers.provider.getCode(offChainComputed)).slice(2));
});
it('deploys a contract with constructor arguments', async function () {
const offChainComputed = ethers.getCreate2Address(
this.factory.target,
saltHex,
ethers.keccak256(this.constructorByteCode),
);
await expect(this.factory.$deploy(0n, saltHex, this.constructorByteCode))
.to.emit(this.factory, 'return$deploy')
.withArgs(offChainComputed);
const instance = await ethers.getContractAt('VestingWallet', offChainComputed);
expect(await instance.owner()).to.equal(this.other);
});
it('deploys a contract with funds deposited in the factory', async function () {
const value = 10n;
await this.deployer.sendTransaction({ to: this.factory, value });
const offChainComputed = ethers.getCreate2Address(
this.factory.target,
saltHex,
ethers.keccak256(this.constructorByteCode),
);
expect(await ethers.provider.getBalance(this.factory)).to.equal(value);
expect(await ethers.provider.getBalance(offChainComputed)).to.equal(0n);
await expect(this.factory.$deploy(value, saltHex, this.constructorByteCode))
.to.emit(this.factory, 'return$deploy')
.withArgs(offChainComputed);
expect(await ethers.provider.getBalance(this.factory)).to.equal(0n);
expect(await ethers.provider.getBalance(offChainComputed)).to.equal(value);
});
it('fails deploying a contract in an existent address', async function () {
await expect(this.factory.$deploy(0n, saltHex, this.constructorByteCode)).to.emit(this.factory, 'return$deploy');
await expect(this.factory.$deploy(0n, saltHex, this.constructorByteCode)).to.be.revertedWithCustomError(
this.factory,
'FailedDeployment',
);
});
it('fails deploying a contract if the bytecode length is zero', async function () {
await expect(this.factory.$deploy(0n, saltHex, '0x')).to.be.revertedWithCustomError(
this.factory,
'Create2EmptyBytecode',
);
});
it('fails deploying a contract if factory contract does not have sufficient balance', async function () {
await expect(this.factory.$deploy(1n, saltHex, this.constructorByteCode))
.to.be.revertedWithCustomError(this.factory, 'InsufficientBalance')
.withArgs(0n, 1n);
});
describe('reverts error thrown during contract creation', function () {
it('bubbles up without message', async function () {
await expect(
this.factory.$deploy(
0n,
saltHex,
ethers.concat([
this.mockFactory.bytecode,
this.mockFactory.interface.encodeDeploy([RevertType.RevertWithoutMessage]),
]),
),
).to.be.revertedWithCustomError(this.factory, 'FailedDeployment');
});
it('bubbles up message', async function () {
await expect(
this.factory.$deploy(
0n,
saltHex,
ethers.concat([
this.mockFactory.bytecode,
this.mockFactory.interface.encodeDeploy([RevertType.RevertWithMessage]),
]),
),
).to.be.revertedWith('ConstructorMock: reverting');
});
it('bubbles up custom error', async function () {
await expect(
this.factory.$deploy(
0n,
saltHex,
ethers.concat([
this.mockFactory.bytecode,
this.mockFactory.interface.encodeDeploy([RevertType.RevertWithCustomError]),
]),
),
).to.be.revertedWithCustomError({ interface: this.mockFactory.interface }, 'CustomError');
});
it('bubbles up panic', async function () {
await expect(
this.factory.$deploy(
0n,
saltHex,
ethers.concat([this.mockFactory.bytecode, this.mockFactory.interface.encodeDeploy([RevertType.Panic])]),
),
).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
});
});
});
});