Have you ever FELT a programming language?
Learning Cairo is full of LOWS and HIGHS, as is Uint256.
In this post, you’ll learn:
✨ What is FELT? How does FELT work?
✨ What is Uint256? Differences between Uint256 and FELT
✨ How do we use FELT and Uint256 in Nostra
Let’s jump right in!
`Felt` is short for `field element` and `felt` is everything in Cairo. Numbers, strings, addresses, booleans… They're all represented as `felt` in Cairo.
`Felt` is a 252-bit number (31 bytes) and it supports all base operations such as:
➕Addition
➖Subtraction
✖️Multiplication
➗Division
Let’s see it with examples:
If someone from JavaScript wants to call a transaction or read contract data using functions with `felt` parameters, they must convert the arguments into a 252-bit number.
A string should be converted into numbers (in `hardhat-plugin` there is a very nice function `shortStringToBigInt`).
False/true should be 0/1, and so on.
Another example is when you try to get return data from the contracts function, but only in the other direction:
You must convert 252-bit numbers into strings/addresses/booleans(for strings, there is also a function in `hardhat-plugin` - `bigIntToShortString`)
Delve deeper reading the `felt` section on the official docs.
There are two libraries can help you work with felt: `math.cairo` and `math_cmp.cairo`.
In `math.cairo`, there are operations like:
Signed and unsigned div
Sqrt
Abs_value
Sign
`math.cairo` also has assert compare functions. These functions can revert a transaction if a condition is not satisfied.
For example: `assert_not_zero`, `assert_not_equal`, `assert_le`, `assert_lt`...
The other library is `math_cmp.cairo`.
Here you’ll find functions that only return 1 or 0 (TRUE/FALSE) based on whether the condition is satisfied or not(`is_not_zero`, `is_le`, `is_in_range`).
In summary, `felt` in its current form and with current core libraries may not satisfy all requirements that are needed to write complex smart contracts.
That’s what we have Uint256.
The main reason for adding Uint256 in Cairo was interoperability.
Uint256 enables interoperability with the Ethereum Virtual Machine (EVM) and completely supports the ERC20/ERC721 standard in #Cairo.
`felt` is a 252-bit number (or 31 bytes number). As the name suggests, Uint256 is a 256-bit number (or 32 bytes)
The only way to have Uint256 in Cairo is through a struct with two `felt` elements: Low and high.
“Low” refers to the lower 128 bits and “high” to the higher 128 bits.**
**
The base problem with `felt` is that there is very limited support for mathematical operations.
If you write a complex smart contract, you’ll probably need to write helper functions to make them work.
On the other hand, `SafeUint256` has almost all the things that are necessary to write a slightly complex #Cairo smart contract.
Besides, using Uint256 is a lot easier (it doesn't need a cast) if you use functions from ERC20/ERC721.
By the way, just a few weeks ago, a discussion to remove Uint256 from ERC20 and only use the `felt` type was started. Read it here.
To sum it all up, this is how we use FELT and Uint256 in Nostra:
For calculations or functions from ERC20, we use Uint256
For indexes for arrays, booleans, addresses, and strings, we use `felt`
Cairo and many other tools/libraries are under heavy development. Iterations are fast, and code changes quickly.
Do your own research and use all of it at your own risk. Stay tuned for more Nostra Devs posts about practical uses of Cairo.
Check out the first post of the Nostra Docs series: What is Cairo?
Our Nostra Famiglia Fund is live! We’re allocating 5 MILLION DAI to build on top of Nostra and StarkNet. Read more here.
Join La Famiglia 🌹👉 https://bit.ly/NostraDiscordInvite
**
**