Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Jul 10, 2024
1 parent afbb7e4 commit 1fd689a
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 275 deletions.
81 changes: 46 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 43 additions & 41 deletions src/TinyENS.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ pragma solidity ^0.8.21;

/// @notice Interface for TinyENS.
interface ITinyENS {
/// @notice Register a new ENS name and link it to an address.
/// @param name The ENS name to register.
function register(string memory name) external;
/// @notice Register a new ENS name and link it to an address.
/// @param name The ENS name to register.
function register(string memory name) external;

/// @notice Update the ENS name linked to an address.
/// @param newName The new ENS name.
function update(string memory newName) external;
/// @notice Update the ENS name linked to an address.
/// @param newName The new ENS name.
function update(string memory newName) external;

/// @notice Resolve the address associated with a given ENS name.
/// @param name The ENS name to resolve.
/// @return The address associated with the ENS name.
function resolve(string memory name) external view returns (address);
/// @notice Resolve the address associated with a given ENS name.
/// @param name The ENS name to resolve.
/// @return The address associated with the ENS name.
function resolve(string memory name) external view returns (address);

/// @notice Reverse resolve an address to its associated ENS name.
/// @param targetAddress The target address to reverse resolve.
/// @return The ENS name associated with the address.
function reverse(address targetAddress) external view returns (string memory);
/// @notice Reverse resolve an address to its associated ENS name.
/// @param targetAddress The target address to reverse resolve.
/// @return The ENS name associated with the address.
function reverse(address targetAddress) external view returns (string memory);
}

/// @title Tiny Ethereum Name Service
Expand All @@ -28,38 +28,40 @@ interface ITinyENS {
/// Ethereum addresses like '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045' and support the reverse
/// resolution.
contract TinyENS is ITinyENS {
/// @notice Thrown when trying to register a name already registered.
error AlreadyRegistered();
/// @notice Thrown when trying to register a name already registered.
error AlreadyRegistered();

/// @notice Map ENS names to addresses.
mapping(string => address) private registry;
/// @notice Map ENS names to addresses.
mapping(string => address) private registry;

/// @notice Map addresses to ENS names.
mapping(address => string) private reverseRegistry;
/// @notice Map addresses to ENS names.
mapping(address => string) private reverseRegistry;

modifier notRegistered(string memory name) {
if (registry[name] != address(0)) revert AlreadyRegistered();
_;
}
modifier notRegistered(string memory name) {
if (registry[name] != address(0)) revert AlreadyRegistered();
_;
}

function register(string memory name) public notRegistered(name) {
registry[name] = msg.sender;
reverseRegistry[msg.sender] = name;
}
function register(string memory name) public notRegistered(name) {
registry[name] = msg.sender;
reverseRegistry[msg.sender] = name;
}

function update(string memory newName) external notRegistered(newName) {
// Unlink the old name and the address.
string memory oldName = reverseRegistry[msg.sender];
registry[oldName] = address(0);
// Register the new name.
register(newName);
}
function update(string memory newName) external notRegistered(newName) {
// Unlink the old name and the address.
string memory oldName = reverseRegistry[msg.sender];
registry[oldName] = address(0);
// Register the new name.
register(newName);
}

function resolve(string memory name) external view returns (address) {
return registry[name];
}
function resolve(string memory name) external view returns (address) {
return registry[name];
}

function reverse(address targetAddress) external view returns (string memory) {
return reverseRegistry[targetAddress];
}
function reverse(
address targetAddress
) external view returns (string memory) {
return reverseRegistry[targetAddress];
}
}
74 changes: 37 additions & 37 deletions src/experiments/LastCallJackpot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pragma solidity ^0.8.21;

/// @notice Interface for TinyENS.
interface ILastCallJackpot {
/// @notice Call the contract and attempt to win the jackpot.
function call() external payable;
/// @notice Call the contract and attempt to win the jackpot.
function call() external payable;
}

/// @title A game where the last caller, before a 10-block inactivity period, wins the entire contract's ETH balance.
Expand All @@ -14,40 +14,40 @@ interface ILastCallJackpot {
/// If the contract isn't called for 10 blocks, the last caller gets the entire ETH balance.
/// How might this game unfold and end? Describe your thinking.
contract LastCallJackpot is ILastCallJackpot {
/// @notice Track the block number when the last call occurred.
uint256 public lastBlock;
/// @notice Store the address of the last caller.
address public lastCaller;

/// @notice Log each call made to the contract.
event Called(address indexed caller, uint256 blockNumber);
/// @notice Log the transfer of the jackpot amount to the winner.
event WinnerPaid(address indexed winner, uint256 amount);

constructor() payable {
require(msg.value == 1000 ether, "Fund the contract with 1000 ETH");
lastBlock = block.number;
}

function call() external payable {
require(msg.value == 1 ether, "Call the contract with 1 ETH");

if (block.number >= lastBlock + 10) {
// Pay the winner.
uint256 balance = address(this).balance;
address winner = lastCaller;

// Update state before external call
lastBlock = block.number;
lastCaller = msg.sender;

payable(winner).transfer(balance);
emit WinnerPaid(winner, balance);
} else {
// Update the last block and caller.
lastBlock = block.number;
lastCaller = msg.sender;
emit Called(msg.sender, block.number);
}
/// @notice Track the block number when the last call occurred.
uint256 public lastBlock;
/// @notice Store the address of the last caller.
address public lastCaller;

/// @notice Log each call made to the contract.
event Called(address indexed caller, uint256 blockNumber);
/// @notice Log the transfer of the jackpot amount to the winner.
event WinnerPaid(address indexed winner, uint256 amount);

constructor() payable {
require(msg.value == 1000 ether, 'Fund the contract with 1000 ETH');
lastBlock = block.number;
}

function call() external payable {
require(msg.value == 1 ether, 'Call the contract with 1 ETH');

if (block.number >= lastBlock + 10) {
// Pay the winner.
uint256 balance = address(this).balance;
address winner = lastCaller;

// Update state before external call
lastBlock = block.number;
lastCaller = msg.sender;

payable(winner).transfer(balance);
emit WinnerPaid(winner, balance);
} else {
// Update the last block and caller.
lastBlock = block.number;
lastCaller = msg.sender;
emit Called(msg.sender, block.number);
}
}
}
Loading

0 comments on commit 1fd689a

Please sign in to comment.