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

185 lines
7.9 KiB
JavaScript

import { network } from 'hardhat';
import { expect } from 'chai';
import { PANIC_CODES } from '@nomicfoundation/hardhat-ethers-chai-matchers/panic';
const {
ethers,
networkHelpers: { loadFixture },
} = await network.create();
async function fixture() {
const mock = await ethers.deployContract('$DoubleEndedQueue');
/** Rebuild the content of the deque as a JS array. */
const getContent = () =>
mock.$length(0).then(length => Promise.all(Array.from({ length: Number(length) }, (_, i) => mock.$at(0, i))));
return { mock, getContent };
}
describe('DoubleEndedQueue', function () {
const coder = ethers.AbiCoder.defaultAbiCoder();
const bytesA = coder.encode(['uint256'], [0xdeadbeef]);
const bytesB = coder.encode(['uint256'], [0x0123456789]);
const bytesC = coder.encode(['uint256'], [0x42424242]);
const bytesD = coder.encode(['uint256'], [0x171717]);
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('when empty', function () {
it('getters', async function () {
await expect(this.mock.$empty(0)).to.eventually.be.true;
await expect(this.getContent()).to.eventually.have.ordered.members([]);
});
it('reverts on accesses', async function () {
await expect(this.mock.$popBack(0)).to.be.revertedWithPanic(PANIC_CODES.POP_ON_EMPTY_ARRAY);
await expect(this.mock.$popFront(0)).to.be.revertedWithPanic(PANIC_CODES.POP_ON_EMPTY_ARRAY);
await expect(this.mock.$back(0)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
await expect(this.mock.$front(0)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
});
it('try getters return false/zero on empty', async function () {
await expect(this.mock.$tryFront(0)).to.eventually.deep.equal([false, ethers.ZeroHash]);
await expect(this.mock.$tryBack(0)).to.eventually.deep.equal([false, ethers.ZeroHash]);
await expect(this.mock.$tryAt(0, 0)).to.eventually.deep.equal([false, ethers.ZeroHash]);
});
it('try pops return false/zero on empty', async function () {
await expect(this.mock.$tryPopFront(0)).to.emit(this.mock, 'return$tryPopFront').withArgs(false, ethers.ZeroHash);
await expect(this.mock.$tryPopBack(0)).to.emit(this.mock, 'return$tryPopBack').withArgs(false, ethers.ZeroHash);
});
it('try pushes succeed on empty', async function () {
await expect(this.mock.$tryPushFront(0, bytesA)).to.emit(this.mock, 'return$tryPushFront').withArgs(true);
await expect(this.mock.$tryPushBack(0, bytesB)).to.emit(this.mock, 'return$tryPushBack').withArgs(true);
await expect(this.getContent()).to.eventually.have.ordered.members([bytesA, bytesB]);
});
it('values returns empty array', async function () {
await expect(this.mock.$values(0, 0, 0)).to.eventually.deep.equal([]);
await expect(this.mock.$values(0, 0, 10)).to.eventually.deep.equal([]);
await expect(this.mock.$values(0, 5, 10)).to.eventually.deep.equal([]);
});
});
describe('when not empty', function () {
beforeEach(async function () {
await this.mock.$pushBack(0, bytesB);
await this.mock.$pushFront(0, bytesA);
await this.mock.$pushBack(0, bytesC);
this.content = [bytesA, bytesB, bytesC];
});
it('getters', async function () {
await expect(this.mock.$empty(0)).to.eventually.be.false;
await expect(this.mock.$length(0)).to.eventually.equal(this.content.length);
await expect(this.mock.$front(0)).to.eventually.equal(this.content[0]);
await expect(this.mock.$back(0)).to.eventually.equal(this.content[this.content.length - 1]);
await expect(this.getContent()).to.eventually.have.ordered.members(this.content);
});
it('out of bounds access', async function () {
await expect(this.mock.$at(0, this.content.length)).to.be.revertedWithPanic(
PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS,
);
});
it('try getters return true/value when not empty', async function () {
await expect(this.mock.$tryFront(0)).to.eventually.deep.equal([true, this.content[0]]);
await expect(this.mock.$tryBack(0)).to.eventually.deep.equal([true, this.content[this.content.length - 1]]);
await expect(this.mock.$tryAt(0, 1)).to.eventually.deep.equal([true, this.content[1]]);
});
it('tryAt returns false/zero on out of bounds', async function () {
await expect(this.mock.$tryAt(0, this.content.length)).to.eventually.deep.equal([false, ethers.ZeroHash]);
});
describe('push', function () {
it('front', async function () {
await this.mock.$pushFront(0, bytesD);
this.content.unshift(bytesD); // add element at the beginning
await expect(this.getContent()).to.eventually.have.ordered.members(this.content);
});
it('back', async function () {
await this.mock.$pushBack(0, bytesD);
this.content.push(bytesD); // add element at the end
await expect(this.getContent()).to.eventually.have.ordered.members(this.content);
});
});
describe('pop', function () {
it('front', async function () {
const value = this.content.shift(); // remove first element
await expect(this.mock.$popFront(0)).to.emit(this.mock, 'return$popFront').withArgs(value);
await expect(this.getContent()).to.eventually.have.ordered.members(this.content);
});
it('back', async function () {
const value = this.content.pop(); // remove last element
await expect(this.mock.$popBack(0)).to.emit(this.mock, 'return$popBack').withArgs(value);
await expect(this.getContent()).to.eventually.have.ordered.members(this.content);
});
it('try front', async function () {
const value = this.content.shift();
await expect(this.mock.$tryPopFront(0)).to.emit(this.mock, 'return$tryPopFront').withArgs(true, value);
await expect(this.getContent()).to.eventually.have.ordered.members(this.content);
});
it('try back', async function () {
const value = this.content.pop();
await expect(this.mock.$tryPopBack(0)).to.emit(this.mock, 'return$tryPopBack').withArgs(true, value);
await expect(this.getContent()).to.eventually.have.ordered.members(this.content);
});
});
it('clear', async function () {
await this.mock.$clear(0);
await expect(this.mock.$empty(0)).to.eventually.be.true;
await expect(this.getContent()).to.eventually.have.ordered.members([]);
});
describe('values', function () {
it('returns the full content for [0, length)', async function () {
await expect(this.mock.$values(0, 0, this.content.length)).to.eventually.deep.equal(this.content);
});
it('paginates across all begin/end combinations', async function () {
for (const begin of [0, 1, 2, 3, 4])
for (const end of [0, 1, 2, 3, 4]) {
await expect(this.mock.$values(0, begin, end)).to.eventually.deep.equal(this.content.slice(begin, end));
}
});
it('clamps end to length', async function () {
await expect(this.mock.$values(0, 0, ethers.MaxUint256)).to.eventually.deep.equal(this.content);
await expect(this.mock.$values(0, 1, ethers.MaxUint256)).to.eventually.deep.equal(this.content.slice(1));
});
it('clamps start to end', async function () {
await expect(this.mock.$values(0, ethers.MaxUint256, ethers.MaxUint256)).to.eventually.deep.equal([]);
await expect(this.mock.$values(0, 2, 1)).to.eventually.deep.equal([]);
});
it('reflects pushFront/pushBack ordering (wraparound indices)', async function () {
await this.mock.$pushFront(0, bytesD);
const expected = [bytesD, ...this.content];
await expect(this.mock.$values(0, 0, expected.length)).to.eventually.deep.equal(expected);
await expect(this.mock.$values(0, 1, 3)).to.eventually.deep.equal(expected.slice(1, 3));
});
});
});
});