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>
113 lines
4.4 KiB
JavaScript
113 lines
4.4 KiB
JavaScript
import { network } from 'hardhat';
|
|
import { expect } from 'chai';
|
|
import { MAX_UINT48 } from '../helpers/constants';
|
|
import { ForwardRequest, getDomain } from '../helpers/eip712';
|
|
import { shouldBehaveLikeRegularContext } from '../utils/Context.behavior';
|
|
|
|
const {
|
|
ethers,
|
|
helpers: { impersonate },
|
|
networkHelpers: { loadFixture },
|
|
} = await network.create();
|
|
|
|
async function fixture() {
|
|
const [sender, other] = await ethers.getSigners();
|
|
|
|
const forwarder = await ethers.deployContract('ERC2771Forwarder', ['ERC2771Forwarder']);
|
|
const forwarderAsSigner = await impersonate(forwarder.target);
|
|
const context = await ethers.deployContract('ERC2771ContextMock', [forwarder]);
|
|
const contextHelper = await ethers.deployContract('ContextMockCaller', []);
|
|
const domain = await getDomain(forwarder);
|
|
|
|
const prepareAndSignRequest = async (signer, request) => {
|
|
// request.to is mandatory
|
|
request.from ??= signer.address;
|
|
request.value ??= 0n;
|
|
request.data ??= '0x';
|
|
request.gas ??= 100000n;
|
|
request.nonce ??= await forwarder.nonces(signer);
|
|
request.deadline ??= MAX_UINT48;
|
|
request.signature = await signer.signTypedData(domain, { ForwardRequest }, request);
|
|
return request;
|
|
};
|
|
|
|
return { sender, other, forwarder, forwarderAsSigner, context, contextHelper, prepareAndSignRequest };
|
|
}
|
|
|
|
describe('ERC2771Context', function () {
|
|
beforeEach(async function () {
|
|
Object.assign(this, await loadFixture(fixture));
|
|
});
|
|
|
|
it('recognize trusted forwarder', async function () {
|
|
await expect(this.context.isTrustedForwarder(this.forwarder)).to.eventually.be.true;
|
|
});
|
|
|
|
it('returns the trusted forwarder', async function () {
|
|
await expect(this.context.trustedForwarder()).to.eventually.equal(this.forwarder);
|
|
});
|
|
|
|
describe('when called directly', function () {
|
|
shouldBehaveLikeRegularContext();
|
|
});
|
|
|
|
describe('when receiving a relayed call', function () {
|
|
describe('msgSender', function () {
|
|
it('returns the relayed transaction original sender', async function () {
|
|
const req = await this.prepareAndSignRequest(this.sender, {
|
|
to: this.context.target,
|
|
data: this.context.interface.encodeFunctionData('msgSender'),
|
|
});
|
|
|
|
await expect(this.forwarder.verify(req)).to.eventually.be.true;
|
|
await expect(this.forwarder.execute(req)).to.emit(this.context, 'Sender').withArgs(this.sender);
|
|
});
|
|
|
|
it('returns the original sender when calldata length is less than 20 bytes (address length)', async function () {
|
|
// The forwarder doesn't produce calls with calldata length less than 20 bytes so `this.forwarderAsSigner` is used instead.
|
|
await expect(this.context.connect(this.forwarderAsSigner).msgSender())
|
|
.to.emit(this.context, 'Sender')
|
|
.withArgs(this.forwarder);
|
|
});
|
|
});
|
|
|
|
describe('msgData', function () {
|
|
it('returns the relayed transaction original data', async function () {
|
|
const args = [42n, 'OpenZeppelin'];
|
|
|
|
const req = await this.prepareAndSignRequest(this.sender, {
|
|
to: this.context.target,
|
|
data: this.context.interface.encodeFunctionData('msgData', args),
|
|
});
|
|
|
|
await expect(this.forwarder.verify(req)).to.eventually.be.true;
|
|
await expect(this.forwarder.execute(req))
|
|
.to.emit(this.context, 'Data')
|
|
.withArgs(req.data, ...args);
|
|
});
|
|
});
|
|
|
|
it('returns the full original data when calldata length is less than 20 bytes (address length)', async function () {
|
|
const data = this.context.interface.encodeFunctionData('msgDataShort');
|
|
|
|
// The forwarder doesn't produce calls with calldata length less than 20 bytes so `this.forwarderAsSigner` is used instead.
|
|
await expect(this.context.connect(this.forwarderAsSigner).msgDataShort())
|
|
.to.emit(this.context, 'DataShort')
|
|
.withArgs(data);
|
|
});
|
|
});
|
|
|
|
it('multicall poison attack', async function () {
|
|
const req = await this.prepareAndSignRequest(this.sender, {
|
|
to: this.context.target,
|
|
data: this.context.interface.encodeFunctionData('multicall', [
|
|
// poisonned call to 'msgSender()'
|
|
[ethers.concat([this.context.interface.encodeFunctionData('msgSender'), this.other.address])],
|
|
]),
|
|
});
|
|
|
|
await expect(this.forwarder.verify(req)).to.eventually.be.true;
|
|
await expect(this.forwarder.execute(req)).to.emit(this.context, 'Sender').withArgs(this.sender);
|
|
});
|
|
});
|