Learning Truffle

Alright, thanks for the tip, I’ll go that route instead

Hey guys, I’m doing the solidity smart contract 201 and following Philip’s steps, but i’m getting an error when I try to call a contract function from the cmd on Visual Studio Code.
I can compile and migrate the code with no problems, but when I try to call some function it throw errors.

truffle(develop)> let instance = await helloWorld.deployed()
Uncaught ReferenceError: global is not defined
    at evalmachine.<anonymous>:2:19

This is the error that I get when I try to get the “instance” (it should return “undifined”)

truffle(develop)> let instance = helloWorld.deployed()
undefined

But if i create the variable without the “await” it return what it should return, but when i try to call some function this error is thown:

truffle(develop)> instance.hello()
evalmachine.<anonymous>:0
instance.hello()
         ^

Uncaught TypeError: instance.hello is not a function
    at evalmachine.<anonymous>
    at sigintHandlersWrap (node:vm:268:12)
    at Script.runInContext (node:vm:137:14)
.
.
.

(This is my contract)

pragma solidity 0.8.6;

contract helloWorld {
    string message = "Hello World";

    function setMessage(string memory newMessage) public payable returns (string memory){
        message = newMessage;
    }
    function hello() public pure returns (string memory){
        return "Hello world";
    }
}

hi, i am still getting the same error message…

ParserError: Source "/C/Users/alexp/Desktop/alex/truffel/ERC20_token/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol" not found
 --> /C/Users/alexp/Desktop/alex/truffel/ERC20_token/contracts/NewToken.sol:3:1:
  |
3 | import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Compilation failed. See above.

Hey @Nuno_Silva

Why is the function setMessage payable?

The issue you are facing is: https://github.com/trufflesuite/truffle/issues/4238

  • Make sure to have node > 12.x installed.
  • Migrate your project then const c = await helloWorld.deployed()
  • You will see the error “global is not defined”
  • Type global = globalThis

The issue is now fixed.

Also the team working on Truffle should soon deploy a fix for that.

I’m doing the assignment to deploy the Multisig Wallet with Truffle.
Now when i want to set the instance variable to wallet.deployed(), it says : Wallet is not defined.
What is going wrong? plz help

Terminal:

3_Multisig_migrations.js
========================

   Replacing 'Wallet'
   ------------------
   > transaction hash:    0x710c128263680f4991aa1a6dcd8169852707a958de1392aeec71924f6453b574
   > Blocks: 0            Seconds: 0
   > contract address:    0xeF43D967BD57662f19B6DcFDFa6cFB2cBb52F244
   > block number:        15
   > block timestamp:     1628689623
   > account:             0xB4bA0A3c079ceE4C239b3cd4A8EdeeAb69e65464
   > balance:             99.992823048
   > gas used:            857299 (0xd14d3)
   > gas price:           2 gwei
   > value sent:          0 ETH
   > total cost:          0.001714598 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:         0.001714598 ETH


Summary
=======
> Total deployments:   3
> Final cost:          0.002805456 ETH


- Blocks: 0            Seconds: 0
- Saving migration to chain.
- Blocks: 0            Seconds: 0
- Saving migration to chain.
- Blocks: 0            Seconds: 0
- Saving migration to chain.

truffle(develop)> 
undefined
truffle(develop)> migrate

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.

Network up to date.

truffle(develop)> instance = await Wallet.deployed()
Uncaught ReferenceError: Wallet is not defined
    at evalmachine.<anonymous>:1:9
truffle(develop)> let instance = await Wallet.deployed()
Uncaught ReferenceError: Wallet is not defined
    at evalmachine.<anonymous>:1:9

Hey @Gos

Can you please share your (wallet) migration file?

1 Like

Hello I’ve run into a Problem with truffle during the lecture First migration.
Up to the migrate command everything works.

when I enter the command:

let instance = await Helloworld.deployed()

I receive uncaught instead of undefined.

truffle(develop)> let instance = await Helloworld.deployed()
Uncaught:
Error: Helloworld has not been deployed to detected network (network/artifact mismatch)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at Function.deployed (C:\Users\edibe\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\contract\constructorMethods.js:83:1)
    at Object.checkNetworkArtifactMatch (C:\Users\edibe\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\utils\index.js:247:1)
truffle(develop)>

migrate feedback:

truffle(develop)> migrate

Compiling your contracts...
===========================
> Compiling .\contracts\Migrations.sol
> Compiling .\contracts\hello.sol
> Compilation warnings encountered:

    Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> project:/contracts/hello.sol


> Artifacts written to C:\Users\edibe\Google Drive\IOT\Programming\Truffle\helloworld\build\contracts
> Compiled successfully using:
   - solc: 0.8.0+commit.c7dfd78e.Emscripten.clang



Starting migrations...
======================
> Network name:    'develop'
> Network id:      5777
> Block gas limit: 6721975 (0x6691b7)


1_initial_migration.js
======================

   Replacing 'Migrations'
   ----------------------
   > transaction hash:    0xbf7a40b154845397906c05b0367fbf68845b88bd0a65ca780290fb61e94cd5c0
   > Blocks: 0            Seconds: 0
   > contract address:    0x042425215b7cb406c234C966b9A6A9112956dCc5
   > block number:        1
   > block timestamp:     1629809996
   > account:             0x7fa14C25D94ACA71799937Cd179f84c31D8c3e41
   > balance:             99.9995114
   > gas used:            244300 (0x3ba4c)
   > gas price:           2 gwei
   > value sent:          0 ETH
   > total cost:          0.0004886 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:           0.0004886 ETH


Summary
=======
> Total deployments:   1
> Final cost:          0.0004886 ETH


- Blocks: 0            Seconds: 0
- Saving migration to chain.

Now the instance cant be called as shown in the Video.
“Uncaught ReferenceError: instance is not defined”

Where did I go wrong?

I am getting the following error when trying to migrate Filip’s multisig wallet contract on truffle (as part of the assignment). I was able to successfully migrate “Helloworld” so I’m not sure why there’s a problem (I followed the same exact steps). Below is the error message that I keep getting. Please help!

Error: *** Deployment Failed ***

“Wallet” – Invalid number of parameters for “undefined”. Got 0 expected 2!.

at C:\Users\jsand\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\deployer\src\deployment.js:365:1
at process._tickCallback (internal/process/next_tick.js:68:7)
  • Blocks: 0 Seconds: 0
  • Saving migration to chain.

Your migrations file should look similar to this:

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

module.exports = function (deployer) {
  deployer.deploy(MultiSig, ["0x...","0x...","0x..."], "3");
};

To find the addresses to input, type:

  • accounts[0]
  • accounts[1]
  • accounts[2]

Into your cmd terminal and replace each “0x…” with your given addresses. The “3” above is how many approvals I wanted, so change that also to your liking.

Thanks, that worked!

for the truffle assignment:
I managed to migrate and deploy the Wallet contract.
I’m trying to interact with the functions now but when I try to interact with the createTranfer()

so I typed the folowing in the console:

truffle(develop)> instance.createTransfer(5, “0x36d098ac992fca6eee584f12d06b8968111d5dcd”)

getting me an error
Uncaught Error: Returned error: VM Exception while processing transaction: revert

by the way this is the function:

function createTransfer(uint _amount, address payable _receiver) public onlyOwners {

    emit TransferRequestCreated(transferRequests.length, _amount, msg.sender, _receiver);

    transferRequests.push(

        Transfer(_amount, _receiver, 0, false, transferRequests.length)

    );
}

any help would be appreciated

I’ve just redone everything and I got rid of the Error.

Hey @Joey

If you worked with the final code of Filip, then the address of the payable_receiver is saved within an Array.

To send the address try to use the array
Instance.createTransfer(10, accounts[i])

But it’s important to have the addresses defined in your migration File.

i cant get the import for the erc20 coding for openzeppelin. it states this message when i compile.

Compiling your contracts...
===========================

> Compiling .\contracts\ERC20.sol
> Compiling .\contracts\Migrations.sol
> Compiling .\contracts\NewToken.sol

> Compilation warnings encountered:
  |
7 | import "../../utils/Context.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

,ParserError: Source "/C/Users/alexp/Desktop/alex/truffel/ERC20_token/contracts/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol" not found
 --> /C/Users/alexp/Desktop/alex/truffel/ERC20_token/contracts/NewToken.sol:3:1:
  |
3 | import "./node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

i dont know why it says its “not found”, since i installed the npm install @openzeppelin/contracts

1 Like

thank you,
I tried again with Instance.createTransfer(10, accounts[1]) but i receive the same error.

How do i define those accounts addresses in the migration file please ?
Mine looks like this:

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

module.exports = function (deployer, network, accounts) {
  deployer.deploy(Wallet, [ accounts[1], accounts[2]], 2);
};

thanks in advance

Your file path might be incorrect.

Try with import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

Carlos Z

Hey @Joey, hope you are ok.

If you are getting a revert error, its because one of your require statements are being trigger, you might want to add a error message on them so you can identify which require is triggered.

Example:
require(condition, "something bad happens");

Carlos Z

Hi @thecil,
Thanks for your feedback. The function that im calling in truffle has no require statement so I’m confused with the error.

    function createTransfer(uint _amount, address payable _receiver) public onlyOwners {
        emit TransferRequestCreated(transferRequests.length, _amount, msg.sender, _receiver);
        transferRequests.push(
            Transfer(_amount, _receiver, 0, false, transferRequests.length)
        );
        
    }

This is how I call it:
let instance = await Wallet.deployed()
then
instance.createTransfer(2, accounts[3])

The error message im getting is:

Uncaught Error: Returned error: VM Exception while processing transaction: revert

here is my migration code


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

module.exports = function (deployer, network, accounts) {

  deployer.deploy(Wallet, [ accounts[1], accounts[2]], 2);

};

any idea what i’m doing wrong here please ?

1 Like

What about the transfer function? Could you please share your contract in the following way so we can have a better picture for the issue?

Carlos Z

Hi Carlos,
I’m using the final wallet code from Filip for this assignement. Here’s the complete contract contract:

pragma solidity >=0.4.22 <0.9.0;
pragma experimental ABIEncoderV2;

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 _initiator, address _receiver);
    event ApprovalReceived(uint _id, uint _approvals, address _approver);
    event TransferApproved(uint _id);

    Transfer[] transferRequests;
    
    mapping(address => mapping(uint => bool)) approvals;
    
    //Should only allow people in the owners list to continue the execution.
    modifier onlyOwners(){
        bool owner = false;
        for(uint i=0; i<owners.length;i++){
            if(owners[i] == msg.sender){
                owner = true;
            }
        }
        require(owner == true);
        _;
    }
    //Should initialize the owners list and the limit 
    constructor(address[] memory _owners, uint _limit) {
        owners = _owners;
        limit = _limit;
    }
    
    //Empty function
    function deposit() public payable {}
    
    //Create an instance of the Transfer struct and add it to the transferRequests array
    function createTransfer(uint _amount, address payable _receiver) public onlyOwners {
        emit TransferRequestCreated(transferRequests.length, _amount, msg.sender, _receiver);
        transferRequests.push(
            Transfer(_amount, _receiver, 0, false, transferRequests.length)
        );
        
    }
    
    //Set your approval for one of the transfer requests.
    //Need to update the Transfer object.
    //Need to update the mapping to record the approval for the msg.sender.
    //When the amount of approvals for a transfer has reached the limit, this function should send the transfer to the recipient.
    //An owner should not be able to vote twice.
    //An owner should not be able to vote on a tranfer request that has already been sent.
    function approve(uint _id) public onlyOwners {
        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);
        }
    }
    
    //Should return all transfer requests
    function getTransferRequests() public view returns (Transfer[] memory){
        return transferRequests;
    }
     
}
1 Like