Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SimSimButDifferent committed Dec 24, 2023
0 parents commit 6f32fd7
Show file tree
Hide file tree
Showing 9 changed files with 4,113 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

#Hardhat files
cache
artifacts

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Sample Hardhat Project

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a script that deploys that contract.

Try running some of the following tasks:

```shell
npx hardhat help
npx hardhat test
GAS_REPORT=true npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.js
```
108 changes: 108 additions & 0 deletions contracts/EthWallet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract EthWallet {
/* structs */
struct User {
address ownerAddress;
uint userBalance;
bool walletStatus;
uint withdrawnToday;
uint lastWithdrawalTime;
}

/* mappings */
mapping(address => User) private users;

/* state variables */
uint currentBalance;
uint userCount;
uint withdrawalAmount;
uint public constant dailyWithdrawalLimit = 10 ether;

modifier withdrawalLimitCheck(uint _withdrawalAmount) {
User storage currentUser = users[msg.sender];

// Reset withdrawal amount if a new day has started
if (isNewDay(currentUser.lastWithdrawalTime)) {
currentUser.withdrawnToday = 0;
}

// Check if the requested withdrawal exceeds the daily limit
require(
currentUser.withdrawnToday + _withdrawalAmount <=
dailyWithdrawalLimit,
"Daily withdrawal limit exceeded"
);

// Update user's withdrawal data
currentUser.withdrawnToday += _withdrawalAmount;
currentUser.lastWithdrawalTime = block.timestamp;
_;
}

modifier sufficientBalance(uint _withdrawalAmount) {
withdrawalAmount = _withdrawalAmount;
currentBalance = users[msg.sender].userBalance;
require(
withdrawalAmount <= currentBalance,
"Amount requested exceeds wallet balance"
);
_;
}

// Helper function to determine if a new day has started
function isNewDay(uint lastWithdrawalTime) private view returns (bool) {
return (block.timestamp / 1 days) > (lastWithdrawalTime / 1 days);
}

function deposit() public payable {
uint depositAmount = msg.value;

if (depositAmount <= 0) {
revert("Deposit amount must be above 0");
}

if (users[msg.sender].walletStatus == true) {
users[msg.sender].userBalance += depositAmount;
} else {
users[msg.sender] = User(msg.sender, depositAmount, true, 0, 0);
}
}

function withdraw(
uint _withdrawalAmount
)
external
withdrawalLimitCheck(_withdrawalAmount)
sufficientBalance(_withdrawalAmount)
{
require(
_withdrawalAmount <= address(this).balance,
"Insufficient contract balance"
);

if (_withdrawalAmount <= 0) {
revert("Withdrawal amount must be above 0");
}

if (users[msg.sender].walletStatus != true) {
revert("Wallet does not exist");
}

if (_withdrawalAmount > users[msg.sender].userBalance) {
revert("Withdrawal exceeds user balance");
}

// Process withdrawal
payable(msg.sender).transfer(_withdrawalAmount);

// Update user Balance
users[msg.sender].userBalance -= _withdrawalAmount;
}

function getUserBalance() public view returns (uint) {
return users[msg.sender].userBalance;
}
}
6 changes: 6 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.9",
};
14 changes: 14 additions & 0 deletions helper-hardhat-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const hre = require("hardhat");

const networkConfig = {
31377: {
name: "localhost",
},
11155111: {
name: "sepolia",
},
};

const developmentChains = ["hardhat", "localhost"];

module.exports = { networkConfig, developmentChains };
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "hardhat-project",
"devDependencies": {
"@nomicfoundation/hardhat-chai-matchers": "^2.0.2",
"@nomicfoundation/hardhat-ethers": "^3.0.5",
"@nomicfoundation/hardhat-network-helpers": "^1.0.10",
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
"@nomicfoundation/hardhat-verify": "^2.0.2",
"@typechain/ethers-v6": "^0.5.1",
"@typechain/hardhat": "^9.1.0",
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.6",
"chai": "^4.3.10",
"ethers": "^6.9.0",
"hardhat-deploy": "^0.11.44",
"hardhat-gas-reporter": "^1.0.9",
"solidity-coverage": "^0.8.5"
},
"scripts": {
"hh node": "yarn hardhat node",
"hh deploy": "yarn hardhat run scripts/deploy.js"
},
"dependencies": {
"dotenv": "^16.3.1",
"hardhat": "^2.19.3",
"prettier": "^3.1.1"
}
}
40 changes: 40 additions & 0 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const hre = require("hardhat");
const { network } = require("hardhat");
const { developmentChains } = require("../helper-hardhat-config");

async function main() {
if (developmentChains.includes(network.name)) {
const EthWallet = await hre.ethers.getContractFactory("EthWallet");

console.log("Deploying...");

const ethWallet = await EthWallet.deploy();
console.log(`EthWallet deployed to: ${ethWallet.target}`);
}

if (
!developmentChains.includes(network.name) &&
process.env.ETHERSCAN_API_KEY
) {
const EthWallet = await hre.ethers.getContractFactory("EthWallet");

console.log("Deploying...");

const ethWallet = await EthWallet.deploy();
console.log(`EthWallet deployed to: ${ethWallet.target}`);

const desiredConfirmations = 2;
const receipt =
await EthWallet.deploymentTransaction().wait(desiredConfirmations);

console.log(`Transaction confirmed. Block number: ${receipt.blockNumber}`);
await hre.run("verify:etherscan", { address: EthWallet.target });
console.log("EthWallet verified!");
console.log("--------------------------------------------------");
}
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
21 changes: 21 additions & 0 deletions utils/verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { run } = require("hardhat");

const verify = async (contractAddress, args) => {
console.log("Verifying contract...");
try {
await run("verify:verify", {
address: contractAddress,
constructorArguments: args,
});
} catch (e) {
if (e.message.toLowerCase().includes("already verified")) {
console.log("Already verified!");
} else {
console.log(e);
}
}
};

module.exports = {
verify,
};
Loading

0 comments on commit 6f32fd7

Please sign in to comment.