Solidity Basics

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

Hello, I think I did what filip does exactly in the video lecture for mappings but I keep on encountering an error: “SolidityBasics.sol:27:9: ParserError: Expected primary expression. returns balance[msg.sender]; ^-----^”

I also used pragma solidity 0.7.5

Thanks for the help

mapping(address => uint) balance;

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

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

I think I got it, I should’ve used “return” instead of “returns”, but what’s the difference between the two? Many thanks

2 Likes

The method for the keyword are different, returns is used on the solidity function header to let know this function should return a value type.

While return is the keyword that you use on solidity and also many programming languages to achieve 2 goals:

  • run a function that returns a value, can be used for calcs.
  • the function procedure ENDs when it returns a value.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

Mant thanks @thecil, I’ll make sure to remember these and add them to my notes! Thanks again for the help.

1 Like

how much does 1 gas cost in eth?

1 Like

I think you are confusing terms, GAS is the term used for the transaction fees, the amount of wei that you are willing to pay for the tx.

wei is the term used to define the most smallest unit for ethereum, like bitcoin which is 100 million satoshis, ether is a trillion of wei, you can find more on internet, there are many tutorials for it.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Hi team!! My question is about how we can verify if a specific value exists in a array (in this case address but this applies to any value).
For example:

address[] arrayOfAddresses = [address1, address2, address3];

address addressToFind = someAddress;

How we can valdiate if this addressToFind exists in the array without using expensive operations like loops in order to avoid the elevated gas costs?

Thanks!!

Hi @lucaszanek

You have to iterate the array and check one by one if array[i] == address.
No other options if you use array.

Cheers,
Dani

image

Does anybody know why I can’t make a new file, when I clicked new file nothing happened.

Hi @Lane11

Which browser are you using? Have you tried with a different one?

Brave, I did try in too Chrome but the same thing. Month ago I could use remix and write smart contracts but now I don’t know where is a problem. Now I downloaded desktop remix and it works fine.

Hi Filip or anyone,

can you help me to understand what does ** Person [ ] people ** mean in below program ?
Can it be Person [ ] = people; ?

pragma solidity 0.7.5;
contract HelloWorld {
struct Person {
uint age;
string name;
}
Person [ ] people;

function addPerson(uint _age, string memory _name) public{
Person memory newPerson = Person ( _age, _name) ;
people.push(newPerson);
}

function getPerson(uint _index) public view returns ( uint , string memory ) {

Person memory personToReturn = people [ _index];
return ( personToReturn.age, personToReturn.name);
}
}

1 Like