To further elaborate Marten's answer, I will try to give a complete script.
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'.
nodejs script for contract deployment goes here:
//load 'ethers' and 'fs'
const ethers = require('ethers');
const 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);
//Ussing async-await for deploy method
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 of the ethers docs https://docs.ethers.io/v5/api/contract/contract-factory/#ContractFactory--creating
ethers.ContractFactory( interface , bytecode [ , signer ] )
Please ask if still having problem.