How do I implement simple JS code to deploy a compiled smart contract to ganache-cli? [on hold]Calling...
Do higher etale homotopy groups of spectrum of a field always vanish?
Dystopian novel where telepathic humans live under a dome
Are paired adjectives bad style?
When was drinking water recognized as crucial in marathon running?
Why do members of Congress in committee hearings ask witnesses the same question multiple times?
How can atoms be electrically neutral when there is a difference in the positions of the charges?
In iTunes 12 on macOS, how can I reset the skip count of a song?
Real life puzzle: Unknown alphabet or shorthand
Get length of the longest sequence of numbers with the same sign
What is the difference between a forward slip and a side slip?
Can a space-faring robot still function over a billion years?
Sometimes a banana is just a banana
What could trigger powerful quakes on icy world?
How do I deal with being jealous of my own players?
What does @RC mean in SSDT SQL Server Unit Testing?
What's the values for the Freq_Interval column in MSDB.dbo.SysSchedules when Freq_Type is weekly and more than one day is selected in the schedule?
If nine coins are tossed, what is the probability that the number of heads is even?
Is there a frame of reference in which I was born before I was conceived?
Inverse of the covariance matrix of a multivariate normal distribution
Starting index at zero
How can I handle a player who pre-plans arguments about my rulings on RAW?
Is divide-by-zero a security vulnerability?
What are the issues with an additional (limited) concentration slot instead of Bladesong?
Why is working on the same position for more than 15 years not a red flag?
How do I implement simple JS code to deploy a compiled smart contract to ganache-cli? [on hold]
Calling event.watch() and event.get() prevents truffle JS tests to terminatehow to make getAccounts() return a list of all 10 accounts?How to properly simulate mining time with Ganache CLIHow to synchronise Ganache-CLI accounts with Ganache-UI?Remix and ganache-cli | Contract overrideHow to deploy contract in Mist Browser connected to Ganache?Create a new contract from other contract by connecting to ganache-cliHow to deploy smart contract with visible source code (etherscan)?How to set maximum block gas in ganache-cliCan I get geth.ipc if I use ganache-cli to deploy a default private test ethereum envWeb3JS throws InvalidResponse without Ganache core ever receiving a request?Why can't I fetch my Metamask account address with Next.js's getInitialProps?
Everything I've tried results in strange errors, and it's been difficult to get this working. I've been struggling with this for a while, especially having problems determining which versions of web3 and ganache will work together. The code is these libraries is evolving quickly and they are constantly in flux. This results in many version combinations having broken functionality that's difficult to diagnose.
In particular, there seems to be promise handling async problems with web3 versions later than 1.0.0-beta.37, which makes them not work at all with ganache. What happens is web3.eth.getAccounts() will fail, with no known workaround.
web3js ganache truffle-deployment
New contributor
put on hold as unclear what you're asking by smarx, shane, Crissi Mariam Robert, Achala Dissanayake, Rosco Kalis 15 hours ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
Everything I've tried results in strange errors, and it's been difficult to get this working. I've been struggling with this for a while, especially having problems determining which versions of web3 and ganache will work together. The code is these libraries is evolving quickly and they are constantly in flux. This results in many version combinations having broken functionality that's difficult to diagnose.
In particular, there seems to be promise handling async problems with web3 versions later than 1.0.0-beta.37, which makes them not work at all with ganache. What happens is web3.eth.getAccounts() will fail, with no known workaround.
web3js ganache truffle-deployment
New contributor
put on hold as unclear what you're asking by smarx, shane, Crissi Mariam Robert, Achala Dissanayake, Rosco Kalis 15 hours ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
What code did you tray that is not working? If you search the site you will find several example working. But probably is better if we explain why your code is not working.
– Ismael
yesterday
add a comment |
Everything I've tried results in strange errors, and it's been difficult to get this working. I've been struggling with this for a while, especially having problems determining which versions of web3 and ganache will work together. The code is these libraries is evolving quickly and they are constantly in flux. This results in many version combinations having broken functionality that's difficult to diagnose.
In particular, there seems to be promise handling async problems with web3 versions later than 1.0.0-beta.37, which makes them not work at all with ganache. What happens is web3.eth.getAccounts() will fail, with no known workaround.
web3js ganache truffle-deployment
New contributor
Everything I've tried results in strange errors, and it's been difficult to get this working. I've been struggling with this for a while, especially having problems determining which versions of web3 and ganache will work together. The code is these libraries is evolving quickly and they are constantly in flux. This results in many version combinations having broken functionality that's difficult to diagnose.
In particular, there seems to be promise handling async problems with web3 versions later than 1.0.0-beta.37, which makes them not work at all with ganache. What happens is web3.eth.getAccounts() will fail, with no known workaround.
web3js ganache truffle-deployment
web3js ganache truffle-deployment
New contributor
New contributor
New contributor
asked yesterday
djenning90djenning90
608
608
New contributor
New contributor
put on hold as unclear what you're asking by smarx, shane, Crissi Mariam Robert, Achala Dissanayake, Rosco Kalis 15 hours ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as unclear what you're asking by smarx, shane, Crissi Mariam Robert, Achala Dissanayake, Rosco Kalis 15 hours ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
What code did you tray that is not working? If you search the site you will find several example working. But probably is better if we explain why your code is not working.
– Ismael
yesterday
add a comment |
What code did you tray that is not working? If you search the site you will find several example working. But probably is better if we explain why your code is not working.
– Ismael
yesterday
What code did you tray that is not working? If you search the site you will find several example working. But probably is better if we explain why your code is not working.
– Ismael
yesterday
What code did you tray that is not working? If you search the site you will find several example working. But probably is better if we explain why your code is not working.
– Ismael
yesterday
add a comment |
2 Answers
2
active
oldest
votes
After more than a week of google searches, scouring stack exchange, combining bits and pieces of what I found, and with my own trial and error, I finally came up with everything needed to successfully deploy a compiled smart contract ganache. It wasn't easy to figure out. I haven't seen a full solution presented anywhere in my searches, so I wanted to share my findings here, in case you're in a similar situation.
The first key thing is to only use versions of web3 and ganache-cli that are compatible. I've noted my findings in the code below. It looks like very recent web3 and recent ganache versions are currently incompatible due to bugs, which are probably being fixed. If you'd like to reproduce those problems, simply upgrade to a version of web3 later than 1.0.0-beta.37 and try the code posted below.
The second thing I learned was how to correctly implement the async programming, which takes a little care, especially if you're new to JavaScript as I am.
Here is fully working code to deploy a contract from its compiled JSON representation using Web3 and gnache:
// Tested using web3@^1.0.0-beta.37 and ganache-cli@^6.4.1
const ganache = require('ganache-cli');
const Web3 = require('web3');
const fs = require("fs");
async function deploy() {
const provider = ganache.provider();
provider.setMaxListeners(15); // Suppress MaxListenersExceededWarning warning
const web3 = new Web3(provider);
this.accounts = await web3.eth.getAccounts();
// Read in the compiled contract code and fetch ABI description and the bytecode as objects
const compiled = JSON.parse(fs.readFileSync("output/contracts.json"));
const abi = compiled.contracts["ContractName.sol"]["ContractName"].abi;
const bytecode = compiled.contracts['ContractName.sol']['ContractName'].evm.bytecode.object;
// Deploy the contract and send it gas to run.
this.contract = await new web3.eth.Contract(abi)
.deploy({data:'0x'+ bytecode, arguments: []})
.send({from: this.accounts[0], gas:'5000000'});
return this;
}
deploy().then(useContract);
function useContract(result) {
// Use result.accounts and result.contract here to do what you like.
console.log('Contract deployed to: ' + result.contract.options.address);
console.log('Owner address is: ' + result.accounts[0]);
}
If this helps you, please upvote. I'm a new contributor here. Thanks!
New contributor
add a comment |
I upvoted your answer because it seems like a nice template for doing it all with nothing but Web3. This is a good thing to know about but a little tedious, in my opinion.
For the benefit of other readers who find this question/answer, the Truffle framework addresses this and other concerns with higher-level abstractions so we don't have to get down in the weeds to address routine tasks.
The problem is that it's another layer to learn which can be a little much for newcomers already faced with cli, web3 and solidity.
Migrations configuration can coordinate multi-contract deployments (roughly):
// pre-amble, dependencies, etc.
deployer.deploy(ContractName);
// carry on ... deploy another contract?
Run the migrations:
$ truffle migrate
Great.
Truffle is helpful for the next step, when working with deployed contracts - unit tests, servers, user-interface.
This sort of thing:
myContract.contractFunction(args) ...
is more pleasant to work with than the Web3 functions that are wrapped up inside.
It's good to know Truffle exists because answers on sites like SE may use truffle syntax.
Hope it helps.
I'm new to this and I only have learned Web3 so far, so your post that there are higher-level frameworks available comes as good news to me. Thanks for sharing that!
– djenning90
yesterday
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
After more than a week of google searches, scouring stack exchange, combining bits and pieces of what I found, and with my own trial and error, I finally came up with everything needed to successfully deploy a compiled smart contract ganache. It wasn't easy to figure out. I haven't seen a full solution presented anywhere in my searches, so I wanted to share my findings here, in case you're in a similar situation.
The first key thing is to only use versions of web3 and ganache-cli that are compatible. I've noted my findings in the code below. It looks like very recent web3 and recent ganache versions are currently incompatible due to bugs, which are probably being fixed. If you'd like to reproduce those problems, simply upgrade to a version of web3 later than 1.0.0-beta.37 and try the code posted below.
The second thing I learned was how to correctly implement the async programming, which takes a little care, especially if you're new to JavaScript as I am.
Here is fully working code to deploy a contract from its compiled JSON representation using Web3 and gnache:
// Tested using web3@^1.0.0-beta.37 and ganache-cli@^6.4.1
const ganache = require('ganache-cli');
const Web3 = require('web3');
const fs = require("fs");
async function deploy() {
const provider = ganache.provider();
provider.setMaxListeners(15); // Suppress MaxListenersExceededWarning warning
const web3 = new Web3(provider);
this.accounts = await web3.eth.getAccounts();
// Read in the compiled contract code and fetch ABI description and the bytecode as objects
const compiled = JSON.parse(fs.readFileSync("output/contracts.json"));
const abi = compiled.contracts["ContractName.sol"]["ContractName"].abi;
const bytecode = compiled.contracts['ContractName.sol']['ContractName'].evm.bytecode.object;
// Deploy the contract and send it gas to run.
this.contract = await new web3.eth.Contract(abi)
.deploy({data:'0x'+ bytecode, arguments: []})
.send({from: this.accounts[0], gas:'5000000'});
return this;
}
deploy().then(useContract);
function useContract(result) {
// Use result.accounts and result.contract here to do what you like.
console.log('Contract deployed to: ' + result.contract.options.address);
console.log('Owner address is: ' + result.accounts[0]);
}
If this helps you, please upvote. I'm a new contributor here. Thanks!
New contributor
add a comment |
After more than a week of google searches, scouring stack exchange, combining bits and pieces of what I found, and with my own trial and error, I finally came up with everything needed to successfully deploy a compiled smart contract ganache. It wasn't easy to figure out. I haven't seen a full solution presented anywhere in my searches, so I wanted to share my findings here, in case you're in a similar situation.
The first key thing is to only use versions of web3 and ganache-cli that are compatible. I've noted my findings in the code below. It looks like very recent web3 and recent ganache versions are currently incompatible due to bugs, which are probably being fixed. If you'd like to reproduce those problems, simply upgrade to a version of web3 later than 1.0.0-beta.37 and try the code posted below.
The second thing I learned was how to correctly implement the async programming, which takes a little care, especially if you're new to JavaScript as I am.
Here is fully working code to deploy a contract from its compiled JSON representation using Web3 and gnache:
// Tested using web3@^1.0.0-beta.37 and ganache-cli@^6.4.1
const ganache = require('ganache-cli');
const Web3 = require('web3');
const fs = require("fs");
async function deploy() {
const provider = ganache.provider();
provider.setMaxListeners(15); // Suppress MaxListenersExceededWarning warning
const web3 = new Web3(provider);
this.accounts = await web3.eth.getAccounts();
// Read in the compiled contract code and fetch ABI description and the bytecode as objects
const compiled = JSON.parse(fs.readFileSync("output/contracts.json"));
const abi = compiled.contracts["ContractName.sol"]["ContractName"].abi;
const bytecode = compiled.contracts['ContractName.sol']['ContractName'].evm.bytecode.object;
// Deploy the contract and send it gas to run.
this.contract = await new web3.eth.Contract(abi)
.deploy({data:'0x'+ bytecode, arguments: []})
.send({from: this.accounts[0], gas:'5000000'});
return this;
}
deploy().then(useContract);
function useContract(result) {
// Use result.accounts and result.contract here to do what you like.
console.log('Contract deployed to: ' + result.contract.options.address);
console.log('Owner address is: ' + result.accounts[0]);
}
If this helps you, please upvote. I'm a new contributor here. Thanks!
New contributor
add a comment |
After more than a week of google searches, scouring stack exchange, combining bits and pieces of what I found, and with my own trial and error, I finally came up with everything needed to successfully deploy a compiled smart contract ganache. It wasn't easy to figure out. I haven't seen a full solution presented anywhere in my searches, so I wanted to share my findings here, in case you're in a similar situation.
The first key thing is to only use versions of web3 and ganache-cli that are compatible. I've noted my findings in the code below. It looks like very recent web3 and recent ganache versions are currently incompatible due to bugs, which are probably being fixed. If you'd like to reproduce those problems, simply upgrade to a version of web3 later than 1.0.0-beta.37 and try the code posted below.
The second thing I learned was how to correctly implement the async programming, which takes a little care, especially if you're new to JavaScript as I am.
Here is fully working code to deploy a contract from its compiled JSON representation using Web3 and gnache:
// Tested using web3@^1.0.0-beta.37 and ganache-cli@^6.4.1
const ganache = require('ganache-cli');
const Web3 = require('web3');
const fs = require("fs");
async function deploy() {
const provider = ganache.provider();
provider.setMaxListeners(15); // Suppress MaxListenersExceededWarning warning
const web3 = new Web3(provider);
this.accounts = await web3.eth.getAccounts();
// Read in the compiled contract code and fetch ABI description and the bytecode as objects
const compiled = JSON.parse(fs.readFileSync("output/contracts.json"));
const abi = compiled.contracts["ContractName.sol"]["ContractName"].abi;
const bytecode = compiled.contracts['ContractName.sol']['ContractName'].evm.bytecode.object;
// Deploy the contract and send it gas to run.
this.contract = await new web3.eth.Contract(abi)
.deploy({data:'0x'+ bytecode, arguments: []})
.send({from: this.accounts[0], gas:'5000000'});
return this;
}
deploy().then(useContract);
function useContract(result) {
// Use result.accounts and result.contract here to do what you like.
console.log('Contract deployed to: ' + result.contract.options.address);
console.log('Owner address is: ' + result.accounts[0]);
}
If this helps you, please upvote. I'm a new contributor here. Thanks!
New contributor
After more than a week of google searches, scouring stack exchange, combining bits and pieces of what I found, and with my own trial and error, I finally came up with everything needed to successfully deploy a compiled smart contract ganache. It wasn't easy to figure out. I haven't seen a full solution presented anywhere in my searches, so I wanted to share my findings here, in case you're in a similar situation.
The first key thing is to only use versions of web3 and ganache-cli that are compatible. I've noted my findings in the code below. It looks like very recent web3 and recent ganache versions are currently incompatible due to bugs, which are probably being fixed. If you'd like to reproduce those problems, simply upgrade to a version of web3 later than 1.0.0-beta.37 and try the code posted below.
The second thing I learned was how to correctly implement the async programming, which takes a little care, especially if you're new to JavaScript as I am.
Here is fully working code to deploy a contract from its compiled JSON representation using Web3 and gnache:
// Tested using web3@^1.0.0-beta.37 and ganache-cli@^6.4.1
const ganache = require('ganache-cli');
const Web3 = require('web3');
const fs = require("fs");
async function deploy() {
const provider = ganache.provider();
provider.setMaxListeners(15); // Suppress MaxListenersExceededWarning warning
const web3 = new Web3(provider);
this.accounts = await web3.eth.getAccounts();
// Read in the compiled contract code and fetch ABI description and the bytecode as objects
const compiled = JSON.parse(fs.readFileSync("output/contracts.json"));
const abi = compiled.contracts["ContractName.sol"]["ContractName"].abi;
const bytecode = compiled.contracts['ContractName.sol']['ContractName'].evm.bytecode.object;
// Deploy the contract and send it gas to run.
this.contract = await new web3.eth.Contract(abi)
.deploy({data:'0x'+ bytecode, arguments: []})
.send({from: this.accounts[0], gas:'5000000'});
return this;
}
deploy().then(useContract);
function useContract(result) {
// Use result.accounts and result.contract here to do what you like.
console.log('Contract deployed to: ' + result.contract.options.address);
console.log('Owner address is: ' + result.accounts[0]);
}
If this helps you, please upvote. I'm a new contributor here. Thanks!
New contributor
New contributor
answered yesterday
djenning90djenning90
608
608
New contributor
New contributor
add a comment |
add a comment |
I upvoted your answer because it seems like a nice template for doing it all with nothing but Web3. This is a good thing to know about but a little tedious, in my opinion.
For the benefit of other readers who find this question/answer, the Truffle framework addresses this and other concerns with higher-level abstractions so we don't have to get down in the weeds to address routine tasks.
The problem is that it's another layer to learn which can be a little much for newcomers already faced with cli, web3 and solidity.
Migrations configuration can coordinate multi-contract deployments (roughly):
// pre-amble, dependencies, etc.
deployer.deploy(ContractName);
// carry on ... deploy another contract?
Run the migrations:
$ truffle migrate
Great.
Truffle is helpful for the next step, when working with deployed contracts - unit tests, servers, user-interface.
This sort of thing:
myContract.contractFunction(args) ...
is more pleasant to work with than the Web3 functions that are wrapped up inside.
It's good to know Truffle exists because answers on sites like SE may use truffle syntax.
Hope it helps.
I'm new to this and I only have learned Web3 so far, so your post that there are higher-level frameworks available comes as good news to me. Thanks for sharing that!
– djenning90
yesterday
add a comment |
I upvoted your answer because it seems like a nice template for doing it all with nothing but Web3. This is a good thing to know about but a little tedious, in my opinion.
For the benefit of other readers who find this question/answer, the Truffle framework addresses this and other concerns with higher-level abstractions so we don't have to get down in the weeds to address routine tasks.
The problem is that it's another layer to learn which can be a little much for newcomers already faced with cli, web3 and solidity.
Migrations configuration can coordinate multi-contract deployments (roughly):
// pre-amble, dependencies, etc.
deployer.deploy(ContractName);
// carry on ... deploy another contract?
Run the migrations:
$ truffle migrate
Great.
Truffle is helpful for the next step, when working with deployed contracts - unit tests, servers, user-interface.
This sort of thing:
myContract.contractFunction(args) ...
is more pleasant to work with than the Web3 functions that are wrapped up inside.
It's good to know Truffle exists because answers on sites like SE may use truffle syntax.
Hope it helps.
I'm new to this and I only have learned Web3 so far, so your post that there are higher-level frameworks available comes as good news to me. Thanks for sharing that!
– djenning90
yesterday
add a comment |
I upvoted your answer because it seems like a nice template for doing it all with nothing but Web3. This is a good thing to know about but a little tedious, in my opinion.
For the benefit of other readers who find this question/answer, the Truffle framework addresses this and other concerns with higher-level abstractions so we don't have to get down in the weeds to address routine tasks.
The problem is that it's another layer to learn which can be a little much for newcomers already faced with cli, web3 and solidity.
Migrations configuration can coordinate multi-contract deployments (roughly):
// pre-amble, dependencies, etc.
deployer.deploy(ContractName);
// carry on ... deploy another contract?
Run the migrations:
$ truffle migrate
Great.
Truffle is helpful for the next step, when working with deployed contracts - unit tests, servers, user-interface.
This sort of thing:
myContract.contractFunction(args) ...
is more pleasant to work with than the Web3 functions that are wrapped up inside.
It's good to know Truffle exists because answers on sites like SE may use truffle syntax.
Hope it helps.
I upvoted your answer because it seems like a nice template for doing it all with nothing but Web3. This is a good thing to know about but a little tedious, in my opinion.
For the benefit of other readers who find this question/answer, the Truffle framework addresses this and other concerns with higher-level abstractions so we don't have to get down in the weeds to address routine tasks.
The problem is that it's another layer to learn which can be a little much for newcomers already faced with cli, web3 and solidity.
Migrations configuration can coordinate multi-contract deployments (roughly):
// pre-amble, dependencies, etc.
deployer.deploy(ContractName);
// carry on ... deploy another contract?
Run the migrations:
$ truffle migrate
Great.
Truffle is helpful for the next step, when working with deployed contracts - unit tests, servers, user-interface.
This sort of thing:
myContract.contractFunction(args) ...
is more pleasant to work with than the Web3 functions that are wrapped up inside.
It's good to know Truffle exists because answers on sites like SE may use truffle syntax.
Hope it helps.
answered yesterday
Rob HitchensRob Hitchens
28.5k74481
28.5k74481
I'm new to this and I only have learned Web3 so far, so your post that there are higher-level frameworks available comes as good news to me. Thanks for sharing that!
– djenning90
yesterday
add a comment |
I'm new to this and I only have learned Web3 so far, so your post that there are higher-level frameworks available comes as good news to me. Thanks for sharing that!
– djenning90
yesterday
I'm new to this and I only have learned Web3 so far, so your post that there are higher-level frameworks available comes as good news to me. Thanks for sharing that!
– djenning90
yesterday
I'm new to this and I only have learned Web3 so far, so your post that there are higher-level frameworks available comes as good news to me. Thanks for sharing that!
– djenning90
yesterday
add a comment |
What code did you tray that is not working? If you search the site you will find several example working. But probably is better if we explain why your code is not working.
– Ismael
yesterday