Skip to content
/ foundry-diamonds Public template

Foundry template for the diamonds (EIP-2535)

Notifications You must be signed in to change notification settings

Yhtiyar/foundry-diamonds

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
        );

About

Foundry template for the diamonds (EIP-2535)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published