Last active
January 5, 2021 10:00
-
-
Save anupam-io/13fd3c87ffe1f5d0506130faa00c4368 to your computer and use it in GitHub Desktop.
Deploy scripts for solidity with web3 and wallet/ganache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require('web3'); | |
const compiledContract = require('./../build/MyContract.json'); | |
const rpcEndpoint = 'rpc-END-POINT'; | |
// you will get from ganache-GUI | |
// Setting up the provider | |
const provider = new Web3.providers.HttpProvider(rpcEndpoint); | |
const web3 = new Web3(provider); | |
const minGas = 1000000; | |
const deploy = async()=>{ | |
const accounts = await web3.eth.getAccounts(); | |
console.log('Attempting to deploy from account ', accounts[0]); | |
const res = await new web3.eth.Contract(compiledContract.abi) | |
.deploy({ data: compiledContract.evm.bytecode.object }) | |
.send({ from: accounts[0], gas: minGas }); | |
console.log("Successfully deployed at: ", res.options.address); | |
return process.exit(1); | |
}; | |
deploy(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HDWalletProvider = require('truffle-hdwallet-provider'); | |
const Web3 = require('web3'); | |
const compiledContract = require('./../build/MyContract.json'); | |
const seedPhrase = 'YOUR-SEED-PHRASE'; | |
// a group of words that allow access to a cryptocurrency wallet | |
const rpcEndpoint = 'rpc-END-POINT'; | |
// an endpint for connecting your web3 instance wallet and the blockchain node | |
// Setting up the provider | |
const provider = new HDWalletProvider(seedPhrase, rpcEndpoint); | |
const web3 = new Web3(provider); | |
const minGas = 1000000; | |
const deploy = async()=>{ | |
const accounts = await web3.eth.getAccounts(); | |
console.log('Attempting to deploy from account ', accounts[0]); | |
const res = await new web3.eth.Contract(compiledContract.abi) | |
.deploy({ data: compiledContract.evm.bytecode.object }) | |
.send({ from: accounts[0], gas: minGas }); | |
console.log("Successfully deployed at: ", res.options.address); | |
return process.exit(1); | |
}; | |
deploy(); |
How to use?
- Configure the
compiledContract
path correctly. - Set
rpcEndPoint
andseedPhrase
appropriately. node deploy_wallet.js
Compatible with:
solc: 0.8.0
truffle-hdwallet-provider: 9.0.1
web3: 1.3.1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do checkout solcTemplate.