Learning Truffle

Hi @empty_nutella

Running truffle compile command will download the compiler version mentioned in the truffle-config.js.
Another possible reason that could cause the error is an issue with the internet connection. If you have any active VPN or any kind of blockers please disable those.

If that’s not the case try running truffle compile --reset --all command.
This will remove the existing installed compiler and will download the compiler again with the version mentioned in the truffle-config.js.

Please try the above suggestions and let us know if it works.

1 Like

Oh dear, I never noticed your reply. Sorry about that, thanks for replying!

2 Likes

Hi, it’s not clear to me what is going here

contract Helloworld {

    string MESSAGE = "HelloWorld";

    function hello() public view returns (string memory){
        return MESSAGE;
    }

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

So if I run instance.setMessage("Hola") and then run instance.hello() the output will be Hola. Is it because the setter function makes a transaction and because of that a global variable changes?

Why the hello() function doesn’t return "HelloWorld", MESSAGE is a local variable inside the hello() function?

Hi @Lane11

MESSAGE value is not a local variable. By using setMessage you are updating the MESSAGE string stored in memory, so this data accessible even after the transactions.
By using hello function you are reading the MESSAGE from the memory.

1 Like

Hi Guys,

I installed Truffle & the VS code, but I don’t have the same option as it’s in the video

image

There’s no Migration.sol file in the Contract file, nor 1_intial_migration.js in the Migration file.

Is that normal in the new version??

Also when I created a new file i got many errors

Hi @Ahmad_Abd_Allah

In the latest truffle versions, the initial sample code files were removed. So you will not find the migration files.

The error on the solidity version should be removed once the compiler is downloaded and installed after the contract build step.
Also, make sure the compiler version in the truffle-config.js file matches the compiler version in the contract.

Let us know if you have any other issues. :raised_hands:

1 Like

Hi @JohnVersus,

Thank you for your response.

I have another question, do I need to create the file 2_hello_migration.js, as Filip did?

Hi @Ahmad_Abd_Allah

Yes, You will also need it. You can type it just like in the tutorial.

1 Like

Hi @JohnVersus,

I tried to deploy the multisig wallet contract in truffle console, but I got this error when I try to migrate it.


And I got this error when I tried to interact with the contract

Hi @Ahmad_Abd_Allah

As per this error message, it looks like you haven’t passed any parameters to the deploy function.

Params can be passed to deploy functions in this way.

deployer.deploy(Wallet, ['0xb10934df75f7b1f064cce0c3ae41b4726fa0a7a6','0xb10934df75f7b1f064cce0c3ae41b4726fa0a7a6'], 2);

Let me know if this is the case.

Hi @JohnVersus,

Ohhh, the constructor!!
I knew it, but the issue now, I don’t know how to deploy it.
Now it says that the deployer is not defined.
Thank you, John

Can you share your current code? and which command are you using to run

Here it’s

// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
pragma abicoder v2;

contract Wallet{

 address [] public  owners;
    uint limit;

 struct Transfer{
    uint amount;
    address payable receiver;
    uint approvals;
    bool hasBeenSent;
    uint id;
 }

 event TransferRequestCreated(uint _id, uint _amount, address _intiator, address _receiver);
 event ApprovalReceived (uint _id, uint _approvals, address approver);
 event TransferApproved(uint _id);


 Transfer [] transferRequests;

 mapping (address => mapping(uint =>bool)) approvals;
 mapping (address=>uint) balance;

 modifier onlyOwner{
  bool owner = false;
  for(uint i = 0; i<    owners.length; i++){
      if (owners[i] == msg.sender){
          owner= true;
      }
  }
  require(owner == true);
  _;
 }

 constructor (address [] memory _owners, uint _limit) {
        owners = _owners;
        limit = _limit;
 }

 function deposit() public payable {}
 

 function creatTransfer(uint _amount, address payable _receiver) public onlyOwner{
    require(balance[msg.sender]>= _amount); 
    transferRequests.push( Transfer (_amount, _receiver, 0, false, transferRequests.length));
    emit TransferRequestCreated(transferRequests.length, _amount, msg.sender, _receiver);
 }

 function approve (uint _id) public onlyOwner{
     require (approvals[msg.sender][_id] == false);
     require(transferRequests[_id].hasBeenSent == false);
     approvals[msg.sender][_id] = true;
     transferRequests[_id].approvals++;

     emit ApprovalReceived (_id, transferRequests[_id].approvals, msg.sender);

     if(transferRequests[_id].approvals >= limit){
        transferRequests[_id].hasBeenSent= true;
        transferRequests[_id].receiver.transfer(transferRequests[_id].amount);
        emit TransferApproved(_id);
     }
 }
 function getTransferRequests() public view returns (Transfer[] memory){
       return transferRequests;
 }   
 
 function getBalance() public  view returns (uint){
     return balance[msg.sender];
 }
 function getContractBalance () public view returns (uint){
     return address (this).balance;
 }

}

Did you define constructor anywhere in 2_initial_migration.js file?

For example: If your contract name is Wallet, you have to define the constructor like this

const constructor = artifacts.require("Wallet");

//... and the you can use the constructor code like below
let instance = await constructor.deployed()
//...

So, the 2_initial_migration.js file should be

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

const constructor = artifacts.require("Wallet");

module.exports = function(deployer){
    deployer.deploy(Wallet);
};

correct??

Ahh, No you don’t need to add constructor here, as you don’t have code with constructor.deployed here.

Do you know which part of your code has constructor.deployed() function used? Can you show that part of the code?

You will have to add const constructor in that code file.

1 Like

this is constructor in the code file

So @JohnVersus, would you please try to deploy my code(I have share it earlier), and help me with the missing part, and the steps to resolve it?

I know that my inquiries seem stupid, sorry for that :blush:

@Ahmad_Abd_Allah This is how I deployed your code.

I have created a new file in contracts folder and created a .sol file with your code.

Then in 1_deploy_contracts.js file I have added the following code.

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

module.exports = function (deployer) {
  deployer.deploy(Wallet, ["0x4B2Bc1399E47DD9F4f2493729eE506dF1Afe7999"], 1);
};

These are following command I ran in console.

truffle compile
truffle deploy

That is it. No other code is added to make it work.

1 Like