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>
216 lines
8.1 KiB
JavaScript
216 lines
8.1 KiB
JavaScript
import { network } from 'hardhat';
|
|
import { expect } from 'chai';
|
|
import * as random from '../helpers/random';
|
|
import { shouldBehaveLikeClone } from './Clones.behaviour';
|
|
|
|
const connection = await network.create();
|
|
const {
|
|
ethers,
|
|
networkHelpers: { loadFixture },
|
|
} = connection;
|
|
|
|
const cloneInitCode = (instance, args = undefined) =>
|
|
args
|
|
? ethers.concat([
|
|
'0x61',
|
|
ethers.toBeHex(0x2d + ethers.dataLength(args), 2),
|
|
'0x3d81600a3d39f3363d3d373d3d3d363d73',
|
|
instance.target ?? instance.address ?? instance,
|
|
'0x5af43d82803e903d91602b57fd5bf3',
|
|
args,
|
|
])
|
|
: ethers.concat([
|
|
'0x3d602d80600a3d3981f3363d3d373d3d3d363d73',
|
|
instance.target ?? instance.address ?? instance,
|
|
'0x5af43d82803e903d91602b57fd5bf3',
|
|
]);
|
|
|
|
async function fixture() {
|
|
const [deployer] = await ethers.getSigners();
|
|
|
|
const factory = await ethers.deployContract('$Clones');
|
|
const implementation = await ethers.deployContract('DummyImplementation');
|
|
|
|
const newClone =
|
|
args =>
|
|
async (opts = {}) => {
|
|
const clone = await (
|
|
args
|
|
? factory.$cloneWithImmutableArgs.staticCall(implementation, args)
|
|
: factory.$clone.staticCall(implementation)
|
|
).then(address => implementation.attach(address));
|
|
const tx = await (args
|
|
? opts.deployValue
|
|
? factory.$cloneWithImmutableArgs(implementation, args, ethers.Typed.uint256(opts.deployValue))
|
|
: factory.$cloneWithImmutableArgs(implementation, args)
|
|
: opts.deployValue
|
|
? factory.$clone(implementation, ethers.Typed.uint256(opts.deployValue))
|
|
: factory.$clone(implementation));
|
|
if (opts.initData || opts.initValue) {
|
|
await deployer.sendTransaction({ to: clone, value: opts.initValue ?? 0n, data: opts.initData ?? '0x' });
|
|
}
|
|
return Object.assign(clone, { deploymentTransaction: () => tx });
|
|
};
|
|
|
|
const newCloneDeterministic =
|
|
args =>
|
|
async (opts = {}) => {
|
|
const salt = opts.salt ?? random.bytes32();
|
|
const clone = await (
|
|
args
|
|
? factory.$cloneDeterministicWithImmutableArgs.staticCall(implementation, args, salt)
|
|
: factory.$cloneDeterministic.staticCall(implementation, salt)
|
|
).then(address => implementation.attach(address));
|
|
const tx = await (args
|
|
? opts.deployValue
|
|
? factory.$cloneDeterministicWithImmutableArgs(
|
|
implementation,
|
|
args,
|
|
salt,
|
|
ethers.Typed.uint256(opts.deployValue),
|
|
)
|
|
: factory.$cloneDeterministicWithImmutableArgs(implementation, args, salt)
|
|
: opts.deployValue
|
|
? factory.$cloneDeterministic(implementation, salt, ethers.Typed.uint256(opts.deployValue))
|
|
: factory.$cloneDeterministic(implementation, salt));
|
|
if (opts.initData || opts.initValue) {
|
|
await deployer.sendTransaction({ to: clone, value: opts.initValue ?? 0n, data: opts.initData ?? '0x' });
|
|
}
|
|
return Object.assign(clone, { deploymentTransaction: () => tx });
|
|
};
|
|
|
|
return { deployer, factory, implementation, newClone, newCloneDeterministic };
|
|
}
|
|
|
|
describe('Clones', function () {
|
|
beforeEach(async function () {
|
|
Object.assign(this, connection, await loadFixture(fixture));
|
|
});
|
|
|
|
for (const args of [undefined, '0x', '0x11223344']) {
|
|
describe(args ? `with immutable args: ${args}` : 'without immutable args', function () {
|
|
const argsLength = ethers.dataLength(args ?? '0x');
|
|
|
|
describe('clone', function () {
|
|
beforeEach(async function () {
|
|
this.createClone = this.newClone(args);
|
|
this.instance = await this.createClone();
|
|
});
|
|
|
|
shouldBehaveLikeClone();
|
|
|
|
it('get immutable arguments', async function () {
|
|
await expect(this.factory['$fetchCloneArgs(address)'](this.instance)).to.eventually.equal(args ?? '0x');
|
|
});
|
|
|
|
it('get immutable arguments (slice)', async function () {
|
|
const start = Math.floor(argsLength / 2);
|
|
const length = Math.floor(argsLength / 3);
|
|
await expect(
|
|
this.factory['$fetchCloneArgs(address,uint256,uint256)'](this.instance, start, length),
|
|
).to.eventually.equal(ethers.dataSlice(args ?? '0x', start, start + length));
|
|
});
|
|
|
|
it('get immutable arguments (slice to end)', async function () {
|
|
const start = Math.floor(argsLength / 2);
|
|
await expect(this.factory['$fetchCloneArgs(address,uint256)'](this.instance, start)).to.eventually.equal(
|
|
ethers.dataSlice(args ?? '0x', start),
|
|
);
|
|
});
|
|
|
|
it('get immutable arguments (slice) truncates to the end', async function () {
|
|
// length past the end is capped
|
|
await expect(
|
|
this.factory['$fetchCloneArgs(address,uint256,uint256)'](this.instance, 0, argsLength + 1),
|
|
).to.eventually.equal(args ?? '0x');
|
|
|
|
// start past the end returns an empty array
|
|
await expect(
|
|
this.factory['$fetchCloneArgs(address,uint256,uint256)'](this.instance, argsLength + 1, 1),
|
|
).to.eventually.equal('0x');
|
|
});
|
|
|
|
it('get immutable arguments (slice to end) truncates start', async function () {
|
|
await expect(
|
|
this.factory['$fetchCloneArgs(address,uint256)'](this.instance, argsLength + 1),
|
|
).to.eventually.equal('0x');
|
|
});
|
|
});
|
|
|
|
describe('cloneDeterministic', function () {
|
|
beforeEach(async function () {
|
|
this.createClone = this.newCloneDeterministic(args);
|
|
});
|
|
|
|
shouldBehaveLikeClone();
|
|
|
|
it('get immutable arguments', async function () {
|
|
const instance = await this.createClone();
|
|
await expect(this.factory['$fetchCloneArgs(address)'](instance)).to.eventually.equal(args ?? '0x');
|
|
});
|
|
|
|
it('revert if address already used', async function () {
|
|
const salt = random.bytes32();
|
|
|
|
const deployClone = () =>
|
|
args
|
|
? this.factory.$cloneDeterministicWithImmutableArgs(this.implementation, args, salt)
|
|
: this.factory.$cloneDeterministic(this.implementation, salt);
|
|
|
|
// deploy once
|
|
await expect(deployClone()).to.not.be.revert(ethers);
|
|
|
|
// deploy twice
|
|
await expect(deployClone()).to.be.revertedWithCustomError(this.factory, 'FailedDeployment');
|
|
});
|
|
|
|
it('address prediction', async function () {
|
|
const salt = random.bytes32();
|
|
|
|
const expected = ethers.getCreate2Address(
|
|
this.factory.target,
|
|
salt,
|
|
ethers.keccak256(cloneInitCode(this.implementation, args)),
|
|
);
|
|
|
|
if (args) {
|
|
const predicted = await this.factory.$predictDeterministicAddressWithImmutableArgs(
|
|
this.implementation,
|
|
args,
|
|
salt,
|
|
);
|
|
expect(predicted).to.equal(expected);
|
|
|
|
await expect(this.factory.$cloneDeterministicWithImmutableArgs(this.implementation, args, salt))
|
|
.to.emit(this.factory, 'return$cloneDeterministicWithImmutableArgs_address_bytes_bytes32')
|
|
.withArgs(predicted);
|
|
} else {
|
|
const predicted = await this.factory.$predictDeterministicAddress(this.implementation, salt);
|
|
expect(predicted).to.equal(expected);
|
|
|
|
await expect(this.factory.$cloneDeterministic(this.implementation, salt))
|
|
.to.emit(this.factory, 'return$cloneDeterministic_address_bytes32')
|
|
.withArgs(predicted);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
it('EIP-170 limit on immutable args', async function () {
|
|
// EIP-170 limits the contract code size to 0x6000
|
|
// This limits the length of immutable args to 0x5fd3
|
|
const args = random.hexBytes(0x5fd4);
|
|
const salt = random.bytes32();
|
|
|
|
await expect(
|
|
this.factory.$predictDeterministicAddressWithImmutableArgs(this.implementation, args, salt),
|
|
).to.be.revertedWithCustomError(this.factory, 'CloneArgumentsTooLong');
|
|
|
|
await expect(this.factory.$cloneWithImmutableArgs(this.implementation, args)).to.be.revertedWithCustomError(
|
|
this.factory,
|
|
'CloneArgumentsTooLong',
|
|
);
|
|
});
|
|
});
|