qie-forge/packages/contracts/lib/openzeppelin-contracts/test/utils/draft-InteroperableAddress.test.js
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

357 lines
15 KiB
JavaScript

import { network } from 'hardhat';
import { expect } from 'chai';
import { addressCoder, nameCoder } from 'interoperable-addresses';
import { CAIP350, chainTypeCoder } from 'interoperable-addresses/dist/CAIP350';
import * as random from '../helpers/random';
const {
ethers,
helpers: { chain },
networkHelpers: { loadFixture },
} = await network.create();
async function fixture() {
return { mock: await ethers.deployContract('$InteroperableAddress') };
}
describe('ERC7390', function () {
before(async function () {
Object.assign(this, await loadFixture(fixture));
});
it('formatEvmV1 address on the local chain', async function () {
await expect(
this.mock.$formatEvmV1(ethers.Typed.uint256(chain.reference), ethers.Typed.address(this.mock)),
).to.eventually.equal(chain.toErc7930(this.mock));
});
it('formatV1 fails if both reference and address are empty', async function () {
await expect(this.mock.$formatV1('0x0000', '0x', '0x')).to.be.revertedWithCustomError(
this.mock,
'InteroperableAddressEmptyReferenceAndAddress',
);
});
describe('reference examples', function () {
for (const { title, name } of [
{
title: 'Example 1: Ethereum mainnet address',
name: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045@eip155:1#4CA88C9C',
},
{
title: 'Example 2: Solana mainnet address',
name: 'MJKqp326RZCHnAAbew9MDdui3iCKWco7fsK9sVuZTX2@solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d#88835C11',
},
{
title: 'Example 3: EVM address without chainid',
name: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045@eip155#B26DB7CB',
},
{
title: 'Example 4: Solana mainnet network, no address',
name: '@solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d#2EB18670',
},
{
title: 'Example 5: Arbitrum One address',
name: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045@eip155:42161#D2E02854',
},
{
title: 'Example 6: Ethereum mainnet, no address',
name: '@eip155:1#F54D4FBF',
},
]) {
const { chainType, reference, address } = nameCoder.decode(name, true);
const binary = addressCoder.encode({ chainType, reference, address });
const binaryExtra = ethers.concat([binary, '0x00']);
const expected = [
chainTypeCoder.decode(chainType),
CAIP350[chainType].reference.decode(reference),
CAIP350[chainType].address.decode(address),
].map(ethers.hexlify);
it(title, async function () {
await expect(this.mock.$parseV1(binary)).to.eventually.deep.equal(expected);
await expect(this.mock.$parseV1Calldata(binary)).to.eventually.deep.equal(expected);
await expect(this.mock.$tryParseV1(binary)).to.eventually.deep.equal([true, ...expected]);
await expect(this.mock.$tryParseV1Calldata(binary)).to.eventually.deep.equal([true, ...expected]);
// default behavior: ignore trailing bytes
await expect(this.mock.$parseV1(binaryExtra)).to.eventually.deep.equal(expected);
await expect(this.mock.$parseV1Calldata(binaryExtra)).to.eventually.deep.equal(expected);
await expect(this.mock.$tryParseV1(binaryExtra)).to.eventually.deep.equal([true, ...expected]);
await expect(this.mock.$tryParseV1Calldata(binaryExtra)).to.eventually.deep.equal([true, ...expected]);
// explicitly ignore trailing bytes
await expect(this.mock.$parseV1(binaryExtra, ethers.Typed.bool(false))).to.eventually.deep.equal(expected);
await expect(this.mock.$parseV1Calldata(binaryExtra, ethers.Typed.bool(false))).to.eventually.deep.equal(
expected,
);
await expect(this.mock.$tryParseV1(binaryExtra, ethers.Typed.bool(false))).to.eventually.deep.equal([
true,
...expected,
]);
await expect(this.mock.$tryParseV1Calldata(binaryExtra, ethers.Typed.bool(false))).to.eventually.deep.equal([
true,
...expected,
]);
// reject trailing bytes
await expect(this.mock.$parseV1(binaryExtra, ethers.Typed.bool(true)))
.to.be.revertedWithCustomError(this.mock, 'InteroperableAddressParsingError')
.withArgs(binaryExtra);
await expect(this.mock.$parseV1Calldata(binaryExtra, ethers.Typed.bool(true)))
.to.be.revertedWithCustomError(this.mock, 'InteroperableAddressParsingError')
.withArgs(binaryExtra);
await expect(this.mock.$tryParseV1(binaryExtra, ethers.Typed.bool(true))).to.eventually.deep.equal([
false,
'0x0000',
'0x',
'0x',
]);
await expect(this.mock.$tryParseV1Calldata(binaryExtra, ethers.Typed.bool(true))).to.eventually.deep.equal([
false,
'0x0000',
'0x',
'0x',
]);
await expect(this.mock.$formatV1(...expected)).to.eventually.equal(binary);
if (chainType == 'eip155') {
await expect(this.mock.$parseEvmV1(binary)).to.eventually.deep.equal([
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
await expect(this.mock.$parseEvmV1Calldata(binary)).to.eventually.deep.equal([
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
await expect(this.mock.$tryParseEvmV1(binary)).to.eventually.deep.equal([
true,
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
await expect(this.mock.$tryParseEvmV1Calldata(binary)).to.eventually.deep.equal([
true,
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
// default behavior: ignore trailing bytes
await expect(this.mock.$parseEvmV1(binaryExtra)).to.eventually.deep.equal([
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
await expect(this.mock.$parseEvmV1Calldata(binaryExtra)).to.eventually.deep.equal([
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
await expect(this.mock.$tryParseEvmV1(binaryExtra)).to.eventually.deep.equal([
true,
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
await expect(this.mock.$tryParseEvmV1Calldata(binaryExtra)).to.eventually.deep.equal([
true,
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
// explicitly ignore trailing bytes
await expect(this.mock.$parseEvmV1(binaryExtra, ethers.Typed.bool(false))).to.eventually.deep.equal([
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
await expect(this.mock.$parseEvmV1Calldata(binaryExtra, ethers.Typed.bool(false))).to.eventually.deep.equal([
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
await expect(this.mock.$tryParseEvmV1(binaryExtra, ethers.Typed.bool(false))).to.eventually.deep.equal([
true,
reference ?? 0n,
address ?? ethers.ZeroAddress,
]);
await expect(
this.mock.$tryParseEvmV1Calldata(binaryExtra, ethers.Typed.bool(false)),
).to.eventually.deep.equal([true, reference ?? 0n, address ?? ethers.ZeroAddress]);
// reject trailing bytes
await expect(this.mock.$parseEvmV1(binaryExtra, ethers.Typed.bool(true)))
.to.be.revertedWithCustomError(this.mock, 'InteroperableAddressParsingError')
.withArgs(binaryExtra);
await expect(this.mock.$parseEvmV1Calldata(binaryExtra, ethers.Typed.bool(true)))
.to.be.revertedWithCustomError(this.mock, 'InteroperableAddressParsingError')
.withArgs(binaryExtra);
await expect(this.mock.$tryParseEvmV1(binaryExtra, ethers.Typed.bool(true))).to.eventually.deep.equal([
false,
0n,
ethers.ZeroAddress,
]);
await expect(this.mock.$tryParseEvmV1Calldata(binaryExtra, ethers.Typed.bool(true))).to.eventually.deep.equal(
[false, 0n, ethers.ZeroAddress],
);
if (!address) {
await expect(this.mock.$formatEvmV1(ethers.Typed.uint256(reference))).to.eventually.equal(
binary.toLowerCase(),
);
} else if (!reference) {
await expect(this.mock.$formatEvmV1(ethers.Typed.address(address))).to.eventually.equal(
binary.toLowerCase(),
);
} else {
await expect(
this.mock.$formatEvmV1(ethers.Typed.uint256(reference), ethers.Typed.address(address)),
).to.eventually.equal(binary.toLowerCase());
}
}
});
}
});
describe('invalid format', function () {
for (const [title, binary] of Object.entries({
// version 2 + some data
'unsupported version': '0x00020000010100',
// version + ref: missing chainReferenceLength and addressLength
'too short (case 1)': '0x00010042',
// version + ref + chainReference: missing addressLength
'too short (case 2)': '0x000100420101',
// version + ref + chainReference + addressLength + part of the address: missing 2 bytes of the address
'too short (case 3)': '0x00010042010114d8da6bf26964af9d7eed9e03e53415d37aa9',
// empty chain reference and address
'empty chain reference and address': '0x000100420000',
})) {
it(title, async function () {
await expect(this.mock.$parseV1(binary))
.to.be.revertedWithCustomError(this.mock, 'InteroperableAddressParsingError')
.withArgs(binary);
await expect(this.mock.$parseV1Calldata(binary))
.to.be.revertedWithCustomError(this.mock, 'InteroperableAddressParsingError')
.withArgs(binary);
await expect(this.mock.$parseEvmV1(binary))
.to.be.revertedWithCustomError(this.mock, 'InteroperableAddressParsingError')
.withArgs(binary);
await expect(this.mock.$parseEvmV1Calldata(binary))
.to.be.revertedWithCustomError(this.mock, 'InteroperableAddressParsingError')
.withArgs(binary);
await expect(this.mock.$tryParseV1(binary)).to.eventually.deep.equal([false, '0x0000', '0x', '0x']);
await expect(this.mock.$tryParseV1Calldata(binary)).to.eventually.deep.equal([false, '0x0000', '0x', '0x']);
await expect(this.mock.$tryParseEvmV1(binary)).to.eventually.deep.equal([false, 0n, ethers.ZeroAddress]);
await expect(this.mock.$tryParseEvmV1Calldata(binary)).to.eventually.deep.equal([
false,
0n,
ethers.ZeroAddress,
]);
});
}
for (const [title, binary] of Object.entries({
'not an evm format: chainid too long':
'0x00010000212dc7f03c13ad47809e88339107c33a612043d704c1c9693a74996e7f9c6bee8f2314d8da6bf26964af9d7eed9e03e53415d37aa96045',
'not an evm format: address in not 20 bytes': '0x00010000010112d8da6bf26964af9d7eed9e03e53415d37aa9',
})) {
it(title, async function () {
await expect(this.mock.$parseEvmV1(binary))
.to.be.revertedWithCustomError(this.mock, 'InteroperableAddressParsingError')
.withArgs(binary);
await expect(this.mock.$parseEvmV1Calldata(binary))
.to.be.revertedWithCustomError(this.mock, 'InteroperableAddressParsingError')
.withArgs(binary);
await expect(this.mock.$tryParseEvmV1(binary)).to.eventually.deep.equal([false, 0n, ethers.ZeroAddress]);
await expect(this.mock.$tryParseEvmV1Calldata(binary)).to.eventually.deep.equal([
false,
0n,
ethers.ZeroAddress,
]);
});
}
});
describe('isValidAddressLength', function () {
for (const { title, chainType, valid, invalid } of [
{ title: 'eip155', chainType: '0x0000', valid: [20], invalid: [0, 19, 21, 32] },
{ title: 'solana', chainType: '0x0002', valid: [32], invalid: [0, 20, 31, 33] },
{ title: 'unknown chain type', chainType: '0x0042', valid: [1, 20, 32, 255], invalid: [0] },
]) {
describe(title, function () {
for (const length of valid) {
it(`${length} bytes address is valid`, async function () {
const addr = random.hexBytes(length);
await expect(this.mock.$isValidAddressLength(chainType, ethers.Typed.uint256(length))).to.eventually.be
.true;
await expect(this.mock.$isValidAddressLength(chainType, ethers.Typed.bytes(addr))).to.eventually.be.true;
await expect(this.mock.$isValidAddressLengthCalldata(chainType, addr)).to.eventually.be.true;
});
}
for (const length of invalid) {
it(`${length} bytes address is invalid`, async function () {
const addr = random.hexBytes(length);
await expect(this.mock.$isValidAddressLength(chainType, ethers.Typed.uint256(length))).to.eventually.be
.false;
await expect(this.mock.$isValidAddressLength(chainType, ethers.Typed.bytes(addr))).to.eventually.be.false;
await expect(this.mock.$isValidAddressLengthCalldata(chainType, addr)).to.eventually.be.false;
});
}
});
}
it('lengths larger than any address', async function () {
for (const [chainType, expected] of [
['0x0000', false],
['0x0002', false],
['0x0042', true],
]) {
await expect(
this.mock.$isValidAddressLength(chainType, ethers.Typed.uint256(ethers.MaxUint256)),
).to.eventually.equal(expected);
}
});
});
describe('handles large references and addresses', function () {
it('large', async function () {
const chainType = '0x0000';
const reference = random.hexBytes(142);
const address = random.hexBytes(142);
const binary = addressCoder.encode({ chainType, reference, address });
// Generic parse
await expect(this.mock.$tryParseV1(binary)).to.eventually.deep.equal([true, chainType, reference, address]);
await expect(this.mock.$tryParseV1Calldata(binary)).to.eventually.deep.equal([
true,
chainType,
reference,
address,
]);
// EVM parse - too long to be a valid evm format
await expect(this.mock.$tryParseEvmV1(binary)).to.eventually.deep.equal([false, 0n, ethers.ZeroAddress]);
await expect(this.mock.$tryParseEvmV1Calldata(binary)).to.eventually.deep.equal([false, 0n, ethers.ZeroAddress]);
});
it('very large', async function () {
const chainType = '0x0000';
const reference = random.hexBytes(255);
const address = random.hexBytes(255);
const binary = addressCoder.encode({ chainType, reference, address });
// Generic parse
await expect(this.mock.$tryParseV1(binary)).to.eventually.deep.equal([true, chainType, reference, address]);
await expect(this.mock.$tryParseV1Calldata(binary)).to.eventually.deep.equal([
true,
chainType,
reference,
address,
]);
// EVM parse - too long to be a valid evm format
await expect(this.mock.$tryParseEvmV1(binary)).to.eventually.deep.equal([false, 0n, ethers.ZeroAddress]);
await expect(this.mock.$tryParseEvmV1Calldata(binary)).to.eventually.deep.equal([false, 0n, ethers.ZeroAddress]);
});
});
});