Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions examples/1_accounts.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
const { ethers } = require("ethers");

const INFURA_ID = ''
const provider = new ethers.providers.JsonRpcProvider(`https://mainnet.infura.io/v3/${INFURA_ID}`)
// const INFURA_ID = "3eaee57df35f4e3e9f41c609cf6d774b";

const address = '0x73BCEb1Cd57C711feaC4224D062b0F6ff338501e'
const provider = new ethers.providers.JsonRpcProvider(
'https://mainnet.infura.io/v3/3eaee57df35f4e3e9f41c609cf6d774b'
);

const address = "0x73BCEb1Cd57C711feaC4224D062b0F6ff338501e";

const main = async () => {
const balance = await provider.getBalance(address)
console.log(`\nETH Balance of ${address} --> ${ethers.utils.formatEther(balance)} ETH\n`)
}
const balance = await provider.getBalance(address);

main()
console.log(
`\n ETH Balance of ${address} --> ${ethers.utils.formatEther(
balance
)} ETH\n`
);
};

main();
32 changes: 0 additions & 32 deletions examples/2_read_smart_contract.js

This file was deleted.

37 changes: 37 additions & 0 deletions examples/2_read_smart_contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const ethers = require("ethers");

const provider = new ethers.providers.JsonRpcProvider(
"https://mainnet.infura.io/v3/3eaee57df35f4e3e9f41c609cf6d774b"
);

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

//creating an instance of the contract

const contract = new ethers.Contract(address, ERC20_ABI, provider);

const main = async () => {
const name = contract.name();
const symbol = contract.symbol();
const totalSupply = contract.totalSupply();

console.log(`\n Reading from ${address}\n`);
console.log(`Name : ${name}`);
console.log(`Symbol : ${symbol}`);
console.log(`TotalSupply : ${totalSupply}`);

const balance = await contract.balanceOf(
"0x6c6Bc977E13Df9b0de53b251522280BB72383700"
);

console.log(`Balance returned: ${balance}`);
console.log(`Balance Formatted: ${ethers.utils.formatEther(balance)}`); // converting DAI to Ethers format
};

77 changes: 48 additions & 29 deletions examples/3_send_signed_transaction.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
const { ethers } = require("ethers");
const ethers = require("ethers");

const INFURA_ID = ''
const provider = new ethers.providers.JsonRpcProvider(`https://kovan.infura.io/v3/${INFURA_ID}`)
const provider = new ethers.providers.JsonRpcProvider(
"https://sepolia.infura.io/v3/3eaee57df35f4e3e9f41c609cf6d774b"
);

const account1 = '' // Your account address 1
const account2 = '' // Your account address 2
const account1 = "0xecCb2450889B3f68C0cd234FE05420Ef87b0a027"; //sender
const account2 = "0xD43cA3765c530D697fa2ECe322d2d43d993d0651"; //recepient
const privateKey1 =
"25202a70c90a9d9b1f20d20066b6b67ec95ce231b09f11ef109406a839fc9794"; //sender private key

const privateKey1 = '' // Private key of account 1
const wallet = new ethers.Wallet(privateKey1, provider)
//creating a wallet 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()
const senderBalanceBefore = await provider.getBalance(account1);
const receiverBalanceBefore = await provider.getBalance(account2);
// Logging the balances before the transaction in a user-friendly format.
console.log(
`\n Sender Balance Before: ${ethers.utils.formatEther(senderBalanceBefore)}`
);
console.log(
`\n Receiver Balance Before: ${ethers.utils.formatEther(
receiverBalanceBefore
)}`
);

//send ether
const tx = await wallet.sendTransaction({
to: account2,
value: ethers.utils.parseEther("0.025"), //0.025 ether to WEI
});

//Wait for transaction to be mined
await tx.wait();
console.log(tx);

// Getting the balances of the sender and receiver accounts after the transaction.
const senderBalanceAfter = await provider.getBalance(account1);
const receiverBalanceAfter = await provider.getBalance(account2);

// Logging the balances after the transaction in a user-friendly format
console.log(
`\n Sender Balance After: ${ethers.utils.formatEther(senderBalanceAfter)}`
);
console.log(
`\n Receiver Balance After: ${ethers.utils.formatEther(
receiverBalanceAfter
)}`
);
};

main();
54 changes: 30 additions & 24 deletions examples/4_write_contract.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
const { ethers } = require("ethers");
const { ethers, Wallet } = require("ethers");

const INFURA_ID = ''
const provider = new ethers.providers.JsonRpcProvider(`https://kovan.infura.io/v3/${INFURA_ID}`)
const rpovider = new ethers.JsonRpcProvider("");

const account1 = '' // Your account address 1
const account2 = '' // Your account address 2
// Defining two Ethereum accounts and their private keys.
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 privateKey1 = ""; // Private key of account 1

// Creating a wallet using the private key and the previously defined provider.
const wallet = new ethers.Wallet(privateKey1, provider);

// Defining an ERC20 token's ABI (Application Binary Interface), specifying functions the contract supports.
const ERC20_ABI = [
"function balanceOf(address) view returns (uint)",
"function transfer(address to, uint amount) returns (bool)",
"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)
//fining the address of the ERC20 token contract.
const address = '';

// Creating an instance of the ERC20 token contract using the ABI, address, and provider.
const contract = new ethers.Contract(address, ERC20_ABI, provider);

const main = async () => {
const balance = await contract.balanceOf(account1)
const balance = await contract.balanceOf(account1);

console.log(`\nReading from ${address}\n`)
console.log(`Balance of sender: ${balance}\n`)
console.log(`\n Reading from ${address}\n`);
console.log(`Balance of sender: ${balance}`);

const contractWithWallet = contract.connect(wallet)
const contractWithWallet = await contract.connect(Wallet);

const tx = await contractWithWallet.transfer(account2, balance)
await tx.wait()
const tx = await contractWithWallet.transfer(account2, balance);
await tx.wait(); //contract mining

console.log(tx)
console.log(tx);

const balanceOfSender = await contract.balanceOf(account1)
const balanceOfReciever = await contract.balanceOf(account2)
const balanceOfSender = await contract.balanceOf(account1);
const balanceOfReceiver = await contract.balanceOf(account2);

console.log(`\nBalance of sender: ${balanceOfSender}`)
console.log(`Balance of reciever: ${balanceOfReciever}\n`)
}
console.log(`\nBalance of sender: ${balanceOfSender}`);
console.log(`Balance of receiver: ${balanceOfReceiver}\n`);
};

main()
main();
2 changes: 1 addition & 1 deletion examples/5_contract_event_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ const main = async () => {
console.log(transferEvents)
}

main()
main();
File renamed without changes.
Loading