Skip to content

Practicing with smart contract and Ethereum network using solidity

Notifications You must be signed in to change notification settings

thanhnguyennguyen/smart-contract-practice

Repository files navigation

Scrutinizer Code Quality Build StatusCode Intelligence Status codecov Travis_Build_Status

Deploy to Docker Cloud

Smart Contract practices

Copyright © 2018 Nguyen Nguyen

Please never write code without tests

Lottery game

Designs can be found here

https://github.com/thanhnguyennguyen/smart-contract-practice/tree/master/contracts/lottery/design

Contract url

https://rinkeby.etherscan.io/address/0xa0a5566cb16c16384f3b7b3a3a67cbbe0bf2f137

Solidity basic knowledge

Solidity data type

Solidity data type 1

Solidity data type 2

Solidity data type 3

Solidity data type 4

Memory and storage data-location

Array in Solidity

avoid nested array Solidity note 1

static and dynamic array dynamic-static

Not allowed not-allowed

bytes and string are special arrays

bytes32 data; (~ byte[32] data ) bytes data (~ byte[] data ) String vss bytes string-vs-bytes

Functions

function

Function visibilities and modifier func-visibilities

Return multiple values func-return-multiple-values

To assign results from multiple returns function to variable, we use tupple tupple

Function overloading func-overloading

Constructor overloading is not supported

Variable initialization

Variables initialized to defaults at the beginning of functions variable-initialization

Mapping

Difference between mapping and hashtable (of java) or object (of javascript)

Mapping is defined as state , storage only, not allowed in function

Enum and struct are not a part of ABI definition, not available for external function call

Global variable:

global-var

Solidity global variable 1

Difference between msg.sender and tx.origin msg-sender-vs-tx-origin

Error handling in Solidity

No try-catch in Solidity

What happen if an exception is thrown

exception

Check and handle exception handle-exception

OOP Support

No keyword abstract contract, function declared but no body provided. That class can not be deployed

Inheritance: multiple inheritance is supported

Polimorphism

Overloading (except constructor)

Interface and abstract contract

difference-interface-abstrct-contract to implement an interface, use keywork is, similar for extending abstract contract

Events and Logs

Events are part of ABI definition

Only indexed event arguments can be used in Filter, maximum 3 indices are allowed

event

Contract design patterns

Contract destruction contract-destruction

Sending tracsaction to dead contract: fail

ALl fund send to dead contract will be lost

Factory contract

Name registry pattern

Using a registry Contract to manage all contracts, then using contract name to access instance of a particular contract

Mapping iterator pattern

Use another array to keep keys mapping-itorator-pattern

Storage cost goes up

Cost of iteration goes up

Withdrawal pattern

Send vs transfer send-vs-transfer

Withdrawal pattern withdrawal-pattern

ERC-20 specification: 6 functions, 2 events

ERC-20

ERC-20-2

ERC-20-3

ERC-20-4

https://github.com/thanhnguyennguyen/ERC20-token

web3 knowledge

Send and Call

Send-vs-call

Filter object: web3.eth.filter

filter

Event vs filter event-vs-filter