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

465 lines
18 KiB
JavaScript

import { ethers } from 'ethers';
import { expect } from 'chai';
import { anyValue } from '@nomicfoundation/hardhat-ethers-chai-matchers/withArgs';
import { RevertType } from '../helpers/enums';
const ids = [17n, 42n];
const values = [100n, 320n];
const RECEIVER_SINGLE_MAGIC_VALUE = '0xf23a6e61';
const RECEIVER_BATCH_MAGIC_VALUE = '0xbc197c81';
export function shouldBehaveLikeBridgeERC1155({ chainAIsCustodial = false, chainBIsCustodial = false } = {}) {
describe('bridge ERC1155 like', function () {
beforeEach(function () {
// helper
this.encodePayload = (from, to, ids, values, data = '0x') =>
ethers.AbiCoder.defaultAbiCoder().encode(
['bytes', 'bytes', 'uint256[]', 'uint256[]', 'bytes'],
[this.helpers.chain.toErc7930(from), to.target ?? to.address ?? to, ids, values, data],
);
});
it('bridge setup', async function () {
await expect(this.bridgeA.getLink(this.helpers.chain.erc7930)).to.eventually.deep.equal([
this.gateway.target,
this.helpers.chain.toErc7930(this.bridgeB),
]);
await expect(this.bridgeB.getLink(this.helpers.chain.erc7930)).to.eventually.deep.equal([
this.gateway.target,
this.helpers.chain.toErc7930(this.bridgeA),
]);
});
describe('crosschain send (both direction)', function () {
it('single', async function () {
const [alice, bruce, chris] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
// Alice sends tokens from chain A to Bruce on chain B.
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256,uint256)')(
alice,
this.helpers.chain.toErc7930(bruce),
ids[0],
values[0],
),
)
// bridge on chain A takes custody of the token
.to.emit(this.tokenA, 'TransferSingle')
.withArgs(
chainAIsCustodial ? this.bridgeA : alice,
alice,
chainAIsCustodial ? this.bridgeA : ethers.ZeroAddress,
ids[0],
values[0],
)
// crosschain transfer sent
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, alice, this.helpers.chain.toErc7930(bruce), ids.slice(0, 1), values.slice(0, 1), '0x')
// ERC-7786 event
.to.emit(this.gateway, 'MessageSent')
// crosschain transfer received
.to.emit(this.bridgeB, 'CrosschainMultiTokenTransferReceived')
.withArgs(anyValue, this.helpers.chain.toErc7930(alice), bruce, ids.slice(0, 1), values.slice(0, 1), '0x')
// tokens are minted on chain B
.to.emit(this.tokenB, 'TransferSingle')
.withArgs(
chainBIsCustodial ? this.bridgeB : this.gateway,
chainBIsCustodial ? this.bridgeB : ethers.ZeroAddress,
bruce,
ids[0],
values[0],
);
// Bruce sends tokens from chain B to Chris on chain A.
await expect(
this.bridgeB.connect(bruce).getFunction('crosschainTransferFrom(address,bytes,uint256,uint256)')(
bruce,
this.helpers.chain.toErc7930(chris),
ids[0],
values[0],
),
)
// tokens are burned on chain B
.to.emit(this.tokenB, 'TransferSingle')
.withArgs(
chainBIsCustodial ? this.bridgeB : bruce,
bruce,
chainBIsCustodial ? this.bridgeB : ethers.ZeroAddress,
ids[0],
values[0],
)
// crosschain transfer sent
.to.emit(this.bridgeB, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, bruce, this.helpers.chain.toErc7930(chris), ids.slice(0, 1), values.slice(0, 1), '0x')
// ERC-7786 event
.to.emit(this.gateway, 'MessageSent')
// crosschain transfer received
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferReceived')
.withArgs(anyValue, this.helpers.chain.toErc7930(bruce), chris, ids.slice(0, 1), values.slice(0, 1), '0x')
// bridge on chain A releases custody of the token
.to.emit(this.tokenA, 'TransferSingle')
.withArgs(
chainAIsCustodial ? this.bridgeA : this.gateway,
chainAIsCustodial ? this.bridgeA : ethers.ZeroAddress,
chris,
ids[0],
values[0],
);
});
it('batch', async function () {
const [alice, bruce, chris] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
// Alice sends tokens from chain A to Bruce on chain B.
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.helpers.chain.toErc7930(bruce),
ids,
values,
),
)
// bridge on chain A takes custody of the token
.to.emit(this.tokenA, 'TransferBatch')
.withArgs(
chainAIsCustodial ? this.bridgeA : alice,
alice,
chainAIsCustodial ? this.bridgeA : ethers.ZeroAddress,
ids,
values,
)
// crosschain transfer sent
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, alice, this.helpers.chain.toErc7930(bruce), ids, values, '0x')
// ERC-7786 event
.to.emit(this.gateway, 'MessageSent')
// crosschain transfer received
.to.emit(this.bridgeB, 'CrosschainMultiTokenTransferReceived')
.withArgs(anyValue, this.helpers.chain.toErc7930(alice), bruce, ids, values, '0x')
// tokens are minted on chain B
.to.emit(this.tokenB, 'TransferBatch')
.withArgs(
chainBIsCustodial ? this.bridgeB : this.gateway,
chainBIsCustodial ? this.bridgeB : ethers.ZeroAddress,
bruce,
ids,
values,
);
// Bruce sends tokens from chain B to Chris on chain A.
await expect(
this.bridgeB.connect(bruce).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
bruce,
this.helpers.chain.toErc7930(chris),
ids,
values,
),
)
// tokens are burned on chain B
.to.emit(this.tokenB, 'TransferBatch')
.withArgs(
chainBIsCustodial ? this.bridgeB : bruce,
bruce,
chainBIsCustodial ? this.bridgeB : ethers.ZeroAddress,
ids,
values,
)
// crosschain transfer sent
.to.emit(this.bridgeB, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, bruce, this.helpers.chain.toErc7930(chris), ids, values, '0x')
// ERC-7786 event
.to.emit(this.gateway, 'MessageSent')
// crosschain transfer received
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferReceived')
.withArgs(anyValue, this.helpers.chain.toErc7930(bruce), chris, ids, values, '0x')
// bridge on chain A releases custody of the token
.to.emit(this.tokenA, 'TransferBatch')
.withArgs(
chainAIsCustodial ? this.bridgeA : this.gateway,
chainAIsCustodial ? this.bridgeA : ethers.ZeroAddress,
chris,
ids,
values,
);
});
});
describe('crosschain send with data', function () {
beforeEach(async function () {
this.receiver = await this.ethers.deployContract('$ERC1155ReceiverMock', [
RECEIVER_SINGLE_MAGIC_VALUE,
RECEIVER_BATCH_MAGIC_VALUE,
RevertType.None,
]);
this.data = '0xdeadbeef';
});
it('single-token overload forwards data to the destination receive hook', async function () {
const [alice] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256,uint256,bytes)')(
alice,
this.helpers.chain.toErc7930(this.receiver),
ids[0],
values[0],
this.data,
),
)
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferSent')
.withArgs(
anyValue,
alice,
this.helpers.chain.toErc7930(this.receiver),
ids.slice(0, 1),
values.slice(0, 1),
this.data,
)
.to.emit(this.bridgeB, 'CrosschainMultiTokenTransferReceived')
.withArgs(
anyValue,
this.helpers.chain.toErc7930(alice),
this.receiver,
ids.slice(0, 1),
values.slice(0, 1),
this.data,
)
.to.emit(this.receiver, 'BatchReceived')
.withArgs(
chainBIsCustodial ? this.bridgeB : this.gateway,
chainBIsCustodial ? this.bridgeB : ethers.ZeroAddress,
ids.slice(0, 1),
values.slice(0, 1),
this.data,
anyValue,
);
});
it('batch overload forwards data to the destination receive hook', async function () {
const [alice] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[],bytes)')(
alice,
this.helpers.chain.toErc7930(this.receiver),
ids,
values,
this.data,
),
)
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, alice, this.helpers.chain.toErc7930(this.receiver), ids, values, this.data)
.to.emit(this.bridgeB, 'CrosschainMultiTokenTransferReceived')
.withArgs(anyValue, this.helpers.chain.toErc7930(alice), this.receiver, ids, values, this.data)
.to.emit(this.receiver, 'BatchReceived')
.withArgs(
chainBIsCustodial ? this.bridgeB : this.gateway,
chainBIsCustodial ? this.bridgeB : ethers.ZeroAddress,
ids,
values,
this.data,
anyValue,
);
});
});
describe('transfer with allowance', function () {
it('spender is owner', async function () {
const [alice, bruce] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.helpers.chain.toErc7930(bruce),
ids,
values,
),
)
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, alice, this.helpers.chain.toErc7930(bruce), ids, values, '0x');
});
it('spender is allowed for all', async function () {
const [alice, bruce, chris] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
await this.tokenA.connect(alice).setApprovalForAll(chris, true);
await expect(
this.bridgeA.connect(chris).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.helpers.chain.toErc7930(bruce),
ids,
values,
),
)
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, alice, this.helpers.chain.toErc7930(bruce), ids, values, '0x');
});
});
describe('invalid transfer', function () {
it('missing allowance', async function () {
const [alice, bruce, chris] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
// chris is not allowed
await expect(
this.bridgeA.connect(chris).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.helpers.chain.toErc7930(bruce),
ids,
values,
),
)
.to.be.revertedWithCustomError(this.tokenA, 'ERC1155MissingApprovalForAll')
.withArgs(chris, alice);
});
it('insufficient balance', async function () {
const [alice, bruce] = this.accounts;
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.helpers.chain.toErc7930(bruce),
ids,
values,
),
)
.to.be.revertedWithCustomError(this.tokenA, 'ERC1155InsufficientBalance')
.withArgs(alice, 0n, values[0], ids[0]);
});
it('reverts if the address part of the interoperable address is empty', async function () {
const [alice] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.helpers.chain.toErc7930(undefined),
ids,
values,
), // No address
).to.be.revertedWithCustomError(this.bridgeA, 'CrosschainMultiTokenInvalidAddress');
});
it('reverts if the address part of the interoperable address has an invalid length', async function () {
const [alice] = this.accounts;
const to = this.helpers.chain.toErc7930(ethers.zeroPadValue(alice.address, 32)); // 32-byte EVM address
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
to,
ids,
values,
),
).to.be.revertedWithCustomError(this.bridgeA, 'CrosschainMultiTokenInvalidAddress');
});
});
describe('restrictions', function () {
it('only gateway can relay messages', async function () {
const [notGateway] = this.accounts;
await expect(
this.bridgeA
.connect(notGateway)
.receiveMessage(
ethers.ZeroHash,
this.helpers.chain.toErc7930(this.tokenB),
this.encodePayload(notGateway, notGateway, ids, values),
),
)
.to.be.revertedWithCustomError(this.bridgeA, 'ERC7786RecipientUnauthorizedGateway')
.withArgs(notGateway, this.helpers.chain.toErc7930(this.tokenB));
});
it('only counterpart can send a crosschain message', async function () {
const [invalid] = this.accounts;
await expect(
this.gateway
.connect(invalid)
.sendMessage(
this.helpers.chain.toErc7930(this.bridgeA),
this.encodePayload(invalid, invalid, ids, values),
[],
),
)
.to.be.revertedWithCustomError(this.bridgeA, 'ERC7786RecipientUnauthorizedGateway')
.withArgs(this.gateway, this.helpers.chain.toErc7930(invalid));
});
});
describe('reconfiguration', function () {
it('updating a link emits an event', async function () {
const newGateway = await this.ethers.deployContract('$ERC7786GatewayMock');
const newCounterpart = this.helpers.chain.toErc7930(this.accounts[0]);
await expect(this.bridgeA.$_setLink(newGateway, newCounterpart, true))
.to.emit(this.bridgeA, 'LinkRegistered')
.withArgs(newGateway, newCounterpart);
await expect(this.bridgeA.getLink(this.helpers.chain.erc7930)).to.eventually.deep.equal([
newGateway.target,
newCounterpart,
]);
});
it('cannot override configuration if "allowOverride" is false', async function () {
const newGateway = await this.ethers.deployContract('$ERC7786GatewayMock');
const newCounterpart = this.helpers.chain.toErc7930(this.accounts[0]);
await expect(this.bridgeA.$_setLink(newGateway, newCounterpart, false))
.to.be.revertedWithCustomError(this.bridgeA, 'LinkAlreadyRegistered')
.withArgs(this.helpers.chain.erc7930);
});
it('reject counterpart with invalid address length', async function () {
const newGateway = await this.ethers.deployContract('$ERC7786GatewayMock');
const newCounterpart = this.helpers.chain.toErc7930(ethers.zeroPadValue(this.accounts[0].address, 32)); // 32-byte EVM address
await expect(this.bridgeA.$_setLink(newGateway, newCounterpart, true))
.to.be.revertedWithCustomError(this.bridgeA, 'InteroperableAddressParsingError')
.withArgs(newCounterpart);
});
it('reject invalid gateway', async function () {
const notAGateway = this.accounts[0];
const newCounterpart = this.helpers.chain.toErc7930(this.accounts[0]);
await expect(this.bridgeA.$_setLink(notAGateway, newCounterpart, false)).to.be.revertedWithoutReason(ethers);
});
});
});
}