Truffle Introduction

Hi @ChrisPap

What were you doing before turning off your laptop?
Which commands are not recognised anymore?

Regards,
Dani

Hello @dan-i,

The last think i did was to succesfully get the getter function.
instance.getMessage()

After that when i turn on my laptop, i tried to continue to the course but unfortunately nothing was applicable any more.
Even my contracts on GANACHE was deleted.

I strongly believe that i need to do everthing from the beggining which is fine cause i will refresh what i have learnt, but do you know what should i do or how to save the progress?

In the future it will be really incovinient to do the entire course from scratch.

Thank you

Hi @ChrisPap

After that when i turn on my laptop, i tried to continue to the course but unfortunately nothing was applicable any more.
Even my contracts on GANACHE was deleted.

This is Ganache normal behaviour, it is indeed a local blockchains that does not save data once it’s shut off.

Nothing to be afraid here :slight_smile:

Cheers,
Dani

Note that with truffle 5.1.60 the console.log doesn’t work when its inside the setMessage like so:

instance.setMessage("Hello Again!").then(function(){
    instance.getMessage().then(function(message){
        console.log("Current message: " + message);
    });
});

it does work and returns the right value if I put the instance.getMessage outside of the instance.setMessage but this is probably not a good practice since those are async functions and theres no ways to be sure they will always perfom in the proper order. Is that right? @filip
I downgraded to 5.0.42 and it now works like it should. Do they often break things in newer versions? is that why you are recommending to use version 5.0.42? is there a general rule or a way of knowing which versions work best as we move through time?

Hey @badtoken

Because these tools continuously improve, sometimes happens that we need to adapt and update with them :slight_smile:

I would use async functions there anyway, the compiler will execute them from top to bottom and they will be executed in order.

Try this one

module.exports = async function (deployer) {
  await deployer.deploy(Testing)
  const instance = await Testing.deployed();
  await instance.setMessage('Hello');
  const result = await instance.getMessage();
  console.log(result)
};

Happy learning,
Dani

1 Like

what if setMessage throws an error? I would think that with the “then” format it will stop there and not execute getMessage? Whereas with the async format it will run anyways? Then you have to add more error handling … not a big issue here cause we are just doing a getMessage but that could be bad if doing things like transfers

unless the migrations scripts default behavior is to stop right away on errors?

Hi @badtoken

what if setMessage throws an error? I would think that with the “then” format it will stop there and not execute getMessage? Whereas with the async format it will run anyways?

Both await and then will act the same way.
Using async function just makes your code much more readable.

If setMessage() reverts you will see a failed transaction in your contract.

1 Like

I agree its much more readable and is what I use in other languages :wink:

1 Like

I’ve been having trouble trying to install Truffle through the terminal. i keep getting this mesage

I send 1 ETH with the { value: xxx } but the balance inside the smart contract is still0. how can this be possible?

Hey @ImRONMAN

Truffle seems to be installed correctly.
Another student had this issue, we fixed by proceeding as follow.

This answer is not useful

I had a similar problem. I ran npm i -g truffle and then when I tried to run truffle init I got an error: zsh: command not found: truffle . What solved it for me is to create a local node_modules with truffle installed in it, and then run that copy.

  1. run npm init and make a new npm project
  2. run npm i truffle
  3. run ./node_modules/.bin/truffle init and it should work!

Hi @niore

There might be an issue in your code, can you please post it here?

Smart contract


pragma solidity 0.5.12;

contract Helloworld {
  string message = "Hello World";

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

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

Migrationfile

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

module.exports = function (deployer, network, accounts) {
  deployer.deploy(Helloworld).then(instance => {
    instance.setMessage('hello again', { value: 1000000000000000000, from: accounts[0] });
  });
};

You should get an error when deploy the contract because you are sending ETH to a non-payable function.
Set setMessage() to payable and you should be able to proceed.

1 Like

Hi everyone

when I run in the truffle console the command

instance.getMessage()

instead of “Hello world” I get:

{ tx:
   '0xe6e50da48c0805736c91aa0e52aabe6955119805d1813557f876c2370704b45c',
  receipt:
   { transactionHash:
      '0xe6e50da48c0805736c91aa0e52aabe6955119805d1813557f876c2370704b45c',
     transactionIndex: 0,
     blockHash:
      '0x550539db79d5637182bf365b759e8cf3a323ca9479d889e7def6bcf3dbc5d83c',
     blockNumber: 17,
     from: '0xd1e940ed08d8500d120fc82dc92db6e3317d6f6d',
     to: '0xbabf7e7c7dcc032c1d4e40ffdceecb1d4df759df',
     gasUsed: 22687,
     cumulativeGasUsed: 22687,
     contractAddress: null,
     logs: [],
     status: true,
     logsBloom:
      '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
     v: '0x1c',
     r:
      '0x1c9d0803a974d105d1ecede3d306657ac69412cff91fae946eb2185d2bd3a8cb',
     s:
      '0x344f1b80221a99734d9d62776a0ade6bc9926d3272e3c5594bf9b6687779d2b7',
     rawLogs: [] },
  logs: [] }

Any ideas?
This is my contract code:

pragma solidity 0.5.12;

contract Helloworld {
    string message = "Hello world";

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

Hi @Adrift

The function getMessage() has to be view.

function getMessage() public view returns(string memory){

Cheers,
Dani

1 Like

@dan-i thanks! know it works.
I don’t get this though, I would expect it to work even without the keyword view. “View” is just to prevent to modify the attributes of the contract, isn’t it?

In Solidity you can return values from functions to the user if these functions are declared view or if they are not declared as view you can still get a return value by using .call()

instance.getMessage().call();

Happy learning,
Dani

1 Like

Got it, thanks again!

1 Like

I am having a lot of difficulty installing [email protected] version

a number of errors seems to arrive.

Can anyone help?

Thanks