- Published on
CryptoZombies Lesson 1 - The Basics of Contract Writing and Interaction
- Authors
- Name
- Yair Mark
- @yairmark
I started having a look at CryptoZombies to get more of a feel for what Ethereum and specifically Solidity are about. This post will give a high level summary of each of the chapters in CryptoZombies Lesson 1. I wrote this post as a cheatsheet/reference to refer back to. I also investigated some of the concepts a little further out of pure curiosity.
Solidity
pragma solidity ^0.4.19;
contract YourAppName {
}
Pragma
Used to specify the version of Solidity being used
Variables
- State variables: permanently stored in contract storage (on the Ethereum blockchain)
var
: You can usevar
to declare a variable where Solidity infers the type itself
Basic Types
- uint: unsigned int: Example:
uint count = 0;
uint
isuint256
: a 256 unsigned integeruint8
,uint16
anduint32
are also available
- int: signed int
- Strings:
string name = "John";
Structs
- Feels like C structs
- A way to define more complex objects
contract YourContract{
...
struct Person{
string name;
uint age;
}
}
To create a new instance of the struct:
Person john = Person("John", 33);
Arrays
- Fixed:
string[4] compassPoints
oruint[3] size
- Dynamic:
uint[] names
YourStruct[] yourStructs
: useful for storing structured data in your contract on the blockchain
- Public Arrays: Specify an array that is accessible (read only) to other contracts. Solidity creates a getter if the
public
keyword is usedYourStruct[] public yourStructs;
To add something to an array:
yourArray.push(thingYouArePushing);
.push
adds items to the end of an arrayuint id = someArray.push(someThingToAddToTheArray) -1;
: this is the index of what was just added to the array
Typecasting / Type conversion
- https://cryptozombies.io/en/lesson/1/chapter/11
- http://solidity.readthedocs.io/en/v0.4.24/types.html#implicit-conversions
- http://solidity.readthedocs.io/en/v0.4.24/types.html#explicit-conversions
Implicit Conversion
According to the docs this can be done when no information is lost and not the other way around: uint128
to uint256
but not the other way around.
Explicit Conversion
To explicitly cast from for example uint to uint8:
uint8 a = 5;
uint b = 6;
// throws an error because a * b returns a uint, not uint8:
uint8 c = a * b;
// we have to typecast b as a uint8 to make it work:
uint8 c = a * uint8(b);
Events
This is a way for a contract to communicate to the blockchain that something has happened.
The definition of an event looks like a struct but uses the event
keyword.
event IntegersAdded(uint x, uint y, uint result);
You then invoke the event the way you would create a struct:
...
IntegersAdded(_x, _y, result);
...
Operations
- Standard operators:
+
,-
,/
,*
and%
- Exponential operator:
**
Access Modifiers
- Variables are private by default (i.e. no access modifier)
public
: Solidity creates a getter for this - readable (not writable) from other contractsuint public age
- Used to store data you want to be public
- Functions are
public
by default, this means any contract can execute your functions- You can and should declare your functions
private
- You can and should declare your functions
Functions
function doSomething(uint _num1, uint _num2) {
}
...
doSomething(1,4);
- It is convention in solidity to prefix function parameter variable names with
_
to differentiate them from global variables - Functions are public by default
- To declare a private function:
- It is convention to prefix private functions with
_
- It is convention to prefix private functions with
function _doSomething(uint _num1, uint _num2) private {
}
- To return something from a function:
function sum(uint _num1, uint _num2) returns (uint) {
return _num1 + _num2;
}
- Solidity supports functions with multiple return types
function getData() returns (string, string)
Function Modifiers
The Solidity compiler gives warnings that suggest modifiers for functions where it is appropriate
view
: This means that a function does not change/mutate any values or write anything - it is viewing data in the contract and not modifying the data:
function sayHello() public view returns (string) {
pure
: These are functions that are not accessing any data in the app - values are returned based only on the function parameters
function _multiply(uint a, uint b) private pure returns (uint) {
return a * b;
}
APIs
Hashing
keccak256
- a version of SHA3
- maps an input string into a random 256-bit hexadecimal number
//6e91ec6b618bb462a4a6ee5aa2cb0e9cf30f7a052bb467b0ba58b8748c00d2e5
keccak256("aaaab");
Web3.js
- web3.js git repo
- This is the JavaScript API for Ethereum
- This allows us to interact with contracts on the Ethereum blockchain
- As per Crypto Zombies Lesson 1 Chapter 14 we need the following to access a contract:
var abi = /* this gets generated by the compiler */;
var YourContractsFactory = web3.eth.contract(abi);
var contractAddress = /*The Ethereum address of the contract after it has been deployed on the Ethereum blockchain*/;
var YourFactory = YourContractsFactory.at(contractAddress);
- To create an instance of your contract:
/*
This is some method in your contract that creates an instance of what your contract manages
in the Crypto Zombies case this is a method called createRandomZombie(name); which creates a new Zombie random traits and the provided name
*/
YourFactory.someMethodThatCreatesInstances()
- To listen to events:
- The error object is the standard JS error object that has a string for the error reason web3.js source
var event = YourFactory.SomeEventInContract(function (error, result) {
if (error) return
/*
Where you have this JS function in this class that works with the result
the result object will have the fields defined on the event in your contract
e.g. event SomeEvent(string name, string surname); would have result with these exact 2 fields
*/
doSomethingWithResult(result)
})
Useful Links
- https://cryptozombies.io/
- Contract using Solidity language
- Interacting with the Ethereum blockchain using the web3.js library