-
Notifications
You must be signed in to change notification settings - Fork 0
[wip]: add contract package #108
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
Open
alanchchen
wants to merge
3
commits into
master
Choose a base branch
from
feature/add-contracts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,107 @@ | ||
| # Logs | ||
| logs | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| lerna-debug.log* | ||
|
|
||
| # Diagnostic reports (https://nodejs.org/api/report.html) | ||
| report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | ||
|
|
||
| # Runtime data | ||
| pids | ||
| *.pid | ||
| *.seed | ||
| *.pid.lock | ||
|
|
||
| # Directory for instrumented libs generated by jscoverage/JSCover | ||
| lib-cov | ||
|
|
||
| # Coverage directory used by tools like istanbul | ||
| coverage | ||
| *.lcov | ||
|
|
||
| # nyc test coverage | ||
| .nyc_output | ||
|
|
||
| # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
| .grunt | ||
|
|
||
| # Bower dependency directory (https://bower.io/) | ||
| bower_components | ||
|
|
||
| # node-waf configuration | ||
| .lock-wscript | ||
|
|
||
| # Compiled binary addons (https://nodejs.org/api/addons.html) | ||
| build/Release | ||
|
|
||
| # Dependency directories | ||
| node_modules/ | ||
| jspm_packages/ | ||
|
|
||
| # TypeScript v1 declaration files | ||
| typings/ | ||
|
|
||
| # TypeScript cache | ||
| *.tsbuildinfo | ||
|
|
||
| # Optional npm cache directory | ||
| .npm | ||
|
|
||
| # Optional eslint cache | ||
| .eslintcache | ||
|
|
||
| # Microbundle cache | ||
| .rpt2_cache/ | ||
| .rts2_cache_cjs/ | ||
| .rts2_cache_es/ | ||
| .rts2_cache_umd/ | ||
|
|
||
| # Optional REPL history | ||
| .node_repl_history | ||
|
|
||
| # Output of 'npm pack' | ||
| *.tgz | ||
|
|
||
| # Yarn Integrity file | ||
| .yarn-integrity | ||
|
|
||
| # dotenv environment variables file | ||
| .env | ||
| .env.test | ||
|
|
||
| # parcel-bundler cache (https://parceljs.org/) | ||
| .cache | ||
|
|
||
| # Next.js build output | ||
| .next | ||
|
|
||
| # Nuxt.js build / generate output | ||
| .nuxt | ||
| dist | ||
|
|
||
| # Gatsby files | ||
| .cache/ | ||
| # Comment in the public line in if your project uses Gatsby and *not* Next.js | ||
| # https://nextjs.org/blog/next-9-1#public-directory-support | ||
| # public | ||
|
|
||
| # vuepress build output | ||
| .vuepress/dist | ||
|
|
||
| # Serverless directories | ||
| .serverless/ | ||
|
|
||
| # FuseBox cache | ||
| .fusebox/ | ||
|
|
||
| # DynamoDB Local files | ||
| .dynamodb/ | ||
|
|
||
| # TernJS port file | ||
| .tern-port | ||
|
|
||
| artifacts/ | ||
| cache/ |
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,10 @@ | ||
| { | ||
| "printWidth": 120, | ||
| "singleQuote": true, | ||
| "trailingComma": "all", | ||
| "bracketSpacing": true, | ||
| "arrowParens": "avoid", | ||
| "semi": true, | ||
| "useTabs": false, | ||
| "tabWidth": 2 | ||
| } |
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,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2021 AMIS | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
Empty file.
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,42 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // OpenZeppelin Contracts (last updated v4.5.0) (metatx/ERC2771Context.sol) | ||
|
|
||
| pragma solidity ^0.8.9; | ||
|
|
||
| import '@openzeppelin/contracts/utils/Context.sol'; | ||
|
|
||
| /** | ||
| * @dev Context variant with ERC2771 support. | ||
| */ | ||
| abstract contract ERC2771Context is Context { | ||
| /// @custom:oz-upgrades-unsafe-allow state-variable-immutable | ||
| address private immutable _trustedForwarder; | ||
|
|
||
| /// @custom:oz-upgrades-unsafe-allow constructor | ||
| constructor(address trustedForwarder) { | ||
| _trustedForwarder = trustedForwarder; | ||
| } | ||
|
|
||
| function isTrustedForwarder(address forwarder) public view virtual returns (bool) { | ||
| return forwarder == _trustedForwarder; | ||
| } | ||
|
|
||
| function _msgSender() internal view virtual override returns (address sender) { | ||
| if (isTrustedForwarder(msg.sender)) { | ||
| // The assembly code is more direct than the Solidity version using `abi.decode`. | ||
| assembly { | ||
| sender := shr(96, calldataload(sub(calldatasize(), 20))) | ||
| } | ||
| } else { | ||
| return super._msgSender(); | ||
| } | ||
| } | ||
|
|
||
| function _msgData() internal view virtual override returns (bytes calldata) { | ||
| if (isTrustedForwarder(msg.sender)) { | ||
| return msg.data[:msg.data.length - 20]; | ||
| } else { | ||
| return super._msgData(); | ||
| } | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
packages/contracts/contracts/metatx/ERC2771ContextUpgradeable.sol
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,50 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // OpenZeppelin Contracts (last updated v4.5.0) (metatx/ERC2771Context.sol) | ||
|
|
||
| pragma solidity ^0.8.9; | ||
|
|
||
| import '@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol'; | ||
| import '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol'; | ||
|
|
||
| /** | ||
| * @dev Context variant with ERC2771 support. | ||
| */ | ||
| abstract contract ERC2771ContextUpgradeable is Initializable, ContextUpgradeable { | ||
| /// @custom:oz-upgrades-unsafe-allow state-variable-immutable | ||
| address private immutable _trustedForwarder; | ||
|
|
||
| /// @custom:oz-upgrades-unsafe-allow constructor | ||
| constructor(address trustedForwarder) { | ||
| _trustedForwarder = trustedForwarder; | ||
| } | ||
|
|
||
| function isTrustedForwarder(address forwarder) public view virtual returns (bool) { | ||
| return forwarder == _trustedForwarder; | ||
| } | ||
|
|
||
| function _msgSender() internal view virtual override returns (address sender) { | ||
| if (isTrustedForwarder(msg.sender)) { | ||
| // The assembly code is more direct than the Solidity version using `abi.decode`. | ||
| assembly { | ||
| sender := shr(96, calldataload(sub(calldatasize(), 20))) | ||
| } | ||
| } else { | ||
| return super._msgSender(); | ||
| } | ||
| } | ||
|
|
||
| function _msgData() internal view virtual override returns (bytes calldata) { | ||
| if (isTrustedForwarder(msg.sender)) { | ||
| return msg.data[:msg.data.length - 20]; | ||
| } else { | ||
| return super._msgData(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev This empty reserved space is put in place to allow future versions to add new | ||
| * variables without shifting down storage in the inheritance chain. | ||
| * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps | ||
| */ | ||
| uint256[50] private __gap; | ||
| } |
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,70 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.9; | ||
|
|
||
| import '@openzeppelin/contracts/access/Ownable.sol'; | ||
| import '@openzeppelin/contracts/utils/math/SafeMath.sol'; | ||
| import '@openzeppelin/contracts/utils/cryptography/ECDSA.sol'; | ||
| import '@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol'; | ||
|
|
||
| /** | ||
| * @dev Simple minimal forwarder to be used together with an ERC2771 compatible contract. See {ERC2771Context}. | ||
| */ | ||
| contract Forwarder is Ownable, EIP712 { | ||
| using ECDSA for bytes32; | ||
| using SafeMath for uint256; | ||
|
|
||
| struct ForwardRequest { | ||
| address from; | ||
| address to; | ||
| uint256 value; | ||
| uint256 gas; | ||
| uint256 nonce; | ||
| bytes data; | ||
| } | ||
|
|
||
| bytes32 private constant _TYPEHASH = | ||
| keccak256('ForwardRequest(address from,address to,uint256 value,uint256 gas,uint256 nonce,bytes data)'); | ||
|
|
||
| mapping(address => uint256) private _nonces; | ||
|
|
||
| constructor(string memory name, string memory version) EIP712(name, version) {} | ||
|
|
||
| function getNonce(address from) public view returns (uint256) { | ||
| return _nonces[from]; | ||
| } | ||
|
|
||
| function verify(ForwardRequest calldata req, bytes calldata signature) public view returns (bool) { | ||
| address signer = _hashTypedDataV4( | ||
| keccak256(abi.encode(_TYPEHASH, req.from, req.to, req.value, req.gas, req.nonce, keccak256(req.data))) | ||
| ).recover(signature); | ||
| return _nonces[req.from] == req.nonce && signer == req.from; | ||
| } | ||
|
|
||
| function execute(ForwardRequest calldata req, bytes calldata signature) | ||
| public | ||
| payable | ||
| onlyOwner | ||
| returns (bool, bytes memory) | ||
| { | ||
| require(verify(req, signature), 'Forwarder: signature does not match request'); | ||
| _nonces[req.from] = req.nonce.add(1); | ||
|
|
||
| (bool success, bytes memory returndata) = req.to.call{ gas: req.gas, value: req.value }( | ||
| abi.encodePacked(req.data, req.from) | ||
| ); | ||
|
|
||
| // Validate that the relayer has sent enough gas for the call. | ||
| // See https://ronan.eth.link/blog/ethereum-gas-dangers/ | ||
| if (gasleft() <= req.gas / 63) { | ||
| // We explicitly trigger invalid opcode to consume all gas and bubble-up the effects, since | ||
| // neither revert or assert consume all gas since Solidity 0.8.0 | ||
| // https://docs.soliditylang.org/en/v0.8.0/control-structures.html#panic-via-assert-and-error-via-require | ||
| assembly { | ||
| invalid() | ||
| } | ||
| } | ||
|
|
||
| return (success, returndata); | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
packages/contracts/contracts/metatx/ForwarderUpgradeable.sol
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,81 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // OpenZeppelin Contracts (last updated v4.5.0) (metatx/MinimalForwarder.sol) | ||
|
|
||
| pragma solidity ^0.8.0; | ||
|
|
||
| import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol'; | ||
| import '@openzeppelin/contracts-upgradeable/utils/cryptography/ECDSAUpgradeable.sol'; | ||
| import '@openzeppelin/contracts-upgradeable/utils/math/SafeMathUpgradeable.sol'; | ||
| import '@openzeppelin/contracts-upgradeable/utils/cryptography/draft-EIP712Upgradeable.sol'; | ||
| import '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol'; | ||
|
|
||
| contract ForwarderUpgradeable is Initializable, OwnableUpgradeable, EIP712Upgradeable { | ||
| using ECDSAUpgradeable for bytes32; | ||
| using SafeMathUpgradeable for uint256; | ||
|
|
||
| struct ForwardRequest { | ||
| address from; | ||
| address to; | ||
| uint256 value; | ||
| uint256 gas; | ||
| uint256 nonce; | ||
| bytes data; | ||
| } | ||
|
|
||
| bytes32 private constant _TYPEHASH = | ||
| keccak256('ForwardRequest(address from,address to,uint256 value,uint256 gas,uint256 nonce,bytes data)'); | ||
|
|
||
| mapping(address => uint256) private _nonces; | ||
|
|
||
| function __Forwarder_init(string memory name, string memory version) internal onlyInitializing { | ||
| __Ownable_init_unchained(); | ||
| __EIP712_init_unchained(name, version); | ||
| } | ||
|
|
||
| function __Forwarder_init_unchained() internal onlyInitializing {} | ||
|
|
||
| function getNonce(address from) public view returns (uint256) { | ||
| return _nonces[from]; | ||
| } | ||
|
|
||
| function verify(ForwardRequest calldata req, bytes calldata signature) public view returns (bool) { | ||
| address signer = _hashTypedDataV4( | ||
| keccak256(abi.encode(_TYPEHASH, req.from, req.to, req.value, req.gas, req.nonce, keccak256(req.data))) | ||
| ).recover(signature); | ||
| return _nonces[req.from] == req.nonce && signer == req.from; | ||
| } | ||
|
|
||
| function execute(ForwardRequest calldata req, bytes calldata signature) | ||
| public | ||
| payable | ||
| onlyOwner | ||
| returns (bool, bytes memory) | ||
| { | ||
| require(verify(req, signature), 'Forwarder: signature does not match request'); | ||
| _nonces[req.from] = req.nonce.add(1); | ||
|
|
||
| (bool success, bytes memory returndata) = req.to.call{ gas: req.gas, value: req.value }( | ||
| abi.encodePacked(req.data, req.from) | ||
| ); | ||
|
|
||
| // Validate that the relayer has sent enough gas for the call. | ||
| // See https://ronan.eth.link/blog/ethereum-gas-dangers/ | ||
| if (gasleft() <= req.gas / 63) { | ||
| // We explicitly trigger invalid opcode to consume all gas and bubble-up the effects, since | ||
| // neither revert or assert consume all gas since Solidity 0.8.0 | ||
| // https://docs.soliditylang.org/en/v0.8.0/control-structures.html#panic-via-assert-and-error-via-require | ||
| assembly { | ||
| invalid() | ||
| } | ||
| } | ||
|
|
||
| return (success, returndata); | ||
| } | ||
|
|
||
| /** | ||
| * @dev This empty reserved space is put in place to allow future versions to add new | ||
| * variables without shifting down storage in the inheritance chain. | ||
| * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps | ||
| */ | ||
| uint256[48] private __gap; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果是想要保持都是50個變數的話應該是uint256[49]? |
||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
要繼承UUPSUpgradeable還有function要有_authorizeUpgrade才能做升級