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!
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!
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
Happy learning,
Dani
Thank you Dani! Appreciate it.
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
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
Happy to see that you understood!
Well done,
Dani
Okay, terrific. This is the course that is going to change my life. Great news.
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!â
}
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
You are welcome Troy
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
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!
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
Happy learning,
Dani