Browse Source

Update 'SimpleBank/banking.js'

main
Anindya Maiti 7 months ago
parent
commit
62f48d47fc
  1. 129
      SimpleBank/banking.js

129
SimpleBank/banking.js

@ -1,61 +1,68 @@
const Web3 = require('web3'); // Prereqs:
// First install npm
// Configuration // mkdir SimpleBank
const web3 = new Web3('https://sepolia.infura.io/v3/YOUR_INFURA_PROJECT_ID'); // cd SimpleBank
const account = 'YOUR_ACCOUNT_ADDRESS'; // npm init -y
const privateKey = 'YOUR_PRIVATE_KEY'; // Be cautious with your private key // npm install web3
const simpleBankContractAddress = 'SIMPLE_BANK_CONTRACT_ADDRESS';
const simpleBankABI = [ const Web3 = require('web3');
// Simplified ABI with only the methods we'll interact with
{ // Configuration
"constant": false, const web3 = new Web3('https://sepolia.infura.io/v3/YOUR_INFURA_PROJECT_ID');
"inputs": [], const account = 'YOUR_ACCOUNT_ADDRESS';
"name": "deposit", const privateKey = 'YOUR_PRIVATE_KEY'; // Be cautious with your private key
"outputs": [], const simpleBankContractAddress = 'SIMPLE_BANK_CONTRACT_ADDRESS';
"payable": true, const simpleBankABI = [
"stateMutability": "payable", // Simplified ABI with only the methods we'll interact with
"type": "function" {
}, "constant": false,
{ "inputs": [],
"constant": false, "name": "deposit",
"inputs": [{"name": "withdrawAmount", "type": "uint256"}], "outputs": [],
"name": "withdraw", "payable": true,
"outputs": [], "stateMutability": "payable",
"stateMutability": "nonpayable", "type": "function"
"type": "function" },
} {
]; "constant": false,
"inputs": [{"name": "withdrawAmount", "type": "uint256"}],
async function depositEther(amount) { "name": "withdraw",
const simpleBank = new web3.eth.Contract(simpleBankABI, simpleBankContractAddress); "outputs": [],
const transaction = simpleBank.methods.deposit(); "stateMutability": "nonpayable",
const options = { "type": "function"
to: transaction._parent._address, }
data: transaction.encodeABI(), ];
gas: await transaction.estimateGas({from: account}),
gasPrice: await web3.eth.getGasPrice(), async function depositEther(amount) {
value: web3.utils.toWei(amount.toString(), 'ether') const simpleBank = new web3.eth.Contract(simpleBankABI, simpleBankContractAddress);
}; const transaction = simpleBank.methods.deposit();
const options = {
const signed = await web3.eth.accounts.signTransaction(options, privateKey); to: transaction._parent._address,
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction); data: transaction.encodeABI(),
console.log('Transaction receipt:', receipt); gas: await transaction.estimateGas({from: account}),
} gasPrice: await web3.eth.getGasPrice(),
value: web3.utils.toWei(amount.toString(), 'ether')
async function withdrawEther(amount) { };
const simpleBank = new web3.eth.Contract(simpleBankABI, simpleBankContractAddress);
const transaction = simpleBank.methods.withdraw(web3.utils.toWei(amount.toString(), 'ether')); const signed = await web3.eth.accounts.signTransaction(options, privateKey);
const options = { const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
to: transaction._parent._address, console.log('Transaction receipt:', receipt);
data: transaction.encodeABI(), }
gas: await transaction.estimateGas({from: account}),
gasPrice: await web3.eth.getGasPrice(), async function withdrawEther(amount) {
}; const simpleBank = new web3.eth.Contract(simpleBankABI, simpleBankContractAddress);
const transaction = simpleBank.methods.withdraw(web3.utils.toWei(amount.toString(), 'ether'));
const signed = await web3.eth.accounts.signTransaction(options, privateKey); const options = {
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction); to: transaction._parent._address,
console.log('Transaction receipt:', receipt); data: transaction.encodeABI(),
} gas: await transaction.estimateGas({from: account}),
gasPrice: await web3.eth.getGasPrice(),
// Example usage };
depositEther(0.01).then(() => withdrawEther(0.01));
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));

Loading…
Cancel
Save