Truffle Introduction

Here’s the contract: @dan-i

pragma solidity 0.5.12;


contract HelloWorld {
  string message = "Hello World!";

  function getMessage() public view returns (string memory){
    return message;
  }

  function setMessage(string memory newMessage) public payable {
    message = newMessage;  
  }

}

And here’s the deployment file:

const HelloWorld = artifacts.require("HelloWorld");

module.exports = function(deployer, network, accounts) { //First parameter is the deployer - used to deploy contract, second is network parameter - which network are we on, third is Ganache accounts array
  deployer.deploy(HelloWorld).then(function(instance){ // instance here is the contract returned by deployer.deploy(HelloWorld) after it has deployed.
    instance.setMessage("Hello Again!", {value: 1000000, from: accounts[0]}).then(function(){ // here we want to call getMessage only ofter setMessage changed the message.
      instance.getMessage().then(function(message){ // Getmessage returns the message, so we can pass that to console.log
        console.log("Current message: " + message)
      });
    });
  });
};

Both are copied from filip’s code, so it’s strange that I’m getting an error where he wasn’t.

EDIT - I got something working by building on the code you posted and browsing Truffle documentation some more. This is what I have now:

const HelloWorld = artifacts.require("HelloWorld"); // This method returns a contract abstraction we can use in the rest of out script.

module.exports = async function (deployer, network, accounts) { //Can get access to accounts = await web3.eth.getAccounts() by specifying accounts parameter here
  await deployer.deploy(HelloWorld)
  const instance = await HelloWorld.deployed();
  await instance.setMessage('YOYO222', {gas: 1000000, from: accounts[0]}); //Using "gas" here instead of "value", it fixed the problem!"
  const result = await instance.getMessage();
  console.log(result)
};

Using {gas: 1000000, from: accounts[0]} instead of {value: 1000000, from: accounts[0]} works, so I assume that the error has something to do with specifying the transaction value, but I’m not sure what exactly the problem is. Any ideas @dan-i?

This works and is much easier to parse, thank you! Do you know why the code I posted didn’t work though? Would love to understand why problems occurred before moving on. Did the getMessage promise not get resolved for some reason?

1 Like

@filip or others who may be able to assist. I managed to successfully deploy the Helloworld contract to Ganache; and also use the console to return “Hello world!”, but after I updated with the setter function and tried to migrate, I received an "ExtendableError: Unknown network “ganache” error (see below). Ganache still shows the Helloworld contract as deployed. Are you able to advise why I am receiving this error? Thank you

"Compiling your contracts…

Everything is up to date, there is nothing to compile.

ExtendableError: Unknown network “ganache”. See your Truffle configuration file for available networks.
at Object.validateNetworkConfig (C:\Users\matth\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\environment\environment.js:110:1)
at Object.detect (C:\Users\matth\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\environment\environment.js:16:1)
at C:\Users\matth\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:206:1"

@Filip - further to my message above, the issue has nothing to do with the setter function. The ExtendableError: Unknown network “ganache” error arises because the “migrate” command produces an error when run under the ganache console. The command works fine when not within the console. Will this present an issue later in the course? While the “migrate” command produces the above error when run within the ganache console, I can run both the getter and setter functions within the console and it returns expected values. Again, is the aforementioned error likely to present issues later on or can it be ignored?

Hey @cswan1

Have you created a truffle project by running truffle init?
Please cd into your main project folder and type truffle migrate --reset with ganache open.

Keep me posted,
Dani

Hi pal. ive been through this course up until phase 1. I am taking some notes on payable functions to create my Dapp and im having the exact same issue here. Both @filip and @dan-i (the legend) “code” are both kicking me in the balls with Error: Error: Error: Returned error: VM Exception while processing transaction: revert

i cant work it out. is it the value of wei im trying to receive from the account? have i got that wrong?

cant work it out.

EDIT.

Done some more coding and i have came to this… An error saying ‘coinflip’ undefined

CODE IS BELLOW

const HelloWorld = artifacts.require("Coinflip"); // This method returns a contract abstraction we can use in the rest of out script.

module.exports = async function (deployer, network, accounts) { //Can get access to accounts = await web3.eth.getAccounts() by specifying accounts parameter here
  await deployer.deploy(Coinflip)
  const instance = await Coinflip.deployed();
  await instance.setMessage('Hello', {gas: 1000000, from: accounts[0]}); //Using "gas" here instead of "value", it fixed the problem!"
  const result = await instance.getMessage();
  console.log(result)
};

:).

Hey @Rob_McCourt

await deployer.deploy(Coinflip) should be await deployer.deploy(HelloWorld)
Also you can remove the gas property from

await instance.setMessage('Hello', {gas: 1000000,
give it a try

Hi pal, yes i get this but i am doing the Betting Dapp Hand in project now and the contract is names Coinflip my migration file is also called coin flip so this would be correct wouldn’t it?

i have a full Project on ATOM now that is for the Betting Smart contract/Dapp now… Does that make sense?

EDIT…

This doesn’t make one bit of sense here, wait for it …

This code worked fine in CLI…

Question is why?

I have no file named Helloworld at all as im building my new Smart Contract Called Coinflip? pmsl… mental.

HERE IS THE CODE.

const Helloworld = artifacts.require("Coinflip");

module.exports = function(deployer, network, accounts){
  deployer.deploy(Helloworld).then(function(instance){
      instance.setMessage("Hello Again!", {gas: 1000000, from: accounts[0]}).then(function(){
        instance.getMessage().then(function(message){
          console.log("Current message: " + message);
        });
      });
  });
};

so i changed my migration

1 Like

Thanks @dan-i … I executed the “truffle migrate --reset” command within the main project folder with Ganache open. The 'ExtendableError: Unknown network “ganache”. ’ remains, but it only appears when running commands within the Truffle console. I can migrate outside of the truffle console, e.g. within the main project folder as you suggested, and it appears to work fine. Doing that, the contracts are deployed to Ganache as expected. It’s just the Truffle console that produces the aforementioned error when “migrate --reset” is executed from the console. That being the case, I could not test the console.log within the deploy file.

Hey Rob,

You are declaring const Helloworld = artifacts.require(“Coinflip”) therefore you will deploy the contact you just declared Helloworld.

image

Let me know if you have questions.

cheers,
Dani

Not sure what you are doing there
The 'ExtendableError: Unknown network “ganache”. ’ remains, but it only appears when running commands within the Truffle console. I can migrate outside of the truffle console, e.g. within the main project folder as you suggested, and it appears to work fine. Doing that, the contracts are deployed to Ganache as expected.

What do you mean that you can migrate outside the truffle console / within the main project folder as you suggested, and it appears to work fine?

@dan-i Thanks for your assistance efforts. What I mean is that when I am in the following folder “c:\users\cs\ethereum-coding-advanced\helloworld>” (ie the main project folder), I can run the script “truffle migrate --reset” and the contracts are deployed in Ganache. There are no errors. When I type “truffle console”, the following prompt is displayed “truffle(ganache)>”. This is expected. I’m therefore within the truffle console. If I type “migrate --reset” while at the “truffle(ganache)>” prompt, that is when the error occurs. In brief, the “migrate” command works if I do not go to the “truffle(ganache)>” prompt. If I’m at that prompt, it will not work. I get the aforementioned error that ganache is an unknown network. If this cannot be resolved, I am hoping I can go through the rest of the course without going to that prompt because, so far, all appears to be working without going there.

1 Like

I just found myself having the same error. I currently go back to the command prompt by closing out of all and re-starting the process. Are you aware of any commands that I could use while in truffle console that can take me back to command prompt?

Hey Everyone. for some bizarre reason the console.log isn’t working for me :frowning: something todo with the promise I guess. works fine thought truffle console…

Does anyone have any clue?

const Helloworld = artifacts.require(“Helloworld”);

module.exports = function (deployer) {
deployer.deploy(Helloworld).then(function (instance) {
instance.setMessage(“Hello again”).then(function () {
instance.getMessage().then(function (message) {
console.log("The current message is: " + message);
});
})
});
};

If I log during a truffle tests its ok, maybe its the truffle version im using i dunno but its rly annoying haha

const Helloworld = artifacts.require(’…/contracts/helloworld.sol’);

contract(‘Helloworld’, () => {

var helloworld;
it(‘Should deploy smart contract properly’, async () => {
helloworld = await Helloworld.deployed();
console.log(helloworld.address);
assert(helloworld.address !== ‘’);
});

it(‘should have updated message’, async () => {
helloworld.getMessage().then(value => {
console.log("the message is " + value)
assert(value === ‘Hello again’); //<-- update has taken
})
})
});

The command “.exit” allows me to quit the truffle console without closing PowerShell

1 Like

Run the command truffle migrate --reset or truffle console >> migrate --reset is the same thing and you should face the same result.
Are you inside your main project folder when running truffle console >> migrate --reset?
If that’s the case please screenshot your terminal so that we can double check that your are running the commands in the right folder.

Thank you, I appreciate your response +1

Hi. Really pumped to start this course after 101 part. I am running into some problems with the truffle installation though. Sorry for being noob with mac OS, windows is more my thing but doing these courses on my macbook for now. While installing truffle with npm I get this message:

I dont know where to start to fix this. I tried to do something with mkdirp but could not get it to work. Could you please give a step by step guide on how to fix this issue. Thanks!

I am running on catalina OS which seems to have some problems with access rights, but I ran into the same problem when trying to do the installation on mojave as well…

Many thanks! Hope I can get back into the study loop as quickly as possible.

I’m completely frustrated with this course at this point. i tried to redo the lesson of ‘Deploying the First Contract’ and am having no success. I uninstalled node.js and reinstalled.

When I type ‘truffle migrate’, it only executes the migration contract and not the Helloworld contract. Any help that I could get would be appreciated.

Plus, what do I need to do to change the pathway of the command line somewhere else? I’m at 'C:\Users\David>
I cannot change this at all and am completely frustrated.

Hey @Kraken,
It looks to me, that you installed npm under a different user, since your user is missing the permission to access npms files in /usr/local/lib/node_modules.
you can check your own user using whoami and the permissions on the npm ls -alrth /usr/local/lib/node_modules. If its not matching thats probably your problem…

I recommend installing npm using brew.