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

281 lines
12 KiB
JavaScript

import { network } from 'hardhat';
import { expect } from 'chai';
import { MAX_UINT64 } from '../helpers/constants';
import { product } from '../helpers/iterate';
import * as random from '../helpers/random';
const {
ethers,
networkHelpers: { loadFixture },
} = await network.create();
async function fixture() {
const mock = await ethers.deployContract('$RLP');
// Resolve function overload ambiguities like in Math.test.js
mock.$encode_bool = mock['$encode(bool)'];
mock.$encode_address = mock['$encode(address)'];
mock.$encode_uint256 = mock['$encode(uint256)'];
mock.$encode_bytes32 = mock['$encode(bytes32)'];
mock.$encode_bytes = mock['$encode(bytes)'];
mock.$encode_string = mock['$encode(string)'];
mock.$encode_list = mock['$encode(bytes[])'];
return { mock };
}
describe('RLP', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
it('encode/decode booleans', async function () {
await expect(this.mock.$encode_bool(false)).to.eventually.equal('0x80'); // 0
await expect(this.mock.$encode_bool(true)).to.eventually.equal('0x01'); // 1
await expect(this.mock.$decodeBool('0x80')).to.eventually.equal(false); // 0
await expect(this.mock.$decodeBool('0x01')).to.eventually.equal(true); // 1
// Non-canonical encodings treated as true
await expect(this.mock.$decodeBool('0x02')).to.eventually.equal(true);
await expect(this.mock.$decodeBool('0x03')).to.eventually.equal(true);
await expect(this.mock.$decodeBool('0x7f')).to.eventually.equal(true);
});
it('encode/decode addresses', async function () {
for (const addr of [
ethers.ZeroAddress, // zero address
'0x0000F90827F1C53a10cb7A02335B175320002935', // address with leading zeros
random.address(), // random address
]) {
const expected = ethers.encodeRlp(addr);
await expect(this.mock.$encode_address(addr)).to.eventually.equal(expected);
await expect(this.mock.$decodeAddress(expected)).to.eventually.equal(addr);
}
await expect(this.mock.$decodeAddress('0x940000000000000000000000000000000000001234')).to.eventually.equal(
'0x0000000000000000000000000000000000001234',
); // Canonical encoding (address as 20 bytes with prefix)
// Single-byte encoding for precompile addresses
await expect(this.mock.$decodeAddress('0x01')).to.eventually.equal('0x0000000000000000000000000000000000000001');
await expect(this.mock.$decodeAddress('0x940000000000000000000000000000000000000001')).to.eventually.equal(
'0x0000000000000000000000000000000000000001',
); // 21-byte encoding
await expect(this.mock.$decodeAddress('0x05')).to.eventually.equal('0x0000000000000000000000000000000000000005');
await expect(this.mock.$decodeAddress('0x940000000000000000000000000000000000000005')).to.eventually.equal(
'0x0000000000000000000000000000000000000005',
); // 21-byte encoding
await expect(this.mock.$decodeAddress('0x7f')).to.eventually.equal('0x000000000000000000000000000000000000007f');
await expect(this.mock.$decodeAddress('0x94000000000000000000000000000000000000007f')).to.eventually.equal(
'0x000000000000000000000000000000000000007f',
); // 21-byte encoding
// Invalid encodings
await expect(this.mock.$decodeAddress('0x')).to.be.revertedWithCustomError(this.mock, 'RLPInvalidEncoding');
await expect(this.mock.$decodeAddress('0x92f90827f1c53a10cb7a02335b175320002935')).to.be.revertedWithCustomError(
this.mock,
'RLPInvalidEncoding',
);
await expect(
this.mock.$decodeAddress('0x96f90827f1c53a10cb7a02335b175320002935786ea099'),
).to.be.revertedWithCustomError(this.mock, 'RLPInvalidEncoding');
});
it('encode/decode uint256', async function () {
for (const input of [0, 1, 127, 128, 256, 1024, 0xffffff, ethers.MaxUint256]) {
const expected = ethers.encodeRlp(ethers.toBeArray(input));
await expect(this.mock.$encode_uint256(input)).to.eventually.equal(expected);
await expect(this.mock.$decodeUint256(expected)).to.eventually.equal(input);
}
await expect(this.mock.$decodeUint256('0x88ab54a98ceb1f0ad2')).to.eventually.equal(12345678901234567890n); // Canonical encoding for 12345678901234567890
await expect(this.mock.$decodeUint256('0x8900ab54a98ceb1f0ad2')).to.eventually.equal(12345678901234567890n); // Non-canonical encoding with leading zero for the same value
await expect(this.mock.$decodeUint256('0x80')).to.eventually.equal(0n); // Canonical encoding for 0
await expect(this.mock.$decodeUint256('0x820000')).to.eventually.equal(0n); // Non-canonical encoding with leading zero
await expect(this.mock.$decodeUint256('0x83000000')).to.eventually.equal(0n); // Non-canonical encoding with two leading zeros
await expect(this.mock.$decodeUint256('0x8400000000')).to.eventually.equal(0n); // Non-canonical encoding with three leading zeros
await expect(this.mock.$decodeUint256('0x8204d2')).to.eventually.equal(1234n); // Canonical
await expect(this.mock.$decodeUint256('0x830004d2')).to.eventually.equal(1234n); // With leading zero
await expect(this.mock.$decodeUint256('0x84000004d2')).to.eventually.equal(1234n); // With two leading zeros
// Invalid encoding: encoded item length >33 bytes (>32 bytes of data)
await expect(
this.mock.$decodeUint256(ethers.encodeRlp(ethers.toBeHex(ethers.MaxUint256 + 1n))),
).to.be.revertedWithCustomError(this.mock, 'RLPInvalidEncoding');
// Invalid encoding: encoded item type is list when data is expected
await expect(this.mock.$decodeUint256(ethers.encodeRlp([]))).to.be.revertedWithCustomError(
this.mock,
'RLPInvalidEncoding',
);
});
it('encode/decode bytes32', async function () {
for (const input of [
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x1000000000000000000000000000000000000000000000000000000000000000',
random.bytes32(),
]) {
const encoded = ethers.encodeRlp(input);
await expect(this.mock.$encode_bytes32(input)).to.eventually.equal(encoded);
await expect(this.mock.$decodeBytes32(encoded)).to.eventually.equal(input);
}
// Compact encoding for 1234
await expect(this.mock.$decodeBytes32('0x8204d2')).to.eventually.equal(
'0x00000000000000000000000000000000000000000000000000000000000004d2',
);
// Encoding with one leading zero
await expect(this.mock.$decodeBytes32('0x830004d2')).to.eventually.equal(
'0x00000000000000000000000000000000000000000000000000000000000004d2',
);
// Encoding with two leading zeros
await expect(this.mock.$decodeBytes32('0x84000004d2')).to.eventually.equal(
'0x00000000000000000000000000000000000000000000000000000000000004d2',
);
// Encoding for the value
await expect(this.mock.$decodeBytes32('0x80')).to.eventually.equal(ethers.ZeroHash);
// Encoding for two zeros (and nothing else)
await expect(this.mock.$decodeBytes32('0x820000')).to.eventually.equal(ethers.ZeroHash);
// Invalid encoding: encoded item length >33 bytes (>32 bytes of data)
await expect(this.mock.$decodeBytes32(ethers.encodeRlp(random.bytes(33)))).to.be.revertedWithCustomError(
this.mock,
'RLPInvalidEncoding',
);
// Invalid encoding: encoded item type is list when data is expected
await expect(this.mock.$decodeBytes32(ethers.encodeRlp([]))).to.be.revertedWithCustomError(
this.mock,
'RLPInvalidEncoding',
);
});
it('encode/decode empty byte', async function () {
const input = '0x';
const expected = ethers.encodeRlp(input);
await expect(this.mock.$encode_bytes(input)).to.eventually.equal(expected);
await expect(this.mock.$decodeBytes(expected)).to.eventually.equal(input);
});
it('encode/decode single byte < 128', async function () {
for (const input of ['0x00', '0x01', '0x7f']) {
const expected = ethers.encodeRlp(input);
await expect(this.mock.$encode_bytes(input)).to.eventually.equal(expected);
await expect(this.mock.$decodeBytes(expected)).to.eventually.equal(input);
}
});
it('encode/decode single byte >= 128', async function () {
for (const input of ['0x80', '0xff']) {
const expected = ethers.encodeRlp(input);
await expect(this.mock.$encode_bytes(input)).to.eventually.equal(expected);
await expect(this.mock.$decodeBytes(expected)).to.eventually.equal(input);
}
});
it('encode/decode short buffers (1-55 bytes)', async function () {
for (const input of [
'0xab', // 1 byte
'0x1234', // 2 bytes
random.hexBytes(55), // 55 bytes (maximum for short encoding)
]) {
const expected = ethers.encodeRlp(input);
await expect(this.mock.$encode_bytes(input)).to.eventually.equal(expected);
await expect(this.mock.$decodeBytes(expected)).to.eventually.equal(input);
}
});
it('encode/decode long buffers (>55 bytes)', async function () {
for (const input of [
random.hexBytes(56), // 56 bytes (minimum for long encoding)
random.hexBytes(128), // 128 bytes
]) {
const expected = ethers.encodeRlp(input);
await expect(this.mock.$encode_bytes(input)).to.eventually.equal(expected);
await expect(this.mock.$decodeBytes(expected)).to.eventually.equal(input);
}
});
it('encode/decode strings', async function () {
for (const input of [
'', // empty string
'000000000cat', // string with leading zeros
'dog',
'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
]) {
const expected = ethers.encodeRlp(ethers.toUtf8Bytes(input));
await expect(this.mock.$encode_string(input)).to.eventually.equal(expected);
await expect(this.mock.$decodeString(expected)).to.eventually.equal(input);
}
});
it('encode/decode array (bytes[])', async function () {
for (const input of [[], ['0x'], ['0x00'], ['0x17', '0x42'], ['0x17', '0x', '0x42', '0x0123456789abcdef', '0x']]) {
await expect(this.mock.$encode_list(input.map(ethers.encodeRlp))).to.eventually.equal(ethers.encodeRlp(input));
}
});
// Failure cases for decodeBytes
for (const { name, input } of [
{ name: 'empty input as string', input: '0x' },
{ name: 'short string with invalid length', input: '0x8100' },
{ name: 'short string with length exceeding item length', input: '0x8200' },
{ name: 'long string without a length length', input: '0xb8' },
{ name: 'long string without a length', input: '0xb901' },
{ name: 'long string with invalid length prefix', input: '0xb800' },
{ name: 'long string with missing content', input: '0xb838' },
{ name: 'long string with short content', input: '0xb80101' },
{ name: 'invalid type (got list, expected item)', input: ethers.encodeRlp([]) },
]) {
it(`rejects ${name}`, async function () {
await expect(this.mock.$decodeBytes(input)).to.be.revertedWithCustomError(this.mock, 'RLPInvalidEncoding');
});
}
// Failure cases for decodeList
for (const { name, input } of [
{ name: 'empty input as list', input: '0x' },
{ name: 'leftover data', input: '0xc000' },
{ name: 'short list missing data', input: '0xc1' },
{ name: 'short list with length exceeding item length', input: '0xc200' },
{ name: 'long list without a length length', input: '0xf8' },
{ name: 'long list without a length', input: '0xf901' },
{ name: 'long list with invalid length prefix', input: '0xf800' },
{ name: 'long list with missing content', input: '0xf838' },
{ name: 'long list with short content', input: '0xf80101' },
{ name: 'invalid type (got item, expected list)', input: ethers.encodeRlp('0x') },
]) {
it(`rejects ${name}`, async function () {
await expect(this.mock.$decodeList(input)).to.be.revertedWithCustomError(this.mock, 'RLPInvalidEncoding');
});
}
it('RLP encoder predict create addresses', async function () {
for (const [from, nonce] of product(
[
ethers.ZeroAddress, // zero address
'0x0000F90827F1C53a10cb7A02335B175320002935', // address with heading zeros
random.address(), // random address
],
[0n, 1n, 42n, 65535n, MAX_UINT64],
)) {
await expect(
this.mock
.$encode_list([this.mock.$encode_address(from), this.mock.$encode_uint256(nonce)])
.then(encoded => ethers.getAddress(ethers.dataSlice(ethers.keccak256(encoded), 12))), // hash the encoded content, take the last 20 bytes and format as (checksummed) address
).to.eventually.equal(ethers.getCreateAddress({ from, nonce }));
}
});
});