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>
97 lines
3.3 KiB
Solidity
97 lines
3.3 KiB
Solidity
pragma solidity ^0.8.20;
|
|
|
|
/**
|
|
* @dev Library for computing storage (and transient storage) locations from namespaces and deriving slots
|
|
* corresponding to standard patterns. The derivation method for array and mapping matches the storage layout used by
|
|
* the solidity language / compiler.
|
|
*
|
|
* See https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html#mappings-and-dynamic-arrays[Solidity docs for mappings and dynamic arrays.].
|
|
*
|
|
* Example usage:
|
|
* ```solidity
|
|
* contract Example {
|
|
* // Add the library methods
|
|
* using StorageSlot for bytes32;
|
|
* using SlotDerivation for *;
|
|
*
|
|
* // Declare a namespace
|
|
* string private constant _NAMESPACE = "<namespace>"; // eg. OpenZeppelin.Slot
|
|
*
|
|
* function setValueInNamespace(uint256 key, address newValue) internal {
|
|
* _NAMESPACE.erc7201Slot().deriveMapping(key).getAddressSlot().value = newValue;
|
|
* }
|
|
*
|
|
* function getValueInNamespace(uint256 key) internal view returns (address) {
|
|
* return _NAMESPACE.erc7201Slot().deriveMapping(key).getAddressSlot().value;
|
|
* }
|
|
* }
|
|
* ```
|
|
*
|
|
* TIP: Consider using this library along with {StorageSlot}.
|
|
*
|
|
* NOTE: This library provides a way to manipulate storage locations in a non-standard way. Tooling for checking
|
|
* upgrade safety will ignore the slots accessed through this library.
|
|
*
|
|
* _Available since v5.1._
|
|
*/
|
|
library SlotDerivation {
|
|
/**
|
|
* @dev Derive an ERC-7201 slot from a string (namespace).
|
|
*/
|
|
function erc7201Slot(string memory namespace) internal pure returns (bytes32 slot) {
|
|
assembly ("memory-safe") {
|
|
mstore(0x00, sub(keccak256(add(namespace, 0x20), mload(namespace)), 1))
|
|
slot := and(keccak256(0x00, 0x20), not(0xff))
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dev Add an offset to a slot to get the n-th element of a structure or an array.
|
|
*/
|
|
function offset(bytes32 slot, uint256 pos) internal pure returns (bytes32 result) {
|
|
unchecked {
|
|
return bytes32(uint256(slot) + pos);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dev Derive the location of the first element in an array from the slot where the length is stored.
|
|
*/
|
|
function deriveArray(bytes32 slot) internal pure returns (bytes32 result) {
|
|
assembly ("memory-safe") {
|
|
mstore(0x00, slot)
|
|
result := keccak256(0x00, 0x20)
|
|
}
|
|
}
|
|
|
|
<%- for (const { type, size } of it.SLOT_TYPES) { %>
|
|
<% if (size) { -%>
|
|
/**
|
|
* @dev Derive the location of a mapping element from the key.
|
|
*/
|
|
function deriveMapping(bytes32 slot, <%= type %> key) internal pure returns (bytes32 result) {
|
|
assembly ("memory-safe") {
|
|
mstore(0x00, <%= (it.sanitize[type] ?? (x => x))('key') %>)
|
|
mstore(0x20, slot)
|
|
result := keccak256(0x00, 0x40)
|
|
}
|
|
}
|
|
<% } else { -%>
|
|
/**
|
|
* @dev Derive the location of a mapping element from the key.
|
|
*/
|
|
function deriveMapping(bytes32 slot, <%= type %> memory key) internal pure returns (bytes32 result) {
|
|
assembly ("memory-safe") {
|
|
let length := mload(key)
|
|
let begin := add(key, 0x20)
|
|
let end := add(begin, length)
|
|
let cache := mload(end)
|
|
mstore(end, slot)
|
|
result := keccak256(begin, add(length, 0x20))
|
|
mstore(end, cache)
|
|
}
|
|
}
|
|
<% } -%>
|
|
<% } -%>
|
|
}
|