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>
70 lines
2.6 KiB
Solidity
70 lines
2.6 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity ^0.8.20;
|
|
|
|
import {Test} from "forge-std/Test.sol";
|
|
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
|
|
import {Memory} from "@openzeppelin/contracts/utils/Memory.sol";
|
|
|
|
contract Base64Test is Test {
|
|
function testEncode(bytes memory input) external pure {
|
|
assertEq(Base64.encode(input), vm.toBase64(input));
|
|
assertEq(Base64.decode(Base64.encode(input)), input);
|
|
}
|
|
|
|
function testEncodeURL(bytes memory input) external pure {
|
|
assertEq(Base64.encodeURL(input), _removePadding(vm.toBase64URL(input)));
|
|
assertEq(Base64.decode(Base64.encodeURL(input)), input);
|
|
}
|
|
|
|
function testTryDecode(bytes memory input) external pure {
|
|
(bool success, bytes memory output) = Base64.tryDecode(Base64.encode(input));
|
|
assertTrue(success);
|
|
assertEq(output, input);
|
|
}
|
|
|
|
function testTryDecodeInvalidRestoresFreeMemoryPointer(bytes memory input) external pure {
|
|
bytes memory buf = bytes.concat(hex"0000", input); // Prepend two 0x00 (outside Base64 alphabet)
|
|
|
|
Memory.Pointer fmpBefore = Memory.getFreeMemoryPointer();
|
|
(bool success, bytes memory output) = Base64.tryDecode(string(buf));
|
|
Memory.Pointer fmpAfter = Memory.getFreeMemoryPointer();
|
|
|
|
assertFalse(success);
|
|
assertEq(output, hex"");
|
|
assertEq(Memory.Pointer.unwrap(fmpAfter), Memory.Pointer.unwrap(fmpBefore));
|
|
}
|
|
|
|
// `_tryDecode` temporarily overwrites the 32 bytes that follow `data` with fake "==" padding. Before
|
|
// `tryDecode`, an invalid character reverted, so restoring that word was unobservable on that path.
|
|
// `data` is exactly 32 bytes so the clobbered word is exactly `guard`'s length slot.
|
|
function testTryDecodeInvalidRestoresMemoryAfterInput() external pure {
|
|
bytes memory data = bytes("TWFuTWFuTWFuTWFuTWFuTWFuTWFuTW@u"); // '@' is outside the alphabet
|
|
bytes memory guard = new bytes(32);
|
|
|
|
(bool success, bytes memory output) = Base64.tryDecode(string(data));
|
|
|
|
assertFalse(success);
|
|
assertEq(output, hex"");
|
|
assertEq(guard.length, 32);
|
|
}
|
|
|
|
function _removePadding(string memory inputStr) internal pure returns (string memory) {
|
|
bytes memory input = bytes(inputStr);
|
|
bytes memory output;
|
|
|
|
for (uint256 i = 0; i < input.length; ++i) {
|
|
if (input[input.length - i - 1] != 0x3d) {
|
|
output = new bytes(input.length - i);
|
|
break;
|
|
}
|
|
}
|
|
|
|
for (uint256 i = 0; i < output.length; ++i) {
|
|
output[i] = input[i];
|
|
}
|
|
|
|
return string(output);
|
|
}
|
|
}
|