Solidity Basics

wow that is such a dumb mistake haha. Thank you so much for the help. Sometimes you look to deep into things when it is so simple in reality. Cheers!

2 Likes

Hi guys,

I got an error message while I was trying to follow Filip’s steps and wanted to throw an “Balance is not sufficient” comment if the balance was below zero. Can someone help me out what I need to change to get rid of the error message?

Thanks!

Schermafbeelding 2021-03-19 om 09.18.59

function transfer(address recipient, uint amount) public {
        require(balance[msg.sender] >= amount), "Balance not sufficient";
        require(msg.sender != recipient), "Don't transfer money to yourself"; 
        
        _transfer(msg.sender, recipient, amount);
        
        //event logs and further checks
    }

Hi @thomascarl

The error message is a parameter of require therefore should be inside the parenthesis:

require(msg.sender != recipient, "Don't transfer money to yourself");

When in doubt, you can find these stuff in the docs too :slight_smile:

Happy learning,
Dani

2 Likes

Thank you Dani! Appreciate it.

1 Like

I am not able to transfer the value of “200” to the new address. I get this error message:

transact to Bank.transfer errored: Error encoding arguments: Error: invalid address (argument=“address”, value=“0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2. 200”, code=INVALID_ARGUMENT, version=address/5.0.5) (argument=null, value=“0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2. 200”, code=INVALID_ARGUMENT, version=abi/5.0.7)

call to Bank.getBalance

call [call]

from: 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2

to: Bank.getBalance()

data: 0x120…65fe0

I have the same question too. I think the reason why we cannot loop through a mapping and get all values as in an array is that the key does not necessary follow any logic or order while the index in an array is ordered from 0 up. Without know all the keys, it is impossible to find their corresponding values. I’ve read somewhere on the web that the keys in solidity are stored in a hash table so that they are not directly accessible by looking in the codes. I wonder if this is true and would like to know more about the mechanism. Could anyone point us to more details?
Thanks!
Eric

1 Like

Hey @lacour1

Make sure to use comma , to divide two arguments:

0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 , 200

Keep me posted,
Dani

Hello @Eric_Toronto

What you wrote is perfectly right :slight_smile:

Happy to see that you understood!

Well done,
Dani

Okay, terrific. This is the course that is going to change my life. Great news.

3 Likes

That fixed it, thanks. However, after I add the balance, then added the address to transfer 200 of the balance to, it didn’t adjust the balance at all. I didn’t get an error message either.

Hi @lacour1

There might be an error in your code, post it here so that I can check.

Please provide the code properly so i can test it properly.

You can use the “Preformatted Text” Button to encapsulate any kind of code you want to show.

function formatText(){

let words = “I’m a preformatted Text box, Please use me wisely!”

}

prefromatted_text-animated

preformatted text

Thanks,
Dani

pragma solidity 0.7.5;

contract Bank {

mapping(address => uint) balance;

function addBalance(uint _toAdd) public returns (uint){
balance[msg.sender] += _toAdd;
return balance [msg.sender];
}

function getBalance() public view returns (uint){
return balance[msg.sender];
}

function transfer(address recipient, uint amount) public {
//Check balance of msg.sender

  _transfer(msg.sender, recipient, amount);
  
    //Event Logs and further Checks

}

function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}

}Preformatted text

Hey Dani, sorry I am just now getting back to you. Here is my code from Remix.

Troy

Hey @lacour1

Please follow this faq to post readable code: FAQ - How to post code in the forum

Your code works, I am able to transfer funds.
You are probably calling getBalance without changing the account in Remix, therefore msg.sender does not change.

Regards,
Dani

Yeah, you’re right. I had to really pay attention to when and how he was changing accounts.

Thanks,
Troy

1 Like

You are welcome Troy :slight_smile:

Hi Filip,

On the course with Setter Functions, the function getNumber doesn’t work for me and the output of it is always 0 no matter what I input as a setNumber…

pragma solidity 0.7.5;

contract HelloWorld {

int number;


function getNumber() public view returns(int){
    return number;
}
function setNumber(int _number) public {
    number == _number;
}

}

helloworld.sol:11:5: Warning: Function state mutability can be restricted to view function setNumber(int _number) public { ^ (Relevant source part starts here and spans across multiple lines).

Help!
@filip

Never mind, I realized I was comparing instead of assigning ’ == '.

Hi @Bartek

Please next time check this faq that will guide you to post readable code in the forum:
https://forum.ivanontech.com/t/faq-how-to-post-code-in-the-forum/35357/2

I have checked your code and the issue is in the function setNumber.
Keep in mind that you use one = to bind a value to a variable, you use two == to compare two variables.

This is the correct way to assign _number to number.

    function setNumber(int _number) public {
        number = _number;
    }

Cheers.
Dani

1 Like

Hi, @filip I just started this course and just finished “Contract Structure” under “Solidity Basics”, you said that we will not use the Ethereum main net in deploying contracts because it has gas fees.

My question is, can I use a local blockchain from ganache, where I also connect my metamask to deploy the contracts there? Will it function the same? Thank you very much!

I watched Ivan’s video on trying to clone Bitcoin on the Youtube channel and I followed those steps tha’s why I asked the question. I’m a newbie and have no experience in programming but I just finished the JavaScript programming course here in the academy. Thank you so much!

1 Like

Hi @CryptoXyz

My question is, can I use a local blockchain from ganache, where I also connect my metamask to deploy the contracts there? Will it function the same? Thank you very much!

Sure, you can do that.

I suggest to follow both the old and new Ethereum 201 course. You will get a huge amount of knowledge :slight_smile:

Happy learning,
Dani

1 Like