I am trying to add Binance Main and test net Network through web3 js. I don't want that user to setup it manually. Did you guys any idea about that how to handle it?
How to Add Custom Network (BNB) in MetaMask Using web3
metamasknetworkidweb3js
Related Solutions
Here is my deploy script that also handles waiting until the transaction is included in a block:
// Copyright 2017 https://tokenmarket.net - MIT licensed
//
// Run with Node 7.x as:
//
// node --harmony-async-await deploy.js
//
let fs = require("fs");
let Web3 = require('web3'); // https://www.npmjs.com/package/web3
// Create a web3 connection to a running geth node over JSON-RPC running at
// http://localhost:8545
// For geth VPS server + SSH tunneling see
// https://gist.github.com/miohtama/ce612b35415e74268ff243af645048f4
let web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
// Read the compiled contract code
// Compile with
// solc SampleContract.sol --combined-json abi,asm,ast,bin,bin-runtime,clone-bin,devdoc,interface,opcodes,srcmap,srcmap-runtime,userdoc > contracts.json
let source = fs.readFileSync("contracts.json");
let contracts = JSON.parse(source)["contracts"];
// ABI description as JSON structure
let abi = JSON.parse(contracts.SampleContract.abi);
// Smart contract EVM bytecode as hex
let code = '0x' + contracts.SampleContract.bin;
// Create Contract proxy class
let SampleContract = web3.eth.contract(abi);
// Unlock the coinbase account to make transactions out of it
console.log("Unlocking coinbase account");
var password = "";
try {
web3.personal.unlockAccount(web3.eth.coinbase, password);
} catch(e) {
console.log(e);
return;
}
console.log("Deploying the contract");
let contract = SampleContract.new({from: web3.eth.coinbase, gas: 1000000, data: code});
// Transaction has entered to geth memory pool
console.log("Your contract is being deployed in transaction at http://testnet.etherscan.io/tx/" + contract.transactionHash);
// http://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// We need to wait until any miner has included the transaction
// in a block to get the address of the contract
async function waitBlock() {
while (true) {
let receipt = web3.eth.getTransactionReceipt(contract.transactionHash);
if (receipt && receipt.contractAddress) {
console.log("Your contract has been deployed at http://testnet.etherscan.io/address/" + receipt.contractAddress);
console.log("Note that it might take 30 - 90 sceonds for the block to propagate befor it's visible in etherscan.io");
break;
}
console.log("Waiting a mined block to include your contract... currently in block " + web3.eth.blockNumber);
await sleep(4000);
}
}
waitBlock();
- Go here and generate a 12 word mnemonic, for "Coin" select ETH.
- Save the mnemonic (the 12 words), and, lower down the page, the first address (derivation path ending with 0) and private key somewhere safe.
- Transfer some ETH from your account to the one that corresponds to the mnemonic you just generated. You can import it into MetaMask with the private key and later return the unused ETH.
- Use this tutorial to deploy your smart contracts through truffle, of course don't forget to replace the mnemonic from the tutorial with the one you just generated.
- Adjust the Provider URL in the tutorial with your personal access URL you received in the mail from infura, also, in the network configuration of the code example of the tutorial, change "ropsten" to "mainnet".
then do truffle migrate --mainnet --dry-run
(only available in v4beta, but a good habit) to check, if everything is fine, and then truffle migrate --mainnet
.
EDIT: Of course you're also free to use the mnemonic from MetaMask.
Best Answer
Use wallet_addEthereumChain method - https://docs.metamask.io/guide/rpc-api.html#other-rpc-methods
Example code: