[TWIL] Week of July 14, 2024

[TWIL] Week of July 14, 2024

·

2 min read

Hello builders! This week, my focus has been on modifying all our smart contracts to integrate a new multi-sig contract. While I didn't encounter any new lessons to write about, I thought I'd share a couple of Solidity facts I've learned in the past.

Create vs. Create2

In Solidity, create and create2 are opcodes used for deploying smart contracts. Here's a quick breakdown of their differences:

  • Create: Requires the deployer's address and nonce.

  • Create2: Requires 0xff, the deployer's address, a salt, and the bytecode of the smart contract.

Introduced in ERC-1014, create2 allows for deterministic contract address generation. This means that if you provide the same arguments to create2 twice, you'll get the same contract address both times. This deterministic feature isn't possible with create since it depends on the nonce, which is unique to each transaction made by the sender on the blockchain.

Here are some additional insights:

  • When you instantiate a smart contract using the new keyword in Solidity, you're using create under the hood.

  • State channels often use counterfactual smart contracts, which rely on create2.

  • The 0xFF constant in create2 is used to differentiate it from the create opcode.

I'll dive deeper into state channels and counterfactual smart contracts in a future article. For now, let's move on to another Solidity tip.

Solidity Constant Variable Getter

In Solidity, you can initialize a constant variable like this:

contract Example {
    uint96 public constant K = 10;
}

But what if you need to create an interface IExample for the Example contract? How do you write the Solidity code in the interface so that other contracts using IExample can access it?

If you guessed it involves a single line of code, you'd be right. You just need to add a function declaration:

interface IExample {
    function K() external view returns (uint96);
}

And, of course, you can call this from another contract like so:

contract Caller {
    IExample example;

    constructor(IExample _example) {
        example = _example;
    }

    function getK() public view returns (uint96) {
        return example.K();
    }
}

Public state variables work the same way as constants in this regard.

That's it for this week! I've shared some random facts about Solidity, and I hope they help you in some way. Happy hacking! ☕️