added example

This commit is contained in:
Vaibhav Khanna 2022-04-15 17:16:15 +05:30
parent e9078dca61
commit f3e7749fbe
6 changed files with 84 additions and 0 deletions

View File

@ -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;
}

View File

@ -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_);
}

View File

@ -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_);
}
}

View File

@ -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_];
}
}

13
package-lock.json generated
View File

@ -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",

View File

@ -47,5 +47,8 @@
"ts-node": "^10.7.0",
"typechain": "^5.2.0",
"typescript": "^4.6.3"
},
"dependencies": {
"@openzeppelin/contracts": "^4.5.0"
}
}