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>
175 lines
6.2 KiB
JavaScript
175 lines
6.2 KiB
JavaScript
import { network } from 'hardhat';
|
|
import { expect } from 'chai';
|
|
import { domainType, domainSeparator, hashTypedData } from '../../helpers/eip712';
|
|
import * as random from '../../helpers/random';
|
|
|
|
const {
|
|
ethers,
|
|
networkHelpers: { loadFixture },
|
|
} = await network.create();
|
|
|
|
async function fixture() {
|
|
return { mock: await ethers.deployContract('$MessageHashUtils') };
|
|
}
|
|
|
|
describe('MessageHashUtils', function () {
|
|
beforeEach(async function () {
|
|
Object.assign(this, await loadFixture(fixture));
|
|
});
|
|
|
|
describe('toEthSignedMessageHash', function () {
|
|
it('prefixes bytes32 data correctly', async function () {
|
|
const message = random.bytes(32);
|
|
const expectedHash = ethers.hashMessage(message);
|
|
|
|
await expect(this.mock.getFunction('$toEthSignedMessageHash(bytes32)')(message)).to.eventually.equal(
|
|
expectedHash,
|
|
);
|
|
});
|
|
|
|
it('prefixes dynamic length data correctly', async function () {
|
|
const message = random.bytes(128);
|
|
const expectedHash = ethers.hashMessage(message);
|
|
|
|
await expect(this.mock.getFunction('$toEthSignedMessageHash(bytes)')(message)).to.eventually.equal(expectedHash);
|
|
});
|
|
|
|
it('version match for bytes32', async function () {
|
|
const message = random.bytes(32);
|
|
const fixed = await this.mock.getFunction('$toEthSignedMessageHash(bytes32)')(message);
|
|
const dynamic = await this.mock.getFunction('$toEthSignedMessageHash(bytes)')(message);
|
|
|
|
expect(fixed).to.equal(dynamic);
|
|
});
|
|
});
|
|
|
|
describe('toDataWithIntendedValidatorHash', function () {
|
|
it('returns the digest of `bytes32 messageHash` correctly', async function () {
|
|
const verifier = ethers.Wallet.createRandom().address;
|
|
const message = random.bytes(32);
|
|
const expectedHash = ethers.solidityPackedKeccak256(
|
|
['string', 'address', 'bytes32'],
|
|
['\x19\x00', verifier, message],
|
|
);
|
|
|
|
await expect(
|
|
this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes32)')(verifier, message),
|
|
).to.eventually.equal(expectedHash);
|
|
});
|
|
|
|
it('returns the digest of `bytes memory message` correctly', async function () {
|
|
const verifier = ethers.Wallet.createRandom().address;
|
|
const message = random.bytes(128);
|
|
const expectedHash = ethers.solidityPackedKeccak256(
|
|
['string', 'address', 'bytes'],
|
|
['\x19\x00', verifier, message],
|
|
);
|
|
|
|
await expect(
|
|
this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes)')(verifier, message),
|
|
).to.eventually.equal(expectedHash);
|
|
});
|
|
|
|
it('version match for bytes32', async function () {
|
|
const verifier = ethers.Wallet.createRandom().address;
|
|
const message = random.bytes(32);
|
|
const fixed = await this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes)')(verifier, message);
|
|
const dynamic = await this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes32)')(
|
|
verifier,
|
|
message,
|
|
);
|
|
|
|
expect(fixed).to.equal(dynamic);
|
|
});
|
|
});
|
|
|
|
describe('toTypedDataHash', function () {
|
|
it('returns the digest correctly', async function () {
|
|
const domain = {
|
|
name: 'Test',
|
|
version: '1',
|
|
chainId: 1n,
|
|
verifyingContract: ethers.Wallet.createRandom().address,
|
|
};
|
|
const structhash = random.bytes(32);
|
|
const expectedHash = hashTypedData(domain, structhash);
|
|
|
|
await expect(this.mock.$toTypedDataHash(domainSeparator(domain), structhash)).to.eventually.equal(expectedHash);
|
|
});
|
|
});
|
|
|
|
describe('ERC-5267', function () {
|
|
const fullDomain = {
|
|
name: random.string(),
|
|
version: random.string(),
|
|
chainId: random.uint256(),
|
|
verifyingContract: random.address(),
|
|
salt: random.bytes32(),
|
|
};
|
|
|
|
for (let fields = 0; fields < 1 << Object.keys(fullDomain).length; ++fields) {
|
|
const domain = Object.fromEntries(Object.entries(fullDomain).filter((_, i) => fields & (1 << i)));
|
|
const domainTypeName = new ethers.TypedDataEncoder({ EIP712Domain: domainType(domain) }).encodeType(
|
|
'EIP712Domain',
|
|
);
|
|
|
|
describe(domainTypeName, function () {
|
|
it('toDomainSeparator(bytes1,string,string,uint256,address,bytes32)', async function () {
|
|
await expect(
|
|
this.mock.$toDomainSeparator(
|
|
ethers.toBeHex(fields),
|
|
ethers.Typed.string(fullDomain.name),
|
|
ethers.Typed.string(fullDomain.version),
|
|
fullDomain.chainId,
|
|
fullDomain.verifyingContract,
|
|
fullDomain.salt,
|
|
),
|
|
).to.eventually.equal(domainSeparator(domain));
|
|
});
|
|
|
|
it('toDomainSeparator(bytes1,bytes32,bytes32,uint256,address,bytes32)', async function () {
|
|
await expect(
|
|
this.mock.$toDomainSeparator(
|
|
ethers.toBeHex(fields),
|
|
ethers.Typed.bytes32(ethers.id(fullDomain.name)),
|
|
ethers.Typed.bytes32(ethers.id(fullDomain.version)),
|
|
fullDomain.chainId,
|
|
fullDomain.verifyingContract,
|
|
fullDomain.salt,
|
|
),
|
|
).to.eventually.equal(domainSeparator(domain));
|
|
});
|
|
|
|
it('toDomainTypeHash', async function () {
|
|
await expect(this.mock.$toDomainTypeHash(ethers.toBeHex(fields))).to.eventually.equal(
|
|
ethers.id(domainTypeName),
|
|
);
|
|
});
|
|
});
|
|
}
|
|
|
|
describe('unsupported fields', function () {
|
|
for (const fields of [0x20, 0x40, 0x80, 0xff]) {
|
|
it(`rejects ${ethers.toBeHex(fields)}`, async function () {
|
|
await expect(this.mock.$toDomainTypeHash(ethers.toBeHex(fields)))
|
|
.to.be.revertedWithCustomError(this.mock, 'ERC5267UnsupportedFields')
|
|
.withArgs(ethers.toBeHex(fields));
|
|
|
|
await expect(
|
|
this.mock.$toDomainSeparator(
|
|
ethers.toBeHex(fields),
|
|
ethers.Typed.string(fullDomain.name),
|
|
ethers.Typed.string(fullDomain.version),
|
|
fullDomain.chainId,
|
|
fullDomain.verifyingContract,
|
|
fullDomain.salt,
|
|
),
|
|
)
|
|
.to.be.revertedWithCustomError(this.mock, 'ERC5267UnsupportedFields')
|
|
.withArgs(ethers.toBeHex(fields));
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|