Skip to content

Latest commit

 

History

History
70 lines (60 loc) · 2.35 KB

README.md

File metadata and controls

70 lines (60 loc) · 2.35 KB

foundry-diamonds

This is a pure foundry template for the diamonds (EIP-2535). Codegen is used to generate a solidity code for the diamondCuts

Note: There is another diamonds template for foundry which uses hardhat

Installation

  • Clone this repo
  • Install dependencies:
$ forge update
  • build
forge build

Deployment:

forge script script/Counter.s.sol:CounterScript  --rpc-url $RPC_URL --broadcast -i 1 --sender $YOUR_ADDRESS

Test:

forge test

Generating diamondCuts:

Unfortunately solidity doesn't support iteration through the contrac functions, so you will need manually write each selector. But you can use the solidity code generator that I've written for this template:

  python3 utils/facet_cut_gen.py -f [FACETS_NAMES ...]

For example,

python3 utils/facet_cut_gen.py -f CounterFacet DecrementFacet

will generate this code:

        // auto-generated by utils/facet_cut_gen.py
        Diamond diamond = Diamond(DIAMOND_ADDRESS);
        IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](2);

        CounterFacet counterFacet = new CounterFacet();
        bytes4[] memory counterFacetfunctionSelectors = new bytes4[](3);
        counterFacetfunctionSelectors[0] = CounterFacet.getNumber.selector;
        counterFacetfunctionSelectors[1] = CounterFacet.increment.selector;
        counterFacetfunctionSelectors[2] = CounterFacet.setNumber.selector;
        cut[0] = IDiamondCut.FacetCut({
                facetAddress: address(counterFacet),
                action: IDiamondCut.FacetCutAction.Add,
                functionSelectors: counterFacetfunctionSelectors
        });

        DecrementFacet decrementFacet = new DecrementFacet();
        bytes4[] memory decrementFacetfunctionSelectors = new bytes4[](1);
        decrementFacetfunctionSelectors[0] = DecrementFacet.decrement.selector;
        cut[1] = IDiamondCut.FacetCut({
                facetAddress: address(decrementFacet),
                action: IDiamondCut.FacetCutAction.Add,
                functionSelectors: decrementFacetfunctionSelectors
        });


        DiamondCutFacet(address(diamond)).diamondCut(
            cut,        //array of of cuts
            address(0), //initializer address
            ""          //initializer data
        );