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

161 lines
6.0 KiB
JavaScript

import { network } from 'hardhat';
import { expect } from 'chai';
import { BeaconLabel } from '../../helpers/storage';
const {
ethers,
helpers: { storage },
networkHelpers: { loadFixture },
} = await network.create();
async function fixture() {
const [admin, other] = await ethers.getSigners();
const v1 = await ethers.deployContract('DummyImplementation');
const v2 = await ethers.deployContract('DummyImplementationV2');
const factory = await ethers.getContractFactory('BeaconProxy');
const beacon = await ethers.deployContract('UpgradeableBeacon', [v1, admin]);
const initData = v1.interface.encodeFunctionData('initializeNonPayableWithValue', [55n]);
const newBeaconProxy = (beacon, data, opts = {}) => factory.deploy(beacon, data, opts);
return { admin, other, factory, beacon, v1, v2, initData, newBeaconProxy };
}
describe('BeaconProxy', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('bad beacon is not accepted', function () {
it('non-contract beacon', async function () {
const notBeacon = this.other;
await expect(this.newBeaconProxy(notBeacon, this.initData))
.to.be.revertedWithCustomError(this.factory, 'ERC1967InvalidBeacon')
.withArgs(notBeacon);
});
it('non-compliant beacon', async function () {
const badBeacon = await ethers.deployContract('BadBeaconNoImpl');
// BadBeaconNoImpl does not provide `implementation()` and has no fallback.
// This causes ERC1967Utils._setBeacon to revert.
await expect(this.newBeaconProxy(badBeacon, this.initData)).to.be.revertedWithoutReason(ethers);
});
it('non-contract implementation', async function () {
const badBeacon = await ethers.deployContract('BadBeaconNotContract');
await expect(this.newBeaconProxy(badBeacon, this.initData))
.to.be.revertedWithCustomError(this.factory, 'ERC1967InvalidImplementation')
.withArgs(await badBeacon.implementation());
});
});
describe('initialization', function () {
async function assertInitialized({ value, balance }) {
const beaconAddress = await storage.getAddressInSlot(this.proxy, BeaconLabel);
expect(beaconAddress).to.equal(this.beacon);
const dummy = this.v1.attach(this.proxy);
expect(await dummy.value()).to.equal(value);
expect(await ethers.provider.getBalance(this.proxy)).to.equal(balance);
}
it('reverts without initialization', async function () {
await expect(this.newBeaconProxy(this.beacon, '0x')).to.be.revertedWithCustomError(
this.factory,
'BeaconProxyUninitialized',
);
});
it('non-payable initialization', async function () {
const value = 55n;
const data = this.v1.interface.encodeFunctionData('initializeNonPayableWithValue', [value]);
this.proxy = await this.newBeaconProxy(this.beacon, data);
await assertInitialized.bind(this)({ value, balance: 0n });
});
it('payable initialization', async function () {
const value = 55n;
const data = this.v1.interface.encodeFunctionData('initializePayableWithValue', [value]);
const balance = 100n;
this.proxy = await this.newBeaconProxy(this.beacon, data, { value: balance });
await assertInitialized.bind(this)({ value, balance });
});
it('reverting initialization function', async function () {
const data = this.v1.interface.encodeFunctionData('reverts');
await expect(this.newBeaconProxy(this.beacon, data)).to.be.revertedWith('DummyImplementation reverted');
});
describe('(unsafe) allowUninitialized is true', function () {
beforeEach(async function () {
this.factory = await ethers.getContractFactory('BeaconProxyUnsafe');
this.newBeaconProxy = (beacon, data, opts = {}) => this.factory.deploy(beacon, data, opts);
});
it('no initialization', async function () {
this.proxy = await this.newBeaconProxy(this.beacon, '0x');
await assertInitialized.bind(this)({ value: 0n, balance: 0n });
});
it('reverting initialization due to value', async function () {
await expect(this.newBeaconProxy(this.beacon, '0x', { value: 1n })).to.be.revertedWithCustomError(
this.factory,
'ERC1967NonPayable',
);
});
});
});
describe('upgrade', function () {
it('upgrade a proxy by upgrading its beacon', async function () {
const value = 10n;
const data = this.v1.interface.encodeFunctionData('initializeNonPayableWithValue', [value]);
const proxy = await this.newBeaconProxy(this.beacon, data).then(instance => this.v1.attach(instance));
// test initial values
expect(await proxy.value()).to.equal(value);
// test initial version
expect(await proxy.version()).to.equal('V1');
// upgrade beacon
await this.beacon.connect(this.admin).upgradeTo(this.v2);
// test upgraded version
expect(await proxy.version()).to.equal('V2');
});
it('upgrade 2 proxies by upgrading shared beacon', async function () {
const value1 = 10n;
const data1 = this.v1.interface.encodeFunctionData('initializeNonPayableWithValue', [value1]);
const proxy1 = await this.newBeaconProxy(this.beacon, data1).then(instance => this.v1.attach(instance));
const value2 = 42n;
const data2 = this.v1.interface.encodeFunctionData('initializeNonPayableWithValue', [value2]);
const proxy2 = await this.newBeaconProxy(this.beacon, data2).then(instance => this.v1.attach(instance));
// test initial values
expect(await proxy1.value()).to.equal(value1);
expect(await proxy2.value()).to.equal(value2);
// test initial version
expect(await proxy1.version()).to.equal('V1');
expect(await proxy2.version()).to.equal('V1');
// upgrade beacon
await this.beacon.connect(this.admin).upgradeTo(this.v2);
// test upgraded version
expect(await proxy1.version()).to.equal('V2');
expect(await proxy2.version()).to.equal('V2');
});
});
});