Circolors Solidity Sundays #2

It’s week 2. Thanks to everyone who read through our Solidity introduction last week. For this week we’re gonna use the ‘daysSinceDegened’ challenge I set last week to introduce two more features of Solidity, require and global variables.

(Also thanks for this weeks banner by @smokeytom1 ❤)

Here’s today’s code:

Solidity Sundays #2
Solidity Sundays #2

First a very brief overview of ‘uint’.

Solidity doesn’t deal with negative numbers so uints are positive integers (whole numbers) from 0 to x. What x is depends on the number you put after the uint declaration, for example the default uint256 means your variable can be any integer from 0 to 2**256-1, a very large number, but because computation costs gas on the blockchain if you know your variable has a certain max you can use a smaller one, uint8 being the smallest (2**8 = 256).

So just remember for future reference, Solidity doesn’t like negative numbers or decimals.

More reading here.

setDaysSinceDegened Function

So lets focus on our setDaysSinceDegened function. It’s purpose is to set the previously declared daysSinceDegened variable. But you’ll notice we have some extra lines of code, time to go line by line.

require(daysSinceDegenedSet== false, "Already set your counter");

So a public blockchain is great but you don’t want just anyone to call any of your functions, this is where require blocks come in. You’ll often see these if you review NFT contracts, to ‘require’ an address is whitelisted to mint, or ‘require’ you send enough Ether to mint.

As you’ll see above our function, the variable daysSinceDegendSet is a bool type (true or false), and our function requires that it is false, with the string after the comma (“Already set your counter”) being the error message a user gets if they do not pass this requirement.

So in short, we’re checking that this user has not already set their daysSinceDegened variable.

daysSinceDegenedSet = true;

Next we’re changing this same daysSinceDegenedSet variable to true, meaning in future they won’t pass the previous requirement.

daysSinceDegenedChange = block.timestamp;

Next we have another variable daysSinceDegenedChange declared at the top of our contract, here we set it to block.timestamp. Block is a global varaible type in solidity, meaning all contracts can access it without having to store it in the contract. Some other examples are block.number, block.basefee (more info: add solidity docs global variables). But block.timestamp gives us a timestamp of the block this transaction is mined on so we know when our variable is set.

daysSinceDegened = _daysSinceDegened;  

Then finally we set the variable to the uint256 we passed into the function.

Mini Overview

require blocks are just a check that certain conditions are met before running function, this is so your Web3 provider (such as MetaMask) can see whether the transaction is likely to fail and warn you before sending the transaction to avoid you wasting gas.

Global variables such as block.timestamp or block.number are variables that all contracts can access, and typically relate to the state of the blockchain. There are some more types of global variables that we’ll get to soon too.

Challenges

  1. Our users are degens at heart so they need an easy way to reset their daysSinceDegened counter back to 0 when they ape into another rug, make them a function to do this.
  2. However on the rare moment they actually make it 24 hours without minting Terminally Tormented Turtles or Nonplussed Nonchalant Narwhals we should let them add an extra day to their daysSinceDegened counter.

More into on how you can use Solidity’s time uints here.

We’ll review these challenges in Solidity Sundays #3 before levelling up a bit and moving onto a new contract. Good job making it this far, one step closer to becoming a shadowy super coder.

Thanks again for reading, as always any questions/feedback/code flexes welcome in the Circolors Discord!

Subscribe to McToady
Receive the latest updates directly to your inbox.
Verification
This entry has been permanently stored onchain and signed by its creator.