-
Notifications
You must be signed in to change notification settings - Fork 14
Fix fallback and receive functions in Diamond contract #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
652669d
Clean deploy file
zguesmi 9a75aa6
Fix diff
zguesmi 4c74150
Fix coverage task
zguesmi 9e296bd
Clean
zguesmi dcd1030
Deploy on Arbitrum sepolia
zguesmi 9b05d83
Add .env template
zguesmi a84ce1e
Copy Diamond contract source code
zguesmi 0607c5b
Remove diamond from dependency compiler config
zguesmi 625798d
Fix test
zguesmi 0562f98
Adapt Diamond contract
zguesmi 368c297
Clean poxy scripts
zguesmi d378b89
Merge branch 'feature/diamond' into feature/diamond-migration-part2
zguesmi 673c789
Remove deployment file
zguesmi afd3bf8
Update changelog
zguesmi 940478e
Add comment
zguesmi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
PRIVATE_KEY= | ||
ARBISCAN_API_KEY= | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
//*************************************************************************************\ | ||
//* Adapted from Nick Mudge <nick@perfectabstractions.com> (https://twitter.com/mudgen) | ||
//* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535 | ||
//* | ||
//* Implementation of a diamond. | ||
//*************************************************************************************/ | ||
|
||
// Diamond proxy implementation adapted from Mudgen's to re-direct | ||
// `receive` and `fallback` calls to the implementations in facets. | ||
|
||
import { LibDiamond } from "@mudgen/diamond-1/contracts/libraries/LibDiamond.sol"; | ||
import { IDiamondCut } from "@mudgen/diamond-1/contracts/interfaces/IDiamondCut.sol"; | ||
import { IDiamondLoupe } from "@mudgen/diamond-1/contracts/interfaces/IDiamondLoupe.sol"; | ||
import { IERC173 } from "@mudgen/diamond-1/contracts/interfaces/IERC173.sol"; | ||
import { IERC165} from "@mudgen/diamond-1/contracts/interfaces/IERC165.sol"; | ||
|
||
// When no function exists for function called | ||
error FunctionNotFound(bytes4 _functionSelector); | ||
|
||
// This is used in diamond constructor | ||
// more arguments are added to this struct | ||
// this avoids stack too deep errors | ||
struct DiamondArgs { | ||
address owner; | ||
address init; | ||
bytes initCalldata; | ||
} | ||
|
||
contract Diamond { | ||
|
||
constructor(IDiamondCut.FacetCut[] memory _diamondCut, DiamondArgs memory _args) payable { | ||
LibDiamond.setContractOwner(_args.owner); | ||
LibDiamond.diamondCut(_diamondCut, _args.init, _args.initCalldata); | ||
|
||
// Code can be added here to perform actions and set state variables. | ||
} | ||
|
||
/** | ||
zguesmi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* `fallback` function must be added to the diamond with selector `0xffffffff`. | ||
* The function is defined in IexecEscrow(Native/Token) facet. | ||
*/ | ||
fallback() external payable{ | ||
_fallback(); | ||
} | ||
|
||
/** | ||
* `receive` function must be added to the diamond with selector `0x00000000`. | ||
* The function is defined in IexecEscrow(Native/Token) facet. | ||
*/ | ||
receive() external payable { | ||
_fallback(); | ||
} | ||
|
||
// Find facet for function that is called and execute the | ||
// function if a facet is found and return any value. | ||
function _fallback() internal { | ||
LibDiamond.DiamondStorage storage ds; | ||
bytes32 position = LibDiamond.DIAMOND_STORAGE_POSITION; | ||
// get diamond storage | ||
assembly { | ||
ds.slot := position | ||
} | ||
// get facet from function selector | ||
address facet = ds.facetAddressAndSelectorPosition[msg.sig].facetAddress; | ||
if (facet == address(0)) { | ||
facet = ds.facetAddressAndSelectorPosition[0xffffffff].facetAddress; | ||
} | ||
if(facet == address(0)) { | ||
revert FunctionNotFound(msg.sig); | ||
} | ||
// Execute external function from facet using delegatecall and return any value. | ||
assembly { | ||
// copy function selector and any arguments | ||
calldatacopy(0, 0, calldatasize()) | ||
// execute function call using the facet | ||
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0) | ||
// get any return value | ||
returndatacopy(0, 0, returndatasize()) | ||
// return any return value or error back to the caller | ||
switch result | ||
case 0 { | ||
revert(0, returndatasize()) | ||
} | ||
default { | ||
return(0, returndatasize()) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.