Solidity Basics

Hi
I have coded the uint arrays, “get” and “set” functions. All compiles fine but i am getting an error message saying i do not have enough GAS. Can someone help out with what’s going on here please?

Thanks

Ok. I also closed down remix and opened up again and all was fine. Strange but not complaining.

1 Like

Hello

Has anybody else had this issue when trying to type in the array numbers after compiling:

pragma solidity 0.5.12;

contract HelloWorld{
string public message = “HelloWorld”;

uint [] public numbers = [0, 20, 45];

function getMessage() public view returns(string memory){
    return message;
}

function setMessage(string memory  newMessage)public{
    message = newMessage;
}

function getNumber(uint index) public view returns(uint){
    return numbers[index];
}

function setNumber(uint newNumber, uint index)public{
    numbers[index] = newNumber;
}

}
i get the error message…transact to HelloWorld.setNumber errored: Error encoding arguments: Error: invalid number value (arg="", coderType=“uint256”, value=“10.0”, version=4.0.45)

I can not see any errors and in any case the compiler indicates the green tick so i don not understand. I have also changed the pragma versions around and still nothing. I’m getting a bit annoyed with this remix editor, i just want to code and i am spending more time trying to get the editor itself to function…

Any help is welcome

Thanks

Hi @NetworkP

Compilation is working but the execution is not. The problem doesn’t come from your editor, it just gives you information that what you are trying to do with your code is not possible.

For example if you are doing this:

setNumber
newNumber:
1
index:
2

Is it working ?

As we can see in your error

Error: invalid number value (arg="", coderType=“uint256”, value=“10.0”, version=4.0.45)

You are using an invalid number value 10.0, the type require is uint256 but you are using a number of type float.

https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/

The function setNumber allow you to modify existing numbers in your array.
So you can modify the index 0 1 and 2 with a value of type uint256 ( a positive whole number between 0 and 2^256 – 1).

For information floating number doesn’t exist in solidity so you ll never be able to use 1,5 or 10.6 ect…

Hi Gabba, thanks for reaching out. I’m a little confused. I am mimicking exactly what filip does in the course and he does input 10.0 in the “set” number box which then returns 10 when you put “0” into the the “get” function icon box. I am only copying what he is doing. it works on the "Types & Arrays course but not for me…

Hi Gabba

I got it to work. it’s a , as apposed to . Thanks anyway.

Steve

Your address in the Remix IDE has run out of Ether would be my guess. When you refresh the Remix page it reloads addresses and they each get their 100 ETH, so you have gas again.

Thanks Petra :slight_smile:

1 Like

You are very welcome.

I accidentally deployed a contract from a zero eth addy somehow interfacing with Metamask/Ropstein and couldn’t interact with it and had to start over… Argh!

Pretty sure it gives you all new addresses too when you reload, so you’ll have to change addresses in your contracts as well or they’ll use the old versions. I’m not sure how persistent the Remix blockchain is though…

Now I’m thinking I need to test that.

2 Likes

In the If & Else - Control Flow I would have at least mentioned the ternary operator:

https://solidity.readthedocs.io/en/v0.4.21/miscellaneous.html

14 Ternary operator <conditional> ? <if-true> : <if-false>

I mean for such a basic setter or an object property, the ternary operator is more suitable:

newPerson.senior = (age > 65) ? true : false;

I prefer to use if/else for more complex operations.

One of the first things I check in a new programming language is the existance of the ternar operator. If it exists it should be used where suitable :wink:

2 Likes

Yes i agree with you @firepol ternary operator are more ‘sexy’.

But as you can see in this discussion with @jon_m it seems that it cost more gas to execute, i was really surprised (he had run his tests without the optimization on)

Integer types; createPerson function alternatives; Gas; Optimization; View v Pure functions; Non-iterable nature of mappings (by @jon_m)

Ternary operator (highest gas cost = 80)
if…else statement (gas cost = 67)
Boolean expression assigned directly (lowest gas cost = 52)

1 Like

Hey @gabba, thanks for the answer. Interesting that the compiler makes such a difference. I mean I thought that code that does the same thing can actually be compiled more efficiently, regardless on how it is written. Wow.

So in the end my code can be optimized further like so newPerson.senior = age > 65; and it is the cheapest way to spare gas. Nice.

I’m really surprised that the compiler doesn’t compile

newPerson.senior = (age > 65) ? true : false;

in the same way as

newPerson.senior = age > 65;

because this code does exactly the same thing (it’s just written a bit “longer”, and if you use if else, even longer but it does the same thing). Why on earth it has a higher cost in gas?!

Yeah i was really surprised too … @jon_m ran this test i should probably test it. Looking at the debugger will help to know which instructions are being used. I think useless op codes are used there.
Doing the same test with optimization enable should output the same amount of gas, because yeah this is the same thing ! :exploding_head:

I did test this myelf (right now) and it’s really true, in my code the execution of

newPerson.senior = (age > 65) ? true : false;
costed: 7001 gas

newPerson.senior = age > 65;
costed: 6973 gas

Wow, mindblowing! Anyway, discussing here made me realize that it’s kinda stupid (not sexy) to use the ternary operator as I wrote. I mean this is the most “sexy” variant: newPerson.senior = age > 65; and I’m happy that it’s also the one that uses less gas :wink:

1 Like

@gabba

Hey @firepol !

I think we need to be clear about what we mean by efficiency. I totally agree with you that a ternary operator is more compact and concise than an if…else statement. And I think in JavaScript it can often be considered more efficient and cleaner code. However, in Solidity we need to consider gas cost, and this is based on the number and type of operations. Having experimented with this, it’s clear to me that shorter doesn’t necessarily mean cheaper.

If you haven’t already done so, when calling the createPerson function with each of the different control flow statements, click on Debug next to the transaction detail (bottom right) to open the Debugger. You can click forward through the different operations contained within the createPerson function until you get to the first one associated with the control flow statement you are testing (you will know as the associated code is highlighted). As you then click through all the operations associated with that particular piece of code, you will notice the different amounts of gas used for each operation. I basically added up all the amounts for each different control flow statement and compared the totals.

I was interested to see that you used the ternary operator differently to me:

My version:
age >= 65 ? newPerson.senior = true : newPerson.senior = false;

So, I’ve tested your ternary operator as well, and with optimization OFF, the gas is the same as with my ternary operator (80), and only 2 less than mine with optimization ON (73 instead of 75).

It’s also worth bearing in mind that in your ternary operator, the  ? true : false  part is actually redundant, because the expression   age >= 65  will return true or false anyway. So just as you’ve already realised, the prize for Sexiest Control Flow Statement (or at least Cheapest) goes to…

:partying_face:

And thanks, because looking in more detail at your version of the ternary operator has made me realise that in the end newPerson.senior = age >= 65; is actually a condensed form of my ternary operator as well! :smiley:

1 Like

@firepol

Actually not! With optimization ON, there is still no difference in the order from most gas (ternary operator) → least gas (boolean expression assigned directly). In fact the difference between the amounts of gas used is actually greater; so, relatively speaking, the ternary operator is even more expensive than the others when optimization is enabled.

You can see my results here.

2 Likes

I still don’t get the difference between mapping and arrays.
Filip in his ethereum solidity mapping video explained how to do mapping, but it looks like inputing information same way like he did with array with the exception of making it private. Please advice and apologies if I am lost.

@jon_m
Many thanks for your explanation. You explained it way better than other sources I have been reading. I am a beginner in terms of mapping. I hope you don’t mind getting back to you in the future should I get stuck with anything.
Sincerely,

1 Like

Hi,
I followed Fillip’s mapping video till the end. I created the code, compiled it (no errors) and deployed it. I still don’t know how to use this way to do mapping though:

people.push (Person (people.length, name, height, age))

He showed it in the previous video before mapping, but did not explain how to continue using it since it looks easier.

1 Like

Filip, I believe that there is a little mistake at the beginning of the video where you describe an unsigned integer (of type uint) as one that can be both positive and negative. It seems to me that “unsigned” means that there is no sign. So unsigned integers, by definition, are greater than or equal to zero. To confirm this, I tried assigning a negative value to a uint variable, and, as expected, the compiler produced a corresponding error message.

1 Like

@filip

Hey, can you tell me why I keep getting a compiling error on the code that I am doing from video 4 on struct please? I have attached a screen shot :slight_smile: