From f3e7749fbeb8437710728970881c25de35df7b86 Mon Sep 17 00:00:00 2001 From: Vaibhav Khanna Date: Fri, 15 Apr 2022 17:16:15 +0530 Subject: [PATCH 1/3] added example --- contracts/example/common/variables.sol | 12 ++++++++++ contracts/example/module1/events.sol | 10 ++++++++ contracts/example/module1/main.sol | 32 ++++++++++++++++++++++++++ contracts/example/module2/main.sol | 14 +++++++++++ package-lock.json | 13 +++++++++++ package.json | 3 +++ 6 files changed, 84 insertions(+) create mode 100644 contracts/example/common/variables.sol create mode 100644 contracts/example/module1/events.sol create mode 100644 contracts/example/module1/main.sol create mode 100644 contracts/example/module2/main.sol diff --git a/contracts/example/common/variables.sol b/contracts/example/common/variables.sol new file mode 100644 index 0000000..dafcac1 --- /dev/null +++ b/contracts/example/common/variables.sol @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract Constants { + // token supported + address public constant token = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48; // USDC +} + +contract Variables is Constants { + // userAddress => amount deposited + mapping(address => uint256) internal _userBalance; +} diff --git a/contracts/example/module1/events.sol b/contracts/example/module1/events.sol new file mode 100644 index 0000000..32c0624 --- /dev/null +++ b/contracts/example/module1/events.sol @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import "../common/variables.sol"; + +contract Events is Variables { + event supplyLog(uint256 amount_); + + event withdrawLog(uint256 amount_); +} diff --git a/contracts/example/module1/main.sol b/contracts/example/module1/main.sol new file mode 100644 index 0000000..65adc3e --- /dev/null +++ b/contracts/example/module1/main.sol @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import "./events.sol"; +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; + +contract UserModule is Events { + using SafeERC20 for IERC20; + + /** + * @dev User function to supply. + * @param amount_ amount to supply. + */ + function supply(uint256 amount_) external { + IERC20(token).safeTransferFrom(msg.sender, address(this), amount_); + _userBalance[msg.sender] += amount_; + + emit supplyLog(amount_); + } + + /** + * @dev User function to withdraw. + * @param amount_ amount to withdraw. + */ + function withdraw(uint256 amount_) external { + _userBalance[msg.sender] -= amount_; + IERC20(token).safeTransfer(msg.sender, amount_); + + event withdrawLog(uint256 amount_); + } +} diff --git a/contracts/example/module2/main.sol b/contracts/example/module2/main.sol new file mode 100644 index 0000000..605135e --- /dev/null +++ b/contracts/example/module2/main.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import "../common/variables.sol"; + +contract ReadModule is Variables { + /** + * @dev Read function to get user's balance in the contract. + * @param user_ address of user. + */ + function userBalance(address user_) public view returns (uint256) { + return _userBalance[user_]; + } +} diff --git a/package-lock.json b/package-lock.json index 9c9e234..cf4bead 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "infinite-proxy", "version": "1.0.0", "license": "ISC", + "dependencies": { + "@openzeppelin/contracts": "^4.5.0" + }, "devDependencies": { "@nomiclabs/hardhat-ethers": "^2.0.5", "@nomiclabs/hardhat-etherscan": "^3.0.3", @@ -1497,6 +1500,11 @@ "hardhat": "^2.0.0" } }, + "node_modules/@openzeppelin/contracts": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-4.5.0.tgz", + "integrity": "sha512-fdkzKPYMjrRiPK6K4y64e6GzULR7R7RwxSigHS8DDp7aWDeoReqsQI+cxHV1UuhAqX69L1lAaWDxenfP+xiqzA==" + }, "node_modules/@resolver-engine/core": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/@resolver-engine/core/-/core-0.3.3.tgz", @@ -24098,6 +24106,11 @@ "@types/web3": "1.0.19" } }, + "@openzeppelin/contracts": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-4.5.0.tgz", + "integrity": "sha512-fdkzKPYMjrRiPK6K4y64e6GzULR7R7RwxSigHS8DDp7aWDeoReqsQI+cxHV1UuhAqX69L1lAaWDxenfP+xiqzA==" + }, "@resolver-engine/core": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/@resolver-engine/core/-/core-0.3.3.tgz", diff --git a/package.json b/package.json index f391990..ff54d70 100644 --- a/package.json +++ b/package.json @@ -47,5 +47,8 @@ "ts-node": "^10.7.0", "typechain": "^5.2.0", "typescript": "^4.6.3" + }, + "dependencies": { + "@openzeppelin/contracts": "^4.5.0" } } From 660ce7e585b32a82c80da4318991eef4162a19d8 Mon Sep 17 00:00:00 2001 From: Vaibhav Khanna Date: Fri, 15 Apr 2022 17:18:58 +0530 Subject: [PATCH 2/3] added dummy implementation --- contracts/example/dummyImplementation.sol | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 contracts/example/dummyImplementation.sol diff --git a/contracts/example/dummyImplementation.sol b/contracts/example/dummyImplementation.sol new file mode 100644 index 0000000..f245c10 --- /dev/null +++ b/contracts/example/dummyImplementation.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract UserModule { + /** + * @dev User function to supply. + * @param amount_ amount to supply. + */ + function supply(uint256 amount_) external {} + + /** + * @dev User function to withdraw. + * @param amount_ amount to withdraw. + */ + function withdraw(uint256 amount_) external {} +} + +contract ReadModule { + /** + * @dev Read function to get user's balance in the contract. + * @param user_ address of user. + */ + function userBalance(address user_) public view returns (uint256) {} +} + +contract DummyImplementation is UserModule, ReadModule {} From 0ffd63080db7e63e865ef90af41120593dbdaebc Mon Sep 17 00:00:00 2001 From: Vaibhav Khanna Date: Fri, 15 Apr 2022 18:09:20 +0530 Subject: [PATCH 3/3] added proxy contract and deployment script --- contracts/example/proxy.sol | 10 ++++ scripts/deploy.ts | 106 ++++++++++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 contracts/example/proxy.sol diff --git a/contracts/example/proxy.sol b/contracts/example/proxy.sol new file mode 100644 index 0000000..b74a00a --- /dev/null +++ b/contracts/example/proxy.sol @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import "../infiniteProxy/proxy.sol"; + +contract Example is Proxy { + constructor(address admin_, address dummyImplementation_) + Proxy(admin_, dummyImplementation_) + {} +} \ No newline at end of file diff --git a/scripts/deploy.ts b/scripts/deploy.ts index eed790a..ceefddf 100644 --- a/scripts/deploy.ts +++ b/scripts/deploy.ts @@ -4,6 +4,17 @@ // When running the script with `npx hardhat run