EPT Slide

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Ethereum smart contract

Day 1 - 2
Documents Database (NoSql)
Implement document database (nosql)
Given interface are just guideline.
Feel free to change but keep these functionaliy:
Can add document to collection
Update all documents in collection that have provided ‘key’
Read all value in documents in collection that have provided ‘key’
Delete all value in documents in collection that have provided ‘key’

Any feature is a plus for example features:


Key can be any type, value can be any type
Can compare two document
Can compare two collections
Can sum value for given `key` (if value is integer type)
Agenda Day 1

Familiar programming with solidity &


Understanding language fundamental

A bit of Evm
Language Data types
Control structure & Contract (Class-like) structure
A bit of toolings
Ethereum virtual machine (roughly)

Imagine that we have multiple contract deployed


We can access or reference using Address which
mapping to underlying deployed EVM code
Ethereum virtual machine (roughly)

An immutable program code ROM, loaded with


the bytecode of the smart contract to be
executed
A volatile memory, with every location explicitly
initialized to zero
A permanent storage that is part of the
Ethereum state, also zero-initialized
Stack used to hold small local variables
Solidity as just another language

Apart from new paradigm for writing smart contract

Data types
Variable
Control structure
Syntax Composed
Function/Method in others many languages
Expression/Operators
Contract oriented (Class-like)
Solidity as just another language

Why learning language fundamental

A pretty much of writing solidity lie on security ensuring & optimization

And with new paradigm and a lot of new concept you will learn and applied in next
session will likely benefit you most when you got some ground on the language first
Solidity as just another language

Types

unlike python or javascript which is dynamically typed,


Solidity is a statically typed language like most of low-level language

And no definition of null or undefined every type has default value


Solidity as just another language

Value Types
Variables will always be passed by value, i.e. they are always copied when they are used as function arguments or in
assignments

Bool
Intergers
Fixed-size byte arrays
Literals
Enums
Address
Contract type
Solidity as just another language

Value Types - Booleans


The possible values are constants true and false.

Operator: !, &&, ||, ==, !=


Solidity as just another language

Value Types - Integers


int / uint: Signed and unsigned integers of various sizes
Keywords uint8 to uint256 in steps of 8 (unsigned of 8 up to 256 bits) and int8 to int256 without quantifier it’s 256 sized

Value are in range based on their size


uint8: 0 to 2**8-1
uint256: 0 to 2**256-1
for int256 it is -2 ** 255 to 2 ** 255 - 1
Solidity as just another language

Value Types - Integers


Operation between intergers:
Comparison: <=, <, ==, !=, >=, > (evaluate to bool)
Bit operators: &, |, ^ (xor), ~ (negation)
Shift operators: << (left shift), >> (right shift)
Arithmetic operators: +, -, unary - (only for signed integers), *, /, % (modulo), ** (exponentiation)

Overflow & underflow (0.8)


By default, all arithmetic is checked for under- or overflow but can
disabled by unchecked {..} block (called wrapping arithmetic)
*except for shift operation it will be truncated instead

Division
alway result in the same type so it will be rounding (towards zero)
i.e. int(-5)/int(2) will be -2
Solidity as just another language

Value Types - Intergers


Implicit conversion
An implicit type conversion is automatically applied by the compiler in some cases during assignments, when passing
arguments to functions and when applying operators. In general, an implicit conversion between value-types is possible
if it makes sense semantically and no information is lost.
Solidity as just another language

Value Types - Intergers


Solidity as just another language

Value Types - Intergers


Explicit conversion
May result in unexpected behavior and surpass compiler security features
Know what you’re doing
Solidity as just another language

Value Types - Fixed-size byte arrays


The value types bytes1, bytes2, bytes3, …, bytes32 hold a sequence of bytes from one to up to 32.

Operators:
Most are like integers
Index access: If x is of type bytesI, then x[k] for 0 <= k < I returns the k-th byte (read-only).
.length return the fixed length of the byte array
<0.8 byte used to be an alias for bytes1
Byte is a 8-bit represent of 0 or 1 default value is 0x00

Only explicit conversion are allow


for byteN and integer
And size need to be equal i.e. bytes1 = uint8
Solidity as just another language

Value Types - Enum


c-like, stored as smallest possible int type (0)

Declaration can declared out side of contract.


Explicit Conversion only
Solidity as just another language

Function
function (<parameter types>) {internal|external|private|public} [pure|view|payable] [returns (<return types>)]

public visible everywhere (within the contract itself and other contracts or addresses).
private visible only by the contract it is defined in, not derived contracts.
internal visible by the contract itself and contracts deriving from it.
external visible only by external contracts / addresses.

View functions can read contract’s storage, but can’t modify the contract storage. Therefore, they are used for getters.

Pure functions can neither read, nor modify the contract’s storage. They are used for pure computation, like functions
that perform mathematic or cryptographic operations.
Solidity as just another language

More on functions
Solidity as just another language

Reference Types
Current reference types support:
Mapping
Array
Struct

Unlike Value types where its store data in its own variable, but reference types only store its reference to the data
Solidity as just another language

Data Location
where the data is stored. There are three data locations:
memory non-persistent
storage persistent
calldata like memory but non-modifiable and non-persistent it’s area where function args stored

Think this way, how it differentiate between persistent data and transient data during execution?
Solidity as just another language

Data Location
Where you must specify the data location?
Function parameter
Local variable inside function
Return value

Some of rules:
Storage cannot be newly created, it
need pre-allocated on contract storage
to refer to
Memory can be newly created or
copied from a storage variable
Function parameter and return can be
only memory and calldata
Solidity as just another language

Array
Special array types
String and bytes
string doesn’t have .length

Memory array inside function


Solidity as just another language

Structs
Struct field can be
Value Types (uint / int, bool, address,
string, etc…)
Reference Types like mapping array
but not struct itself
It can be array of struct
Solidity as just another language

Mapping
Similar to HashMap or Dict
Don’t have length and can’t iterate
Solidity as just another language

Hash, typecasting
Solidity as just another language

Inheritance Import
Solidity as just another language

Multiple Return
Foundry

forge init --template https://github.com/felynoir/ept-template [folder_name]


forge build
compile and build source code
forge test -v
test all function with verbosity level

More on https://book.getfoundry.sh/
OrderPreservedMapping
Can be iterate in mapping and getting list of value by order insertion

i.e.
set(“keyA”, 10)
set(“keyB”, 20)
set(“keyC”, 30)
set(“keyB”, 10)

listValues() => [10,10,30]


Stack
Queue
Queue
implement in solidity
enqueue(uint256 value) - add new data
dequeue() returns (uint256 value) - remove data
Ethernaut
https://ethernaut.openzeppelin.com
contract.abi
check function interface of contract
await web3.eth.getStorageAt(address A, uint256 slot)
Get data store data at given slot
await contract.functionName()
Execution function/ Read contract function
contract.address
Return problem’s contract address
await contract.contribute.sendTransaction({ from: player, value: toWei('0.0009')})
call contribute and send 0.0009 ether (msg.value = 0.0009 ether (0.0009*1e18))
await sendTransaction({from: player,to: contract.address, value:
toWei(‘0.000000001‘)})
This will trigger fallback() {} or receive() {}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy