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

200 lines
7.8 KiB
JavaScript

import { network } from 'hardhat';
import { expect } from 'chai';
import { PANIC_CODES } from '@nomicfoundation/hardhat-ethers-chai-matchers/panic';
import { shouldBehaveLikeERC20, shouldBehaveLikeERC20Transfer, shouldBehaveLikeERC20Approve } from './ERC20.behavior';
const connection = await network.create();
const {
ethers,
networkHelpers: { loadFixture },
} = connection;
const TOKENS = [{ Token: '$ERC20' }, { Token: '$ERC20ApprovalMock', forcedApproval: true }];
const name = 'My Token';
const symbol = 'MTKN';
const initialSupply = 100n;
describe('ERC20', function () {
for (const { Token, forcedApproval } of TOKENS) {
describe(Token, function () {
const fixture = async () => {
// this.accounts is used by shouldBehaveLikeERC20
const accounts = await ethers.getSigners();
const [holder, recipient] = accounts;
const token = await ethers.deployContract(Token, [name, symbol]);
await token.$_mint(holder, initialSupply);
return { accounts, holder, recipient, token };
};
beforeEach(async function () {
Object.assign(this, connection, await loadFixture(fixture));
});
shouldBehaveLikeERC20(initialSupply, { forcedApproval });
it('has a name', async function () {
expect(await this.token.name()).to.equal(name);
});
it('has a symbol', async function () {
expect(await this.token.symbol()).to.equal(symbol);
});
it('has 18 decimals', async function () {
expect(await this.token.decimals()).to.equal(18n);
});
describe('_mint', function () {
const value = 50n;
it('rejects a null account', async function () {
await expect(this.token.$_mint(ethers.ZeroAddress, value))
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
.withArgs(ethers.ZeroAddress);
});
it('rejects overflow', async function () {
await expect(this.token.$_mint(this.recipient, ethers.MaxUint256)).to.be.revertedWithPanic(
PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
);
});
describe('for a non zero account', function () {
beforeEach('minting', async function () {
this.tx = await this.token.$_mint(this.recipient, value);
});
it('increments totalSupply', async function () {
await expect(await this.token.totalSupply()).to.equal(initialSupply + value);
});
it('increments recipient balance', async function () {
await expect(this.tx).to.changeTokenBalance(ethers, this.token, this.recipient, value);
});
it('emits Transfer event', async function () {
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.recipient, value);
});
});
});
describe('_burn', function () {
it('rejects a null account', async function () {
await expect(this.token.$_burn(ethers.ZeroAddress, 1n))
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidSender')
.withArgs(ethers.ZeroAddress);
});
describe('for a non zero account', function () {
it('rejects burning more than balance', async function () {
await expect(this.token.$_burn(this.holder, initialSupply + 1n))
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
.withArgs(this.holder, initialSupply, initialSupply + 1n);
});
const describeBurn = function (description, value) {
describe(description, function () {
beforeEach('burning', async function () {
this.tx = await this.token.$_burn(this.holder, value);
});
it('decrements totalSupply', async function () {
expect(await this.token.totalSupply()).to.equal(initialSupply - value);
});
it('decrements holder balance', async function () {
await expect(this.tx).to.changeTokenBalance(ethers, this.token, this.holder, -value);
});
it('emits Transfer event', async function () {
await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.holder, ethers.ZeroAddress, value);
});
});
};
describeBurn('for entire balance', initialSupply);
describeBurn('for less value than balance', initialSupply - 1n);
});
});
describe('_update', function () {
const value = 1n;
beforeEach(async function () {
this.totalSupply = await this.token.totalSupply();
});
it('from is the zero address', async function () {
const tx = await this.token.$_update(ethers.ZeroAddress, this.holder, value);
await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.holder, value);
expect(await this.token.totalSupply()).to.equal(this.totalSupply + value);
await expect(tx).to.changeTokenBalance(ethers, this.token, this.holder, value);
});
it('to is the zero address', async function () {
const tx = await this.token.$_update(this.holder, ethers.ZeroAddress, value);
await expect(tx).to.emit(this.token, 'Transfer').withArgs(this.holder, ethers.ZeroAddress, value);
expect(await this.token.totalSupply()).to.equal(this.totalSupply - value);
await expect(tx).to.changeTokenBalance(ethers, this.token, this.holder, -value);
});
describe('from and to are the same address', function () {
it('zero address', async function () {
const tx = await this.token.$_update(ethers.ZeroAddress, ethers.ZeroAddress, value);
await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, ethers.ZeroAddress, value);
expect(await this.token.totalSupply()).to.equal(this.totalSupply);
await expect(tx).to.changeTokenBalance(ethers, this.token, ethers.ZeroAddress, 0n);
});
describe('non zero address', function () {
it('reverts without balance', async function () {
await expect(this.token.$_update(this.recipient, this.recipient, value))
.to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
.withArgs(this.recipient, 0n, value);
});
it('executes with balance', async function () {
const tx = await this.token.$_update(this.holder, this.holder, value);
await expect(tx).to.changeTokenBalance(ethers, this.token, this.holder, 0n);
await expect(tx).to.emit(this.token, 'Transfer').withArgs(this.holder, this.holder, value);
});
});
});
});
describe('_transfer', function () {
beforeEach(function () {
this.transfer = this.token.$_transfer;
});
shouldBehaveLikeERC20Transfer(initialSupply);
it('reverts when the sender is the zero address', async function () {
await expect(this.token.$_transfer(ethers.ZeroAddress, this.recipient, initialSupply))
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidSender')
.withArgs(ethers.ZeroAddress);
});
});
describe('_approve', function () {
beforeEach(function () {
this.approve = this.token.$_approve;
});
shouldBehaveLikeERC20Approve(initialSupply);
it('reverts when the owner is the zero address', async function () {
await expect(this.token.$_approve(ethers.ZeroAddress, this.recipient, initialSupply))
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidApprover')
.withArgs(ethers.ZeroAddress);
});
});
});
}
});