Skip to content

Instantly share code, notes, and snippets.

View CJ42's full-sized avatar

Jean Cvllr CJ42

View GitHub Profile
@CJ42
CJ42 / FontsSettings.sol
Created January 23, 2023 17:41
example to show how to use `enum` to setup `immutable` variables on deployment
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
abstract contract FontsSettings {
enum Font { Roboto, CourierNew, ComicSans, Helvetica, TimesNewRoman, Garamond }
Font constant fontHeader = Font.Helvetica;
Font immutable fontBody;
constant immutable (assignement on declaration) immutable (assignement in constructor)
blockhash(uint blocknumber) return (bytes32)
block.basefee
block.chainid
block.coinbase
block.difficulty
block.gaslimit
block.number
block.timestamp
constant immutable
bytesN
intN / uintN
bytes
string
bool
address
contract (using new)
struct
@CJ42
CJ42 / MyContractWithoutConstructor.md
Created January 18, 2023 19:38
Understanding the creation code of a basic contract that does not contain any bytecode

Note: the creation and bytecode of this contract was compiled using solc version 0.8.15 with the optimiser on and the number of runs set to 1,000.

// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

contract MyContract {
    string internal _myName;

 function setName(string memory name) public {

Note: the creation and bytecode of this contract was compiled using solc version 0.8.15 with the optimiser on and the number of runs set to 1,000.

Creation Code

0x**608060405234801561001057600080fd5b5060405161062938038061062983398101604081905261002f91610058565b600061003b82826101b0565b505061026f565b634e487b7160e01b600052604160045260246000fd5b6000602080838503121561006b57600080fd5b82516001600160401b038082111561008257600080fd5b818501915085601f83011261009657600080fd5b8151818111156100a8576100a8610042565b604051601f8201601f19908116603f011681019083821181831017156100d0576100d0610042565b8160405282815288868487010111156100e857600080fd5b600093505b8284101561010a57848401860151818501870152928501926100ed565b8284111561011b5760008684830101525b98975050505050505050565b600181811c9082168061013b57607f821691505b60208210810361015b57634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156101ab57600081815260208120601f850160051c810160208610156101885750805b601f850160051c820191505b818110156101a757828155600101610194565b

@CJ42
CJ42 / MyContract.sol
Created January 17, 2023 17:28
basic example to show the difference of the difference between the creation and runtime code
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
contract MyContract {
string internal _myName;
constructor(string memory initialName) {
_myName = initialName;
}
PUSH1 40
MSTORE
CALLVALUE
DUP1
ISZERO
PUSH1 0f
JUMPI
; ...
JUMPDEST
POP
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import {BytesLib} from "./BytesLib.sol";
import "hardhat/console.sol";
contract Immutable {
uint48 immutable private A;
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Constant {
uint48 constant private A = 25;
constructor() {
uint256 readConstantA = A + 2;
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Immutables {
uint48 immutable private A = 0x112233445566;
bytes6 immutable private B;
constructor() {
B = 0xaabbccddeeff;