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

219 lines
8.0 KiB
JavaScript

import { network } from 'hardhat';
import { expect } from 'chai';
import { MAX_UINT64 } from '../../helpers/constants';
const { ethers } = await network.create();
describe('Initializable', function () {
describe('basic testing without inheritance', function () {
beforeEach('deploying', async function () {
this.mock = await ethers.deployContract('InitializableMock');
});
describe('before initialize', function () {
it('initializer has not run', async function () {
expect(await this.mock.initializerRan()).to.be.false;
});
it('_initializing returns false before initialization', async function () {
expect(await this.mock.isInitializing()).to.be.false;
});
});
describe('after initialize', function () {
beforeEach('initializing', async function () {
await this.mock.initialize();
});
it('initializer has run', async function () {
expect(await this.mock.initializerRan()).to.be.true;
});
it('_initializing returns false after initialization', async function () {
expect(await this.mock.isInitializing()).to.be.false;
});
it('initializer does not run again', async function () {
await expect(this.mock.initialize()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
});
});
describe('nested under an initializer', function () {
it('initializer modifier reverts', async function () {
await expect(this.mock.initializerNested()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
});
it('onlyInitializing modifier succeeds', async function () {
await this.mock.onlyInitializingNested();
expect(await this.mock.onlyInitializingRan()).to.be.true;
});
});
it('cannot call onlyInitializable function outside the scope of an initializable function', async function () {
await expect(this.mock.initializeOnlyInitializing()).to.be.revertedWithCustomError(this.mock, 'NotInitializing');
});
});
it('nested initializer can run during construction', async function () {
const mock = await ethers.deployContract('ConstructorInitializableMock');
expect(await mock.initializerRan()).to.be.true;
expect(await mock.onlyInitializingRan()).to.be.true;
});
it('multiple constructor levels can be initializers', async function () {
const mock = await ethers.deployContract('ChildConstructorInitializableMock');
expect(await mock.initializerRan()).to.be.true;
expect(await mock.childInitializerRan()).to.be.true;
expect(await mock.onlyInitializingRan()).to.be.true;
});
describe('reinitialization', function () {
beforeEach('deploying', async function () {
this.mock = await ethers.deployContract('ReinitializerMock');
});
it('can reinitialize', async function () {
expect(await this.mock.counter()).to.equal(0n);
await this.mock.initialize();
expect(await this.mock.counter()).to.equal(1n);
await this.mock.reinitialize(2);
expect(await this.mock.counter()).to.equal(2n);
await this.mock.reinitialize(3);
expect(await this.mock.counter()).to.equal(3n);
});
it('can jump multiple steps', async function () {
expect(await this.mock.counter()).to.equal(0n);
await this.mock.initialize();
expect(await this.mock.counter()).to.equal(1n);
await this.mock.reinitialize(128);
expect(await this.mock.counter()).to.equal(2n);
});
it('cannot nest reinitializers', async function () {
expect(await this.mock.counter()).to.equal(0n);
await expect(this.mock.nestedReinitialize(2, 2)).to.be.revertedWithCustomError(
this.mock,
'InvalidInitialization',
);
await expect(this.mock.nestedReinitialize(2, 3)).to.be.revertedWithCustomError(
this.mock,
'InvalidInitialization',
);
await expect(this.mock.nestedReinitialize(3, 2)).to.be.revertedWithCustomError(
this.mock,
'InvalidInitialization',
);
});
it('can chain reinitializers', async function () {
expect(await this.mock.counter()).to.equal(0n);
await this.mock.chainReinitialize(2, 3);
expect(await this.mock.counter()).to.equal(2n);
});
it('_getInitializedVersion returns right version', async function () {
await this.mock.initialize();
expect(await this.mock.getInitializedVersion()).to.equal(1n);
await this.mock.reinitialize(12);
expect(await this.mock.getInitializedVersion()).to.equal(12n);
});
describe('contract locking', function () {
it('prevents initialization', async function () {
await this.mock.disableInitializers();
await expect(this.mock.initialize()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
});
it('prevents re-initialization', async function () {
await this.mock.disableInitializers();
await expect(this.mock.reinitialize(255n)).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
});
it('can lock contract after initialization', async function () {
await this.mock.initialize();
await this.mock.disableInitializers();
await expect(this.mock.reinitialize(255n)).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
});
});
});
describe('events', function () {
it('constructor initialization emits event', async function () {
const mock = await ethers.deployContract('ConstructorInitializableMock');
await expect(mock.deploymentTransaction()).to.emit(mock, 'Initialized').withArgs(1n);
});
it('initialization emits event', async function () {
const mock = await ethers.deployContract('ReinitializerMock');
await expect(mock.initialize()).to.emit(mock, 'Initialized').withArgs(1n);
});
it('reinitialization emits event', async function () {
const mock = await ethers.deployContract('ReinitializerMock');
await expect(mock.reinitialize(128)).to.emit(mock, 'Initialized').withArgs(128n);
});
it('chained reinitialization emits multiple events', async function () {
const mock = await ethers.deployContract('ReinitializerMock');
await expect(mock.chainReinitialize(2, 3))
.to.emit(mock, 'Initialized')
.withArgs(2n)
.to.emit(mock, 'Initialized')
.withArgs(3n);
});
});
describe('complex testing with inheritance', function () {
const mother = 12n;
const gramps = '56';
const father = 34n;
const child = 78n;
beforeEach('deploying', async function () {
this.mock = await ethers.deployContract('SampleChild');
await this.mock.initialize(mother, gramps, father, child);
});
it('initializes human', async function () {
expect(await this.mock.isHuman()).to.be.true;
});
it('initializes mother', async function () {
expect(await this.mock.mother()).to.equal(mother);
});
it('initializes gramps', async function () {
expect(await this.mock.gramps()).to.equal(gramps);
});
it('initializes father', async function () {
expect(await this.mock.father()).to.equal(father);
});
it('initializes child', async function () {
expect(await this.mock.child()).to.equal(child);
});
});
describe('disabling initialization', function () {
it('old and new patterns in bad sequence', async function () {
const DisableBad1 = await ethers.getContractFactory('DisableBad1');
await expect(DisableBad1.deploy()).to.be.revertedWithCustomError(DisableBad1, 'InvalidInitialization');
const DisableBad2 = await ethers.getContractFactory('DisableBad2');
await expect(DisableBad2.deploy()).to.be.revertedWithCustomError(DisableBad2, 'InvalidInitialization');
});
it('old and new patterns in good sequence', async function () {
const ok = await ethers.deployContract('DisableOk');
await expect(ok.deploymentTransaction())
.to.emit(ok, 'Initialized')
.withArgs(1n)
.to.emit(ok, 'Initialized')
.withArgs(MAX_UINT64);
});
});
});