💜Solidity

Smart contracting

Contract Accounts

Contracts are created on a specific Network.

TermDescription

Balance

Amount of tokens this account is address

Value

Data Storage for this Contract

Data

Raw Machine Code for this Contract

This is deployed as a class, to an instance of a Ethereum network. We can copy and redeploy the contract account to any network that is EVM compatible.

General view of Solidity

Solidity is used to author Smart Contracts, written in .sol files. It is strongly typed like Java, ruby or python, and looks very similar to Javascript. However, Javascript is dynamically typed; do not confuse the two. Most of what people write in Solidity is written rather quickly. What takes up a lot of programming time is the code written around solidity like HTML, CSS, JS and the frameworks used to interact with the contract on the web. Solidity is rather easy.

When we write a contract, we write a contract definition. This is fed into a Solidity compiler, which pumps out byte code and the ABI. The ABI helps us interact with deployed scripts and are very important to know while building your application. When you deploy a contract, you will write some front end Javascript code to interact with the contract. Javascript cannot interpret the deployed byte code, so the ABI acts as a middle man to help you interface with your deployed contracts.

External account to create contract transactions

All Contracts upon creation have a nonce: the amount of times the creator (sender) of a contract issued the contract. Because a contract is NOT a transaction of payment, the 'To' field is left blank - if it weren't then it would be considered a transaction (Tx). All contracts have data, which is the compiled byte code associated with all the fields of a contract. Contracts include a value, which is the 'Wei' (gas ---> $) used to issue to contract to the blockchain. This gasPrice can be adjusted by the sender; there is always a gasLimit consumed by the contract. Contracts have 3 fields that verify the Contracts private key of the sender's address -> v, r and s.

All of these pieces are here for us to make sure that all participants on any EVM blockchain network, commits to spending real value when changing any amount of data on the blockchain. There's no way around it. The way Solidity is designed forces us to follow these rules and make trust-less / fair transactions while we use the network.

When is contracting free?

Running contract functions --> Cost of running functions

'Calling' a FunctionSending a Transaction to a Function

Cannot modify the contracts Data

You CAN modify the contracts data.

Return's Data immediately.

takes time to execute / return

Free

Runs instantly

returns a new transaction hash

Contracts Structure / Breakdown

TypesDefinitions

Public (cannot be both public and private)

function can be called by anyone in the world. Will create a new function automatically for you, even for your instance variables.

Private

'Calling' is limited to only the contract, nothing else. you will see this being Called within other functions, acting as helper functions.

View

function keyword that returns data but does not modify the contracts data

Constant

means the same as view

Returns

Only ever used on view or constant functions. You cannot return data to a function that modifies data at the same time.

Pure

Cannot modify or even read the Contracts data. Rarely used.

Payable

If called, it would send payment in order to call this function

BASIC TYPES

NAMENOTEEXAMPLE

string

a sequence of characters

"hi there"

bool

true or false value

true, false

int (int8,16,32,256)

positive or negative, non decimal number

0, -20, 45

uint

unsigned integer, only positive

1,2,3,4+

fixed/unfixed

decimal numbers

20.22, -21.32

address

hexadecimal for sending/receiving money

0x1823dgv38x2b2n7...

Types of Messages

Arrays in Solidity

Reference types

NameDescriptionExample

Fixed Array

immutable length with a single type.

int[3] --> [1,2,3]

Dynamic Array

mutable length with single type.

bool[] --> [true, false]

Mapping

key-value pairs, all keys are same type and then all values must be the same type.

mapping(string => string) mapping(int => bool)

struct

collection of key-value with different types.

struct Car{ string make; string model; uint value; }

The bridge between Solidity and Javascript can limit programmers from making certain design choices in their contracts. For example, Nested Arrays cannot be pulled by javascript from Solidity because of the lack of communication layers Solidity and Javascript have together. All strings in Solidity are actually "dynamic arrays". Put two & two together, this means we cannot transfer arrays of strings over to javascript.

Modifiers

this is a classic "don't repeat yourself" solution

Keywords used often

Payables

  • require() : A global variable that is used for validation and returns a boolean expression. Some requirement must be satisfied in order to complete the rest of the function.

  • .value : a property used with the msg global variables, which returns the amount of value in the addressed wallet being called.

  • ether : This keyword is used to determine how much wei is required, used in payable functions. (0.01 ether ==> 100000000 wei)

  • transfer() : will attempt to deposit or withdraw value from the current contract, which can be called using transfer(this.balance); --> "this will transfer the balance of this contract".

global keywords

  • keccak256 : This is a global function call that can return sudo random numbers. Known as sha3.

  • block. : will return the state of the current block. You may call block.difficulty, a global variable that returns the current block difficulty.

  • now : This global variable returns the current time of day you are in.

  • this. : reference to the current contract

  • new : can create a new instance of another keyword, such as an address

Last updated