How do I implement simple JS code to deploy a compiled smart contract to ganache-cli?Calling event.watch()...
Find the next monthly expiration date
Is there a low-level alternative to Animate Objects?
Which aircraft had such a luxurious-looking navigator's station?
A "strange" unit radio astronomy
Where was Karl Mordo in Infinity War?
Auto Insert date into Notepad
chrony vs. systemd-timesyncd – What are the differences and use cases as NTP clients?
Has the Isbell–Freyd criterion ever been used to check that a category is concretisable?
Must a tritone substitution use a dominant seventh chord?
Why do members of Congress in committee hearings ask witnesses the same question multiple times?
How to avoid being sexist when trying to employ someone to function in a very sexist environment?
Hacker Rank: Array left rotation
Did 5.25" floppies undergo a change in magnetic coating?
"Murder!" The knight said
What am I? I am in theaters and computer programs
Easy code troubleshooting in wordpress
How would we write a misogynistic character without offending people?
What is the wife of a henpecked husband called?
Did Amazon pay $0 in taxes last year?
Where is this triangular-shaped space station from?
Linear regression when Y is bounded and discrete
What is the difference between throw e and throw new Exception(e)?
Does music exist in Panem? And if so, what kinds of music?
Logistics of a hovering watercraft in a fantasy setting
How do I implement simple JS code to deploy a compiled smart contract to ganache-cli?
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
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
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 10 hours ago
djenning90djenning90
608
608
New contributor
New contributor
add a comment |
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
6 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "642"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
djenning90 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f67910%2fhow-do-i-implement-simple-js-code-to-deploy-a-compiled-smart-contract-to-ganache%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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 10 hours ago
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
6 hours ago
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
6 hours ago
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 9 hours ago
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
6 hours ago
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
6 hours ago
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
6 hours ago
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
6 hours ago
add a comment |
djenning90 is a new contributor. Be nice, and check out our Code of Conduct.
djenning90 is a new contributor. Be nice, and check out our Code of Conduct.
djenning90 is a new contributor. Be nice, and check out our Code of Conduct.
djenning90 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Ethereum Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f67910%2fhow-do-i-implement-simple-js-code-to-deploy-a-compiled-smart-contract-to-ganache%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown