Understanding (Unsigned) Integers
In case you donāt know what exactly integers are, then letās define first what weāre talking about:
An integer is [ā¦] a number that can be written without a fractional component. For example, 21, 4, 0, and ā2048 are integers, while 9.75, ⦠is not. https://en.wikipedia.org/wiki/Integerā
Integral types may be unsigned (capable of representing only non-negative integers) or signed (capable of representing negative integers as well) https://en.wikipedia.org/wiki/Integer_(computer_science)ā
In laymanās terms: Integers are numbers without decimals. Unsiged integers cannot be negative.
Letās define a simple Smart Contract first so we can set an integer and then read the value again.
Smart Contract
Create a new file and name it IntegerExample.sol and fill in the following content:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.1;
contract IntegerExample {
uint public myUint;
}
There are two important things to see here:
- The variable is not initialized, but as we will see in a moment, still has a default value
- A public variable automatically creates a getter function in solidity.
Deploy the Smart Contract
Open the āDeploy & Run Transactionsā Plugin. Make sure the correct Smart Contract is selected from the dropdown. Then simply hit āDeployā:
You will be able to observe a few things:
-
A new transaction will be sent and you can see that in the console of Remix (bottom right).
-
Your Smart Contract is available in the āDeploy & Run Transactionsā Plugin, at the bottom. You might need to uncollapse it.
You can now interact with your smart contract.
Interact with the Smart Contract
To interact with your Smart Contract, Remix is automatically generating buttons for every function. If you click on āmyUintā, you call the auto-generated getter fucntion from the public variable called āmyUintā.
This is a standard workflow, change the smart contract, redeploy. Currently, there are no in-place updates. For every change we do, we have to deploy a new version of the Smart Contract.
Write a Setter Function
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.1;
contract IntegerExample {
uint public myUint;
function setMyUint(uint _myUint) public {
myUint = _myUint;
}
}
Deploy a new version of the Smart Contract - simply hit āDeployā again. You wil see that you have two instances of your Smart Contract on the bottom of the āDeploy & Run Transactionsā Plugin. You can safely close the old version - the new version is on the bottom.
If you click on āmyUintā it will be initially 0 again.
Letās update it to 5. You can enter the number ā5ā into the input field next to āsetMyUintā, then click on āsetMyUintā:
If you do so, you can again observe the console of Remix on the bottom right that it sent a transaction.
When you click on āmyUintā now, it should show you ā5ā instead of ā0ā.
When you click on a function that only reads a value, then no transaction is sent to the network, but a call. You can see this in the console on the right side again.
A transaction is necessary, if a value in a Smart Contract needs to be updated (writing to state). A call is done, if a value is read. Transactions cost Ether (gas), need to be mined and therefore take a while until the value is reflected, which you will see later. Calls are virtually free and instant.
Great! Now you know the basic workflow, how to deploy and how to update your code during development. But working only with Integers is a bit boring. Letās level up a bit and learn some more types before doing our first project.
Please note: In the next sections I will silently assume you are deploying a version of a Smart Contract and delete the old instance, whenever we do some changes. I recommend repeating this a few times so you donāt forget it.