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>
121 lines
4.5 KiB
Solidity
121 lines
4.5 KiB
Solidity
pragma solidity ^0.8.24;
|
|
|
|
/**
|
|
* @dev Library for reading and writing value-types to specific transient storage slots.
|
|
*
|
|
* Transient slots are often used to store temporary values that are removed after the current transaction.
|
|
* This library helps with reading and writing to such slots without the need for inline assembly.
|
|
*
|
|
* Example reading and writing values using transient storage:
|
|
* ```solidity
|
|
* contract Lock {
|
|
* using TransientSlot for *;
|
|
*
|
|
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
|
|
* bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
|
|
*
|
|
* modifier locked() {
|
|
* require(!_LOCK_SLOT.asBoolean().tload());
|
|
*
|
|
* _LOCK_SLOT.asBoolean().tstore(true);
|
|
* _;
|
|
* _LOCK_SLOT.asBoolean().tstore(false);
|
|
* }
|
|
* }
|
|
* ```
|
|
*
|
|
* Alternatively, the slot can be reserved by declaring a state variable, letting the compiler allocate it:
|
|
* ```solidity
|
|
* contract Lock {
|
|
* using TransientSlot for *;
|
|
*
|
|
* // Reserve a slot. The compiler allocates it, just like it would for any other state variable.
|
|
* TransientSlot.TBoolean private _lock;
|
|
*
|
|
* modifier locked() {
|
|
* require(!_lock.tload());
|
|
*
|
|
* _lock.tstore(true);
|
|
* _;
|
|
* _lock.tstore(false);
|
|
* }
|
|
* }
|
|
* ```
|
|
*
|
|
* NOTE: A reserved slot follows the persistent storage layout. It moves if state variables are added, removed or
|
|
* reordered, so it fits transient values private to the contract that declares it, not values that several contracts
|
|
* (or several facets of a diamond) must agree on. The value is held at the bitwise complement of the reserved slot,
|
|
* which keeps reservations clear of native `transient` state variables: the compiler allocates those in a separate
|
|
* layout starting at slot 0, while the complements grow down from the top of the transient space.
|
|
*
|
|
* TIP: Consider using this library along with {SlotDerivation}.
|
|
*/
|
|
library TransientSlot {
|
|
<%- for (const { name, type } of it.SLOT_TYPES.filter(({ size }) => size)) { %>
|
|
/**
|
|
* @dev Struct that reserves a slot in the (persistent) storage layout, to hold <%= type == 'address' ? 'an' : 'a' %> <%= type %> in transient storage.
|
|
*
|
|
* The reserved slot is never written to: the value is held in the transient slot whose number is the bitwise
|
|
* complement (`not`) of the reserved slot's number. This delegates the allocation of transient slots to the
|
|
* compiler, which is particularly useful for namespacing them: reserving the slot inside an ERC-7201 namespaced
|
|
* storage struct namespaces the transient slot the same way it namespaces the persistent ones.
|
|
*/
|
|
struct T<%= name %> {
|
|
bytes32 _placeholder;
|
|
}
|
|
|
|
/**
|
|
* @dev UDVT that represents a slot holding <%= type == 'address' ? 'an' : 'a' %> <%= type %>.
|
|
*/
|
|
type <%= name %>Slot is bytes32;
|
|
|
|
/**
|
|
* @dev Cast an arbitrary slot to a <%= name %>Slot.
|
|
*/
|
|
function as<%= name %>(bytes32 slot) internal pure returns (<%= name %>Slot) {
|
|
return <%= name %>Slot.wrap(slot);
|
|
}
|
|
<% } -%>
|
|
<%- for (const { name, type } of it.SLOT_TYPES.filter(({ size }) => size)) { %>
|
|
/**
|
|
* @dev Load the value held at location `slot` in transient storage.
|
|
*/
|
|
function tload(<%= name %>Slot slot) internal view returns (<%= type %> value) {
|
|
assembly ("memory-safe") {
|
|
value := tload(slot)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dev Load the value held in the transient slot reserved by `self`.
|
|
*/
|
|
function tload(T<%= name %> storage self) internal view returns (<%= type %> value) {
|
|
assembly ("memory-safe") {
|
|
// Reservations grow down from the top of the transient space, away from the `transient`
|
|
// variables that the compiler allocates from slot 0.
|
|
value := tload(not(self.slot))
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dev Store `value` at location `slot` in transient storage.
|
|
*/
|
|
function tstore(<%= name %>Slot slot, <%= type %> value) internal {
|
|
assembly ("memory-safe") {
|
|
tstore(slot, value)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dev Store `value` in the transient slot reserved by `self`.
|
|
*/
|
|
function tstore(T<%= name %> storage self, <%= type %> value) internal {
|
|
assembly ("memory-safe") {
|
|
// Reservations grow down from the top of the transient space, away from the `transient`
|
|
// variables that the compiler allocates from slot 0.
|
|
tstore(not(self.slot), value)
|
|
}
|
|
}
|
|
<% } -%>
|
|
}
|