Repository: dappuniversity/ethers_examples Branch: master Commit: ba72b711ebdf Files: 9 Total size: 7.6 KB Directory structure: gitextract_mhniizkg/ ├── .gitignore ├── README.md ├── examples/ │ ├── 1_accounts.js │ ├── 2_read_smart_contract.js │ ├── 3_send_signed_transaction.js │ ├── 4_write_contract.js │ ├── 5_contract_event_stream.js │ └── 6_inspecting_blocks.js └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies /node_modules /.pnp .pnp.js # testing /coverage # production /build # misc .DS_Store .env .env.local .env.development.local .env.test.local .env.production.local npm-debug.log* yarn-debug.log* yarn-error.log* ================================================ FILE: README.md ================================================ # Ethers.js by Example Learn how to use ethers.js from these examples ## Technology Stack & Tools - Javascript (Writing scripts) - [Ethers.js](https://docs.ethers.io/v5/) (Blockchain Interaction) - [Node.js](https://nodejs.org/en/) (To run our scripts and install ethers.js) - [Infura](https://infura.io/) (Node provider) ## Setting Up ### 1. Clone/Download the Repository ### 2. Install Dependencies: ``` $ npm install ``` ## Ethers.js scripts ### 1_accounts.js - Reads balance of ether of wallet address - Input your infura project ID ``` $ node examples/1_accounts.js ``` ### 2_read_smart_contract.js - Reads the balance of Dai wallet address from the Dai contract - Input your infura project ID ``` $ node examples/2_read_smart_contract.js ``` ### 3_send_signed_transaction.js - Transfers 0.025 ether from account1 to account2 - Input your infura project ID - Input your account1 public key - Input your account2 public key - Input your account1 private key ``` $ node examples/3_send_signed_transaction.js ``` ### 4_deploy_contract.js - Deploys contract on Kovan testnet by using Factory contract - Input your infura project ID - Input your account private key ``` $ node examples/4_deploy_contract.js ``` ### 5_write_contract.js - Transfers entire balance of token of your choosing from account1 to account2 (on Kovan testnet) - Input your infura project ID - Input your account1 public key - Input your account2 public key - Input your account1 private key - Input contract address of the token you want to transfer (You can use the deployed contract address from the previous script) ``` $ node examples/5_write_contract.js ``` ### 6_contract_event_stream.js - Queries a block for transfer events - Input your infura project ID ``` $ node examples/6_contract_event_stream.js ``` ### 7_inspecting_blocks.js - Get transactions from block - Input your infura project ID ``` $ node examples/7_inspecting_blocks.js ``` ================================================ FILE: examples/1_accounts.js ================================================ const { ethers } = require("ethers"); const INFURA_ID = '' const provider = new ethers.providers.JsonRpcProvider(`https://mainnet.infura.io/v3/${INFURA_ID}`) const address = '0x73BCEb1Cd57C711feaC4224D062b0F6ff338501e' const main = async () => { const balance = await provider.getBalance(address) console.log(`\nETH Balance of ${address} --> ${ethers.utils.formatEther(balance)} ETH\n`) } main() ================================================ FILE: examples/2_read_smart_contract.js ================================================ const { ethers } = require("ethers"); const INFURA_ID = '' const provider = new ethers.providers.JsonRpcProvider(`https://mainnet.infura.io/v3/${INFURA_ID}`) const ERC20_ABI = [ "function name() view returns (string)", "function symbol() view returns (string)", "function totalSupply() view returns (uint256)", "function balanceOf(address) view returns (uint)", ]; const address = '0x6B175474E89094C44Da98b954EedeAC495271d0F' // DAI Contract const contract = new ethers.Contract(address, ERC20_ABI, provider) const main = async () => { const name = await contract.name() const symbol = await contract.symbol() const totalSupply = await contract.totalSupply() console.log(`\nReading from ${address}\n`) console.log(`Name: ${name}`) console.log(`Symbol: ${symbol}`) console.log(`Total Supply: ${totalSupply}\n`) const balance = await contract.balanceOf('0x6c6Bc977E13Df9b0de53b251522280BB72383700') console.log(`Balance Returned: ${balance}`) console.log(`Balance Formatted: ${ethers.utils.formatEther(balance)}\n`) } main() ================================================ FILE: examples/3_send_signed_transaction.js ================================================ const { ethers } = require("ethers"); const INFURA_ID = '' const provider = new ethers.providers.JsonRpcProvider(`https://kovan.infura.io/v3/${INFURA_ID}`) const account1 = '' // Your account address 1 const account2 = '' // Your account address 2 const privateKey1 = '' // Private key of account 1 const wallet = new ethers.Wallet(privateKey1, provider) const main = async () => { const senderBalanceBefore = await provider.getBalance(account1) const recieverBalanceBefore = await provider.getBalance(account2) console.log(`\nSender balance before: ${ethers.utils.formatEther(senderBalanceBefore)}`) console.log(`reciever balance before: ${ethers.utils.formatEther(recieverBalanceBefore)}\n`) const tx = await wallet.sendTransaction({ to: account2, value: ethers.utils.parseEther("0.025") }) await tx.wait() console.log(tx) const senderBalanceAfter = await provider.getBalance(account1) const recieverBalanceAfter = await provider.getBalance(account2) console.log(`\nSender balance after: ${ethers.utils.formatEther(senderBalanceAfter)}`) console.log(`reciever balance after: ${ethers.utils.formatEther(recieverBalanceAfter)}\n`) } main() ================================================ FILE: examples/4_write_contract.js ================================================ const { ethers } = require("ethers"); const INFURA_ID = '' const provider = new ethers.providers.JsonRpcProvider(`https://kovan.infura.io/v3/${INFURA_ID}`) const account1 = '' // Your account address 1 const account2 = '' // Your account address 2 const privateKey1 = '' // Private key of account 1 const wallet = new ethers.Wallet(privateKey1, provider) const ERC20_ABI = [ "function balanceOf(address) view returns (uint)", "function transfer(address to, uint amount) returns (bool)", ]; const address = '' const contract = new ethers.Contract(address, ERC20_ABI, provider) const main = async () => { const balance = await contract.balanceOf(account1) console.log(`\nReading from ${address}\n`) console.log(`Balance of sender: ${balance}\n`) const contractWithWallet = contract.connect(wallet) const tx = await contractWithWallet.transfer(account2, balance) await tx.wait() console.log(tx) const balanceOfSender = await contract.balanceOf(account1) const balanceOfReciever = await contract.balanceOf(account2) console.log(`\nBalance of sender: ${balanceOfSender}`) console.log(`Balance of reciever: ${balanceOfReciever}\n`) } main() ================================================ FILE: examples/5_contract_event_stream.js ================================================ const { ethers } = require("ethers"); const INFURA_ID = '' const provider = new ethers.providers.JsonRpcProvider(`https://mainnet.infura.io/v3/${INFURA_ID}`) const ERC20_ABI = [ "function name() view returns (string)", "function symbol() view returns (string)", "function totalSupply() view returns (uint256)", "function balanceOf(address) view returns (uint)", "event Transfer(address indexed from, address indexed to, uint amount)" ]; const address = '0x6B175474E89094C44Da98b954EedeAC495271d0F' // DAI Contract const contract = new ethers.Contract(address, ERC20_ABI, provider) const main = async () => { const block = await provider.getBlockNumber() const transferEvents = await contract.queryFilter('Transfer', block - 1, block) console.log(transferEvents) } main() ================================================ FILE: examples/6_inspecting_blocks.js ================================================ const { ethers } = require("ethers"); const INFURA_ID = '' const provider = new ethers.providers.JsonRpcProvider(`https://mainnet.infura.io/v3/${INFURA_ID}`) const main = async () => { const block = await provider.getBlockNumber() console.log(`\nBlock Number: ${block}\n`) const blockInfo = await provider.getBlock(block) console.log(blockInfo) const { transactions } = await provider.getBlockWithTransactions(block) console.log(`\nLogging first transaction in block:\n`) console.log(transactions[0]) } main() ================================================ FILE: package.json ================================================ { "name": "ethers_examples", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "ethers": "^5.5.4" } }