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>
123 lines
4.8 KiB
Solidity
123 lines
4.8 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity ^0.8.20;
|
|
|
|
import {Test} from "forge-std/Test.sol";
|
|
import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol";
|
|
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
|
|
|
|
contract ClonesTest is Test {
|
|
function getNumber() external pure returns (uint256) {
|
|
return 42;
|
|
}
|
|
|
|
function testSymbolicPredictDeterministicAddressSpillage(address implementation, bytes32 salt) public view {
|
|
address predicted = Clones.predictDeterministicAddress(implementation, salt);
|
|
bytes32 spillage;
|
|
assembly ("memory-safe") {
|
|
spillage := and(predicted, 0xffffffffffffffffffffffff0000000000000000000000000000000000000000)
|
|
}
|
|
assertEq(spillage, bytes32(0));
|
|
}
|
|
|
|
function testSymbolicPredictDeterministicAddressWithImmutableArgsSpillage(
|
|
address implementation,
|
|
bytes32 salt,
|
|
bytes memory args
|
|
) public view {
|
|
vm.assume(args.length < 0xbfd3);
|
|
|
|
address predicted = Clones.predictDeterministicAddressWithImmutableArgs(implementation, args, salt);
|
|
bytes32 spillage;
|
|
assembly ("memory-safe") {
|
|
spillage := and(predicted, 0xffffffffffffffffffffffff0000000000000000000000000000000000000000)
|
|
}
|
|
assertEq(spillage, bytes32(0));
|
|
}
|
|
|
|
function testCloneDirty() external {
|
|
address cloneClean = Clones.clone(address(this));
|
|
address cloneDirty = Clones.clone(_dirty(address(this)));
|
|
|
|
// both clones have the same code
|
|
assertEq(cloneClean.code, cloneDirty.code);
|
|
|
|
// both clones behave as expected
|
|
assertEq(ClonesTest(cloneClean).getNumber(), this.getNumber());
|
|
assertEq(ClonesTest(cloneDirty).getNumber(), this.getNumber());
|
|
}
|
|
|
|
function testCloneDeterministicDirty(bytes32 salt) external {
|
|
address cloneClean = Clones.cloneDeterministic(address(this), salt);
|
|
address cloneDirty = Clones.cloneDeterministic(_dirty(address(this)), ~salt);
|
|
|
|
// both clones have the same code
|
|
assertEq(cloneClean.code, cloneDirty.code);
|
|
|
|
// both clones behave as expected
|
|
assertEq(ClonesTest(cloneClean).getNumber(), this.getNumber());
|
|
assertEq(ClonesTest(cloneDirty).getNumber(), this.getNumber());
|
|
}
|
|
|
|
function testPredictDeterministicAddressDirty(bytes32 salt) external view {
|
|
address predictClean = Clones.predictDeterministicAddress(address(this), salt);
|
|
address predictDirty = Clones.predictDeterministicAddress(_dirty(address(this)), salt);
|
|
|
|
//prediction should be similar
|
|
assertEq(predictClean, predictDirty);
|
|
}
|
|
|
|
function testFetchCloneArgs(bytes memory args, bytes32 salt) external {
|
|
vm.assume(args.length < 0xbfd3);
|
|
|
|
address instance1 = Clones.cloneWithImmutableArgs(address(this), args);
|
|
address instance2 = Clones.cloneDeterministicWithImmutableArgs(address(this), args, salt);
|
|
|
|
// both clones have the same code
|
|
assertEq(instance1.code, instance2.code);
|
|
|
|
// both clones behave as expected and args can be fetched
|
|
assertEq(ClonesTest(instance1).getNumber(), this.getNumber());
|
|
assertEq(ClonesTest(instance2).getNumber(), this.getNumber());
|
|
assertEq(Clones.fetchCloneArgs(instance1), args);
|
|
assertEq(Clones.fetchCloneArgs(instance2), args);
|
|
}
|
|
|
|
function testFetchCloneArgsSlice(bytes memory args, uint256 start, uint256 length, bytes32 salt) external {
|
|
vm.assume(args.length < 0xbfd3);
|
|
|
|
address instance = Clones.cloneDeterministicWithImmutableArgs(address(this), args, salt);
|
|
|
|
// slice is truncated to the length of the immutable args
|
|
uint256 expectedStart = Math.min(start, args.length);
|
|
uint256 expectedLength = Math.min(length, args.length - expectedStart);
|
|
|
|
bytes memory expected = new bytes(expectedLength);
|
|
for (uint256 i = 0; i < expectedLength; ++i) {
|
|
expected[i] = args[expectedStart + i];
|
|
}
|
|
assertEq(Clones.fetchCloneArgs(instance, start, length), expected);
|
|
}
|
|
|
|
function testFetchCloneArgsSliceToEnd(bytes memory args, uint256 start, bytes32 salt) external {
|
|
vm.assume(args.length < 0xbfd3);
|
|
|
|
address instance = Clones.cloneDeterministicWithImmutableArgs(address(this), args, salt);
|
|
|
|
// start is truncated to the length of the immutable args
|
|
uint256 expectedStart = Math.min(start, args.length);
|
|
|
|
bytes memory expected = new bytes(args.length - expectedStart);
|
|
for (uint256 i = 0; i < expected.length; ++i) {
|
|
expected[i] = args[expectedStart + i];
|
|
}
|
|
assertEq(Clones.fetchCloneArgs(instance, start), expected);
|
|
}
|
|
|
|
function _dirty(address input) private pure returns (address output) {
|
|
assembly ("memory-safe") {
|
|
output := or(input, shl(160, not(0)))
|
|
}
|
|
}
|
|
}
|