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

198 lines
7.8 KiB
JavaScript

import { network } from 'hardhat';
import { expect } from 'chai';
import {
CALL_TYPE_CALL,
CALL_TYPE_BATCH,
CALL_TYPE_DELEGATE,
encodeMode,
encodeSingle,
encodeBatch,
encodeDelegate,
} from '../helpers/erc7579';
const connection = await network.create();
const {
ethers,
helpers: { chain },
networkHelpers: { loadFixture },
} = connection;
async function fixture() {
const [admin, other] = await ethers.getSigners();
const gateway = await ethers.deployContract('$ERC7786GatewayMock');
const target = await ethers.deployContract('CallReceiverMock');
// Deploy executor
const executor = await ethers.deployContract('$CrosschainRemoteExecutor', [gateway, chain.toErc7930(admin)]);
const remoteExecute = (from, target, mode, data) =>
gateway.connect(from).sendMessage(target, ethers.concat([mode, data]), []);
return { gateway, target, executor, admin, other, remoteExecute };
}
describe('CrosschainRemoteController & CrosschainRemoteExecutor', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
it('setup', async function () {
await expect(this.executor.gateway()).to.eventually.equal(this.gateway);
await expect(this.executor.controller()).to.eventually.equal(chain.toErc7930(this.admin));
});
for (const [name, controller] of Object.entries({
empty: () => '0x',
unparsable: () => '0xdeadbeef',
padded: admin => ethers.concat([chain.toErc7930(admin), '0x00']),
'chain-only': () => chain.erc7930,
'evm with 32-byte address': admin => chain.toErc7930(ethers.zeroPadValue(admin.address, 32)),
})) {
it(`rejects ${name} controller at construction`, async function () {
const invalid = controller(this.admin);
await expect(ethers.deployContract('$CrosschainRemoteExecutor', [this.gateway, invalid]))
.to.be.revertedWithCustomError(this.executor, 'InteroperableAddressParsingError')
.withArgs(invalid);
});
}
describe('crosschain operation', function () {
it('support single mode', async function () {
const mode = encodeMode({ callType: CALL_TYPE_CALL });
const data = encodeSingle(this.target, 0n, this.target.interface.encodeFunctionData('mockFunctionExtra'));
await expect(this.remoteExecute(this.admin, chain.toErc7930(this.executor), mode, data))
.to.emit(this.target, 'MockFunctionCalledExtra')
.withArgs(this.executor, 0n);
});
it('support batch mode', async function () {
const mode = encodeMode({ callType: CALL_TYPE_BATCH });
const data = encodeBatch(
[this.target, 0n, this.target.interface.encodeFunctionData('mockFunctionWithArgs', [42, '0x1234'])],
[this.target, 0n, this.target.interface.encodeFunctionData('mockFunctionExtra')],
);
await expect(this.remoteExecute(this.admin, chain.toErc7930(this.executor), mode, data))
.to.emit(this.target, 'MockFunctionCalledWithArgs')
.withArgs(42, '0x1234')
.to.emit(this.target, 'MockFunctionCalledExtra')
.withArgs(this.executor, 0n);
});
it('support delegate mode', async function () {
const mode = encodeMode({ callType: CALL_TYPE_DELEGATE });
const data = encodeDelegate(this.target, this.target.interface.encodeFunctionData('mockFunctionExtra'));
await expect(this.remoteExecute(this.admin, chain.toErc7930(this.executor), mode, data))
.to.emit(this.target.attach(this.executor.target), 'MockFunctionCalledExtra')
.withArgs(this.gateway, 0n);
});
it('revert when mode is invalid', async function () {
const mode = encodeMode({ callType: '0x42' });
const data = '0x';
await expect(this.remoteExecute(this.admin, chain.toErc7930(this.executor), mode, data))
.to.be.revertedWithCustomError(this.executor, 'ERC7579UnsupportedCallType')
.withArgs('0x42');
});
});
describe('reconfigure', function () {
beforeEach(async function () {
this.newGateway = await ethers.deployContract('$ERC7786GatewayMock');
});
it('through a crosschain call: success', async function () {
const mode = encodeMode({ callType: CALL_TYPE_CALL });
const data = encodeSingle(
this.executor,
0n,
this.executor.interface.encodeFunctionData('reconfigure', [
this.newGateway.target,
chain.toErc7930(this.other),
]),
);
await expect(this.remoteExecute(this.admin, chain.toErc7930(this.executor), mode, data))
.to.emit(this.executor, 'CrosschainControllerSet')
.withArgs(this.newGateway, chain.toErc7930(this.other));
await expect(this.executor.gateway()).to.eventually.equal(this.newGateway);
await expect(this.executor.controller()).to.eventually.equal(chain.toErc7930(this.other));
// the previous controller is no longer authorized
await expect(this.remoteExecute(this.admin, chain.toErc7930(this.executor), mode, data))
.to.be.revertedWithCustomError(this.executor, 'ERC7786RecipientUnauthorizedGateway')
.withArgs(this.gateway, chain.toErc7930(this.admin));
});
for (const [name, controller] of Object.entries({
unparsable: () => '0xdeadbeef',
padded: admin => ethers.concat([chain.toErc7930(admin), '0x00']),
'chain-only': () => chain.erc7930,
'evm with 32-byte address': admin => chain.toErc7930(ethers.zeroPadValue(admin.address, 32)),
})) {
it(`through a crosschain call with ${name} controller: revert`, async function () {
const invalid = controller(this.admin);
const mode = encodeMode({ callType: CALL_TYPE_CALL });
const data = encodeSingle(
this.executor,
0n,
this.executor.interface.encodeFunctionData('reconfigure', [this.gateway.target, invalid]),
);
await expect(this.remoteExecute(this.admin, chain.toErc7930(this.executor), mode, data))
.to.be.revertedWithCustomError(this.executor, 'InteroperableAddressParsingError')
.withArgs(invalid);
// the controller is unchanged and still able to drive the executor
await expect(this.executor.controller()).to.eventually.equal(chain.toErc7930(this.admin));
await expect(
this.remoteExecute(
this.admin,
chain.toErc7930(this.executor),
mode,
encodeSingle(this.target, 0n, this.target.interface.encodeFunctionData('mockFunctionExtra')),
),
).to.emit(this.target, 'MockFunctionCalledExtra');
});
}
it('through the internal setter: success', async function () {
await expect(this.executor.$_setup(this.newGateway, chain.toErc7930(this.other)))
.to.emit(this.executor, 'CrosschainControllerSet')
.withArgs(this.newGateway, chain.toErc7930(this.other));
await expect(this.executor.gateway()).to.eventually.equal(this.newGateway);
await expect(this.executor.controller()).to.eventually.equal(chain.toErc7930(this.other));
});
it('with an invalid new gateway: revert', async function () {
// directly using the internal setter
await expect(this.executor.$_setup(this.other, chain.toErc7930(this.other))).to.revert(ethers);
// through a crosschain call
const mode = encodeMode({ callType: CALL_TYPE_CALL });
const data = encodeSingle(
this.executor,
0n,
this.executor.interface.encodeFunctionData('reconfigure', [this.other.address, chain.toErc7930(this.other)]),
);
await expect(
this.remoteExecute(this.admin, chain.toErc7930(this.executor), mode, data),
).to.be.revertedWithCustomError(this.executor, 'FailedCall');
});
it('is access controlled', async function () {
await expect(
this.executor.reconfigure(this.newGateway, chain.toErc7930(this.other)),
).to.be.revertedWithCustomError(this.executor, 'AccessRestricted');
});
});
});