#1 Simple Registry

https://github.com/yieldprotocol/mentorship2022/issues/1

Objectives

  1. Users(addresses) can claim a name, which is recorded on-chain.

  2. Each name can only have one owner. (1-to-1 mapping)

  3. A user can own multiple names (1-to-many mapping)

  4. User can relinquish their ownership of a name -> therefore, available for acquisition.

Base Code

SimpleNameRegister.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

/**
@title An on-chain name registry
@author Calnix
@notice A registed name can be released, availing to be registered by another user
*/
contract SimpleNameRegister {
    
    /// @notice Map a name to an address to identify current holder 
    mapping (string => address) public holder;    

    /// @notice Emit event when a name is registered
    event Register(address indexed holder, string name);

    /// @notice Emit event when a name is released
    event Release(address indexed holder, string name);

    /// @notice User can register an available name
    /// @param name The string to register
    function register(string calldata name) external {
        require(holder[name] == address(0), "Already registered!");
        holder[name] = msg.sender;
        emit Register(msg.sender, name);
    }

    /// @notice Holder can release a name, making it available
    /// @param name The string to release
    function release(string calldata name) external {
        require(holder[name] == msg.sender, "Not your name!");
        delete holder[name];
        emit Release(msg.sender, name);
    }
}

1. Mapping associating names to their respective owner's address

  • initialized as a public state variable.

  • solidity will automatically create a getter function for it, which we can use to pass a name as parameter to check ownership.

  • If there is no owner, the address returned will be 0x0000000000000000000000000000000000000000

2. Function to allow a user to register their ownership of a name

  • require statement checks if the name passed is available to be claimed.

  • If available, passed name is mapped to the address of the msg.sender via the mapping holder

  • event Registered emitted each time a name is registered.

3. Function to allow a user to relinquish their ownership of a name

  • require statement checks if the name passed is indeed registered to the function caller (msg.sender)

  • If so, we reset the mapped address of the name to the zero address.

    • mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to default values.

    • the default value for address type will be 0x0000000000000000000000000000000000000000 or address(0)

  • event Releaseemitted each time a name is relinquished.

Events

Events allow us to “print” information on the blockchain in a way that is more searchable and gas efficient than just saving to public storage variables in our smart contracts.

Problem:

  • A single address could be the owner of multiple names.

  • It would be useful if the end-user could simply supply their address to check which are the names registered to them.

  • Achieving this one to many structure on-chain would not be gas efficient.

Solution: Using Events

  • Run a background task that subscribes to events from the contract.

  • This background task will listen to events as they are emitted and track the list of addresses for each owner via some centralized database.

  • Website front-end can read from this database and reflect accordingly.

Testing

Testing in Solidity is somewhat different from other frameworks like brownie, as they would be done in Solidity.

If the test function reverts, the test fails, otherwise it passes.

Essentially we deploy a test contract (0xb4c79daB8f259C7Aee6E5b2Aa729821864227e84) containing our test functions.

function setUp()

The setup function is invoked before each test case is run. Serves to setup the necessary variables and conditions for your test functions to operate.

  • deploy a fresh instance of SimpleNameRegister before each test function is ran via simpleNameRegister = new SimpleNameRegister()

  • address adversary will be used in test 3 & 4

Each test is run as independent cases -> changes made in a prior test function will not spill out of scope into a following test function (See more: https://calnix.gitbook.io/solidity-lr/foundry/testing#to-show-that-there-is-no-spillover)

Linear vs State Inheritance Approach

https://calnix.gitbook.io/solidity-lr/yield-mentorship-2022/simple-registry-1/state-inheritance-testing

Deployment

  • Forge can deploy only one contract at a time.

  • Solidity files may contain multiple contracts. :MyContract above specifies which contract to deploy from the src/MyContract.sol file

Verifying

Set --num-of-optimizations 200

If not set on deployment, foundry defaults to 200. So make sure you pass --num-of-optimizations 200 if you left the default compilation settings

Check verification

Running the above three commands manually, each time can be bothersome. To that end, create a new file 'MakeFile' in the project root directory if one does not exist.

Now you will only need to run:

  • make deploy

  • make verify

  • make verify-check

Last updated