diff --git a/SimpleBank/SimpleBank.so b/SimpleBank/SimpleBank.so new file mode 100644 index 0000000..8ea497b --- /dev/null +++ b/SimpleBank/SimpleBank.so @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +// SimpleBank contract definition +contract SimpleBank { + // Mapping from addresses to balances + mapping(address => uint) private balances; + + // Event declarations + event DepositMade(address indexed accountAddress, uint amount); + event WithdrawalMade(address indexed accountAddress, uint withdrawAmount, uint newBalance); + + // Deposit function allows users to deposit Ether into the bank + function deposit() public payable { + require(msg.value > 0, "Deposit amount must be greater than zero."); + balances[msg.sender] += msg.value; + emit DepositMade(msg.sender, msg.value); + } + + // Withdraw function allows users to withdraw Ether from the bank + function withdraw(uint withdrawAmount) public { + require(withdrawAmount <= balances[msg.sender], "Insufficient balance."); + payable(msg.sender).transfer(withdrawAmount); + balances[msg.sender] -= withdrawAmount; + emit WithdrawalMade(msg.sender, withdrawAmount, balances[msg.sender]); + } + + // Get balance function returns the Ether balance of the user + function getBalance() public view returns (uint) { + return balances[msg.sender]; + } +} diff --git a/SimpleBank/banking.js b/SimpleBank/banking.js new file mode 100644 index 0000000..3cece85 --- /dev/null +++ b/SimpleBank/banking.js @@ -0,0 +1,61 @@ +const Web3 = require('web3'); + +// Configuration +const web3 = new Web3('https://sepolia.infura.io/v3/YOUR_INFURA_PROJECT_ID'); +const account = 'YOUR_ACCOUNT_ADDRESS'; +const privateKey = 'YOUR_PRIVATE_KEY'; // Be cautious with your private key +const simpleBankContractAddress = 'SIMPLE_BANK_CONTRACT_ADDRESS'; +const simpleBankABI = [ + // Simplified ABI with only the methods we'll interact with + { + "constant": false, + "inputs": [], + "name": "deposit", + "outputs": [], + "payable": true, + "stateMutability": "payable", + "type": "function" + }, + { + "constant": false, + "inputs": [{"name": "withdrawAmount", "type": "uint256"}], + "name": "withdraw", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } +]; + +async function depositEther(amount) { + const simpleBank = new web3.eth.Contract(simpleBankABI, simpleBankContractAddress); + const transaction = simpleBank.methods.deposit(); + const options = { + to: transaction._parent._address, + data: transaction.encodeABI(), + gas: await transaction.estimateGas({from: account}), + gasPrice: await web3.eth.getGasPrice(), + value: web3.utils.toWei(amount.toString(), 'ether') + }; + + const signed = await web3.eth.accounts.signTransaction(options, privateKey); + const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction); + console.log('Transaction receipt:', receipt); +} + +async function withdrawEther(amount) { + const simpleBank = new web3.eth.Contract(simpleBankABI, simpleBankContractAddress); + const transaction = simpleBank.methods.withdraw(web3.utils.toWei(amount.toString(), 'ether')); + const options = { + to: transaction._parent._address, + data: transaction.encodeABI(), + gas: await transaction.estimateGas({from: account}), + gasPrice: await web3.eth.getGasPrice(), + }; + + const signed = await web3.eth.accounts.signTransaction(options, privateKey); + const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction); + console.log('Transaction receipt:', receipt); +} + +// Example usage +depositEther(0.01).then(() => withdrawEther(0.01));