Newbie Tutorial to Smart Contract in Solidity

realquadrant

timfong888

Posted on September 3, 2021

Newbie Tutorial to Smart Contract in Solidity

I am basically rehashing tutorials found on YouTube but putting it into a format of learning and recall that helps me.

Alt Text

Hopefully it will help you, too.

I broke it down conceptually, into building blocks of what I think are the basics to get up and running quickly.

Going to do another one shortly with more details, but wanted to get feedback.

My approach

I think about programming as one would with writing an essay.

I am trying to come up with ways to organize the paragraph, subjects, verbs, objects.

Please let me know what's not clear.

NOTE: How to start jamming right away!

All of this can be done with this awesome IDE called Remix: https://remix.ethereum.org

Some syntax notes

  1. Every standalone line should end in ;
  2. For functions, including constructor, no space before ()

How do I define a contract?

A contract is like a class.

It looks like this:

pragma solidity ^.0.8.0;

contract ContractName {
  //stuff goes here
}
Enter fullscreen mode Exit fullscreen mode

How do I set initial conditions?

When the smart contract is first deployed, you may want a set of initial conditions.

This can be variables, like time, or a specific state.

To do this, you use the constructor, which is run once and only once when running the smart contract.

contract MyContract { 
  constructor() public {
     // initialized state goes here
  }
}
Enter fullscreen mode Exit fullscreen mode

But what kinds of things can be set up in a constructor?

There might be values you want to start with. So this means understanding how to store values.

Concepts / Data Types

  • address: Ethereum address for sender, contract owner, or wallet for example
  • string: string
  • bytes32: string, but smaller
  • address: address
  • bool: boolean
  • enum: a list
  • uint: unsigned integer
  • uint8: unsigned integer where 8 is the "size"
  • int: integer
  • struct: custom data type, see more below

You can also create/initialize Array types:

  • string[]: this is an Array of strings
  • uint[]: this is an Array of unsigned integers

So, to set up a given variable of a data type, do something like this within the contract:

Contract MyContract {
  uint count;

  constructor() public {
     count = 0;
  }
}
Enter fullscreen mode Exit fullscreen mode

Notice we I am using the constructor to set the initial conditions?

The same count be done at the time the state variable count is defined.

contract MyContract {
  uint count = 0;
}
Enter fullscreen mode Exit fullscreen mode

How to store values

Using structs

Structs is a way to group different types of data into a single data object.

You define it:

struct CustomStruct {
  uint id;
  string name;
}
Enter fullscreen mode Exit fullscreen mode

Then you initialize your actual struct based on the struct you created:

CustomStruct public person = CustomStruct("0","Tom Jones");
Enter fullscreen mode Exit fullscreen mode

Using Arrays

This goes into more depth of how to initialize, add elements, and retrieve values in an array.

So this is how we can initialize an array as state variables:

contract myContract {
  uint[] public unitArray = [1,2,3];
  string[] public stringArray = ["happy", "sad", "angry"];
}
Enter fullscreen mode Exit fullscreen mode

Mapping to an array with key-value

So for the key and value, define the data types

Mapping(key => value) public ArrayName

So ArrayName(key) = value

An example is

mapping(address => uint256) public balances;

This means balances[msg.sender] = 0

How to "do" stuff - functions

The actual "doing" is through functions.

These functions execute logic. For example:

contract MyContract {

  function getCount() public view returns(uint) {
    return count;
  }
}
Enter fullscreen mode Exit fullscreen mode

That first function has a bunch of things:

Elements in a Function

  • public: this means it is accessible by any actor on the blockchain
  • view: means displaying but not changing it
  • returns(uint): defines what is returned

Making state variables public

So while we have this cool function defined above which returns the value of the state variable count, that could be done at the time it is defined.

This is done by making the variable public.

This means the value is available on the blockchain. These variables are accessible by functions.

contract MyContract {
  uint public count = 0;
}
Enter fullscreen mode Exit fullscreen mode

Making a local variable

These are not stored on the blockchain, and only work within the function.

contract MyContract {
  function getValue() public view {
    uint public count = 0; 
  }
}
Enter fullscreen mode Exit fullscreen mode

Operations in a Function

The meat of the logic in a function is actually doing things, changing state, applying logic, stuff like that.

contract myContract {

  function increaseCount() {
    count = count + 1;
   }
}
Enter fullscreen mode Exit fullscreen mode

References:

💖 💪 🙅 🚩
realquadrant
timfong888

Posted on September 3, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related