Learning Truffle

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

Hey Carlos,
I found the issue. Its was my onlyOwners modifier that rejected the transaction. I noticed that when you deploy a contract in truffle, the msg.sender = account[0].
:man_facepalming:

so i adjusted my migration file by adding the accounts[0] as parameter and now it works

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

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

Hi All,
I have another question. I want to approve a transaction with the function:

    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);
        }
    }

All good I can approve transaction ID 0 by calling instance.approve(0) in the command line. so now i have one of the two required approvals to perform the transaction.
obviously I can’t re-approve with the same msg.sender because of require(approvals[msg.sender][_id] == false);
my question is: how do I call the function from another address in the console ? By default you call the functions with account[0] address right ? how do i call it with account[1] for example ?

many thanks

1 Like

Hey @Joey, hope you are well.

If you need to use another account from your truffle node, in its console you can use something like:

instance.approve(0, {from:accounts[1]})

you could also save the result in a variable to check the transaction details:

let tx = await instance.approve(0, {from:accounts[1]})

Carlos Z

1 Like

great stuff . thanks for the help @thecil

Hi i;m trying to install my truffle but i keep getting “npm should be run outside of the Node.js REPL, in your normal shell”
I don’t know what that means and don’t know how to proceed
thanks in advance

1 Like

Hey @jahh, hope you are ok.

Would be great if you can share an screenshot about the error that the console shows to you, it will help us to identify how to solve your issue :face_with_monocle:

Carlos Z

hey there,
im trying to install truffle and im having some issues,
image
this is what i see after i try to install.

Note:

  • i have installed node,js(not latest vs) and npm

Hey @Bogdan_Manzar, hope you are well.

The “vulnerabilities” warning message is just that, a warning message, you could either try to solve them (by running npm audit fix, which i do not advice for your case) or just ignore them (also the “WARN”, they are warning messages about an error in the module that you are downloading, but will not affect the procedure of it).

Carlos Z

hey thanks for your time,

I tried solving the warns but it only ended up with me getting new ones
image
maybe there is another way to install truffle?

  • Bogdan

(edit) if you want to see the picture right click and click open image in new tab

The only way to install truffle is through npm, but you could try to run the command truffle version, which will return the version of all truffle modules, and its returned, means that your truffle should be installed properly and ready to go.

Here is mine, if you get a message like this one (versions could be different, but does not matter) means that your truffle package should be working properly:
image

Carlos Z

1 Like

Hey everyone,

Regarding the first migration lesson,

I’m getting errors when I am trying to migrate the hello file,
this is the full command prompt message:
image
the helloworld contract does not appear under the build/contracts after this.

I’m unsure if this is just something that is from a new update or something but if you know how to fix this or have any ideas why this happened plz lmk,
thanks for your time
-Bogdan Manzar :smiley:

1 Like

Hey there @thecil,

I’ve got the truffle download down, now I am having a little more trouble with the migrations, when you have some time do you mind taking a quick look?

thanks!

  • Bogdan Manzar :smiley:
1 Like

Hey @Bogdan_Manzar, hope you are well.

Based on your console message, you have 2 errors:

First, your hello contract is apparently missing the solidity version.
Second is on your migration file, which apparently have invalid parameters.

Please share your contract and migration file in the following way:
FAQ - How to post code in the forum

Carlos Z