Skip to content

Commit

Permalink
feat: add protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
0xzio committed Jan 27, 2024
1 parent 76ddcd3 commit c9c842a
Show file tree
Hide file tree
Showing 46 changed files with 7,362 additions and 192 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/deploy-hardhat-development.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Deploy to hardhat Development Server

on:
push:
branches:
- 'develop'

jobs:
deploy-hardhat-network:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Copy files to server
uses: easingthemes/ssh-deploy@main
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
ARGS: '-rlgoDzvc -i --delete'
REMOTE_USER: ${{ secrets.REMOTE_USER }}
TARGET: '/home/penx/penx'
SOURCE: './'

- name: Run Node.js service
uses: appleboy/[email protected]
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
whoami
ls -al
cd /home/penx/penx/apps/protocol
npm i --no-package-lock
npm run hardhat:development
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
release_id: ${{ steps.create-release.outputs.result }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 20
- name: get version
run: echo "PACKAGE_VERSION=$(node -p "require('./apps/desktop/package.json').version")" >> $GITHUB_ENV
- name: create release
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"search.followSymlinks": false
"search.followSymlinks": false,
"cSpell.enableFiletypes": ["solidity"]
}
17 changes: 17 additions & 0 deletions apps/protocol/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
node_modules
.env
.DS_Store
.deps
.VSCodeCounter
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

types
test/generated.ts
abi
16 changes: 16 additions & 0 deletions apps/protocol/.mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'
process.env.TS_NODE_FILES = true
module.exports = {
'allow-uncaught': true,
diff: true,
extension: ['ts'],
recursive: true,
reporter: 'spec',
require: ['ts-node/register', 'hardhat/register'], // ['ts-node/register/transpile-only'], (for yarn link <plugin>)
slow: 300,
spec: 'test/**/*.test.ts',
timeout: 20000,
ui: 'bdd',
watch: false,
'watch-files': ['src/**/*.sol', 'test/**/*.ts'],
}
25 changes: 25 additions & 0 deletions apps/protocol/.prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
bracketSpacing: true
plugins:
- "prettier-plugin-solidity"
printWidth: 120
proseWrap: "always"
singleQuote: true
semi: false
tabWidth: 2
trailingComma: "all"

overrides:
- files: "*.sol"
options:
semi: true
compiler: "0.8.17"
singleQuote: false
parser: "solidity-parse"
tabWidth: 4
- files: "*.ts"
options:
importOrder: ["<THIRD_PARTY_MODULES>", "^[./]"]
importOrderParserPlugins: ["typescript"]
importOrderSeparation: true
importOrderSortSpecifiers: true
parser: "typescript"
Empty file added apps/protocol/README.md
Empty file.
30 changes: 30 additions & 0 deletions apps/protocol/contracts/facets/DiamondCutFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

/******************************************************************************\
* Author: Nick Mudge <[email protected]> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
/******************************************************************************/

import { IDiamondCut } from "../interfaces/IDiamondCut.sol";
import { LibDiamond } from "../storage/LibDiamond.sol";

// Remember to add the loupe functions from DiamondLoupeFacet to the diamond.
// The loupe functions are required by the EIP2535 Diamonds standard

contract DiamondCutFacet is IDiamondCut {
/// @notice Add/replace/remove any number of functions and optionally execute
/// a function with delegatecall
/// @param _diamondCut Contains the facet addresses and function selectors
/// @param _init The address of the contract or facet to execute _calldata
/// @param _calldata A function call, including function selector and arguments
/// _calldata is executed with delegatecall on _init
function diamondCut(
FacetCut[] calldata _diamondCut,
address _init,
bytes calldata _calldata
) external override {
LibDiamond.enforceIsContractOwner();
LibDiamond.diamondCut(_diamondCut, _init, _calldata);
}
}
147 changes: 147 additions & 0 deletions apps/protocol/contracts/facets/DiamondLoupeFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
/******************************************************************************\
* Author: Nick Mudge <[email protected]> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
/******************************************************************************/

// The functions in DiamondLoupeFacet MUST be added to a diamond.
// The EIP-2535 Diamond standard requires these functions.

import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import { LibDiamond } from "../storage/LibDiamond.sol";
import { IDiamondLoupe } from "../interfaces/IDiamondLoupe.sol";

contract DiamondLoupeFacet is IDiamondLoupe, IERC165 {
// Diamond Loupe Functions
////////////////////////////////////////////////////////////////////
/// These functions are expected to be called frequently by tools.
//
// struct Facet {
// address facetAddress;
// bytes4[] functionSelectors;
// }
/// @notice Gets all facets and their selectors.
/// @return facets_ Facet
function facets() external override view returns (Facet[] memory facets_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 selectorCount = ds.selectors.length;
// create an array set to the maximum size possible
facets_ = new Facet[](selectorCount);
// create an array for counting the number of selectors for each facet
uint16[] memory numFacetSelectors = new uint16[](selectorCount);
// total number of facets
uint256 numFacets;
// loop through function selectors
for (uint256 selectorIndex; selectorIndex < selectorCount; selectorIndex++) {
bytes4 selector = ds.selectors[selectorIndex];
address facetAddress_ = ds.facetAddressAndSelectorPosition[selector].facetAddress;
bool continueLoop = false;
// find the functionSelectors array for selector and add selector to it
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
if (facets_[facetIndex].facetAddress == facetAddress_) {
facets_[facetIndex].functionSelectors[numFacetSelectors[facetIndex]] = selector;
numFacetSelectors[facetIndex]++;
continueLoop = true;
break;
}
}
// if functionSelectors array exists for selector then continue loop
if (continueLoop) {
continueLoop = false;
continue;
}
// create a new functionSelectors array for selector
facets_[numFacets].facetAddress = facetAddress_;
facets_[numFacets].functionSelectors = new bytes4[](selectorCount);
facets_[numFacets].functionSelectors[0] = selector;
numFacetSelectors[numFacets] = 1;
numFacets++;
}
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
uint256 numSelectors = numFacetSelectors[facetIndex];
bytes4[] memory selectors = facets_[facetIndex].functionSelectors;
// setting the number of selectors
assembly {
mstore(selectors, numSelectors)
}
}
// setting the number of facets
assembly {
mstore(facets_, numFacets)
}
}

/// @notice Gets all the function selectors supported by a specific facet.
/// @param _facet The facet address.
/// @return _facetFunctionSelectors The selectors associated with a facet address.
function facetFunctionSelectors(address _facet) external override view returns (bytes4[] memory _facetFunctionSelectors) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 selectorCount = ds.selectors.length;
uint256 numSelectors;
_facetFunctionSelectors = new bytes4[](selectorCount);
// loop through function selectors
for (uint256 selectorIndex; selectorIndex < selectorCount; selectorIndex++) {
bytes4 selector = ds.selectors[selectorIndex];
address facetAddress_ = ds.facetAddressAndSelectorPosition[selector].facetAddress;
if (_facet == facetAddress_) {
_facetFunctionSelectors[numSelectors] = selector;
numSelectors++;
}
}
// Set the number of selectors in the array
assembly {
mstore(_facetFunctionSelectors, numSelectors)
}
}

/// @notice Get all the facet addresses used by a diamond.
/// @return facetAddresses_
function facetAddresses() external override view returns (address[] memory facetAddresses_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 selectorCount = ds.selectors.length;
// create an array set to the maximum size possible
facetAddresses_ = new address[](selectorCount);
uint256 numFacets;
// loop through function selectors
for (uint256 selectorIndex; selectorIndex < selectorCount; selectorIndex++) {
bytes4 selector = ds.selectors[selectorIndex];
address facetAddress_ = ds.facetAddressAndSelectorPosition[selector].facetAddress;
bool continueLoop = false;
// see if we have collected the address already and break out of loop if we have
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
if (facetAddress_ == facetAddresses_[facetIndex]) {
continueLoop = true;
break;
}
}
// continue loop if we already have the address
if (continueLoop) {
continueLoop = false;
continue;
}
// include address
facetAddresses_[numFacets] = facetAddress_;
numFacets++;
}
// Set the number of facet addresses in the array
assembly {
mstore(facetAddresses_, numFacets)
}
}

/// @notice Gets the facet address that supports the given selector.
/// @dev If facet is not found return address(0).
/// @param _functionSelector The function selector.
/// @return facetAddress_ The facet address.
function facetAddress(bytes4 _functionSelector) external override view returns (address facetAddress_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
facetAddress_ = ds.facetAddressAndSelectorPosition[_functionSelector].facetAddress;
}

// This implements ERC-165.
function supportsInterface(bytes4 _interfaceId) external override view returns (bool) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
return ds.supportedInterfaces[_interfaceId];
}
}
20 changes: 20 additions & 0 deletions apps/protocol/contracts/interfaces/IDiamond.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/******************************************************************************\
* Author: Nick Mudge <[email protected]> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
/******************************************************************************/

interface IDiamond {
enum FacetCutAction {Add, Replace, Remove}
// Add=0, Replace=1, Remove=2

struct FacetCut {
address facetAddress;
FacetCutAction action;
bytes4[] functionSelectors;
}

event DiamondCut(FacetCut[] _diamondCut, address _init, bytes _calldata);
}
24 changes: 24 additions & 0 deletions apps/protocol/contracts/interfaces/IDiamondCut.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/******************************************************************************\
* Author: Nick Mudge <[email protected]> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
/******************************************************************************/

import { IDiamond } from "./IDiamond.sol";

interface IDiamondCut is IDiamond {

/// @notice Add/replace/remove any number of functions and optionally execute
/// a function with delegatecall
/// @param _diamondCut Contains the facet addresses and function selectors
/// @param _init The address of the contract or facet to execute _calldata
/// @param _calldata A function call, including function selector and arguments
/// _calldata is executed with delegatecall on _init
function diamondCut(
FacetCut[] calldata _diamondCut,
address _init,
bytes calldata _calldata
) external;
}
38 changes: 38 additions & 0 deletions apps/protocol/contracts/interfaces/IDiamondLoupe.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/******************************************************************************\
* Author: Nick Mudge <[email protected]> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
/******************************************************************************/

// A loupe is a small magnifying glass used to look at diamonds.
// These functions look at diamonds
interface IDiamondLoupe {
/// These functions are expected to be called frequently
/// by tools.

struct Facet {
address facetAddress;
bytes4[] functionSelectors;
}

/// @notice Gets all facet addresses and their four byte function selectors.
/// @return facets_ Facet
function facets() external view returns (Facet[] memory facets_);

/// @notice Gets all the function selectors supported by a specific facet.
/// @param _facet The facet address.
/// @return facetFunctionSelectors_
function facetFunctionSelectors(address _facet) external view returns (bytes4[] memory facetFunctionSelectors_);

/// @notice Get all the facet addresses used by a diamond.
/// @return facetAddresses_
function facetAddresses() external view returns (address[] memory facetAddresses_);

/// @notice Gets the facet that supports the given selector.
/// @dev If facet is not found return address(0).
/// @param _functionSelector The function selector.
/// @return facetAddress_ The facet address.
function facetAddress(bytes4 _functionSelector) external view returns (address facetAddress_);
}
Loading

0 comments on commit c9c842a

Please sign in to comment.