Skip to content

Commit

Permalink
setter and getter facets
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamChndrvnshi committed May 5, 2022
1 parent 05b6e6b commit 3d23d94
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions abi/DiamondLoupeFacet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"type":"function","name":"facetAddress","constant":true,"stateMutability":"view","payable":false,"gas":1100000,"inputs":[{"type":"bytes4","name":"_functionSelector"}],"outputs":[{"type":"address","name":"facetAddress_"}]},{"type":"function","name":"facetAddresses","constant":true,"stateMutability":"view","payable":false,"gas":1100000,"inputs":[],"outputs":[{"type":"address[]","name":"facetAddresses_"}]},{"type":"function","name":"facetFunctionSelectors","constant":true,"stateMutability":"view","payable":false,"gas":1100000,"inputs":[{"type":"address","name":"_facet"}],"outputs":[{"type":"bytes4[]","name":"facetFunctionSelectors_"}]},{"type":"function","name":"facets","constant":true,"stateMutability":"view","payable":false,"gas":1100000,"inputs":[],"outputs":[{"type":"tuple[]","name":"facets_","components":[{"type":"address","name":"facetAddress"},{"type":"bytes4[]","name":"functionSelectors"}]}]},{"type":"function","name":"supportsInterface","constant":true,"stateMutability":"view","payable":false,"gas":1100000,"inputs":[{"type":"bytes4","name":"_interfaceId"}],"outputs":[{"type":"bool"}]}]
1 change: 1 addition & 0 deletions abi/Facet1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"type":"function","name":"setStr1","constant":false,"payable":false,"gas":1100000,"inputs":[{"type":"string","name":"str"}],"outputs":[]},{"type":"function","name":"setStr2","constant":false,"payable":false,"gas":1100000,"inputs":[],"outputs":[]}]
1 change: 1 addition & 0 deletions abi/Facet2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"type":"function","name":"getStr1","constant":true,"stateMutability":"view","payable":false,"gas":1100000,"inputs":[],"outputs":[{"type":"string"}]},{"type":"function","name":"getStr2","constant":true,"stateMutability":"view","payable":false,"gas":1100000,"inputs":[],"outputs":[{"type":"string"}]}]
1 change: 1 addition & 0 deletions abi/OwnershipFacet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"type":"event","anonymous":false,"name":"OwnershipTransferred","inputs":[{"type":"address","name":"previousOwner","indexed":true},{"type":"address","name":"newOwner","indexed":true}]},{"type":"function","name":"owner","constant":true,"stateMutability":"view","payable":false,"gas":1100000,"inputs":[],"outputs":[{"type":"address","name":"owner_"}]},{"type":"function","name":"transferOwnership","constant":false,"payable":false,"gas":1100000,"inputs":[{"type":"address","name":"_newOwner"}],"outputs":[]}]
18 changes: 18 additions & 0 deletions contracts/facets/Facet1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { AppStorageOpen, LibCommon } from "../libraries/LibCommon.sol";

contract Facet1 {

function setStr1(string memory str) external {
AppStorageOpen storage appStore = LibCommon.appStorage();
appStore.str1 = str;
}

function setStr2() external {
AppStorageOpen storage appStore = LibCommon.appStorage();
appStore.str2 = "store string 2";
}

}
18 changes: 18 additions & 0 deletions contracts/facets/Facet2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { AppStorageOpen, LibCommon } from "../libraries/LibCommon.sol";

contract Facet2 {

function getStr1() external view returns(string memory){
AppStorageOpen memory appStore = LibCommon.appStorage();
return appStore.str1;
}

function getStr2() external view returns(string memory){
AppStorageOpen memory appStore = LibCommon.appStorage();
return appStore.str2;
}

}
4 changes: 3 additions & 1 deletion contracts/facets/Test1Facet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pragma solidity ^0.8.0;
contract Test1Facet {
event TestEvent(address something);

function test1Func1() external {}
function test1Func1() external pure returns(string memory){
return "this works";
}

function test1Func2() external {}

Expand Down
13 changes: 13 additions & 0 deletions contracts/libraries/LibCommon.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./storage.sol";

library LibCommon {

function appStorage() internal pure returns (AppStorageOpen storage appStore) {
assembly {
appStore.slot := 0
}
}
}
7 changes: 7 additions & 0 deletions contracts/libraries/storage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

struct AppStorageOpen {
string str1;
string str2;
}
2 changes: 1 addition & 1 deletion diamondAddress.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "diamond": "0x25b5A1874F31E4d329e3Fe21F9De9628d9804465"}
{ "diamond": "0x10BCE52147909587F190D12E8e08e6BDc9e7f6F6"}
14 changes: 12 additions & 2 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* global ethers */
/* eslint prefer-const: "off" */
const { writeFileSync } = require("fs");
const { writeFileSync, mkdirSync } = require("fs");
const { getSelectors, FacetCutAction } = require('./libraries/diamond.js')

async function deployDiamond () {
mkdirSync("abi", { recursive: true });
const accounts = await ethers.getSigners()
const contractOwner = accounts[0]

Expand Down Expand Up @@ -32,7 +33,9 @@ async function deployDiamond () {
console.log('Deploying facets')
const FacetNames = [
'DiamondLoupeFacet',
'OwnershipFacet'
'OwnershipFacet',
'Facet1',
'Facet2',
]
const cut = []
for (const FacetName of FacetNames) {
Expand All @@ -45,6 +48,7 @@ async function deployDiamond () {
action: FacetCutAction.Add,
functionSelectors: getSelectors(facet)
})
createAbiJSON(facet, FacetName)
}

// upgrade diamond with facets
Expand Down Expand Up @@ -77,4 +81,10 @@ if (require.main === module) {
})
}

/// CREATE ABI OF CONTRACTS
function createAbiJSON(artifact, filename) {
const data = JSON.parse(artifact.interface.format("json"));
writeFileSync(`${__dirname}/../abi/${filename}.json`, JSON.stringify(data));
}

exports.deployDiamond = deployDiamond

0 comments on commit 3d23d94

Please sign in to comment.