Personal Notes-Ether.js
- installing
npm i ethers
we pass the rpc as the following to connect to the blockchain in the main().
const provider = new ethers.providers.JsonRpcProvider("http://0.0.0.0:8545")
Private keys basically prove you are the owner of the address mentioned.
const waller = new ethers.Wallet("7dbe32jhbdskjbb1hh1hj3jn",provider);
In order to deploy our contract we need the ABI and the binary contract compiled in the contract.
const fs=require("fs-extra");
main(){
//entry point to the blockchain network
const provider = new ethers.providers.JsonRpcProvider("http://0.0.0.0:8545") ;
//private key to sign and prove you are the owner of the network
=>so we can sign deploying the contract
==> basically says yes, this address has deployed the contract.
const waller = new ethers.Wallet("7dbe32jhbdskjbb1hh1hj3jn",provider);
//get the abi file. => our code knows how to interact with the contract
const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi","utf8");
//get the binary file. => binary file is the main compiled code
const binary = fs.readSync("./SimpleStorage_sol_SimpleStorage.bin","utf8")
//contractFactory is just an object you can use to deploy contracts
const contractFactory = new ethers.ContractFactory(abi,binary,wallet);
//deploy the contract
const contract = await contractFactory.deploy() //main should be async
}
Adding Tracsaction Overrides
We can add additional information for a given contract
//deploy the contract
const contract = await contractFactory.deploy ({
gasPrice: 10000000,
gasLimit: 100000000,
})
Transaction Receipts
- Transaction Receipts Is the receipt you get after one block is deployed on the network
const transactionReceipt = await contract.deployTransaction.wait(1)
Transaction Response is what you get after you've created your transaction
contract.deployTransaction
calling an ABI
const contract = await contractFactory.deploy()
//retrieve is a abi
const currentNumber = await contract.retrieve().toString();
currentNumber is a big number as javascript does not handle decimal points well, and tends to lose precision. so to convert a big Number we convert the result to a string
- It's best practice to pass variables as strings in solidity.
-.wait(1) returns a receipt after its added to the block
Environment Variables
- create a .env file -
.env________________
PRIVATE_KEY=0x......
________________npm i________________
npm i dotenv
deploy.js________________
require("dotenv").config();
(process.env.PRIVATE_KEY)
- create a .gitignore file
- ``` .env node_modules