Skip to content

Commit

Permalink
⚡ Smol optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
z0r0z committed Feb 21, 2024
1 parent 35fdf9b commit 6111627
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
39 changes: 19 additions & 20 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
IETest:testCommandDepositETH() (gas: 188523)
IETest:testCommandSendETH() (gas: 71778)
IETest:testCommandSendETHRawAddr() (gas: 66606)
IETest:testCommandStakeETH() (gas: 184060)
IETest:testCommandSwapDAI() (gas: 160386)
IETest:testCommandSwapETH() (gas: 164553)
IETest:testCommandSwapForETH() (gas: 163723)
IETest:testCommandSwapUSDC() (gas: 203643)
IETest:testCommandSwapUSDCForWBTC() (gas: 205482)
IETest:testCommandUnstakeETH() (gas: 312292)
IETest:testCommandWithdrawETH() (gas: 314715)
IETest:testDeploy() (gas: 2922278)
IETest:testENSNameOwnership() (gas: 44499)
IETest:testIENameSetting() (gas: 8186)
IETest:testPreviewCommandSendDecimals() (gas: 100903)
IETest:testPreviewCommandSendUSDC() (gas: 74256)
IETest:testPreviewSend() (gas: 53679)
IETest:testPreviewSendCommand() (gas: 64493)
IETest:testCommandDepositETH() (gas: 189229)
IETest:testCommandSendETH() (gas: 72512)
IETest:testCommandSendETHRawAddr() (gas: 66584)
IETest:testCommandStakeETH() (gas: 184578)
IETest:testCommandSwapDAI() (gas: 169938)
IETest:testCommandSwapETH() (gas: 196652)
IETest:testCommandSwapForETH() (gas: 173275)
IETest:testCommandSwapUSDC() (gas: 204597)
IETest:testCommandSwapUSDCForWBTC() (gas: 206144)
IETest:testCommandUnstakeETH() (gas: 313639)
IETest:testCommandWithdrawETH() (gas: 316149)
IETest:testDeploy() (gas: 2902560)
IETest:testENSNameOwnership() (gas: 45255)
IETest:testIENameSetting() (gas: 8208)
IETest:testPreviewCommandSendDecimals() (gas: 102437)
IETest:testPreviewCommandSendUSDC() (gas: 75034)
IETest:testPreviewSend() (gas: 54413)
IETest:testPreviewSendCommandRawAddr() (gas: 59337)
IETest:testPreviewSendRawAddr() (gas: 29412)
IETest:testPreviewSendRawAddr() (gas: 29390)
NAMITest:testFailRegister() (gas: 9392)
NAMITest:testRegister() (gas: 55699)
NAMITest:testRegister() (gas: 56425)
10 changes: 5 additions & 5 deletions src/IE.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ contract IE {
/// @dev Bad math.
error Overflow();

/// @dev Caller fails.
error Unauthorized();

/// @dev 0-liquidity.
error InvalidSwap();

Expand Down Expand Up @@ -140,7 +137,7 @@ contract IE {
1461446703485210103287273052203988822378723970341;

/// @dev The NAMI naming system on Arbitrum.
INames internal constant NAMI = INames(0x00000000abf9C10002296091cb47b6662bCf00d3);
INames internal constant NAMI = INames(0x000000006641B4C250AEA6B62A1e0067D300697a);

/// ========================== STORAGE ========================== ///

Expand Down Expand Up @@ -368,7 +365,10 @@ contract IE {
}
if (amount0Delta <= 0 && amount1Delta <= 0) revert InvalidSwap();
(address pool, bool zeroForOne) = _computePoolAddress(tokenIn, tokenOut);
if (msg.sender != pool) revert Unauthorized(); // Only pair pool can call.
assembly ("memory-safe") {
// Only pair pool can call.
if iszero(eq(caller(), pool)) { revert(codesize(), 0x00) }
}
if (ETHIn) {
_wrapETH(uint256(zeroForOne ? amount0Delta : amount1Delta));
} else {
Expand Down
41 changes: 27 additions & 14 deletions src/NAMI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.19;

/// @title NANI ARBITRUM MAGIC INVOICE (NAMI)
/// @title NANI ARBITRUM MAGIC INVOICING (NAMI)
/// @notice A contract for managing ENS domain name ownership and resolution on Arbitrum L2.
/// @dev Provides functions for registering names, verifying ownership, and resolving addresses.
/// @dev Provides logic for registering names, verifying ownership, and resolving addresses.
/// @author nani.eth (https://github.com/NaniDAO/ie)
/// @custom:version 1.0.0
contract NAMI {
Expand All @@ -15,8 +15,16 @@ contract NAMI {

/// =========================== EVENTS =========================== ///

/// @dev Logs the registration of a name to an owner.
event Registered(address indexed owner, bytes32 indexed node);
/// @dev Logs the registration of a name node into ownership.
event Registered(bytes32 indexed node, Ownership ownership);

/// ========================== STRUCTS ========================== ///

/// @dev The name node ownership information struct.
struct Ownership {
address owner;
bool subnode;
}

/// =========================== ENUMS =========================== ///

Expand Down Expand Up @@ -65,7 +73,7 @@ contract NAMI {
bytes1[] internal _idnamap;

/// @dev Internal mapping of registered node owners.
mapping(bytes32 node => address) internal _owners;
mapping(bytes32 node => Ownership) internal _owners;

/// ======================== CONSTRUCTOR ======================== ///

Expand All @@ -83,7 +91,7 @@ contract NAMI {

/// ====================== ENS VERIFICATION ====================== ///

/// @dev Returns ENS name ownership details.
/// @dev Returns ENS name ownership information.
function whatIsTheAddressOf(string calldata name)
public
view
Expand All @@ -92,7 +100,7 @@ contract NAMI {
{
_node = _namehash(string(abi.encodePacked(name, ".eth")));
_owner = owner(_node);
_receiver = _owner;
_receiver = _owner; // On L2, owner receives for simplicity.
}

/// @dev Computes an ENS domain namehash.
Expand Down Expand Up @@ -132,35 +140,40 @@ contract NAMI {

/// ======================== REGISTRATION ======================== ///

/// @dev Registers a new name under an owner. ENS L1 node must be bridged.
/// @dev Registers a name node under an owner. ENS L1 node must be bridged.
function register(address _owner, bytes32 _node) public payable virtual {
if (!isOwner(_owner, _node)) revert Unregistered();
emit Registered(_owners[_node] = _owner, _node);
emit Registered(_node, _owners[_node] = Ownership(_owner, false));
}

/// @dev Registers a new subname under an owner. Only the DAO may call this function.
/// @dev Registers a name subnode under an owner. Only the DAO may call this function.
function registerSub(address _owner, string calldata _subname) public payable virtual {
assembly ("memory-safe") {
if iszero(eq(caller(), DAO)) { revert(codesize(), 0x00) } // Optimized for repeat.
}
bytes32 subnode = _namehash(string(abi.encodePacked(_subname, ".nani.eth")));
emit Registered(_owners[subnode] = _owner, subnode);
emit Registered(subnode, _owners[subnode] = Ownership(_owner, true)); // Yay.
}

/// ====================== OWNERSHIP LOGIC ====================== ///

/// @dev Returns the registered owner of a given ENS L1 node. Must be bridged.
/// note: Alternatively, NAMI provides subdomains issued under `nani.eth` node.
function owner(bytes32 _node) public view virtual returns (address _owner) {
_owner = _owners[_node];
if (_owner == address(0) || !isOwner(_owner, _node)) revert Unregistered();
Ownership storage ownership = _owners[_node];
if (ownership.owner == address(0)) revert Unregistered();
if (ownership.subnode) return ownership.owner;
if (isOwner(ownership.owner, _node)) return ownership.owner;
else revert Unregistered(); // Reverts for safety measure.
}

/// @dev Checks if an address is the owner of a given ENS L1 node represented as `l2Token`.
/// note: NAMI operates under the assumption that the proper owner-receiver holds majority.
function isOwner(address _owner, bytes32 _node) public view virtual returns (bool) {
(, address l2Token) = predictDeterministicAddresses(_node);
return IToken(l2Token).balanceOf(_owner) > (IToken(l2Token).totalSupply() / 2);
unchecked {
return IToken(l2Token).balanceOf(_owner) > (IToken(l2Token).totalSupply() / 2);
}
}

/// @dev Returns the deterministic create2 addresses for ENS node tokens on L1 & L2.
Expand Down

0 comments on commit 6111627

Please sign in to comment.