rahuldev-17
Posted on December 29, 2021
Assuming you have installed metamask, and know the seed phrase, here are steps to deploy contract using 'ethers' and 'fs':
- compile the contract to .bin and .abi files
- load 'ethers' and 'fs'
- create a 'signer' object using 'provider', 'Wallet', and 'connect' methods from 'ethers'
- create a contract instance from 'ContractFactory' method
- use deploy method as promise
- Here I have used 'getblock' as web3 provider for example (see https://getblock.io/docs/get-started/auth-with-api-key/). Other alternatives are 'quicknode', 'alchemy' and 'infura'.
We will deploy the code to the BSC testnet, but same procedure will apply for other Ethereum Virtual Machine(EVM) compatible chains, such as : Avalanche Contract Chain (C-Chain), Binance Smart Chain (BSC) mainnet, Fantom Opera and Polygon etc.
nodejs script for contract deployment goes here:
//load 'ethers' and 'fs'
ethers = require('ethers');
fs = require('fs');
//Read bin and abi file to object; names of the solcjs-generated files renamed
bytecode = fs.readFileSync('storage.bin').toString();
abi = JSON.parse(fs.readFileSync('storage.abi').toString());
//to create 'signer' object;here 'account'
const mnemonic = "<see-phrase>" // seed phrase for your Metamask account
const provider = new ethers.providers.WebSocketProvider("wss://bsc.getblock.io/testnet/?api_key=<your-api-key>");
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
const account = wallet.connect(provider);
const myContract = new ethers.ContractFactory(abi, bytecode, account);
//Using 'deploy method 'in 'async-await'
async function main() {
// If your contract requires constructor args, you can specify them here
const contract = await myContract.deploy();
console.log(contract.address);
console.log(contract.deployTransaction);
}
main();
In the above code, 'account' is the 'signer' of the ethers docs
https://docs.ethers.io/v5/api/contract/contract-factory/#ContractFactory--creating
ethers.ContractFactory( interface , bytecode [ , signer ] )
Do not hesitate to ask in discussion if you face any problem in deploying your contract.
Posted on December 29, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.